-
Notifications
You must be signed in to change notification settings - Fork 10
/
gfw.sh
executable file
·102 lines (86 loc) · 2.23 KB
/
gfw.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
#! /bin/zsh
# Generated by gfwlist2pac
# created by @clowwindy via python
# modified by @cube via native zsh
# https://github.com/cuber/gfwlist2pac
# url
XLDURL="https://publicsuffix.org/list/effective_tld_names.dat"
GFWURL="https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"
# socks5 proxy ssh -D, shadowsocks or others
PROXY="127.0.0.1:1080"
# curl & openssl cli command path
CURL=/usr/bin/curl
CURLOPT=(-s -x socks5://$PROXY)
OPENSSL=/usr/bin/openssl
# get current dirname
DIR=$(cd $(dirname $0); pwd)
# load util functions
source $DIR/util.sh
typeset -A XLD
typeset -A UNIQ
for i in $($CURL $CURLOPT $XLDURL | grep -v -e '^\s*$' -e '^/'); do XLD[$i]=1; done
# output pac file
PAC=$1
if [ -z $PAC ]; then
echo "Usage. ./gfw.sh [filename.pac]"
echo " Eg. ./gfw.sh release/proxy.pac"
echo " The proxy.pac will be generated to release/proxy.pac"
exit 1
fi
# make sure pac dirname exists
PACDIR=$(dirname $PAC)
if [ ! -d $PACDIR ]; then mkdir -p $PACDIR; fi
cat > $PAC <<EOF
// Generated by gfwlist2pac
// created by @clowwindy via python
// modified by @cube via native zsh
// https://github.com/cuber/gfwlist2pac
var domains = {
EOF
cat $DIR/custom.txt \
| grep -v '^\s*$' \
| sed -e s'/^\s*//'g -e s'/\s*$//'g \
| valid
$CURL $CURLOPT $GFWURL \
| $OPENSSL base64 -d \
| urldecode \
| grep -v \
-e 'google' \
-e '^\s*$' \
-e '^[\[!]' \
| sed \
-e s'/^[@|]*//'g \
-e s'/^http[s]*:\/\///'g \
-e s'/\/.*$//'g \
-e s'/\*.*$//'g \
-e s'/^\s*//'g \
-e s'/\s*$//'g \
-e s'/^\.//'g 2>/dev/null \
| grep \
-e '\.' \
| grep -v \
-e '^\s*$' \
-e '^!' \
| valid
DOMAINS=()
for i in ${(k)UNIQ}; do DOMAINS+=(" '$i': 1"); done
echo ${(j:,\n:)DOMAINS} >> $PAC
cat >> $PAC <<EOF
} // end of domains
// proxy failover
var proxy = 'SOCKS5 $PROXY; SOCKS $PROXY; DIRECT;'
var direct = 'DIRECT;'
// function of proxy router
function FindProxyForURL(url, host) {
// restrict all google related domains to proxy
if (/google/i.test(host)) return proxy;
// recursive detection domains
do {
if (domains.hasOwnProperty(host)) return proxy;
off = host.indexOf('.') + 1;
host = host.slice(off);
} while (off >= 1);
return direct;
}
EOF
exit 0