-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgfwlist2dnsmasq.sh
executable file
·59 lines (50 loc) · 1.16 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
#!/bin/sh
#
# Created Time: 2016.12.06 zhangzf
# Translate the gfwlist in base64 to dnsmasq rules with ipset
#
MYDNSIP='127.0.0.1'
MYDNSPORT='5353'
IPSETNAME='gfwlist'
GFWURL="https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"
GFWLIST_TMP="/tmp/gfwlist.txt"
GFWLIST_D_TMP=$(mktemp)
# curl & base64 command path
CURL=$(which curl)
CURLOPT="-s -k -o $GFWLIST_TMP"
BASE64=$(which base64)
c_conf() {
echo "# Updated on $(date '+%F %T')" >$GFWLIST_D_TMP
cat <<-EOF >>$GFWLIST_D_TMP
$(while read LINE; do \
printf 'server=/.%s/%s#%s\n' $LINE $MYDNSIP $MYDNSPORT; \
printf 'ipset=/.%s/%s\n' $LINE $IPSETNAME; \
done)
EOF
}
# download
if [ ! -f $GFWLIST_TMP ]; then
$CURL $CURLOPT $GFWURL
[ "$?" -eq 0 ] || {
echo "Gfwlist download failed."
exit 1
}
fi
# parse gfwlist
$BASE64 -d $GFWLIST_TMP \
| grep -v \
-e '^\s*$' \
-e '^[\[!@@]' \
-e '[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]' \
| sed \
-e s'/^[@|]*//'g \
-e s'/^http[s]*:\/\///'g \
-e s'/[\/\%].*$//'g \
-e s'/[^a-z]\+$//'g \
-e s'/.*\*[^\.]*//'g \
-e s'/^\.//'g 2>/dev/null \
| grep -e '\.' \
| sort -u \
| c_conf
cp $GFWLIST_D_TMP ./dnsmasq_gfwlist.conf -f
rm $GFWLIST_D_TMP -f