Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #
- # THIS IS A DRAFT
- # A lot of stuff missing, some code is unfinished and won't work
- #
- # Notes:
- # We are utilizing the following scripts:
- # /jffs/scripts/dnsmasq.postconf
- # /jffs/scripts/firewall-start
- # /jffs/configs/passwd.add
- # /jffs/configs/group.add
- #
- # IDEA: perhaps instead of writing a lot of code to these
- # merlin scripts, a simple helper script could be introduced
- # and each of mentioned scripts would just call it like:
- # /jffs/scripts/pihole_helper.sh dnsmasq_postconf
- # /jffs/scripts/pihole_helper.sh firewall_start
- # etc...
- set -e
- if ! grep -q "^pihole:" /etc/passwd; then
- echo "Adding 'pihole' user and preparting .add configs in /jffs/configs..."
- used_uids=$(cut -d ':' -f 3 /etc/passwd)
- next_uid=1501
- while true; do
- for test_uid in $used_uids; do
- if [ "$test_uid" -eq "$next_uid" ]; then
- next_uid=$((next_uid + 1))
- continue 2
- fi
- done
- break;
- done
- mkdir -p /jffs/configs
- contents="pihole:x:$next_uid:$next_uid::/dev/null:/dev/null"
- echo "$contents" >> /etc/passwd
- if ! grep -q "^pihole:" /jffs/configs/passwd.add; then
- echo "$contents" >> /jffs/configs/passwd.add
- fi
- contents="pihole:x:$next_uid:"
- echo "$contents" >> /etc/group
- if ! grep -q "^pihole:" /jffs/configs/group.add; then
- echo "$contents" >> /jffs/configs/group.add
- fi
- fi
- platform="$(uname -m)"
- case "$platform" in
- armv7l)
- repo_url='https://jacklul.github.io/entware-pi-hole/armv7sf-k3.2/'
- ;;
- aarch64)
- repo_url='https://jacklul.github.io/entware-pi-hole/aarch64-k3.10/'
- ;;
- x86_64)
- repo_url='https://jacklul.github.io/entware-pi-hole/x64-k3.2/'
- ;;
- *)
- echo "Unsupported platform: $platform"
- exit 1
- ;;
- esac
- installed="$(/opt/bin/opkg list-installed)"
- if ! echo "$installed" | grep -Fq "wget-ssl " || ! echo "$installed" | grep -Fq "ca-certificates "; then
- echo "Installing needed packages to support HTTPS repositories..."
- /opt/bin/opkg update
- /opt/bin/opkg install wget-ssl ca-certificates
- fi
- if ! grep -Fq "/entware-pi-hole/" /opt/etc/opkg.conf; then
- echo "Adding repository '$repo_url' to opkg.conf..."
- echo "src/gz pi-hole $repo_url" >> /opt/etc/opkg.conf
- fi
- echo "Installing Pi-hole's OPKG package..."
- /opt/bin/opkg update
- /opt/bin/opkg install pi-hole
- # Installer already installs minimal /opt/etc/pihole/pihole.toml with recommended starting values
- # First call to pihole-FTL --config should populate the config with missing variables
- if [ ! -f /jffs/scripts/dnsmasq.postconf ]; then
- mkdir -p /jffs/scripts
- cat <<EOF > /jffs/scripts/dnsmasq.postconf
- #!/bin/sh
- [ -z "\$1" ] && exit 1
- EOF
- chmod +x /jffs/scripts/dnsmasq.postconf
- fi
- if ! grep -Fq "# entware-pi-hole" /jffs/scripts/dnsmasq.postconf; then
- echo "Installing /jffs/scripts/dnsmasq.postconf script..."
- # Perhaps this could be simplified by using helper functions from /usr/sbin/helper.sh?
- cat <<EOF > /jffs/scripts/dnsmasq.postconf
- # entware-pi-hole START
- # Make firmware Dnsmasq listen for DNS on non-default port (we need it for the reverse lookups)
- # If this device is not a DHCP server then you may replace 8053 with 0 to disable Dnsmasq's DNS server
- if ! grep -q "^port=8053" "\$1"; then
- sed '/^port=/ s/^/#/' -i "\$1"
- sed "/^user=/a port=8053" -i "\$1"
- fi
- # Force firmware dnsmasq to advertise itself as DNS server via DHCP
- if ! grep -q "^dhcp-option=lan,6,0.0.0.0" "\$1"; then
- sed '/^dhcp-option=lan,6,/ s/^/#/' -i "\$1"
- sed "/^user=/a dhcp-option=lan,6,0.0.0.0" -i "\$1"
- fi
- # Make sure system uses Pi-hole for DNS requests
- # This is optional and disabled (commented) by default.
- #resolvconf="\$(readlink -f /etc/resolv.conf)"
- #if ! head -n 1 "\$resolvconf" | grep -q "^nameserver 127.0.0.1"; then
- # sed '/127.0.0.1/d' -i "\$resolvconf"
- # sed '1i nameserver 127.0.0.1' -i "\$resolvconf"
- #fi
- # entware-pi-hole END
- EOF
- fi
- # Update Pi-hole config
- echo "Updating Pi-hole configuration..."
- # Upstream DNS servers to use
- # It would be great to import DNS servers from the router's WAN page here instead
- # I believe we could also use "127.0.0.1:8053" here to forward to dnsmasq (and respect router's DNS settings)
- # but generally I would recommend to just set target DNS servers here
- /opt/bin/pihole-FTL --config dns.upstreams '["1.1.1.1","1.0.0.1"]'
- # Correct interface to use should be fetched from nvram
- /opt/bin/pihole-FTL --config dns.interface br0
- # Only queries at most one hop away will be accepted
- /opt/bin/pihole-FTL --config dns.listeningMode LOCAL
- # Reverse queries server, could be made optional but without it
- # you will proably not be able to resolve LAN names
- # '192.168.1.0/24' and 'lan' must be somehow grabbed from the router (nvram?)
- # If the device we are installing on has DHCP disabled then we must also
- # replace '127.0.0.1#8053' with correct DHCP server IP (and use #53 port)
- /opt/bin/pihole-FTL --config dns.revServers "true,192.168.1.0/24,127.0.0.1#8053,lan"
- # Ports to listen on for the webserver
- # '192.168.1.1' - router IP should be fetched from nvram
- # '127.0.0.1:5080' is required for Pi-hole scripts to access the API without issues
- /opt/bin/pihole-FTL --config webserver.port "127.0.0.1:5080,192.168.1.1:5080,192.168.1.1:5443s"
- # Disable NTP features
- /opt/bin/pihole-FTL --config ntp.ipv4.active false
- /opt/bin/pihole-FTL --config ntp.ipv6.active false
- /opt/bin/pihole-FTL --config ntp.sync.active false
- # Situation specific configuration changes:
- # When user wants to integrate with local Unbound (unbound_manager) - ask user for this?
- # https://github.com/jacklul/entware-pi-hole/wiki/Using-Unbound-as-upstream-DNS-server
- if [ "$unbound_integration" = true ]; then
- echo "Integrating with local Unbound instance..."
- /opt/bin/pihole-FTL --config dns.upstreams "127.0.0.1#53535"
- fi
- # Dedicated interface to run Pi-hole on so that the pi.hole domain can work - ask user for this?
- # https://github.com/jacklul/entware-pi-hole/wiki/Using-dedicated-IP-for-the-web-interface
- if [ "$extra_ip_setup" = true ]; then
- echo "Adding dedicated interface and IP for Pi-hole (pi.hole domain support)..."
- if [ ! -f /jffs/scripts/firewall-start ]; then
- cat <<EOF > /jffs/scripts/firewall-start
- #!/bin/sh
- [ -z "\$1" ] && exit 1
- EOF
- chmod +x /jffs/scripts/firewall-start
- fi
- if ! grep -Fq "# entware-pi-hole" /jffs/scripts/firewall-start; then
- echo "Installing /jffs/scripts/firewall-start script..."
- # I assume this is the correct place to make sure the interface is added
- cat <<EOF > /jffs/scripts/firewall-start
- # entware-pi-hole START
- # These should be read dynamically from nvram...
- ADDRESS=192.168.1.254/24
- INTERFACE=br0
- if ! ip addr show dev "\$INTERFACE" | grep -q "inet \$ADDRESS "; then
- ip -4 addr add "\$ADDRESS" brd + dev "\$INTERFACE" label "\$INTERFACE:pihole"
- fi
- # entware-pi-hole END
- EOF
- fi
- # Interface to listen for DNS on, 'br0' should be fetched from nvram depending on situation
- /opt/bin/pihole-FTL --config dns.interface br0:pihole
- # Bind to given interface
- /opt/bin/pihole-FTL --config dns.listeningMode BIND
- # Force pi.hole domain to have IP we specify
- /opt/bin/pihole-FTL --config dns.reply.host.force4 true
- # The IP address to use for pi.hole domain, must equal the one from firewall-start script
- /opt/bin/pihole-FTL --config dns.reply.host.IPv4 "192.168.1.254"
- # Webserver listening ports
- # The IP address ('192.168.1.254') must equal the one from firewall-start script
- # 127.0.0.1:5080 is required for Pi-hole scripts to access the API without issues
- /opt/bin/pihole-FTL --config webserver.port "127.0.0.1:5080,192.168.1.254:80,192.168.1.254:443s"
- # Alternative - append to the earlier config
- # In case we lose access to pi.hole domain we can access the UI through 192.168.1.1:5080 (or asusrouter.net:5080)
- #current="$(/opt/bin/pihole-FTL --config webserver.port)"
- #/opt/bin/pihole-FTL --config webserver.port "$current,192.168.1.254:80,192.168.1.254:443s"
- fi
- # LAN -> DHCP Server - DNS server fields should be empty
- # 'Forward local domain queries to upstream DNS' should be disabled
- # or at least prompt the user to make those changed
- # Apply changes and start the service
- echo "Launching Pi-hole..."
- service restart_dnsmasq
- /opt/etc/init.d/S65pihole-FTL start
- # Perhaps check with /opt/etc/init.d/S65pihole-FTL check
- # and parse 'pihole status' looking for 'FTL is listening on port' and 'blocking is enabled'
Advertisement
Add Comment
Please, Sign In to add comment