forked from lisaac/tproxy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathss-tproxy
executable file
·579 lines (506 loc) · 28.3 KB
/
ss-tproxy
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/bin/bash
ss_tproxy_config='/etc/ss-tproxy/ss-tproxy.conf'
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
[ ! -f "$ss_tproxy_config" ] && { echo "[ERR] No such file or directory: '$ss_tproxy_config'" 1>&2; exit 1; } || source "$ss_tproxy_config"
[ ! -f "$file_gfwlist_txt" ] && { echo "[ERR] No such file or directory: '$file_gfwlist_txt'" 1>&2; exit 1; }
[ ! -f "$file_gfwlist_ext" ] && { echo "[ERR] No such file or directory: '$file_gfwlist_ext'" 1>&2; exit 1; }
[ ! -f "$file_chnroute_txt" ] && { echo "[ERR] No such file or directory: '$file_chnroute_txt'" 1>&2; exit 1; }
[ ! -f "$file_chnroute_set" ] && { echo "[ERR] No such file or directory: '$file_chnroute_set'" 1>&2; exit 1; }
function check_command {
[ "$opts_ss_netstat" = "auto" ] && { command -v ss &>/dev/null && netstat="ss" || netstat="netstat"; }
[ "$opts_ss_netstat" = "netstat" ] && netstat="netstat"
[ "$opts_ss_netstat" = "ss" ] && netstat="ss"
command -v ip &>/dev/null || { echo "[ERR] Command not found: 'ip'" 1>&2; exit 1; }
command -v dnsmasq &>/dev/null || { echo "[ERR] Command not found: 'dnsmasq'" 1>&2; exit 1; }
case "$mode" in
gfwlist)
command -v curl &>/dev/null || { echo "[WRN] Command not found: 'curl'" 1>&2; }
command -v base64 &>/dev/null || { echo "[WRN] Command not found: 'base64'" 1>&2; }
command -v ipset &>/dev/null || { echo "[ERR] Command not found: 'ipset'" 1>&2; exit 1; }
command -v perl &>/dev/null || { echo "[ERR] Command not found: 'perl'" 1>&2; exit 1; }
;;
chnroute)
command -v curl &>/dev/null || { echo "[WRN] Command not found: 'curl'" 1>&2; }
command -v ipset &>/dev/null || { echo "[ERR] Command not found: 'ipset'" 1>&2; exit 1; }
command -v chinadns &>/dev/null || { echo "[ERR] Command not found: 'chinadns'" 1>&2; exit 1; }
;;
esac
}
function update_chnonly {
data="$(curl -4sSkL https://raw.github.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf)"
[ "$data" ] || { echo "update failed, please check the error output of curl"; exit 1; }
echo "$data" | perl -ne 'm@/\K.+(?=/)@; print "$&\n"' >$file_gfwlist_txt
}
function update_gfwlist {
data="$(curl -4sSkL https://raw.github.com/gfwlist/gfwlist/master/gfwlist.txt)"
[ "$data" ] || { echo "update failed, please check the error output of curl"; exit 1; }
base64 -d </dev/null &>/dev/null && base64='base64 -d'
base64 --decode </dev/null &>/dev/null && base64='base64 --decode'
[ "$base64" ] || { echo "[ERR] Command not found: 'base64'" 1>&2; exit 1; }
echo "$data" | $base64 | { perl -pe '
if (/URL Keywords/i) { $null = <> until $null =~ /^!/ }
s#^\s*+$|^!.*+$|^@@.*+$|^\[AutoProxy.*+$|^/.*/$##i;
s@^\|\|?|\|$@@;
s@^https?:/?/?@@i;
s@(?:/|%).*+$@@;
s@\*[^.*]++$@\n@;
s@^.*?\*[^.]*+(?=[^*]+$)@@;
s@^\*?\.|^.*\.\*?$@@;
s@(?=[^0-9a-zA-Z.-]).*+$@@;
s@^\d+\.\d+\.\d+\.\d+(?::\d+)?$@@;
s@^\s*+$@@'
echo 'twimg.edgesuite.net'
echo -e 'blogspot.ae\nblogspot.al\nblogspot.am\nblogspot.ba\nblogspot.be\nblogspot.bg\nblogspot.bj\nblogspot.ca\nblogspot.cat\nblogspot.cf\nblogspot.ch\nblogspot.cl\nblogspot.co.at\nblogspot.co.id\nblogspot.co.il\nblogspot.co.ke\nblogspot.com\nblogspot.com.ar\nblogspot.com.au\nblogspot.com.br\nblogspot.com.by\nblogspot.com.co\nblogspot.com.cy\nblogspot.com.ee\nblogspot.com.eg\nblogspot.com.es\nblogspot.com.mt\nblogspot.com.ng\nblogspot.com.tr\nblogspot.com.uy\nblogspot.co.nz\nblogspot.co.uk\nblogspot.co.za\nblogspot.cv\nblogspot.cz\nblogspot.de\nblogspot.dk\nblogspot.fi\nblogspot.fr\nblogspot.gr\nblogspot.hk\nblogspot.hr\nblogspot.hu\nblogspot.ie\nblogspot.in\nblogspot.is\nblogspot.it\nblogspot.jp\nblogspot.kr\nblogspot.li\nblogspot.lt\nblogspot.lu\nblogspot.md\nblogspot.mk\nblogspot.mr\nblogspot.mx\nblogspot.my\nblogspot.nl\nblogspot.no\nblogspot.pe\nblogspot.pt\nblogspot.qa\nblogspot.re\nblogspot.ro\nblogspot.rs\nblogspot.ru\nblogspot.se\nblogspot.sg\nblogspot.si\nblogspot.sk\nblogspot.sn\nblogspot.td\nblogspot.tw\nblogspot.ug\nblogspot.vn'
echo -e 'google.ac\ngoogle.ad\ngoogle.ae\ngoogle.al\ngoogle.am\ngoogle.as\ngoogle.at\ngoogle.az\ngoogle.ba\ngoogle.be\ngoogle.bf\ngoogle.bg\ngoogle.bi\ngoogle.bj\ngoogle.bs\ngoogle.bt\ngoogle.by\ngoogle.ca\ngoogle.cat\ngoogle.cc\ngoogle.cd\ngoogle.cf\ngoogle.cg\ngoogle.ch\ngoogle.ci\ngoogle.cl\ngoogle.cm\ngoogle.cn\ngoogle.co.ao\ngoogle.co.bw\ngoogle.co.ck\ngoogle.co.cr\ngoogle.co.id\ngoogle.co.il\ngoogle.co.in\ngoogle.co.jp\ngoogle.co.ke\ngoogle.co.kr\ngoogle.co.ls\ngoogle.com\ngoogle.co.ma\ngoogle.com.af\ngoogle.com.ag\ngoogle.com.ai\ngoogle.com.ar\ngoogle.com.au\ngoogle.com.bd\ngoogle.com.bh\ngoogle.com.bn\ngoogle.com.bo\ngoogle.com.br\ngoogle.com.bz\ngoogle.com.co\ngoogle.com.cu\ngoogle.com.cy\ngoogle.com.do\ngoogle.com.ec\ngoogle.com.eg\ngoogle.com.et\ngoogle.com.fj\ngoogle.com.gh\ngoogle.com.gi\ngoogle.com.gt\ngoogle.com.hk\ngoogle.com.jm\ngoogle.com.kh\ngoogle.com.kw\ngoogle.com.lb\ngoogle.com.lc\ngoogle.com.ly\ngoogle.com.mm\ngoogle.com.mt\ngoogle.com.mx\ngoogle.com.my\ngoogle.com.na\ngoogle.com.nf\ngoogle.com.ng\ngoogle.com.ni\ngoogle.com.np\ngoogle.com.om\ngoogle.com.pa\ngoogle.com.pe\ngoogle.com.pg\ngoogle.com.ph\ngoogle.com.pk\ngoogle.com.pr\ngoogle.com.py\ngoogle.com.qa\ngoogle.com.sa\ngoogle.com.sb\ngoogle.com.sg\ngoogle.com.sl\ngoogle.com.sv\ngoogle.com.tj\ngoogle.com.tr\ngoogle.com.tw\ngoogle.com.ua\ngoogle.com.uy\ngoogle.com.vc\ngoogle.com.vn\ngoogle.co.mz\ngoogle.co.nz\ngoogle.co.th\ngoogle.co.tz\ngoogle.co.ug\ngoogle.co.uk\ngoogle.co.uz\ngoogle.co.ve\ngoogle.co.vi\ngoogle.co.za\ngoogle.co.zm\ngoogle.co.zw\ngoogle.cv\ngoogle.cz\ngoogle.de\ngoogle.dj\ngoogle.dk\ngoogle.dm\ngoogle.dz\ngoogle.ee\ngoogle.es\ngoogle.fi\ngoogle.fm\ngoogle.fr\ngoogle.ga\ngoogle.ge\ngoogle.gf\ngoogle.gg\ngoogle.gl\ngoogle.gm\ngoogle.gp\ngoogle.gr\ngoogle.gy\ngoogle.hn\ngoogle.hr\ngoogle.ht\ngoogle.hu\ngoogle.ie\ngoogle.im\ngoogle.io\ngoogle.iq\ngoogle.is\ngoogle.it\ngoogle.je\ngoogle.jo\ngoogle.kg\ngoogle.ki\ngoogle.kz\ngoogle.la\ngoogle.li\ngoogle.lk\ngoogle.lt\ngoogle.lu\ngoogle.lv\ngoogle.md\ngoogle.me\ngoogle.mg\ngoogle.mk\ngoogle.ml\ngoogle.mn\ngoogle.ms\ngoogle.mu\ngoogle.mv\ngoogle.mw\ngoogle.ne\ngoogle.net\ngoogle.nl\ngoogle.no\ngoogle.nr\ngoogle.nu\ngoogle.org\ngoogle.pl\ngoogle.pn\ngoogle.ps\ngoogle.pt\ngoogle.ro\ngoogle.rs\ngoogle.ru\ngoogle.rw\ngoogle.sc\ngoogle.se\ngoogle.sh\ngoogle.si\ngoogle.sk\ngoogle.sm\ngoogle.sn\ngoogle.so\ngoogle.sr\ngoogle.st\ngoogle.td\ngoogle.tg\ngoogle.tk\ngoogle.tl\ngoogle.tm\ngoogle.tn\ngoogle.to\ngoogle.tt\ngoogle.vg\ngoogle.vu\ngoogle.ws'
} | sort | uniq >$file_gfwlist_txt
}
function update_chnroute {
data="$(curl -4sSkL https://raw.github.com/17mon/china_ip_list/master/china_ip_list.txt)"
[ "$data" ] || { echo "update failed, please check the error output of curl"; exit 1; }
echo "$data" | grep -Ev '^[ \t]*$|^[ \t]*#' >$file_chnroute_txt
echo '-N chnroute hash:net' >$file_chnroute_set; sed -r 's/^.+$/-A chnroute &/' $file_chnroute_txt >>$file_chnroute_set
reserved_address=('0.0.0.0/8' '10.0.0.0/8' '127.0.0.0/8' '169.254.0.0/16' '172.16.0.0/12' '192.168.0.0/16' '224.0.0.0/4' '240.0.0.0/4')
for addr in "${reserved_address[@]}"; do echo "$addr" >>$file_chnroute_txt; done
}
function flush_dnscache {
if [ "$opts_ss_netstat" != "netstat" -a "$(command -v ss &>/dev/null && echo 'true')" ]; then
ss -lnpu | awk -F, '/:60053[ \t].+"dnsmasq"/ {print $(NF-1)}' | awk -F= '{print $2}' | sort | uniq | xargs kill -HUP &>/dev/null
else
netstat -lnpu | awk -F/ '/:60053[ \t].+dnsmasq/ {print $(NF-1)}' | awk '{print $NF}' | sort | uniq | xargs kill -HUP &>/dev/null
fi
}
function start_proxy {
eval "$proxy_runcmd"
}
function start_resolver {
for server in "${proxy_server[@]}"; do
if [ $(grep -Ec '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' <<<"$server") -eq 0 ]; then
server_addrs+=("/$server/$(ping -nq -c1 -t1 -W1 $server | head -n1 | awk -F'[()]' '{print $2}')")
fi
done
case "$mode" in
global)
dnsmasq -C <(cat <<EOF
$([ "$dnsmasq_log_enable" = 'true' ] && echo 'log-queries')
log-facility = $dnsmasq_log_file
log-async = 20
domain-needed
cache-size = $dnsmasq_cache_size
$([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
no-negcache
addn-hosts=$dnsmasq_addn_hosts
no-resolv
port = 60053
server = ${dns_remote/:/#}
$(for server_addr in "${server_addrs[@]}"; do echo "address = $server_addr"; done)
EOF
)
;;
gfwlist)
dnsmasq -C <(cat <<EOF
$([ "$dnsmasq_log_enable" = 'true' ] && echo 'log-queries')
log-facility = $dnsmasq_log_file
log-async = 20
domain-needed
cache-size = $dnsmasq_cache_size
$([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
no-negcache
addn-hosts=$dnsmasq_addn_hosts
no-resolv
port = 60053
server = $dns_direct
$(for server_addr in "${server_addrs[@]}"; do echo "address = $server_addr"; done)
$(perl -pe "s@^.*+\$@server=/$&/${dns_remote/:/#}\nipset=/$&/gfwlist@" $file_gfwlist_txt <(
perl -ne 'print unless m@^\s|\s\n$|^#|^\d++\.\d++\.\d++\.\d++(?:/\d++)?$@' $file_gfwlist_ext
))
EOF
)
;;
chnroute)
temp_chnroute_txt=$(mktemp)
cat $file_chnroute_txt >$temp_chnroute_txt
echo "$dns_direct/32" >>$temp_chnroute_txt
for intranet in "${ipts_intranet[@]}"; do echo "$intranet" >>$temp_chnroute_txt; done
chinadns_params="-b 0.0.0.0 -p 65353 -s $dns_direct,$dns_remote -c $temp_chnroute_txt -y 0"
[ "$chinadns_mutation" = 'true' ] && chinadns_params+=' -m'
[ "$chinadns_verbose" = 'true' ] && chinadns_params+=' -v'
(chinadns $chinadns_params </dev/null &>>$chinadns_logfile &)
dnsmasq -C <(cat <<EOF
$([ "$dnsmasq_log_enable" = 'true' ] && echo 'log-queries')
log-facility = $dnsmasq_log_file
log-async = 20
domain-needed
cache-size = $dnsmasq_cache_size
$([ $(dnsmasq --help | grep -c min-cache-ttl) -ne 0 ] && echo "min-cache-ttl = $dnsmasq_cache_time")
no-negcache
addn-hosts=$dnsmasq_addn_hosts
no-resolv
port = 60053
server = 127.0.0.1#65353
$(for server_addr in "${server_addrs[@]}"; do echo "address = $server_addr"; done)
EOF
)
;;
esac
}
function start_iptables {
sysctl -w net.ipv4.ip_forward=1 &>/dev/null
for dir in $(ls /proc/sys/net/ipv4/conf); do
sysctl -w net.ipv4.conf.$dir.send_redirects=0 &>/dev/null
done
function check_snat_rule {
if [ "$ipts_non_snat" != 'true' ]; then
if ! iptables -t nat -C POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE &>/dev/null; then
iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
fi
fi
}
function dec2bin {
for ((n = $1; n > 0; n >>= 1)); do bit="$((n & 1))$bit"; done
printf "%08d\n" "$bit"
}
function net2bin {
net=$(awk -F/ '{print $1}' <<<$1)
len=$(awk -F/ '{print $2}' <<<$1)
IFS='.' read -ra bytes <<<"$net"
for byte in "${bytes[@]}"; do result+="$(dec2bin $byte)"; done
echo "${result:0:len}"
}
for cidr in "${ipts_intranet[@]}"; do
curnet=$(net2bin $cidr)
for stdnet in 00001010 101011000001 1010100111111110 1100000010101000; do
[[ "$curnet" == "$stdnet"* ]] && continue 2
done
ipts_intranet_nonstd+=($cidr)
done
iptables -t mangle -N SSTP_OUT
iptables -t mangle -N SSTP_PRE
iptables -t nat -N SSTP_OUT
iptables -t nat -N SSTP_PRE
iptables -t raw -N SSTP_OUT
iptables -t mangle -A OUTPUT -j SSTP_OUT
iptables -t mangle -A PREROUTING -j SSTP_PRE
iptables -t nat -A OUTPUT -j SSTP_OUT
iptables -t nat -A PREROUTING -j SSTP_PRE
iptables -t raw -A OUTPUT -j SSTP_OUT
ip route add local 0/0 dev lo table $ipts_rt_tab
ip rule add fwmark $ipts_rt_mark table $ipts_rt_tab
if [ "$proxy_tproxy" = 'true' ]; then
case "$mode" in
gfwlist)
ipset -N gfwlist hash:net &>/dev/null
perl -ne 'print if m@^\d++\.\d++\.\d++\.\d++(?:/\d++)?$@' $file_gfwlist_ext | xargs -n1 ipset -A gfwlist &>/dev/null
iptables -t mangle -N SETMARK
if ! [ "$proxy_dports" ]; then
for server in "${proxy_server[@]}"; do
iptables -t mangle -A SETMARK -d $server -j RETURN
done
else
for server in "${proxy_server[@]}"; do
iptables -t mangle -A SETMARK -d $server -p tcp -m multiport --dports $proxy_dports -j RETURN
iptables -t mangle -A SETMARK -d $server -p udp -m multiport --dports $proxy_dports -j RETURN
done
fi
iptables -t mangle -A SETMARK -p udp -d ${dns_remote%:*} -j MARK --set-mark $ipts_rt_mark
iptables -t mangle -A SETMARK -m set --match-set gfwlist dst -j MARK --set-mark $ipts_rt_mark
iptables -t mangle -A SSTP_OUT -p tcp -j SETMARK
iptables -t mangle -A SSTP_OUT -p udp -j SETMARK
iptables -t nat -A SSTP_OUT -p udp -d 127.0.0.1 --dport 53 -j REDIRECT --to-ports 60053
for intranet in "${ipts_intranet[@]}"; do
iptables -t mangle -A SSTP_PRE -p udp -s $intranet --dport 53 -m mark ! --mark $ipts_rt_mark -j ACCEPT
iptables -t nat -A SSTP_PRE -p udp -s $intranet --dport 53 -m mark ! --mark $ipts_rt_mark -j REDIRECT --to-ports 60053
iptables -t mangle -A SSTP_PRE -p tcp -s $intranet -m mark ! --mark $ipts_rt_mark -j SETMARK
iptables -t mangle -A SSTP_PRE -p udp -s $intranet -m mark ! --mark $ipts_rt_mark -j SETMARK
check_snat_rule
done
iptables -t mangle -A SSTP_PRE -m mark --mark $ipts_rt_mark -p tcp -j TPROXY --on-ip 127.0.0.1 --on-port $proxy_tcport
iptables -t mangle -A SSTP_PRE -m mark --mark $ipts_rt_mark -p udp -j TPROXY --on-ip 127.0.0.1 --on-port $proxy_udport
[ "$proxy_ipv6" = 'false' ] && iptables -t raw -A SSTP_OUT -p udp -d ${dns_remote%:*} --dport ${dns_remote#*:} -m string --hex-string "|00001c|" --algo bm -j DROP
;;
global|chnroute)
iptables -t mangle -N SETMARK
iptables -t mangle -A SETMARK -d 0/8 -j RETURN
iptables -t mangle -A SETMARK -d 10/8 -j RETURN
iptables -t mangle -A SETMARK -d 127/8 -j RETURN
iptables -t mangle -A SETMARK -d 169.254/16 -j RETURN
iptables -t mangle -A SETMARK -d 172.16/12 -j RETURN
iptables -t mangle -A SETMARK -d 192.168/16 -j RETURN
for cidr in "${ipts_intranet_nonstd[@]}"; do
iptables -t mangle -A SETMARK -d $cidr -j RETURN
done
iptables -t mangle -A SETMARK -d 224/4 -j RETURN
iptables -t mangle -A SETMARK -d 240/4 -j RETURN
if ! [ "$proxy_dports" ]; then
for server in "${proxy_server[@]}"; do
iptables -t mangle -A SETMARK -d $server -j RETURN
done
else
for server in "${proxy_server[@]}"; do
iptables -t mangle -A SETMARK -d $server -p tcp -m multiport --dports $proxy_dports -j RETURN
iptables -t mangle -A SETMARK -d $server -p udp -m multiport --dports $proxy_dports -j RETURN
done
fi
if [ "$mode" = chnroute ]; then
ipset -X chnroute &>/dev/null
ipset -R <$file_chnroute_set
iptables -t mangle -A SETMARK -m set --match-set chnroute dst -j RETURN
fi
iptables -t mangle -A SETMARK -j MARK --set-mark $ipts_rt_mark
iptables -t mangle -A SSTP_OUT -p tcp -j SETMARK
iptables -t mangle -A SSTP_OUT -p udp -j SETMARK
iptables -t nat -A SSTP_OUT -p udp -d 127.0.0.1 --dport 53 -j REDIRECT --to-ports 60053
for intranet in "${ipts_intranet[@]}"; do
iptables -t mangle -A SSTP_PRE -p udp -s $intranet --dport 53 -m mark ! --mark $ipts_rt_mark -j ACCEPT
iptables -t nat -A SSTP_PRE -p udp -s $intranet --dport 53 -m mark ! --mark $ipts_rt_mark -j REDIRECT --to-ports 60053
iptables -t mangle -A SSTP_PRE -p tcp -s $intranet -m mark ! --mark $ipts_rt_mark -j SETMARK
iptables -t mangle -A SSTP_PRE -p udp -s $intranet -m mark ! --mark $ipts_rt_mark -j SETMARK
check_snat_rule
done
iptables -t mangle -A SSTP_PRE -m mark --mark $ipts_rt_mark -p tcp -j TPROXY --on-ip 127.0.0.1 --on-port $proxy_tcport
iptables -t mangle -A SSTP_PRE -m mark --mark $ipts_rt_mark -p udp -j TPROXY --on-ip 127.0.0.1 --on-port $proxy_udport
[ "$proxy_ipv6" = 'false' ] && iptables -t raw -A OUTPUT -p udp -d 127.0.0.1 --dport 65353 -m string --hex-string "|00001c|" --algo bm -j DROP
;;
esac
else
case "$mode" in
gfwlist)
ipset -N gfwlist hash:net &>/dev/null
perl -ne 'print if m@^\d++\.\d++\.\d++\.\d++(?:/\d++)?$@' $file_gfwlist_ext | xargs -n1 ipset -A gfwlist &>/dev/null
iptables -t nat -N TCPCHAIN
for server in "${proxy_server[@]}"; do
iptables -t nat -A TCPCHAIN -d $server $([ "$proxy_dports" ] && echo "-p tcp -m multiport --dports $proxy_dports") -j RETURN
done
iptables -t nat -A TCPCHAIN -p tcp -m set --match-set gfwlist dst -j REDIRECT --to-ports $proxy_tcport
iptables -t mangle -N UDPCHAIN
for server in "${proxy_server[@]}"; do
iptables -t mangle -A UDPCHAIN -d $server $([ "$proxy_dports" ] && echo "-p udp -m multiport --dports $proxy_dports") -j RETURN
done
iptables -t mangle -A UDPCHAIN -d ${dns_remote%:*} -j MARK --set-mark $ipts_rt_mark
iptables -t mangle -A UDPCHAIN -m set --match-set gfwlist dst -j MARK --set-mark $ipts_rt_mark
iptables -t nat -A SSTP_OUT -p tcp -j TCPCHAIN
iptables -t mangle -A SSTP_OUT -p udp -j UDPCHAIN
iptables -t nat -A SSTP_OUT -p udp -d 127.0.0.1 --dport 53 -j REDIRECT --to-ports 60053
for intranet in "${ipts_intranet[@]}"; do
iptables -t mangle -A SSTP_PRE -p udp -s $intranet --dport 53 -m mark ! --mark $ipts_rt_mark -j ACCEPT
iptables -t nat -A SSTP_PRE -p udp -s $intranet --dport 53 -m mark ! --mark $ipts_rt_mark -j REDIRECT --to-ports 60053
iptables -t nat -A SSTP_PRE -p tcp -s $intranet -j TCPCHAIN
iptables -t mangle -A SSTP_PRE -p udp -s $intranet -m mark ! --mark $ipts_rt_mark -j UDPCHAIN
check_snat_rule
done
iptables -t mangle -A SSTP_PRE -m mark --mark $ipts_rt_mark -p udp -j TPROXY --on-ip 127.0.0.1 --on-port $proxy_udport
[ "$proxy_ipv6" = 'false' ] && iptables -t raw -A SSTP_OUT -p udp -d ${dns_remote%:*} --dport ${dns_remote#*:} -m string --hex-string "|00001c|" --algo bm -j DROP
;;
global|chnroute)
iptables -t nat -N TCPCHAIN
iptables -t nat -A TCPCHAIN -d 0/8 -j RETURN
iptables -t nat -A TCPCHAIN -d 10/8 -j RETURN
iptables -t nat -A TCPCHAIN -d 127/8 -j RETURN
iptables -t nat -A TCPCHAIN -d 169.254/16 -j RETURN
iptables -t nat -A TCPCHAIN -d 172.16/12 -j RETURN
iptables -t nat -A TCPCHAIN -d 192.168/16 -j RETURN
for cidr in "${ipts_intranet_nonstd[@]}"; do
iptables -t nat -A TCPCHAIN -d $cidr -j RETURN
done
iptables -t nat -A TCPCHAIN -d 224/4 -j RETURN
iptables -t nat -A TCPCHAIN -d 240/4 -j RETURN
for server in "${proxy_server[@]}"; do
iptables -t nat -A TCPCHAIN -d $server $([ "$proxy_dports" ] && echo "-p tcp -m multiport --dports $proxy_dports") -j RETURN
done
if [ "$mode" = chnroute ]; then
ipset -X chnroute &>/dev/null
ipset -R <$file_chnroute_set
iptables -t nat -A TCPCHAIN -m set --match-set chnroute dst -j RETURN
fi
iptables -t nat -A TCPCHAIN -p tcp -j REDIRECT --to-ports $proxy_tcport
iptables -t mangle -N UDPCHAIN
iptables -t mangle -A UDPCHAIN -d 0/8 -j RETURN
iptables -t mangle -A UDPCHAIN -d 10/8 -j RETURN
iptables -t mangle -A UDPCHAIN -d 127/8 -j RETURN
iptables -t mangle -A UDPCHAIN -d 169.254/16 -j RETURN
iptables -t mangle -A UDPCHAIN -d 172.16/12 -j RETURN
iptables -t mangle -A UDPCHAIN -d 192.168/16 -j RETURN
for cidr in "${ipts_intranet_nonstd[@]}"; do
iptables -t mangle -A UDPCHAIN -d $cidr -j RETURN
done
iptables -t mangle -A UDPCHAIN -d 224/4 -j RETURN
iptables -t mangle -A UDPCHAIN -d 240/4 -j RETURN
for server in "${proxy_server[@]}"; do
iptables -t mangle -A UDPCHAIN -d $server $([ "$proxy_dports" ] && echo "-p udp -m multiport --dports $proxy_dports") -j RETURN
done
if [ "$mode" = chnroute ]; then
iptables -t mangle -A UDPCHAIN -m set --match-set chnroute dst -j RETURN
fi
iptables -t mangle -A UDPCHAIN -j MARK --set-mark $ipts_rt_mark
iptables -t nat -A SSTP_OUT -p tcp -j TCPCHAIN
iptables -t mangle -A SSTP_OUT -p udp -j UDPCHAIN
iptables -t nat -A SSTP_OUT -p udp -d 127.0.0.1 --dport 53 -j REDIRECT --to-ports 60053
for intranet in "${ipts_intranet[@]}"; do
iptables -t mangle -A SSTP_PRE -p udp -s $intranet --dport 53 -m mark ! --mark $ipts_rt_mark -j ACCEPT
iptables -t nat -A SSTP_PRE -p udp -s $intranet --dport 53 -m mark ! --mark $ipts_rt_mark -j REDIRECT --to-ports 60053
iptables -t nat -A SSTP_PRE -p tcp -s $intranet -j TCPCHAIN
iptables -t mangle -A SSTP_PRE -p udp -s $intranet -m mark ! --mark $ipts_rt_mark -j UDPCHAIN
check_snat_rule
done
iptables -t mangle -A SSTP_PRE -m mark --mark $ipts_rt_mark -p udp -j TPROXY --on-ip 127.0.0.1 --on-port $proxy_udport
[ "$proxy_ipv6" = 'false' ] && iptables -t raw -A OUTPUT -p udp -d 127.0.0.1 --dport 65353 -m string --hex-string "|00001c|" --algo bm -j DROP
;;
esac
fi
}
function start_dnsproxy {
if [ "$dns_modify" = 'false' ]; then
while mount|grep overlay|grep /etc/resolv.conf; do umount /etc/resolv.conf; done &>/dev/null
resolv=$(mktemp)
chmod 0644 $resolv
cat <<EOF >$resolv
# Generated by ss-tproxy at $(date '+%F %T')
nameserver 127.0.0.1
EOF
mount -o bind $resolv /etc/resolv.conf
rm -f $resolv &>/dev/null
else
echo "# Generated by ss-tproxy at $(date '+%F %T')" >/etc/resolv.conf
echo "nameserver 127.0.0.1" >>/etc/resolv.conf
fi
}
function start {
[ "$(type -t pre_start)" = 'function' ] && pre_start
if [ "$dns_modify" = 'false' ]; then
while mount|grep overlay|grep /etc/resolv.conf; do umount /etc/resolv.conf; done &>/dev/null
else
echo "# Generated by ss-tproxy at $(date '+%F %T')" >/etc/resolv.conf
echo "nameserver $dns_direct" >>/etc/resolv.conf
fi
start_proxy
start_resolver
start_iptables
start_dnsproxy
rm -f $temp_chnroute_txt &>/dev/null
[ "$(type -t post_start)" = 'function' ] && post_start
}
function stop {
[ "$(type -t pre_stop)" = 'function' ] && pre_stop
if [ "$dns_modify" = 'false' ]; then
while mount|grep overlay|grep /etc/resolv.conf; do umount /etc/resolv.conf; done &>/dev/null
else
echo "# Generated by ss-tproxy at $(date '+%F %T')" >/etc/resolv.conf
echo "nameserver $dns_direct" >>/etc/resolv.conf
fi
iptables -t mangle -D OUTPUT -j SSTP_OUT &>/dev/null
iptables -t mangle -D PREROUTING -j SSTP_PRE &>/dev/null
iptables -t nat -D OUTPUT -j SSTP_OUT &>/dev/null
iptables -t nat -D PREROUTING -j SSTP_PRE &>/dev/null
iptables -t raw -D OUTPUT -j SSTP_OUT &>/dev/null
iptables -t mangle -F SSTP_OUT &>/dev/null
iptables -t mangle -X SSTP_OUT &>/dev/null
iptables -t mangle -F SSTP_PRE &>/dev/null
iptables -t mangle -X SSTP_PRE &>/dev/null
iptables -t nat -F SSTP_OUT &>/dev/null
iptables -t nat -X SSTP_OUT &>/dev/null
iptables -t nat -F SSTP_PRE &>/dev/null
iptables -t nat -X SSTP_PRE &>/dev/null
iptables -t raw -F SSTP_OUT &>/dev/null
iptables -t raw -X SSTP_OUT &>/dev/null
iptables -t mangle -F SETMARK &>/dev/null
iptables -t mangle -X SETMARK &>/dev/null
iptables -t nat -F TCPCHAIN &>/dev/null
iptables -t nat -X TCPCHAIN &>/dev/null
iptables -t mangle -F UDPCHAIN &>/dev/null
iptables -t mangle -X UDPCHAIN &>/dev/null
if [ "$ipts_non_snat" != 'true' ]; then
for intranet in "${ipts_intranet[@]}"; do
if ! iptables -t nat -C POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE &>/dev/null; then
iptables -t nat -A POSTROUTING -s $intranet ! -d $intranet -j MASQUERADE
fi
done
fi
ipset -X chnroute &>/dev/null
ip rule show | grep "fwmark $ipts_rt_mark" | awk -F: '{print $1}' | xargs -n1 ip rule del pref &>/dev/null
ip route flush table $ipts_rt_tab &>/dev/null
eval "$proxy_kilcmd" &>/dev/null
kill -9 $(pidof 'chinadns') &>/dev/null
if [ "$opts_ss_netstat" != "netstat" -a "$(command -v ss &>/dev/null && echo 'true')" ]; then
ss -lnpu | awk -F, '/:60053[ \t].+"dnsmasq"/ {print $(NF-1)}' | awk -F= '{print $2}' | sort | uniq | xargs kill -9 &>/dev/null
else
netstat -lnpu | awk -F/ '/:60053[ \t].+dnsmasq/ {print $(NF-1)}' | awk '{print $NF}' | sort | uniq | xargs kill -9 &>/dev/null
fi
[ "$(type -t post_stop)" = 'function' ] && post_stop
}
function status {
case "$mode" in
global)
echo "mode: global"
[ $($netstat -lnpt | grep -E ":$proxy_tcport[ \t]" | wc -l) -ne 0 ] && echo -e "pxy/tcp: \e[32m[running]\e[0m" || echo -e "pxy/tcp: \e[35m[stopped]\e[0m"
[ $($netstat -lnpu | grep -E ":$proxy_udport[ \t]" | wc -l) -ne 0 ] && echo -e "pxy/udp: \e[32m[running]\e[0m" || echo -e "pxy/udp: \e[35m[stopped]\e[0m"
[ $($netstat -lnpu | grep -E ":60053[ \t].+dnsmasq" | wc -l) -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
;;
gfwlist)
echo "mode: gfwlist"
[ $($netstat -lnpt | grep -E ":$proxy_tcport[ \t]" | wc -l) -ne 0 ] && echo -e "pxy/tcp: \e[32m[running]\e[0m" || echo -e "pxy/tcp: \e[35m[stopped]\e[0m"
[ $($netstat -lnpu | grep -E ":$proxy_udport[ \t]" | wc -l) -ne 0 ] && echo -e "pxy/udp: \e[32m[running]\e[0m" || echo -e "pxy/udp: \e[35m[stopped]\e[0m"
[ $($netstat -lnpu | grep -E ":60053[ \t].+dnsmasq" | wc -l) -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
;;
chnroute)
echo "mode: chnroute"
[ $($netstat -lnpt | grep -E ":$proxy_tcport[ \t]" | wc -l) -ne 0 ] && echo -e "pxy/tcp: \e[32m[running]\e[0m" || echo -e "pxy/tcp: \e[35m[stopped]\e[0m"
[ $($netstat -lnpu | grep -E ":$proxy_udport[ \t]" | wc -l) -ne 0 ] && echo -e "pxy/udp: \e[32m[running]\e[0m" || echo -e "pxy/udp: \e[35m[stopped]\e[0m"
[ $($netstat -lnpu | grep -E ":60053[ \t].+dnsmasq" | wc -l) -ne 0 ] && echo -e "dnsmasq: \e[32m[running]\e[0m" || echo -e "dnsmasq: \e[35m[stopped]\e[0m"
[ $($netstat -lnpu | grep -E ":65353[ \t].+chinadns" | wc -l) -ne 0 ] && echo -e "chinadns: \e[32m[running]\e[0m" || echo -e "chinadns: \e[35m[stopped]\e[0m"
;;
esac
}
function show_iptables {
echo -e "\e[36;1m==> mangle <==\e[0m"
iptables -t mangle -nvL --line-numbers
echo
echo -e "\e[36;1m==> nat <==\e[0m"
iptables -t nat -nvL --line-numbers
}
function flush_iptables {
iptables -t raw -F
iptables -t raw -X
iptables -t mangle -F
iptables -t mangle -X
iptables -t nat -F
iptables -t nat -X
iptables -t filter -F
iptables -t filter -X
}
function help {
cat <<EOF
Usage: ss-tproxy <COMMAND>
COMMAND := {
start start ss-tproxy
stop stop ss-tproxy
restart restart ss-tproxy
status status of ss-tproxy
show-iptables show iptables rules
flush-iptables flush iptables rules
flush-dnscache flush dns cache
flush-gfwlist flush ipset-gfwlist
update-chnonly update chnonly
update-gfwlist update gfwlist
update-chnroute update chnroute
check-command check commands
help show help and exit
}
Issues or Bug report: [email protected] (Otokaze)
See https://www.zfl9.com/ss-redir.html for more details
EOF
}
case $1 in
start) check_command; start; status;;
stop) check_command; stop; status;;
status) check_command; status;;
r*) check_command; stop; status; echo; start; status;;
show-iptables) show_iptables;;
flush-iptables) flush_iptables;;
flush-gfwlist) ipset -F gfwlist &>/dev/null;;
flush-dnscache) check_command; flush_dnscache;;
update-chnonly) check_command; update_chnonly;;
update-gfwlist) check_command; update_gfwlist;;
update-chnroute) check_command; update_chnroute;;
c*) check_command;;
h*) help;;
*) help; exit 1;;
esac