#!/bin/sh
# for each TOR client, replace the rule for the redirected TOR DNS port,
# this moves the redirected port 9053 (TOR) to 9953 (dnsmasq)
tor_dnsport=$(nvram get Tor_dnsport)
dnsmasq_dnsport=9953
line_nums=$(iptables -t nat -nL PREROUTING --line-numbers | grep -F "redir ports $tor_dnsport" | cut -d' ' -f1)
for line_num in $line_nums; do
old_rule=$(iptables -t nat -S PREROUTING $line_num | cut -d' ' -f3-)
new_rule=${old_rule/REDIRECT --to-ports $tor_dnsport/REDIRECT --to-ports $dnsmasq_dnsport}
iptables -t nat -R PREROUTING $line_num $new_rule
done
# for each TOR client, prevent banned addresses from being routed through the Tor network
tor_transport=$(nvram get Tor_transport)
line_nums=$(iptables -t nat -nL PREROUTING --line-numbers | grep -F "redir ports $tor_transport" | cut -d' ' -f1)
lines_inserted=0
for line_num in $line_nums; do
let line_num+=lines_inserted
existing_rule=$(iptables -t nat -S PREROUTING $line_num | cut -d' ' -f3-)
# this rule will block Windows Updates and Microsoft Telemetry, for TOR clients
ipset_name="mstracking"
if [ -f /jffs/home/${ipset_name}.txt ]; then
ipset -N ${ipset_name} iphash > /dev/null 2>&1
new_rule=${existing_rule/-j REDIRECT --to-ports $tor_transport/-m set --match-set ${ipset_name} dst -j RETURN}
iptables -t nat -C PREROUTING $new_rule > /dev/null 2>&1
[ $? -eq 1 ] && iptables -t nat -I PREROUTING $line_num $new_rule && let lines_inserted++
fi
#
# TODO: add additional rules here
#
done