Skip to content

Commit 2100a32

Browse files
authored
Add DNS traffic routing (#582)
1 parent eddc975 commit 2100a32

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

nspawn-container/scripts/10-setup-network.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ IPV4_GW="10.0.5.1/24"
2525
IPV6_IP="fd62:89a2:fda9:e23::3"
2626
IPV6_GW="fd62:89a2:fda9:e23::1/64"
2727

28+
# Set this to the interface(s) on which you want DNS TCP/UDP port 53 traffic
29+
# re-routed through this container. Separate interfaces with spaces.
30+
# This is useful when runinng a DNS service, like Adguard Home
31+
# e.g. "br0" or "br0 br1" etc.
32+
FORCED_INTFC=""
33+
2834
## END OF CONFIGURATION
2935

3036
# set VLAN bridge promiscuous
@@ -56,3 +62,23 @@ if ! grep -qxF "interface=br${VLAN}.mac" /run/dnsmasq.conf.d/custom.conf; then
5662
echo "interface=br${VLAN}.mac" >>/run/dnsmasq.conf.d/custom.conf
5763
kill -9 "$(cat /run/dnsmasq.pid)"
5864
fi
65+
66+
# (optional) IPv4 force DNS (TCP/UDP 53) through DNS container
67+
for intfc in ${FORCED_INTFC}; do
68+
if [ -d "/sys/class/net/${intfc}" ]; then
69+
for proto in udp tcp; do
70+
prerouting_rule="PREROUTING -i ${intfc} -p ${proto} ! -s ${IPV4_IP} ! -d ${IPV4_IP} --dport 53 -j LOG --log-prefix [DNAT-${intfc}-${proto}]"
71+
iptables -t nat -C ${prerouting_rule} 2>/dev/null || iptables -t nat -A ${prerouting_rule}
72+
prerouting_rule="PREROUTING -i ${intfc} -p ${proto} ! -s ${IPV4_IP} ! -d ${IPV4_IP} --dport 53 -j DNAT --to ${IPV4_IP}"
73+
iptables -t nat -C ${prerouting_rule} 2>/dev/null || iptables -t nat -A ${prerouting_rule}
74+
75+
# (optional) IPv6 force DNS (TCP/UDP 53) through DNS container
76+
if [ -n "${IPV6_IP}" ]; then
77+
prerouting_rule="PREROUTING -i ${intfc} -p ${proto} ! -s ${IPV6_IP} ! -d ${IPV6_IP} --dport 53 -j LOG --log-prefix [DNAT-${intfc}-${proto}]"
78+
ip6tables -t nat -C ${prerouting_rule} 2>/dev/null || ip6tables -t nat -A ${prerouting_rule}
79+
prerouting_rule="PREROUTING -i ${intfc} -p ${proto} ! -s ${IPV6_IP} ! -d ${IPV6_IP} --dport 53 -j DNAT --to ${IPV6_IP}"
80+
ip6tables -t nat -C ${prerouting_rule} 2>/dev/null || ip6tables -t nat -A ${prerouting_rule}
81+
fi
82+
done
83+
fi
84+
done

0 commit comments

Comments
 (0)