-
Notifications
You must be signed in to change notification settings - Fork 4
/
gfwlist2dnsmasq.sh
executable file
·115 lines (110 loc) · 2.51 KB
/
gfwlist2dnsmasq.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/sh
gfwlist="/tmp/gfwlist.txt"
gfwlist_url="https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"
gfwlist_dnsmasq_conf="/usr/local/etc/dnsmasq.d/gfwlist.conf"
host="127.0.0.1"
port="5353"
ipset="gfwlist"
noipset=""
format=""
user_rule=""
skip_domain="/dev/null"
usage() {
cat << EOF
Usage: $0 [-i <url>] [-o <file>] [-h <host>] [-p <port>] [-s <ipset>] [-S] [-f format] [[-u <file>]...] [-k <file>] [-h]
-i <url>
URL of gfwlist.txt,
default is https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt
-o <file>
Write generated dnsmasq configuration file to <file>,
default is /usr/local/etc/dnsmasq.d/gfwlist.conf.
-h <host>
The host of DNS server, default is 127.0.0.1.
-p <port>
The port of DNS server, default is 5353.
-s <ipset>
The ipset of the output, default is gfwlist.
-S
Do not print the ipset line to the output.
-f
The output format for each line,
default is server=/.%domain/%host#%port\nipset=/.%domain/gfwlist\n
-u <file>
User rule file.
-k <file>
Skip domain file.
-h
Display this help.
EOF
}
while getopts ":i:o:h:p:s:Sf:u:k:" o; do
case "${o}" in
i)
gfwlist_url="${OPTARG}"
;;
o)
gfwlist_dnsmasq_conf="${OPTARG}"
;;
h)
host="${OPTARG}"
;;
p)
port="${OPTARG}"
;;
s)
ipset="${OPTARG}"
;;
S)
noipset="noipset"
;;
f)
format="${OPTARG}"
;;
u)
if [ -z "${OPTARG}" -o ! -r "${OPTARG}" ]; then
echo "\"${OPTARG}\" does not exist."
usage
exit 1
fi
user_rule="${user_rule} ${OPTARG}"
;;
k)
if [ -z "${OPTARG}" -o ! -r "${OPTARG}" ]; then
echo "\"${OPTARG}\" does not exist."
usage
exit 1
fi
skip_domain="${OPTARG}"
;;
h)
usage
exit
;;
*)
usage
exit
;;
esac
done
shift $((OPTIND-1))
tmp_gfwlist_dnsmasq_conf="/tmp/gfwlist.conf.`cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1`"
curl -sf -o "${gfwlist}" \
"${gfwlist_url}" \
&& base64 -d "${gfwlist}" \
| awk -f $(cd "$(dirname "$0")"; pwd)/gfwlist2domainlist.awk \
- ${user_rule} \
| grep -F -v -f "${skip_domain}" \
| awk -f $(cd "$(dirname "$0")"; pwd)/domainlist2dnsmasq.awk \
-v "host=${host}" \
-v "port=${port}" \
-v "ipset=${ipset}" \
-v "noipset=${noipset}" \
-v "format=${format}" \
- \
> "${tmp_gfwlist_dnsmasq_conf}" 2>/dev/null \
&& \
(
diff -I '^#.*' "${gfwlist_dnsmasq_conf}" "${tmp_gfwlist_dnsmasq_conf}" || \
(cp "${tmp_gfwlist_dnsmasq_conf}" "${gfwlist_dnsmasq_conf}" && service dnsmasq restart)
)
rm -f "${tmp_gfwlist_dnsmasq_conf}"