forked from zfl9/ss-tproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathss-tproxy
executable file
·1235 lines (1052 loc) · 49.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
trap "exit 1" HUP INT QUIT TERM PIPE
PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
ss_tproxy_config='/etc/ss-tproxy/ss-tproxy.conf'
readonly IPV4_RESERVED_IPADDRS=(
0.0.0.0/8
10.0.0.0/8
100.64.0.0/10
127.0.0.0/8
169.254.0.0/16
172.16.0.0/12
192.0.0.0/24
192.0.2.0/24
192.88.99.0/24
192.168.0.0/16
198.18.0.0/15
198.51.100.0/24
203.0.113.0/24
224.0.0.0/4
240.0.0.0/4
255.255.255.255/32
)
readonly IPV6_RESERVED_IPADDRS=(
::/128
::1/128
::ffff:0:0/96
::ffff:0:0:0/96
64:ff9b::/96
100::/64
2001::/32
2001:20::/28
2001:db8::/32
2002::/16
fc00::/7
fe80::/10
ff00::/8
)
font_bold() {
printf "\e[1m$*\e[0m"
}
color_red() {
printf "\e[35m$*\e[0m"
}
color_green() {
printf "\e[32m$*\e[0m"
}
color_yellow() {
printf "\e[31m$*\e[0m"
}
log_error() {
echo "$(font_bold $(color_yellow '[ERROR]')) $*" 1>&2
exit 1
}
is_true() {
[ "$1" = 'true' ]
}
is_false() {
[ "$1" = 'false' ]
}
file_is_exists() {
[ -f "$1" ]
}
command_is_exists() {
command -v "$1" &>/dev/null
}
process_is_running() {
kill -0 "$1" &>/dev/null
}
tcp_port_is_exists() {
[ $($netstat -lnpt | grep -E ":$1[ \t]" | wc -l) -ne 0 ]
}
udp_port_is_exists() {
[ $($netstat -anpu | grep -E ":$1[ \t]" | wc -l) -ne 0 ]
}
ss_tproxy_is_started() {
process_is_running "$status_dnsmasq_pid" ||
process_is_running "$status_chinadns_pid" ||
process_is_running "$status_dns2tcp4_pid" ||
process_is_running "$status_dns2tcp6_pid" ||
iptables -t mangle -nL SSTP_OUTPUT &>/dev/null ||
iptables -t nat -nL SSTP_OUTPUT &>/dev/null ||
ip6tables -t mangle -nL SSTP_OUTPUT &>/dev/null ||
ip6tables -t nat -nL SSTP_OUTPUT &>/dev/null ||
[ $(ip -4 route show table $ipts_rt_tab 2>/dev/null | wc -l) -ne 0 ] ||
[ $(ip -6 route show table $ipts_rt_tab 2>/dev/null | wc -l) -ne 0 ] ||
[ $(ip -4 rule 2>/dev/null | grep -c "fwmark $ipts_rt_mark") -ne 0 ] ||
[ $(ip -6 rule 2>/dev/null | grep -c "fwmark $ipts_rt_mark") -ne 0 ]
}
is_ipv4_ipts() {
[ "$1" = 'iptables' ]
}
is_ipv6_ipts() {
[ "$1" = 'ip6tables' ]
}
is_global_mode() {
[ "$mode" = 'global' ]
}
is_gfwlist_mode() {
[ "$mode" = 'gfwlist' ]
}
is_chnroute_mode() {
[ "$mode" = 'chnroute' ]
}
is_enabled_udp() {
is_false "$tcponly"
}
is_need_iproute() {
is_true "$tproxy" || is_enabled_udp
}
is_usrgrp_mode() {
[ "$proxy_procuser" -o "$proxy_procgroup" ]
}
get_usrgrp_args() {
if [ "$proxy_procuser" -a "$proxy_procgroup" ]; then
echo "--uid-owner $proxy_procuser --gid-owner $proxy_procgroup"
elif [ "$proxy_procuser" ]; then
echo "--uid-owner $proxy_procuser"
elif [ "$proxy_procgroup" ]; then
echo "--gid-owner $proxy_procgroup"
fi
}
is_nonstd_dnsport() {
[ "$1" != '53' ]
}
is_empty_iptschain() {
local ipts="$1" table="$2" chain="$3"
[ $($ipts -t $table -nvL $chain --line-numbers | grep -Ec '^[0-9]') -eq 0 ]
}
is_ipv4_address() {
[ $(grep -Ec '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' <<<"$1") -ne 0 ]
}
is_ipv6_address() {
[ $(grep -c ':' <<<"$1") -ne 0 ]
}
is_domain_name() {
! is_ipv4_address "$1" && ! is_ipv6_address "$1"
}
set_sysctl_option() {
local option_name="$1" option_value="$2"
if command_is_exists "sysctl"; then
sysctl -w "$option_name=$option_value" >/dev/null
else
local option_path="/proc/sys/${option_name//.//}"
echo "$option_value" >$option_path
fi
}
resolve_hostname_by_hosts() {
cat /etc/hosts | sed 's/#.*//g' | grep -F "$1" | head -n1 | awk '{print $1}'
}
resolve_hostname_by_dig() {
local addr_family="$1" hostname="$2"
local ipaddr=$(resolve_hostname_by_hosts "$hostname")
if [ "$ipaddr" ]; then
if [ "$addr_family" = '-4' ] && is_ipv4_address "$ipaddr"; then
echo "$ipaddr"
return
fi
if [ "$addr_family" = '-6' ] && is_ipv6_address "$ipaddr"; then
echo "$ipaddr"
return
fi
fi
[ "$addr_family" = '-4' ] && local dns_qtype='A' || local dns_qtype='AAAA'
dig +short "$dns_qtype" "$hostname" | grep -Ev '^;|\.$' | head -n1
}
resolve_hostname_by_getent() {
local addr_family="$1" hostname="$2"
[ "$addr_family" = '-4' ] && local db_name='ahostsv4' || local db_name='ahostsv6'
getent "$db_name" "$hostname" | head -n1 | awk '{print $1}'
}
resolve_hostname_by_ping() {
local addr_family="$1" hostname="$2"
[ "$addr_family" = '-4' ] && local ping_cmd="$ping4" || local ping_cmd="$ping6"
$ping_cmd -nq -c1 -t1 -W1 "$hostname" | head -n1 | sed -r 's/\(|\)/|/g' | awk -F'|' '{print $2}'
}
resolve_hostname4() {
local ipaddr=""
while [ -z "$ipaddr" ]; do
ipaddr=$($resolver_func -4 "$1")
[ -z "$ipaddr" ] && sleep 1
done
echo "$ipaddr"
}
resolve_hostname6() {
local ipaddr=""
while [ -z "$ipaddr" ]; do
ipaddr=$($resolver_func -6 "$1")
[ -z "$ipaddr" ] && sleep 1
done
echo "$ipaddr"
}
waiting_network() {
[ -z "$1" ] && return
is_ipv4_address "$1" && local ping_cmd="$ping4" || local ping_cmd="$ping6"
until $ping_cmd -nq -c1 -W1 "$1" >/dev/null; do
echo "waiting for network available..."
sleep 1
done
}
load_pidfile() {
source "$file_dnsserver_pid" || log_error "load pidfile failed, exit-code: $?"
}
update_pidfile() {
echo "status_dnsmasq_pid=$status_dnsmasq_pid" >$file_dnsserver_pid
echo "status_chinadns_pid=$status_chinadns_pid" >>$file_dnsserver_pid
echo "status_dns2tcp4_pid=$status_dns2tcp4_pid" >>$file_dnsserver_pid
echo "status_dns2tcp6_pid=$status_dns2tcp6_pid" >>$file_dnsserver_pid
}
delete_pidfile() {
rm -f $file_dnsserver_pid &>/dev/null
}
load_config() {
if ! file_is_exists "$ss_tproxy_config"; then
log_error "file not found: $ss_tproxy_config"
else
source "$ss_tproxy_config" "${arguments[@]}" || log_error "load config failed, exit-code: $?"
fi
for optentry in "${optentries[@]}"; do eval "$optentry"; done
}
check_config() {
file_is_exists "$file_gfwlist_txt" || log_error "file not found: $file_gfwlist_txt"
file_is_exists "$file_gfwlist_ext" || log_error "file not found: $file_gfwlist_ext"
file_is_exists "$file_ignlist_ext" || log_error "file not found: $file_ignlist_ext"
file_is_exists "$file_chnroute_set" || log_error "file not found: $file_chnroute_set"
file_is_exists "$file_chnroute6_set" || log_error "file not found: $file_chnroute6_set"
file_is_exists "$file_dnsserver_pid" && load_pidfile
{ ! is_global_mode && ! is_gfwlist_mode && ! is_chnroute_mode; } && log_error "the value of the mode option is invalid: $mode"
{ is_false "$ipv4" && is_false "$ipv6"; } && log_error "both ipv4 and ipv6 are disabled, nothing to do"
if ! is_usrgrp_mode; then
[ "${#proxy_svraddr4[@]}" -eq 0 -a "${#proxy_svraddr6[@]}" -eq 0 ] && log_error "both proxy_svraddr4 and proxy_svraddr6 are empty"
[ -z "$proxy_svrport" ] && log_error "the value of the proxy_svrport option is empty: $proxy_svrport"
fi
command_is_exists 'ipset' || log_error "command not found: ipset"
command_is_exists 'dnsmasq' || log_error "command not found: dnsmasq"
is_need_iproute && { command_is_exists 'ip' || log_error "command not found: ip"; }
is_true "$ipv4" && { command_is_exists 'iptables' || log_error "command not found: iptables"; }
is_true "$ipv6" && { command_is_exists 'ip6tables' || log_error "command not found: ip6tables"; }
is_chnroute_mode && { command_is_exists 'chinadns-ng' || log_error "command not found: chinadns-ng"; }
! is_enabled_udp && { command_is_exists "dns2tcp" || log_error "command not found: dns2tcp"; }
case "$opts_ss_netstat" in
auto)
if command_is_exists 'ss'; then
netstat='ss'
elif command_is_exists 'netstat'; then
netstat='netstat'
else
log_error "command not found: ss/netstat"
fi
;;
ss)
command_is_exists 'ss' && netstat='ss' || log_error "command not found: ss"
;;
netstat)
command_is_exists 'netstat' && netstat='netstat' || log_error "command not found: netstat"
;;
*)
log_error "the value of the opts_ss_netstat option is invalid: $opts_ss_netstat"
;;
esac
case "$opts_ping_cmd_to_use" in
auto)
if command_is_exists 'ping' && command_is_exists 'ping6' && ! [ -L "$(command -v ping6)" ]; then
ping4='ping'; ping6='ping6'
elif command_is_exists 'ping'; then
ping4='ping -4'; ping6='ping -6'
else
log_error "command not found: ping/ping6"
fi
;;
standalone)
{ command_is_exists 'ping' && command_is_exists 'ping6'; } && { ping4='ping'; ping6='ping6'; } || log_error "command not found: ping/ping6"
;;
parameter)
command_is_exists 'ping' && { ping4='ping -4'; ping6='ping -6'; } || log_error "command not found: ping"
;;
*)
log_error "the value of the opts_ping_cmd_to_use option is invalid: $opts_ping_cmd_to_use"
;;
esac
if ! is_usrgrp_mode; then
case "$opts_hostname_resolver" in
auto)
if command_is_exists 'dig'; then
resolver_func='resolve_hostname_by_dig'
elif command_is_exists 'getent'; then
resolver_func='resolve_hostname_by_getent'
elif command_is_exists 'ping'; then
resolver_func='resolve_hostname_by_ping'
else
log_error "command not found: dig/getent/ping"
fi
;;
dig)
command_is_exists 'dig' && resolver_func='resolve_hostname_by_dig' || log_error "command not found: dig"
;;
getent)
command_is_exists 'getent' && resolver_func='resolve_hostname_by_getent' || log_error "command not found: getent"
;;
ping)
command_is_exists 'ping' && resolver_func='resolve_hostname_by_ping' || log_error "command not found: ping"
;;
*)
log_error "the value of the opts_hostname_resolver option is invalid: $opts_hostname_resolver"
;;
esac
fi
}
resolve_svraddr() {
is_usrgrp_mode && return
if is_true "$ipv4"; then
proxy_svripv4=()
for svraddr in "${proxy_svraddr4[@]}"; do
is_ipv4_address "$svraddr" && local svripv4="$svraddr" || local svripv4=$(resolve_hostname4 "$svraddr")
proxy_svripv4+=("$svripv4")
done
fi
if is_true "$ipv6"; then
proxy_svripv6=()
for svraddr in "${proxy_svraddr6[@]}"; do
is_ipv6_address "$svraddr" && local svripv6="$svraddr" || local svripv6=$(resolve_hostname6 "$svraddr")
proxy_svripv6+=("$svripv6")
done
fi
}
update_chnlist() {
command_is_exists 'curl' || log_error "command not found: curl"
local url='https://raw.github.com/felixonmars/dnsmasq-china-list/master/accelerated-domains.china.conf'
local data; data=$(curl -4sSkL "$url") || log_error "download failed, exit-code: $?"
echo "$data" | awk -F/ '{print $2}' >$file_gfwlist_txt
}
readonly GFWLIST_TXT_PERL_SCRIPT_STRING='
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@^\s*+$@@
'
gfwlist_txt_append_domain_names() {
printf "twimg.edgesuite.net\n"
printf "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\n"
printf "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\n"
}
update_gfwlist() {
command_is_exists 'curl' || log_error "command not found: curl"
command_is_exists 'perl' || log_error "command not found: perl"
command_is_exists 'base64' || log_error "command not found: base64"
local url='https://raw.github.com/gfwlist/gfwlist/master/gfwlist.txt'
local data; data=$(curl -4sSkL "$url") || log_error "download failed, exit-code: $?"
local base64_decode=''
base64 -d </dev/null &>/dev/null && base64_decode='base64 -d'
base64 --decode </dev/null &>/dev/null && base64_decode='base64 --decode'
[ "$base64_decode" ] || log_error "command args is not support: base64 -d/--decode"
echo "$data" | $base64_decode | { perl -pe "$GFWLIST_TXT_PERL_SCRIPT_STRING"; gfwlist_txt_append_domain_names; } | sort | uniq >$file_gfwlist_txt
}
update_chnroute() {
command_is_exists 'curl' || log_error "command not found: curl"
local url='http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'
local data; data=$(curl -4sSkL "$url") || log_error "download failed, exit-code: $?"
{
echo "create chnroute hash:net family inet"
echo "$data" | grep CN | grep ipv4 | awk -F'|' '{printf("add chnroute %s/%d\n", $4, 32-log($5)/log(2))}'
} >$file_chnroute_set
{
echo "create chnroute6 hash:net family inet6"
echo "$data" | grep CN | grep ipv6 | awk -F'|' '{printf("add chnroute6 %s/%d\n", $4, $5)}'
} >$file_chnroute6_set
}
start_dnsserver_global() {
local dnsmasq_config_string=$(cat <<EOF
$(echo "$dnsmasq_common_config")
$(is_true "$ipv4" && echo "server = $dns_remote")
$(is_true "$ipv6" && echo "server = $dns_remote6")
$(grep -E '^@' $file_ignlist_ext | cut -c2- | while read domain_name; do
is_true "$ipv4" && echo "server = /$domain_name/$dns_direct"
is_true "$ipv6" && echo "server = /$domain_name/$dns_direct6"
if is_true "$ipv4" && is_true "$ipv6"; then
echo "ipset = /$domain_name/privaddr,privaddr6"
elif is_true "$ipv4"; then
echo "ipset = /$domain_name/privaddr"
else
echo "ipset = /$domain_name/privaddr6"
fi
done)
EOF
)
status_dnsmasq_pid=$(dnsmasq --keep-in-foreground --conf-file=- <<<"$dnsmasq_config_string" & echo $!)
}
start_dnsserver_gfwlist() {
local dnsmasq_config_string=$(cat <<EOF
$(echo "$dnsmasq_common_config")
$(is_true "$ipv4" && echo "server = $dns_direct")
$(is_true "$ipv6" && echo "server = $dns_direct6")
$({ cat $file_gfwlist_txt; grep -E '^@' $file_gfwlist_ext | cut -c2-; } | while read domain_name; do
is_true "$ipv4" && echo "server = /$domain_name/$dns_remote"
is_true "$ipv6" && echo "server = /$domain_name/$dns_remote6"
if is_true "$ipv4" && is_true "$ipv6"; then
echo "ipset = /$domain_name/gfwlist,gfwlist6"
elif is_true "$ipv4"; then
echo "ipset = /$domain_name/gfwlist"
else
echo "ipset = /$domain_name/gfwlist6"
fi
done)
EOF
)
status_dnsmasq_pid=$(dnsmasq --keep-in-foreground --conf-file=- <<<"$dnsmasq_config_string" & echo $!)
}
start_dnsserver_chnroute() {
local chinadns_args="-b 127.0.0.1 -l $chinadns_bind_port -o $chinadns_timeout -p $chinadns_repeat"
is_true "$chinadns_noip_as_chnip" && chinadns_args+=" -n"
is_true "$chinadns_gfwlist_mode" && chinadns_args+=" -g-"
is_true "$chinadns_fairmode" && chinadns_args+=" -f"
is_true "$chinadns_verbose" && chinadns_args+=" -v"
if is_true "$ipv4" && is_true "$ipv6"; then
chinadns_args+=" -c $dns_direct,$dns_direct6"
chinadns_args+=" -t $dns_remote,$dns_remote6"
elif is_true "$ipv4"; then
chinadns_args+=" -c $dns_direct"
chinadns_args+=" -t $dns_remote"
else
chinadns_args+=" -c $dns_direct6"
chinadns_args+=" -t $dns_remote6"
fi
ipset -X chnroute &>/dev/null
ipset -X chnroute6 &>/dev/null
ipset -R -exist <$file_chnroute_set
ipset -R -exist <$file_chnroute6_set
for privaddr in "${chinadns_privaddr4[@]}"; do echo "-A chnroute $privaddr"; done | ipset -R -exist &>/dev/null
for privaddr in "${chinadns_privaddr6[@]}"; do echo "-A chnroute6 $privaddr"; done | ipset -R -exist &>/dev/null
if is_true "$chinadns_gfwlist_mode"; then
local gfwlist_domain_string=$(cat $file_gfwlist_txt; grep -E '^@' $file_gfwlist_ext | cut -c2-)
status_chinadns_pid=$(chinadns-ng $chinadns_args <<<"$gfwlist_domain_string" &>>$chinadns_logfile & echo $!)
else
status_chinadns_pid=$(chinadns-ng $chinadns_args </dev/null &>>$chinadns_logfile & echo $!)
fi
local dnsmasq_config_string=$(cat <<EOF
$(echo "$dnsmasq_common_config")
server = 127.0.0.1#$chinadns_bind_port
$(grep -E '^@' $file_ignlist_ext | cut -c2- | while read domain_name; do
is_true "$ipv4" && echo "server = /$domain_name/$dns_direct"
is_true "$ipv6" && echo "server = /$domain_name/$dns_direct6"
if is_true "$ipv4" && is_true "$ipv6"; then
echo "ipset = /$domain_name/privaddr,privaddr6"
elif is_true "$ipv4"; then
echo "ipset = /$domain_name/privaddr"
else
echo "ipset = /$domain_name/privaddr6"
fi
done)
EOF
)
status_dnsmasq_pid=$(dnsmasq --keep-in-foreground --conf-file=- <<<"$dnsmasq_config_string" & echo $!)
}
start_dnsserver() {
local dnsmasq_append_config=("${dnsmasq_conf_string[@]}")
if ! is_usrgrp_mode; then
if is_true "$ipv4"; then
for ((i = 0; i < ${#proxy_svraddr4[@]}; ++i)); do
local server_host="${proxy_svraddr4[i]}"
local server_addr="${proxy_svripv4[i]}"
if is_domain_name "$server_host"; then
dnsmasq_append_config+=("address = /$server_host/$server_addr")
fi
done
fi
if is_true "$ipv6"; then
for ((i = 0; i < ${#proxy_svraddr6[@]}; ++i)); do
local server_host="${proxy_svraddr6[i]}"
local server_addr="${proxy_svripv6[i]}"
if is_domain_name "$server_host"; then
dnsmasq_append_config+=("address = /$server_host/$server_addr")
fi
done
fi
fi
local dnsmasq_common_config=$(cat <<EOF
$(is_true "$dnsmasq_log_enable" && 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
no-resolv
port = $dnsmasq_bind_port
dns-forward-max = $dnsmasq_query_maxcnt
$(for append_config in "${dnsmasq_append_config[@]}"; do echo "$append_config"; done)
$(for conf_dir_arg in "${dnsmasq_conf_dir[@]}"; do echo "conf-dir = $conf_dir_arg"; done)
$(for conf_file_arg in "${dnsmasq_conf_file[@]}"; do echo "conf-file = $conf_file_arg"; done)
EOF
)
if ! is_enabled_udp; then
local original_dns_remote="$dns_remote"
local original_dns_remote6="$dns_remote6"
dns_remote="127.0.0.1#$dns2tcp_bind_port"
dns_remote6="::1#$dns2tcp_bind_port"
fi
if is_global_mode; then
start_dnsserver_global
elif is_gfwlist_mode; then
start_dnsserver_gfwlist
elif is_chnroute_mode; then
start_dnsserver_chnroute
fi
if ! is_enabled_udp; then
local dns2tcp_listen_addr4="$dns_remote"
local dns2tcp_listen_addr6="$dns_remote6"
local dns2tcp_remote_addr4="$original_dns_remote"
local dns2tcp_remote_addr6="$original_dns_remote6"
dns_remote="$original_dns_remote"
dns_remote6="$original_dns_remote6"
if is_true "$ipv4"; then
local dns2tcp_args="-L $dns2tcp_listen_addr4 -R $dns2tcp_remote_addr4"
is_true "$dns2tcp_verbose" && dns2tcp_args+=" -v"
is_true "$dns2tcp_tcp_quickack" && dns2tcp_args+=" -a"
is_true "$dns2tcp_tcp_fastopen" && dns2tcp_args+=" -f"
[ "$dns2tcp_tcp_syncnt" ] && dns2tcp_args+=" -s $dns2tcp_tcp_syncnt"
status_dns2tcp4_pid=$(dns2tcp $dns2tcp_args </dev/null &>>$dns2tcp_logfile & echo $!)
fi
if is_true "$ipv6"; then
local dns2tcp_args="-L $dns2tcp_listen_addr6 -R $dns2tcp_remote_addr6"
is_true "$dns2tcp_verbose" && dns2tcp_args+=" -v"
is_true "$dns2tcp_tcp_quickack" && dns2tcp_args+=" -a"
is_true "$dns2tcp_tcp_fastopen" && dns2tcp_args+=" -f"
[ "$dns2tcp_tcp_syncnt" ] && dns2tcp_args+=" -s $dns2tcp_tcp_syncnt"
status_dns2tcp6_pid=$(dns2tcp $dns2tcp_args </dev/null &>>$dns2tcp_logfile & echo $!)
fi
fi
update_pidfile
}
stop_dnsserver() {
kill -9 $status_dnsmasq_pid &>/dev/null
kill -9 $status_chinadns_pid &>/dev/null
kill -9 $status_dns2tcp4_pid &>/dev/null
kill -9 $status_dns2tcp6_pid &>/dev/null
delete_pidfile
}
flush_dnscache() {
! ss_tproxy_is_started && return
kill -HUP "$status_dnsmasq_pid"
}
modify_resolvconf() {
[ -z "$opts_overwrite_resolv" ] && return
if is_false "$opts_overwrite_resolv"; then
while umount /etc/resolv.conf &>/dev/null; do true; done
local temp_resolv_conf=$(mktemp)
chmod 0644 $temp_resolv_conf
mount -o bind $temp_resolv_conf /etc/resolv.conf
rm -f $temp_resolv_conf
fi
echo "# Generated by ss-tproxy at $(date '+%F %T')" >/etc/resolv.conf
is_true "$ipv4" && echo "nameserver 127.0.0.1" >>/etc/resolv.conf
is_true "$ipv6" && echo "nameserver ::1" >>/etc/resolv.conf
}
restore_resolvconf() {
[ -z "$opts_overwrite_resolv" ] && return
if is_false "$opts_overwrite_resolv"; then
while umount /etc/resolv.conf &>/dev/null; do true; done
else
echo "# Generated by ss-tproxy at $(date '+%F %T')" >/etc/resolv.conf
is_true "$ipv4" && echo "nameserver $dns_direct" >>/etc/resolv.conf
is_true "$ipv6" && echo "nameserver $dns_direct6" >>/etc/resolv.conf
fi
}
start_proxy_proc() {
eval "$proxy_startcmd" || log_error "failed to start local proxy process, exit-code: $?"
}
stop_proxy_proc() {
eval "$proxy_stopcmd" &>/dev/null
}
enable_ipforward() {
is_true "$ipv4" && set_sysctl_option 'net.ipv4.ip_forward' 1
is_true "$ipv6" && set_sysctl_option 'net.ipv6.conf.all.forwarding' 1
}
enable_rtlocalnet() {
set_sysctl_option 'net.ipv4.conf.all.route_localnet' 1
}
disable_icmpredir() {
for dir in $(ls /proc/sys/net/ipv4/conf); do
set_sysctl_option "net.ipv4.conf.${dir//.//}.send_redirects" 0
done
}
delete_gfwlist() {
ss_tproxy_is_started && return
is_true "$ipv4" && ipset -X gfwlist &>/dev/null
is_true "$ipv6" && ipset -X gfwlist6 &>/dev/null
}
delete_chnroute() {
ipset -X privaddr &>/dev/null
ipset -X privaddr6 &>/dev/null
ipset -X chnroute &>/dev/null
ipset -X chnroute6 &>/dev/null
}
delete_iproute2() {
is_true "$ipv4" && {
ip -4 rule del table $ipts_rt_tab
ip -4 route flush table $ipts_rt_tab
} &>/dev/null
is_true "$ipv6" && {
ip -6 rule del table $ipts_rt_tab
ip -6 route flush table $ipts_rt_tab
} &>/dev/null
}
_flush_iptables() {
$1 -t mangle -D PREROUTING -j SSTP_PREROUTING &>/dev/null
$1 -t mangle -D OUTPUT -j SSTP_OUTPUT &>/dev/null
$1 -t nat -D PREROUTING -j SSTP_PREROUTING &>/dev/null
$1 -t nat -D OUTPUT -j SSTP_OUTPUT &>/dev/null
$1 -t nat -D POSTROUTING -j SSTP_POSTROUTING &>/dev/null
$1 -t mangle -F SSTP_PREROUTING &>/dev/null
$1 -t mangle -X SSTP_PREROUTING &>/dev/null
$1 -t mangle -F SSTP_OUTPUT &>/dev/null
$1 -t mangle -X SSTP_OUTPUT &>/dev/null
$1 -t nat -F SSTP_PREROUTING &>/dev/null
$1 -t nat -X SSTP_PREROUTING &>/dev/null
$1 -t nat -F SSTP_OUTPUT &>/dev/null
$1 -t nat -X SSTP_OUTPUT &>/dev/null
$1 -t nat -F SSTP_POSTROUTING &>/dev/null
$1 -t nat -X SSTP_POSTROUTING &>/dev/null
$1 -t mangle -F SSTP_RULE &>/dev/null
$1 -t mangle -X SSTP_RULE &>/dev/null
$1 -t nat -F SSTP_RULE &>/dev/null
$1 -t nat -X SSTP_RULE &>/dev/null
}
flush_iptables() {
is_true "$ipv4" && _flush_iptables "iptables"
is_true "$ipv6" && _flush_iptables "ip6tables"
}
_show_iptables() {
echo "$(color_green "==> $1-mangle <==")"
$1 -t mangle -nvL --line-numbers
echo
echo "$(color_green "==> $1-nat <==")"
$1 -t nat -nvL --line-numbers
}
show_iptables() {
is_true "$ipv4" && _show_iptables "iptables"
{ is_true "$ipv4" && is_true "$ipv6"; } && echo
is_true "$ipv6" && _show_iptables "ip6tables"
}
check_dnsredir() {
is_false "$ipts_reddns_onstop" && return
local direct_dns_ip
is_ipv4_ipts $1 && direct_dns_ip="$dns_direct" || direct_dns_ip="$dns_direct6"
$1 -t nat -N SSTP_PREROUTING &>/dev/null
$1 -t nat -N SSTP_POSTROUTING &>/dev/null
$1 -t nat -A SSTP_PREROUTING -m addrtype ! --src-type LOCAL --dst-type LOCAL -p udp --dport 53 -j DNAT --to-destination $direct_dns_ip
$1 -t nat -A SSTP_POSTROUTING -m addrtype ! --src-type LOCAL -p udp -d $direct_dns_ip --dport 53 -j MASQUERADE
}
check_snatrule() {
local set_snat_rule='false'
{ is_ipv4_ipts $1 && is_true "$ipts_set_snat"; } && set_snat_rule='true'
{ is_ipv6_ipts $1 && is_true "$ipts_set_snat6"; } && set_snat_rule='true'
is_false "$set_snat_rule" && return
$1 -t nat -N SSTP_POSTROUTING &>/dev/null
$1 -t nat -A SSTP_POSTROUTING -m addrtype ! --src-type LOCAL -m conntrack --ctstate SNAT,DNAT -j RETURN
$1 -t nat -A SSTP_POSTROUTING -m addrtype ! --src-type LOCAL -p tcp --syn -j MASQUERADE
$1 -t nat -A SSTP_POSTROUTING -m addrtype ! --src-type LOCAL -p udp -m conntrack --ctstate NEW -j MASQUERADE
$1 -t nat -A SSTP_POSTROUTING -m addrtype ! --src-type LOCAL -p icmp -m conntrack --ctstate NEW -j MASQUERADE
}
check_iptschain() {
$1 -t nat -nL SSTP_PREROUTING &>/dev/null && $1 -t nat -A PREROUTING -j SSTP_PREROUTING
$1 -t nat -nL SSTP_POSTROUTING &>/dev/null && $1 -t nat -A POSTROUTING -j SSTP_POSTROUTING
}
check_postrule() {
ss_tproxy_is_started && return
{ is_false "$ipts_reddns_onstop" && is_false "$ipts_set_snat" && is_false "$ipts_set_snat6"; } && return
is_true "$ipv4" && { check_dnsredir "iptables"; check_snatrule "iptables"; check_iptschain "iptables"; }
is_true "$ipv6" && { check_dnsredir "ip6tables"; check_snatrule "ip6tables"; check_iptschain "ip6tables"; }
}
_flush_postrule() {
$1 -t nat -D PREROUTING -j SSTP_PREROUTING &>/dev/null
$1 -t nat -D POSTROUTING -j SSTP_POSTROUTING &>/dev/null
$1 -t nat -F SSTP_PREROUTING &>/dev/null
$1 -t nat -X SSTP_PREROUTING &>/dev/null
$1 -t nat -F SSTP_POSTROUTING &>/dev/null
$1 -t nat -X SSTP_POSTROUTING &>/dev/null
}
flush_postrule() {
ss_tproxy_is_started && return
is_true "$ipv4" && _flush_postrule "iptables"
is_true "$ipv6" && _flush_postrule "ip6tables"
}
_delete_unused_iptchains() {
if is_empty_iptschain $1 mangle SSTP_PREROUTING; then
$1 -t mangle -D PREROUTING -j SSTP_PREROUTING
$1 -t mangle -X SSTP_PREROUTING
fi
if is_empty_iptschain $1 mangle SSTP_OUTPUT; then
$1 -t mangle -D OUTPUT -j SSTP_OUTPUT
$1 -t mangle -X SSTP_OUTPUT
fi
if is_empty_iptschain $1 nat SSTP_PREROUTING; then
$1 -t nat -D PREROUTING -j SSTP_PREROUTING
$1 -t nat -X SSTP_PREROUTING
fi
if is_empty_iptschain $1 nat SSTP_OUTPUT; then
$1 -t nat -D OUTPUT -j SSTP_OUTPUT
$1 -t nat -X SSTP_OUTPUT
fi
if is_empty_iptschain $1 nat SSTP_POSTROUTING; then
$1 -t nat -D POSTROUTING -j SSTP_POSTROUTING
$1 -t nat -X SSTP_POSTROUTING
fi
}
delete_unused_iptchains() {
is_true "$ipv4" && _delete_unused_iptchains "iptables"
is_true "$ipv6" && _delete_unused_iptchains "ip6tables"
}
start_iptables_pre_rules() {
$1 -t mangle -N SSTP_PREROUTING
$1 -t mangle -N SSTP_OUTPUT
$1 -t nat -N SSTP_PREROUTING
$1 -t nat -N SSTP_OUTPUT
$1 -t nat -N SSTP_POSTROUTING
if is_need_iproute; then
local iproute2_family
is_ipv4_ipts $1 && iproute2_family="-4" || iproute2_family="-6"
ip $iproute2_family route add local default dev $ipts_if_lo table $ipts_rt_tab
ip $iproute2_family rule add fwmark $ipts_rt_mark table $ipts_rt_tab
fi
}
start_iptables_post_rules() {
$1 -t mangle -A PREROUTING -j SSTP_PREROUTING
$1 -t mangle -A OUTPUT -j SSTP_OUTPUT
$1 -t nat -A PREROUTING -j SSTP_PREROUTING
$1 -t nat -A OUTPUT -j SSTP_OUTPUT
$1 -t nat -A POSTROUTING -j SSTP_POSTROUTING
}
start_iptables_tproxy_mode() {
local loopback_addr
is_ipv4_ipts $1 && loopback_addr="127.0.0.1" || loopback_addr="::1"
local gfwlist_setname
is_ipv4_ipts $1 && gfwlist_setname="gfwlist" || gfwlist_setname="gfwlist6"
local gfwlist_setfamily
is_ipv4_ipts $1 && gfwlist_setfamily="inet" || gfwlist_setfamily="inet6"
local grep_pattern
is_ipv4_ipts $1 && grep_pattern="^-" || grep_pattern="^~"
local svr_ip_array
if ! is_usrgrp_mode; then
is_ipv4_ipts $1 && svr_ip_array=("${proxy_svripv4[@]}") || svr_ip_array=("${proxy_svripv6[@]}")
fi
local direct_dns_ip
is_ipv4_ipts $1 && direct_dns_ip="$dns_direct" || direct_dns_ip="$dns_direct6"
local remote_dns_ip remote_dns_port
is_ipv4_ipts $1 && remote_dns_ip="${dns_remote%%#*}" || remote_dns_ip="${dns_remote6%%#*}"
is_ipv4_ipts $1 && remote_dns_port="${dns_remote##*#}" || remote_dns_port="${dns_remote6##*#}"
local chnroute_setname
is_ipv4_ipts $1 && chnroute_setname="chnroute" || chnroute_setname="chnroute6"
local privaddr_setname
is_ipv4_ipts $1 && privaddr_setname="privaddr" || privaddr_setname="privaddr6"
local privaddr_array
is_ipv4_ipts $1 && privaddr_array=("${IPV4_RESERVED_IPADDRS[@]}") || privaddr_array=("${IPV6_RESERVED_IPADDRS[@]}")
if is_global_mode || is_chnroute_mode; then
ipset -X $privaddr_setname &>/dev/null
ipset -N $privaddr_setname hash:net family $gfwlist_setfamily
for privaddr in "${privaddr_array[@]}"; do echo "-A $privaddr_setname $privaddr"; done | ipset -R -exist &>/dev/null
grep -E "$grep_pattern" $file_ignlist_ext | cut -c2- | while read ip_addr; do echo "-A $privaddr_setname $ip_addr"; done | ipset -R -exist &>/dev/null
elif is_gfwlist_mode; then
ipset -N $gfwlist_setname hash:net family $gfwlist_setfamily &>/dev/null
grep -E "$grep_pattern" $file_gfwlist_ext | cut -c2- | while read ip_addr; do echo "-A $gfwlist_setname $ip_addr"; done | ipset -R -exist &>/dev/null
fi
######################### SSTP_RULE (tcp and udp) #########################
$1 -t mangle -N SSTP_RULE
$1 -t mangle -A SSTP_RULE -j CONNMARK --restore-mark
$1 -t mangle -A SSTP_RULE -m mark --mark $ipts_rt_mark -j RETURN
if ! is_usrgrp_mode; then
for svr_ip in "${svr_ip_array[@]}"; do
$1 -t mangle -A SSTP_RULE -p tcp -d $svr_ip -m multiport --dports $proxy_svrport -j RETURN
is_enabled_udp && $1 -t mangle -A SSTP_RULE -p udp -d $svr_ip -m multiport --dports $proxy_svrport -j RETURN
done
fi
if is_enabled_udp; then
$1 -t mangle -A SSTP_RULE -p udp -d $direct_dns_ip --dport 53 -j RETURN
$1 -t mangle -A SSTP_RULE -p udp -d $remote_dns_ip --dport $remote_dns_port -j MARK --set-mark $ipts_rt_mark
$1 -t mangle -A SSTP_RULE -p udp -d $remote_dns_ip --dport $remote_dns_port -j RETURN
else
$1 -t mangle -A SSTP_RULE -p tcp -d $remote_dns_ip --dport $remote_dns_port -j MARK --set-mark $ipts_rt_mark
$1 -t mangle -A SSTP_RULE -p tcp -d $remote_dns_ip --dport $remote_dns_port -j RETURN
fi
if is_gfwlist_mode; then
$1 -t mangle -A SSTP_RULE -p tcp -m set --match-set $gfwlist_setname dst -m multiport --dports $ipts_proxy_dst_port --syn -j MARK --set-mark $ipts_rt_mark
is_enabled_udp && $1 -t mangle -A SSTP_RULE -p udp -m set --match-set $gfwlist_setname dst -m multiport --dports $ipts_proxy_dst_port -m conntrack --ctstate NEW -j MARK --set-mark $ipts_rt_mark
elif is_global_mode; then
$1 -t mangle -A SSTP_RULE -m set --match-set $privaddr_setname dst -j RETURN
$1 -t mangle -A SSTP_RULE -p tcp -m multiport --dports $ipts_proxy_dst_port --syn -j MARK --set-mark $ipts_rt_mark
is_enabled_udp && $1 -t mangle -A SSTP_RULE -p udp -m multiport --dports $ipts_proxy_dst_port -m conntrack --ctstate NEW -j MARK --set-mark $ipts_rt_mark
elif is_chnroute_mode; then
$1 -t mangle -A SSTP_RULE -m set --match-set $privaddr_setname dst -j RETURN
$1 -t mangle -A SSTP_RULE -m set --match-set $chnroute_setname dst -j RETURN
$1 -t mangle -A SSTP_RULE -p tcp -m multiport --dports $ipts_proxy_dst_port --syn -j MARK --set-mark $ipts_rt_mark
is_enabled_udp && $1 -t mangle -A SSTP_RULE -p udp -m multiport --dports $ipts_proxy_dst_port -m conntrack --ctstate NEW -j MARK --set-mark $ipts_rt_mark
fi
$1 -t mangle -A SSTP_RULE -j CONNMARK --save-mark
######################### SSTP_OUTPUT/SSTP_PREROUTING #########################
if is_usrgrp_mode; then
$1 -t mangle -A SSTP_OUTPUT -m owner $(get_usrgrp_args) -j RETURN
$1 -t nat -A SSTP_OUTPUT -m owner $(get_usrgrp_args) -p udp -d $loopback_addr --dport 53 -j DNAT --to $direct_dns_ip:53
$1 -t nat -A SSTP_POSTROUTING -m owner $(get_usrgrp_args) -p udp -d $direct_dns_ip --dport 53 -j MASQUERADE
fi
if is_nonstd_dnsport "$dnsmasq_bind_port"; then
$1 -t nat -A SSTP_OUTPUT -p udp -d $loopback_addr --dport 53 -j REDIRECT --to-ports $dnsmasq_bind_port
fi
$1 -t mangle -A SSTP_OUTPUT -m addrtype --src-type LOCAL ! --dst-type LOCAL -p tcp -j SSTP_RULE
is_enabled_udp && $1 -t mangle -A SSTP_OUTPUT -m addrtype --src-type LOCAL ! --dst-type LOCAL -p udp -j SSTP_RULE
$1 -t mangle -A SSTP_PREROUTING -i $ipts_if_lo -m mark ! --mark $ipts_rt_mark -j RETURN
if is_false "$selfonly"; then
if is_nonstd_dnsport "$dnsmasq_bind_port"; then
is_enabled_udp && $1 -t mangle -A SSTP_PREROUTING -m addrtype ! --src-type LOCAL --dst-type LOCAL -p udp --dport 53 -j RETURN
$1 -t nat -A SSTP_PREROUTING -m addrtype ! --src-type LOCAL --dst-type LOCAL -p udp --dport 53 -j REDIRECT --to-ports $dnsmasq_bind_port
fi
$1 -t mangle -A SSTP_PREROUTING -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -p tcp -j SSTP_RULE
is_enabled_udp && $1 -t mangle -A SSTP_PREROUTING -m addrtype ! --src-type LOCAL ! --dst-type LOCAL -p udp -j SSTP_RULE
fi
$1 -t mangle -A SSTP_PREROUTING -p tcp -m mark --mark $ipts_rt_mark -j TPROXY --on-ip $loopback_addr --on-port $proxy_tcpport
is_enabled_udp && $1 -t mangle -A SSTP_PREROUTING -p udp -m mark --mark $ipts_rt_mark -j TPROXY --on-ip $loopback_addr --on-port $proxy_udpport
check_snatrule $1
}
start_iptables_redirect_mode() {
local loopback_addr
is_ipv4_ipts $1 && loopback_addr="127.0.0.1" || loopback_addr="::1"
local gfwlist_setname
is_ipv4_ipts $1 && gfwlist_setname="gfwlist" || gfwlist_setname="gfwlist6"
local gfwlist_setfamily
is_ipv4_ipts $1 && gfwlist_setfamily="inet" || gfwlist_setfamily="inet6"
local grep_pattern
is_ipv4_ipts $1 && grep_pattern="^-" || grep_pattern="^~"
local svr_ip_array
if ! is_usrgrp_mode; then
is_ipv4_ipts $1 && svr_ip_array=("${proxy_svripv4[@]}") || svr_ip_array=("${proxy_svripv6[@]}")
fi
local direct_dns_ip
is_ipv4_ipts $1 && direct_dns_ip="$dns_direct" || direct_dns_ip="$dns_direct6"
local remote_dns_ip remote_dns_port
is_ipv4_ipts $1 && remote_dns_ip="${dns_remote%%#*}" || remote_dns_ip="${dns_remote6%%#*}"
is_ipv4_ipts $1 && remote_dns_port="${dns_remote##*#}" || remote_dns_port="${dns_remote6##*#}"
local chnroute_setname
is_ipv4_ipts $1 && chnroute_setname="chnroute" || chnroute_setname="chnroute6"
local privaddr_setname
is_ipv4_ipts $1 && privaddr_setname="privaddr" || privaddr_setname="privaddr6"
local privaddr_array
is_ipv4_ipts $1 && privaddr_array=("${IPV4_RESERVED_IPADDRS[@]}") || privaddr_array=("${IPV6_RESERVED_IPADDRS[@]}")
if is_global_mode || is_chnroute_mode; then
ipset -X $privaddr_setname &>/dev/null
ipset -N $privaddr_setname hash:net family $gfwlist_setfamily
for privaddr in "${privaddr_array[@]}"; do echo "-A $privaddr_setname $privaddr"; done | ipset -R -exist &>/dev/null
grep -E "$grep_pattern" $file_ignlist_ext | cut -c2- | while read ip_addr; do echo "-A $privaddr_setname $ip_addr"; done | ipset -R -exist &>/dev/null
elif is_gfwlist_mode; then
ipset -N $gfwlist_setname hash:net family $gfwlist_setfamily &>/dev/null