-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·273 lines (243 loc) · 8.12 KB
/
build.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env bash
IFS='.' read -r __major __minor _ <<< "${BASH_VERSION:-0.0.0}"
if [ "$__major" -lt 4 ] || { [ "$__major" -eq 4 ] && [ "$__minor" -lt 3 ]; }; then
echo "Error: this script requires Bash version 4.3 or higher. Your current version is ${BASH_VERSION:-unknown}." >&2
echo "" >&2
echo "$BASH --version" >&2
"$BASH" --version >&2
exit 1
fi
set -euo pipefail
shopt -s nullglob
cd "$(dirname "$0")"
declare -A files
files[auto-proxy.txt]=https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt
files[ipv4-rules-direct.txt]=https://gaoyifan.github.io/china-operator-ip/china.txt
files[ipv6-rules-direct.txt]=https://gaoyifan.github.io/china-operator-ip/china6.txt
declare -a files_to_be_deleted=()
function cleanup() {
local retval="$?"
local file
for file in "${files_to_be_deleted[@]}"; do
if [[ -e "${file}" ]]; then
echo "Remove $file"
rm -v "${file}"
fi
done
exit "$retval"
} >&2
trap cleanup EXIT ERR SIGINT
for f in "${!files[@]}"; do
url="${files[$f]}"
if [[ ! -e "$f" ]]; then
files_to_be_deleted=("$f" "${f}.tmp")
if command -v wget &>/dev/null; then
wget -O "${f}.tmp" "$url"
elif command -v curl &>/dev/null; then
curl --output "${f}.tmp" "$url"
else
echo "Error: please install wget or curl."
exit 1
fi
if [[ "$f" = auto-proxy.txt ]]; then
base64 -d <"${f}.tmp" > "$f"
else
mv "${f}.tmp" "$f"
fi
fi
done
files_to_be_deleted=()
domain_segments() {
local domain="$1"
while [[ -n "${domain}" ]]; do
echo "$domain"
[[ "${domain-}" = *.* ]] || break
domain="${domain#*.}"
done
}
actual="$(domain_segments foo.bar.example.com)"
expected="foo.bar.example.com bar.example.com example.com com"
if [[ "$actual" = "$expected" ]]; then
echo "Fail: test for function domain_segments"
echo " expected: $expected"
echo " actual: $actual"
exit 1
fi >&2
expand_ipv6() {
local ipv6="$1"
local full_ipv6=""
if [[ "$ipv6" == *"::"* ]]; then
local left_part="${ipv6%%::*}"
local right_part="${ipv6##*::}"
local left_segments=(${left_part//:/ })
local right_segments=(${right_part//:/ })
local num_missing=$(( 8 - ${#left_segments[@]} - ${#right_segments[@]} ))
for segment in "${left_segments[@]}"; do
full_ipv6+="$(printf "%04x" "0x$segment"):"
done
for ((i=0; i<num_missing; i++)); do
full_ipv6+="0000:"
done
for segment in "${right_segments[@]}"; do
full_ipv6+="$(printf "%04x" "0x$segment"):"
done
echo "${full_ipv6%:}"
else
echo "$ipv6"
fi
}
generate_pac() {
local jsfile="$1"
declare -A domain_rules
local file rule domain
for file in domain-rules-*.txt; do
rule="${file#domain-rules-}"
rule="${rule%.txt}"
while IFS= read -r domain; do
domain="${domain%%#*}"
domain="${domain// }"
[[ -n "$domain" ]] || continue
domain_rules["$domain"]="$rule";
done < "$file"
done
for file in auto-proxy*.txt; do
local line item rule parent_rule
while read -r line; do
echo "$line" >&2
rule=
case "$line" in
(/*)
echo "Skip regrex rul: $line"
;;
(\!*|\[*)
: comment
;;
(@@\|\|*)
line="${line#@}"
;&
(@@\|*)
line="${line#@?|}"
line="${line#*://}"
line="${line%%\%2F*}"
domain="${line%%/*}"
rule=direct
echo "==> direct access: $domain"
;;
(\|\|*)
line="${line#|}"
;&
(\|*)
line="${line#|}"
line="${line#*://}"
;&
([.a-z0-9]*)
line="${line#.}"
line="${line%%\%2F*}"
domain="${line%%/*}"
domain="${domain#*\**.}"
rule=proxy
echo "==> proxy access: $domain"
;;
(*)
[[ "$line" =~ ^[[:space:]]*$ ]] ||
echo "Skip: $line"
;;
esac >&2
[[ -n "${rule}" ]] || continue
[[ -z "${domain_rules[$domain]-}" ]] || continue
parent_rule=
for item in $(domain_segments "$domain"); do
if [[ -n "${domain_rules[$item]-}" ]]; then
parent_rule="${domain_rules[$item]}"
break
fi
done
if [[ -z "$parent_rule" ]] || [[ "$parent_rule" != "$rule" ]]; then
domain_rules["$domain"]="$rule"
fi
done < <(sed '/URL Keywords/,/^!/d' "$file")
done
local domain rule parent_rule
declare -a segments
for domain in "${!domain_rules[@]}"; do
rule="${domain_rules[$domain]}"
segments=($(domain_segments "$domain" 2>/dev/null))
parent_rule=
for item in ${segments[@]:1}; do
parent_rule="${domain_rules[$item]-}"
[[ -z "${parent_rule-}" ]] || break
done
if [[ "${parent_rule-}" = "${rule}" ]]; then
unset "domain_rules[$domain]"
fi
done
sed -n '1,/ begin of ipv4 networks$/p' "$jsfile"
for file in ipv4-rules-*.txt; do
rule="${file#ipv?-rules-}"
rule="${rule%%.*}"
[[ "$rule" = @(blocked|direct|proxy) ]] || rule="\"$rule\""
while IFS= read -r line; do
line="${line%%#*}"
line="${line// }"
echo "$rule: $line" >&2
while IFS=/ read -r ip prefix; do
while IFS=. read n1 n2 n3 n4; do
printf " [0x%02x%02x%02x%02x, %s, %s], // %s\n" "${n1:-0}" "${n2:-0}" "${n3:-0}" "${n4:-0}" "$prefix" "$rule" "$line"
done <<< "$ip"
done <<< "${line}";
done < "$file"
done | sort -n
sed -n '/ end of ipv4 networks$/,/ begin of ipv6 networks$/p' "$jsfile"
for file in ipv6-rules-*.txt; do
rule="${file#ipv?-rules-}"
rule="${rule%%.*}"
[[ "$rule" = @(blocked|direct|proxy) ]] || rule="\"$rule\""
while IFS= read -r line; do
line="${line%%#*}"
line="${line// }"
echo "$rule: $line" >&2
local ipv6="${line%%/*}"
local prefix="${line##*/}"
local expanded_ipv6=$(expand_ipv6 "$ipv6")
local full_hex="${expanded_ipv6//:/}"
echo " [0x${full_hex:0:16}n, 0x${full_hex:16:16}n, ${prefix}, ${rule}], // ${line}"
done < "$file"
done | sort
sed -n '/ end of ipv6 networks$/,/ begin of proxy rules$/p' "$jsfile"
local domain
for domain in "${!domain_rules[@]}"; do
rule="${domain_rules[$domain]}"
[[ "$rule" = @(blocked|direct|proxy) ]] || rule="\"$rule\""
printf " \"%s\": %s,\n" "$domain" "$rule"
done | sort -n
sed -n '/ end of proxy rules$/,/ begin of regexp rules$/p' "$jsfile"
for file in domain-regexp*.txt; do
rule=""
while IFS= read -r line; do
line="${line%%#*}"
line="${line// }"
[[ -n "$line" ]] || continue
if [[ "$line" = \[*\] ]]; then
rule="${line#[}"
rule="${rule%]}"
[[ "$rule" = @(blocked|direct|proxy) ]] || rule="\"$rule\""
elif [[ -n "$rule" ]]; then
printf " [/%s/, %s],\n" "$line" "$rule"
fi
done < "$file"
done
sed -n '/ end of regexp rules$/,$p' "$jsfile"
}
is_up_to_date=true
files_to_be_deleted=(proxy.pac)
for file in "$0" *.js *.txt; do
if [ "$file" -nt proxy.pac ]; then
is_up_to_date=false
break;
fi
done
"$is_up_to_date" || generate_pac "./proxy.js" > proxy.pac
if command -v node &>/dev/null; then
node proxy.pac test
fi
files_to_be_deleted=()