From fe76bf3564e72b148a33fae91a979f08724f1a64 Mon Sep 17 00:00:00 2001 From: Lienol Date: Sun, 21 Jun 2020 22:13:28 +0800 Subject: [PATCH] dnsmasq: add dns redirect option --- .../files/zzz-default-settings | 4 ---- .../network/services/dnsmasq/files/dhcp.conf | 1 + .../services/dnsmasq/files/dnsmasq.init | 23 +++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/package/lean/default-settings/files/zzz-default-settings b/package/lean/default-settings/files/zzz-default-settings index e913b8072c..9410e6cc85 100755 --- a/package/lean/default-settings/files/zzz-default-settings +++ b/package/lean/default-settings/files/zzz-default-settings @@ -32,10 +32,6 @@ sed -i "s/# //g" /etc/opkg/distfeeds.conf sed -i 's/root::0:0:99999:7:::/root:$1$V4UetPzk$CYXluq4wUazHjmCDBCqXF.:0:0:99999:7:::/g' /etc/shadow sed -i 's|root:x:0:0:root:/root:/bin/ash|root:x:0:0:root:/root:/bin/bash|g' /etc/passwd -sed -i '/REDIRECT --to-ports 53/d' /etc/firewall.user -echo "iptables -t nat -A PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53" >> /etc/firewall.user -echo "iptables -t nat -A PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53" >> /etc/firewall.user - sed -i '/option disabled/d' /etc/config/wireless sed -i '/set wireless.radio${devidx}.disabled/d' /lib/wifi/mac80211.sh wifi up diff --git a/package/network/services/dnsmasq/files/dhcp.conf b/package/network/services/dnsmasq/files/dhcp.conf index c2b4a8c56f..cc28a08bd6 100644 --- a/package/network/services/dnsmasq/files/dhcp.conf +++ b/package/network/services/dnsmasq/files/dhcp.conf @@ -21,6 +21,7 @@ config dnsmasq #list bogusnxdomain '64.94.110.11' option localservice 1 # disable to allow DNS requests from non-local subnets option filter_aaaa 0 + option dns_redirect 1 config dhcp lan option interface lan diff --git a/package/network/services/dnsmasq/files/dnsmasq.init b/package/network/services/dnsmasq/files/dnsmasq.init index bebcfa517c..8b31539e7d 100644 --- a/package/network/services/dnsmasq/files/dnsmasq.init +++ b/package/network/services/dnsmasq/files/dnsmasq.init @@ -1084,6 +1084,10 @@ dnsmasq_start() procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile procd_close_instance + + config_get_bool dns_redirect "$cfg" dns_redirect 0 + config_get dns_port "$cfg" port 53 + [ "$dns_redirect" = 1 ] && iptables -t nat -A PREROUTING -m comment --comment "DNSMASQ" -p udp --dport 53 -j REDIRECT --to-ports $dns_port } dnsmasq_stop() @@ -1101,6 +1105,23 @@ dnsmasq_stop() rm -f ${BASEDHCPSTAMPFILE}.${cfg}.*.dhcp } +iptables_clear() +{ + nums=$(iptables -t nat -n -L PREROUTING 2>/dev/null | grep -c "DNSMASQ") + if [ -n "$nums" ]; then + until [ "$nums" = 0 ] + do + rules=$(iptables -t nat -n -L PREROUTING --line-num 2>/dev/null | grep "DNSMASQ" | awk '{print $1}') + for rule in $rules + do + iptables -t nat -D PREROUTING $rule 2> /dev/null + break + done + nums=$(expr $nums - 1) + done + fi +} + add_interface_trigger() { local interface ignore @@ -1153,6 +1174,7 @@ start_service() { } reload_service() { + iptables_clear rc_procd start_service "$@" procd_send_signal dnsmasq "$@" } @@ -1179,4 +1201,5 @@ stop_service() { else config_foreach dnsmasq_stop dnsmasq fi + iptables_clear }