This repository has been archived by the owner on Sep 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
dnsmasq.sh
executable file
·69 lines (54 loc) · 1.5 KB
/
dnsmasq.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
ipv6_iface() {
ip -6 route | grep '^default' | sed 's/.*dev[[:space:]]\+\([^[:space:]]\+\).*/\1/'
}
has_global_ipv6() {
local x
for x in $(ipv6_iface); do
if ip -6 addr show dev "$x" | grep -q 'scope global'; then
return 0
fi
done
return 1
}
get_ext_ip() {
dig +short myip.opendns.com @resolver1.opendns.com 2> /dev/null
}
get_ext_ipv6() {
if has_global_ipv6; then
dig AAAA +short myip.opendns.com @2620:0:ccc::2 2> /dev/null
fi
}
cache_server=0
[ "$1" == "--cache" ] && cache_server=1
if [ "$cache_server" -eq 1 ]; then
conf=/tmp/dnsmasq-cache.conf
resolv=/tmp/dnsmasq-cache.resolv
else
conf=/tmp/dnsmasq.conf
resolv=/tmp/dnsmasq.resolv
fi
rm -f $conf $resolve
cat > $conf << EOF
keep-in-foreground
no-hosts
resolv-file=$resolv
EOF
if [ "$cache_server" -eq 1 ]; then
echo "port=5399" >> $conf
iptables -w -t nat -A OUTPUT -s 127.0.0.1 -p udp -m udp --dport 53 -j REDIRECT --to 5399
iptables -w -t nat -A OUTPUT -s 127.0.0.1 -p tcp -m tcp --dport 53 -j REDIRECT --to 5399
else
EXT_IP=${EXT_IP:-$(get_ext_ip)}
EXT_IPV6=${EXT_IPV6:-$(get_ext_ipv6)}
for x in $(cat /opt/proxflix/domains); do
[[ -n "$EXT_IP" ]] && echo "address=/$x/$EXT_IP" >> $conf
[[ -n "$EXT_IPV6" ]] && echo "address=/$x/$EXT_IPV6" >> $conf
done
fi
DNS_SERVER="${DNS_SERVER:-8.8.8.8,8.8.4.4}"
DNS_SERVER="${DNS_SERVER//,/ }"
for x in $DNS_SERVER; do
echo "nameserver $x" >> $resolv
done
exec dnsmasq -C $conf