forked from CHIZI-0618/box4magisk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbox.tproxy
More file actions
executable file
·1388 lines (1221 loc) · 48.9 KB
/
box.tproxy
File metadata and controls
executable file
·1388 lines (1221 loc) · 48.9 KB
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
#!/system/bin/sh
scripts=$(realpath $0)
scripts_dir=$(dirname ${scripts})
# Mark values
MARK_VALUE=20
MARK_VALUE6=25
# Routing table ID
TABLE_ID=2025
# Proxy traffic mark
ROUTING_MARK=""
log() {
local level="$1"
local message="$2"
local timestamp
local color_code
export TZ=Asia/Shanghai
timestamp="$(date +"%Y-%m-%d %H:%M:%S")"
case "$level" in
Debug) color_code="\033[0;36m" ;;
Info) color_code="\033[1;32m" ;;
Warn) color_code="\033[1;33m" ;;
Error) color_code="\033[1;31m" ;;
*)
level="Unknown"
color_code="\033[0m"
;;
esac
if [ -t 2 ]; then
printf "%b\n" "${color_code}${timestamp} [${level}]: ${message}\033[0m" >&2
else
printf "%s\n" "${timestamp} [${level}]: ${message}" >&2
fi
}
load_config() {
CONFIG_FILE=${scripts_dir}/box.config
if [ ! -f "$CONFIG_FILE" ]; then
log Error "Configuration file not found: '$CONFIG_FILE'"
exit 1
fi
source $CONFIG_FILE
}
validate_config() {
log Debug "Validating configuration..."
if ! echo "$PROXY_TCP_PORT" | grep -E '^[0-9]+$' > /dev/null || [ "$PROXY_TCP_PORT" -lt 1 ] || [ "$PROXY_TCP_PORT" -gt 65535 ]; then
log Error "Invalid PROXY_TCP_PORT: $PROXY_TCP_PORT"
return 1
fi
if ! echo "$PROXY_UDP_PORT" | grep -E '^[0-9]+$' > /dev/null || [ "$PROXY_UDP_PORT" -lt 1 ] || [ "$PROXY_UDP_PORT" -gt 65535 ]; then
log Error "Invalid PROXY_UDP_PORT: $PROXY_UDP_PORT"
return 1
fi
if ! echo "$DNS_HIJACK_ENABLE" | grep -E '^[0-2]$' > /dev/null; then
log Error "Invalid DNS_HIJACK_ENABLE: $DNS_HIJACK_ENABLE (must be 0=disabled, 1=tproxy, 2=redirect)"
return 1
fi
if ! echo "$DNS_PORT" | grep -E '^[0-9]+$' > /dev/null || [ "$DNS_PORT" -lt 1 ] || [ "$DNS_PORT" -gt 65535 ]; then
log Error "Invalid DNS_PORT: $DNS_PORT"
return 1
fi
if ! echo "$MARK_VALUE" | grep -E '^[0-9]+$' > /dev/null || [ "$MARK_VALUE" -lt 1 ] || [ "$MARK_VALUE" -gt 2147483647 ]; then
log Error "Invalid MARK_VALUE: $MARK_VALUE"
return 1
fi
if ! echo "$MARK_VALUE6" | grep -E '^[0-9]+$' > /dev/null || [ "$MARK_VALUE6" -lt 1 ] || [ "$MARK_VALUE6" -gt 2147483647 ]; then
log Error "Invalid MARK_VALUE6: $MARK_VALUE6"
return 1
fi
if ! echo "$TABLE_ID" | grep -E '^[0-9]+$' > /dev/null || [ "$TABLE_ID" -lt 1 ] || [ "$TABLE_ID" -gt 65535 ]; then
log Error "Invalid TABLE_ID: $TABLE_ID"
return 1
fi
case "$CORE_USER_GROUP" in
*:*)
CORE_USER=$(echo "$CORE_USER_GROUP" | cut -d: -f1)
CORE_GROUP=$(echo "$CORE_USER_GROUP" | cut -d: -f2)
log Debug "Parsed user:group as '$CORE_USER:$CORE_GROUP'"
;;
*)
CORE_USER="root"
CORE_GROUP="net_admin"
log Debug "Using default user:group '$CORE_USER:$CORE_GROUP'"
;;
esac
if [ -z "$CORE_USER" ] || [ -z "$CORE_GROUP" ]; then
log Warn "Empty user or group detected, using defaults"
CORE_USER="root"
CORE_GROUP="net_admin"
fi
log Info "Final user:group configuration: '$CORE_USER:$CORE_GROUP'"
case "$APP_PROXY_MODE" in
blacklist | whitelist) ;;
*)
log Error "Invalid APP_PROXY_MODE: $APP_PROXY_MODE"
return 1
;;
esac
case "$MAC_PROXY_MODE" in
blacklist | whitelist) ;;
*)
log Error "Invalid MAC_PROXY_MODE: $MAC_PROXY_MODE"
return 1
;;
esac
log Debug "Configuration validation passed"
return 0
}
check_root() {
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] Skip root check"
return 0
fi
if [ "$(id -u 2> /dev/null || echo 1)" != "0" ]; then
log Error "Must run with root privileges"
exit 1
fi
}
check_dependencies() {
export PATH="$PATH:/data/data/com.termux/files/usr/bin"
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] Skip dependency check"
return 0
fi
local missing=""
local required_commands="ip iptables curl"
local cmd
for cmd in $required_commands; do
if ! command -v "$cmd" > /dev/null 2>&1; then
missing="$missing $cmd"
fi
done
if [ -n "$missing" ]; then
log Error "Missing required commands: $missing"
log Info "Check PATH: $PATH"
exit 1
fi
}
check_kernel_feature() {
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] Skip kernel feature check for $1"
return 0
fi
if [ "$SKIP_CHECK_FEATURE" = "1" ]; then
log Debug "Kernel feature check skipped"
return 0
fi
local feature="$1"
local config_name="CONFIG_${feature}"
if [ -f /proc/config.gz ]; then
if zcat /proc/config.gz 2> /dev/null | grep -qE "^${config_name}=[ym]$"; then
log Debug "Kernel feature $feature is enabled"
return 0
else
log Debug "Kernel feature $feature is disabled or not found"
return 1
fi
else
log Debug "Cannot check kernel feature $feature: /proc/config.gz not available"
return 1
fi
}
check_tproxy_support() {
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] TPROXY support check skipped"
return 0
fi
if check_kernel_feature "NETFILTER_XT_TARGET_TPROXY"; then
log Debug "Kernel TPROXY support confirmed"
return 0
else
log Debug "Kernel TPROXY support not available"
return 1
fi
}
# Unified command wrapper functions
run_ipt_command() {
local cmd="$1"
shift
local args="$*"
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] $cmd $args"
return 0
else
command $cmd -w 100 $args
fi
}
iptables() {
run_ipt_command iptables "$@"
}
ip6tables() {
run_ipt_command ip6tables "$@"
}
ip_rule() {
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] ip rule $*"
return 0
else
command ip rule "$@"
fi
}
ip6_rule() {
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] ip -6 rule $*"
return 0
else
command ip -6 rule "$@"
fi
}
ip_route() {
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] ip route $*"
return 0
else
command ip route "$@"
fi
}
ip6_route() {
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] ip -6 route $*"
return 0
else
command ip -6 route "$@"
fi
}
get_package_uid() {
local pkg="$1"
local line
local uid
if [ ! -r /data/system/packages.list ]; then
log Debug "Cannot read /data/system/packages.list"
return 1
fi
line=$(grep -m1 "^${pkg}[[:space:]]" /data/system/packages.list 2> /dev/null || true)
if [ -z "$line" ]; then
log Debug "Package not found in packages.list: $pkg"
return 1
fi
uid=$(echo "$line" | awk '{print $2}' 2> /dev/null || true)
case "$uid" in
'' | *[!0-9]*)
uid=$(echo "$line" | awk '{print $(NF-1)}' 2> /dev/null || true)
;;
esac
case "$uid" in
'' | *[!0-9]*)
log Debug "Invalid UID format for package: $pkg"
return 1
;;
*)
echo "$uid"
return 0
;;
esac
}
find_packages_uid() {
local out
local token
local uid_base
local final_uid
# shellcheck disable=SC2048
for token in $*; do
local user_prefix=0
local package="$token"
case "$token" in
*:*)
user_prefix=$(echo "$token" | cut -d: -f1)
package=$(echo "$token" | cut -d: -f2-)
case "$user_prefix" in
'' | *[!0-9]*)
log Warn "Invalid user prefix in token: $token, using 0"
user_prefix=0
;;
esac
;;
esac
if uid_base=$(get_package_uid "$package" 2> /dev/null); then
final_uid=$((user_prefix * 100000 + uid_base))
out="$out $final_uid"
log Debug "Resolved package $token to UID $final_uid"
else
log Warn "Failed to resolve UID for package: $package"
fi
done
echo "$out" | awk '{$1=$1;print}'
}
safe_chain_exists() {
local family="$1"
local table="$2"
local chain="$3"
local cmd="iptables"
if [ "$family" = "6" ]; then
cmd="ip6tables"
fi
if $cmd -t "$table" -L "$chain" > /dev/null 2>&1; then
return 0
fi
return 1
}
safe_chain_create() {
local family="$1"
local table="$2"
local chain="$3"
local cmd="iptables"
if [ "$family" = "6" ]; then
cmd="ip6tables"
fi
if [ "$DRY_RUN" -eq 1 ] || ! safe_chain_exists "$family" "$table" "$chain"; then
$cmd -t "$table" -N "$chain"
fi
$cmd -t "$table" -F "$chain"
}
download_cn_ip_list() {
if [ "$BYPASS_CN_IP" -eq 0 ]; then
log Debug "CN IP bypass is disabled, skipping download"
return 0
fi
log Info "Checking/Downloading China mainland IP list to $CN_IP_FILE"
if [ ! -f "$CN_IP_FILE" ]; then
log Info "Fetching latest China IP list from $CN_IP_URL"
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] curl -fsSL --connect-timeout 10 --retry 3 $CN_IP_URL -o $CN_IP_FILE.tmp"
else
if ! curl -fsSL --connect-timeout 10 --retry 3 \
"$CN_IP_URL" \
-o "$CN_IP_FILE.tmp"; then
log Error "Failed to download China IP list"
rm -f "$CN_IP_FILE.tmp"
return 1
fi
fi
if [ "$DRY_RUN" -eq 0 ]; then
mv "$CN_IP_FILE.tmp" "$CN_IP_FILE"
fi
log Info "China IP list saved to $CN_IP_FILE"
else
log Debug "Using existing China IP list: $CN_IP_FILE"
fi
if [ "$PROXY_IPV6" -eq 1 ]; then
log Info "Checking/Downloading China mainland IPv6 list to $CN_IPV6_FILE"
if [ ! -f "$CN_IPV6_FILE" ]; then
log Info "Fetching latest China IPv6 list from $CN_IPV6_URL"
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] curl -fsSL --connect-timeout 10 --retry 3 $CN_IPV6_URL -o $CN_IPV6_FILE.tmp"
else
if ! curl -fsSL --connect-timeout 10 --retry 3 \
"$CN_IPV6_URL" \
-o "$CN_IPV6_FILE.tmp"; then
log Error "Failed to download China IPv6 list"
rm -f "$CN_IPV6_FILE.tmp"
return 1
fi
fi
if [ "$DRY_RUN" -eq 0 ]; then
mv "$CN_IPV6_FILE.tmp" "$CN_IPV6_FILE"
fi
log Info "China IPv6 list saved to $CN_IPV6_FILE"
else
log Debug "Using existing China IPv6 list: $CN_IPV6_FILE"
fi
fi
}
setup_cn_ipset() {
if [ "$BYPASS_CN_IP" -eq 0 ]; then
log Debug "CN IP bypass is disabled, skipping ipset setup"
return 0
fi
if ! command -v ipset > /dev/null 2>&1; then
log Error "ipset not found. Cannot bypass CN IPs"
return 1
fi
log Info "Setting up ipset for China mainland IPs"
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] ipset destroy cnip"
log Debug "[DRY-RUN] ipset destroy cnip6"
else
ipset destroy cnip 2> /dev/null || true
ipset destroy cnip6 2> /dev/null || true
fi
if [ -d "/tmp" ] && [ -w "/tmp" ]; then
export TMPDIR="/tmp"
elif [ -d "/data/local/tmp" ] && [ -w "/data/local/tmp" ]; then
export TMPDIR="/data/local/tmp"
else
mkdir -p "${scripts_dir}/tmp"
export TMPDIR="${scripts_dir}/tmp"
fi
if [ -f "$CN_IP_FILE" ]; then
log Debug "Loading IPv4 CIDR from $CN_IP_FILE"
ipv4_count=$(wc -l < "$CN_IP_FILE" 2> /dev/null || echo "0")
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] Would load $ipv4_count IPv4 CIDR entries via ipset restore"
log Debug "[DRY-RUN] ipset create cnip hash:net family inet hashsize 8192 maxelem 65536"
else
temp_file=$(mktemp)
{
echo "create cnip hash:net family inet hashsize 8192 maxelem 65536"
awk '!/^[[:space:]]*#/ && NF > 0 {printf "add cnip %s\n", $0}' "$CN_IP_FILE"
} > "$temp_file"
if ipset restore -f "$temp_file" 2> /dev/null; then
log Info "Successfully loaded $ipv4_count IPv4 CIDR entries"
else
log Error "Failed to create ipset 'cnip' or load IPv4 CIDR entries"
rm -f "$temp_file"
return 1
fi
rm -f "$temp_file"
fi
else
log Warn "CN IP file not found: $CN_IP_FILE"
return 1
fi
log Info "ipset 'cnip' loaded with China mainland IPs"
if [ "$PROXY_IPV6" -eq 1 ]; then
if [ -f "$CN_IPV6_FILE" ]; then
log Debug "Loading IPv6 CIDR from $CN_IPV6_FILE"
ipv6_count=$(wc -l < "$CN_IPV6_FILE" 2> /dev/null || echo "0")
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] Would load $ipv6_count IPv6 CIDR entries via ipset restore"
log Debug "[DRY-RUN] ipset create cnip6 hash:net family inet6 hashsize 8192 maxelem 65536"
else
temp_file6=$(mktemp)
{
echo "create cnip6 hash:net family inet6 hashsize 8192 maxelem 65536"
awk '!/^[[:space:]]*#/ && NF > 0 {printf "add cnip6 %s\n", $0}' "$CN_IPV6_FILE"
} > "$temp_file6"
if ipset restore -f "$temp_file6" 2> /dev/null; then
log Info "Successfully loaded $ipv6_count IPv6 CIDR entries"
else
log Error "Failed to create ipset 'cnip6' or load IPv6 CIDR entries"
rm -f "$temp_file6"
return 1
fi
rm -f "$temp_file6"
fi
log Info "ipset 'cnip6' loaded with China mainland IPv6 IPs"
else
log Warn "CN IPv6 file not found: $CN_IPV6_FILE"
fi
fi
return 0
}
# Unified setup function for both IPv4 and IPv6 with mode selection
setup_proxy_chain() {
local family="$1"
local mode="$2" # tproxy or redirect
local suffix=""
local mark="$MARK_VALUE"
local cmd="iptables"
if [ "$family" = "6" ]; then
suffix="6"
mark="$MARK_VALUE6"
cmd="ip6tables"
fi
# Set mode name for logging
local mode_name="$mode"
if [ "$mode" = "tproxy" ]; then
mode_name="TPROXY"
else
mode_name="REDIRECT"
fi
log Info "Setting up $mode_name chains for IPv${family}"
# Define chains based on family
local chains=""
if [ "$family" = "6" ]; then
chains="PROXY_PREROUTING6 PROXY_OUTPUT6 PROXY_IP6 BYPASS_IP6 BYPASS_INTERFACE6 PROXY_INTERFACE6 DNS_HIJACK_PRE6 DNS_HIJACK_OUT6 APP_CHAIN6 GID_CHAIN6 MAC_CHAIN6"
else
chains="PROXY_PREROUTING PROXY_OUTPUT PROXY_IP BYPASS_IP BYPASS_INTERFACE PROXY_INTERFACE DNS_HIJACK_PRE DNS_HIJACK_OUT APP_CHAIN GID_CHAIN MAC_CHAIN"
fi
local table="mangle"
if [ "$mode" = "redirect" ]; then
table="nat"
fi
# Create chains
for c in $chains; do
safe_chain_create "$family" "$table" "$c"
done
$cmd -t "$table" -A "PROXY_PREROUTING$suffix" -j "PROXY_IP$suffix"
$cmd -t "$table" -A "PROXY_PREROUTING$suffix" -j "BYPASS_IP$suffix"
$cmd -t "$table" -A "PROXY_PREROUTING$suffix" -j "PROXY_INTERFACE$suffix"
$cmd -t "$table" -A "PROXY_PREROUTING$suffix" -j "MAC_CHAIN$suffix"
$cmd -t "$table" -A "PROXY_PREROUTING$suffix" -j "DNS_HIJACK_PRE$suffix"
$cmd -t "$table" -A "PROXY_OUTPUT$suffix" -j "PROXY_IP$suffix"
$cmd -t "$table" -A "PROXY_OUTPUT$suffix" -j "BYPASS_IP$suffix"
$cmd -t "$table" -A "PROXY_OUTPUT$suffix" -j "BYPASS_INTERFACE$suffix"
$cmd -t "$table" -A "PROXY_OUTPUT$suffix" -j "APP_CHAIN$suffix"
$cmd -t "$table" -A "PROXY_OUTPUT$suffix" -j "GID_CHAIN$suffix"
$cmd -t "$table" -A "PROXY_OUTPUT$suffix" -j "DNS_HIJACK_OUT$suffix"
if [ "$family" = "6" ]; then
if [ -n "$PROXY_IPv6_LIST" ]; then
for subnet6 in $PROXY_IPv6_LIST; do
$cmd -t "$table" -A "PROXY_IP$suffix" -d "$subnet6" -j RETURN
done
log Info "Added proxy rules for PROXY IPv6 ranges"
fi
else
if [ -n "$PROXY_IPv4_LIST" ]; then
for subnet4 in $PROXY_IPv4_LIST; do
$cmd -t "$table" -A "PROXY_IP$suffix" -d "$subnet4" -j RETURN
done
log Info "Added proxy rules for PROXY IPv4 ranges"
fi
fi
if check_kernel_feature "NETFILTER_XT_MATCH_ADDRTYPE"; then
$cmd -t "$table" -A "BYPASS_IP$suffix" -m addrtype --dst-type LOCAL -p udp ! --dport 53 -j ACCEPT
$cmd -t "$table" -A "BYPASS_IP$suffix" -m addrtype --dst-type LOCAL ! -p udp -j ACCEPT
log Info "Added local address type bypass"
fi
if check_kernel_feature "NETFILTER_XT_MATCH_CONNTRACK"; then
$cmd -t "$table" -A "BYPASS_IP$suffix" -m conntrack --ctdir REPLY -j ACCEPT
log Info "Added reply connection direction bypass"
fi
if [ "$family" = "6" ]; then
for subnet6 in $BYPASS_IPv6_LIST; do
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet6" -p udp ! --dport 53 -j ACCEPT
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet6" ! -p udp -j ACCEPT
done
log Info "Added bypass rules for BYPASS IPv6 ranges"
else
for subnet4 in $BYPASS_IPv4_LIST; do
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet4" -p udp ! --dport 53 -j ACCEPT
$cmd -t "$table" -A "BYPASS_IP$suffix" -d "$subnet4" ! -p udp -j ACCEPT
done
log Info "Added bypass rules for BYPASS IPv4 ranges"
fi
if [ "$BYPASS_CN_IP" -eq 1 ]; then
ipset_name="cnip"
if [ "$family" = "6" ]; then
ipset_name="cnip6"
fi
if command -v ipset > /dev/null 2>&1 && ipset list "$ipset_name" > /dev/null 2>&1; then
$cmd -t "$table" -A "BYPASS_IP$suffix" -m set --match-set "$ipset_name" dst -p udp ! --dport 53 -j ACCEPT
$cmd -t "$table" -A "BYPASS_IP$suffix" -m set --match-set "$ipset_name" dst ! -p udp -j ACCEPT
log Info "Added ipset-based CN IP bypass rule"
else
log Warn "ipset '$ipset_name' not available, skipping CN IP bypass"
fi
fi
log Info "Configuring interface proxy rules"
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i lo -j RETURN
if [ "$PROXY_MOBILE" -eq 1 ]; then
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$MOBILE_INTERFACE" -j RETURN
log Info "Mobile interface $MOBILE_INTERFACE will be proxied"
else
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$MOBILE_INTERFACE" -j ACCEPT
$cmd -t "$table" -A "BYPASS_INTERFACE$suffix" -o "$MOBILE_INTERFACE" -j ACCEPT
log Info "Mobile interface $MOBILE_INTERFACE will bypass proxy"
fi
if [ "$PROXY_WIFI" -eq 1 ]; then
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$WIFI_INTERFACE" -j RETURN
log Info "WiFi interface $WIFI_INTERFACE will be proxied"
else
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$WIFI_INTERFACE" -j ACCEPT
$cmd -t "$table" -A "BYPASS_INTERFACE$suffix" -o "$WIFI_INTERFACE" -j ACCEPT
log Info "WiFi interface $WIFI_INTERFACE will bypass proxy"
fi
if [ "$PROXY_HOTSPOT" -eq 1 ]; then
if [ "$HOTSPOT_INTERFACE" = "$WIFI_INTERFACE" ]; then
local subnet=""
if [ "$family" = "6" ]; then
subnet="fe80::/10"
else
subnet="192.168.43.0/24"
fi
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$WIFI_INTERFACE" ! -s "$subnet" -j RETURN
log Info "Hotspot interface $WIFI_INTERFACE will be proxied"
else
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$HOTSPOT_INTERFACE" -j RETURN
log Info "Hotspot interface $HOTSPOT_INTERFACE will be proxied"
fi
else
$cmd -t "$table" -A "BYPASS_INTERFACE$suffix" -o "$HOTSPOT_INTERFACE" -j ACCEPT
log Info "Hotspot interface $HOTSPOT_INTERFACE will bypass proxy"
fi
if [ "$PROXY_USB" -eq 1 ]; then
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$USB_INTERFACE" -j RETURN
log Info "USB interface $USB_INTERFACE will be proxied"
else
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$USB_INTERFACE" -j ACCEPT
$cmd -t "$table" -A "BYPASS_INTERFACE$suffix" -o "$USB_INTERFACE" -j ACCEPT
log Info "USB interface $USB_INTERFACE will bypass proxy"
fi
if [ -n "$OTHER_PROXY_INTERFACES" ]; then
for interface in $OTHER_PROXY_INTERFACES; do
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$interface" -j RETURN
done
log Info "Other interface $OTHER_PROXY_INTERFACES will be proxied"
fi
if [ -n "$OTHER_BYPASS_INTERFACES" ]; then
for interface in $OTHER_BYPASS_INTERFACES; do
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -i "$interface" -j ACCEPT
$cmd -t "$table" -A "BYPASS_INTERFACE$suffix" -o "$interface" -j ACCEPT
done
log Info "Other interface $OTHER_PROXY_INTERFACES will bypass proxy"
fi
$cmd -t "$table" -A "PROXY_INTERFACE$suffix" -j ACCEPT
log Info "Interface proxy rules configuration completed"
if [ "$MAC_FILTER_ENABLE" -eq 1 ] && [ "$PROXY_HOTSPOT" -eq 1 ] && [ -n "$HOTSPOT_INTERFACE" ]; then
if check_kernel_feature "NETFILTER_XT_MATCH_MAC"; then
log Info "Setting up MAC address filter rules for interface $HOTSPOT_INTERFACE"
case "$MAC_PROXY_MODE" in
blacklist)
if [ -n "$BYPASS_MACS_LIST" ]; then
for mac in $BYPASS_MACS_LIST; do
if [ -n "$mac" ]; then
$cmd -t "$table" -A "MAC_CHAIN$suffix" -m mac --mac-source "$mac" -i "$HOTSPOT_INTERFACE" -j ACCEPT
log Info "Added MAC bypass rule for $mac"
fi
done
else
log Warn "MAC blacklist mode enabled but no bypass MACs configured"
fi
$cmd -t "$table" -A "MAC_CHAIN$suffix" -i "$HOTSPOT_INTERFACE" -j RETURN
;;
whitelist)
if [ -n "$PROXY_MACS_LIST" ]; then
for mac in $PROXY_MACS_LIST; do
if [ -n "$mac" ]; then
$cmd -t "$table" -A "MAC_CHAIN$suffix" -m mac --mac-source "$mac" -i "$HOTSPOT_INTERFACE" -j RETURN
log Info "Added MAC proxy rule for $mac"
fi
done
else
log Warn "MAC whitelist mode enabled but no proxy MACs configured"
fi
$cmd -t "$table" -A "MAC_CHAIN$suffix" -i "$HOTSPOT_INTERFACE" -j ACCEPT
;;
esac
else
log Warn "MAC filtering requires NETFILTER_XT_MATCH_MAC kernel feature which is not available"
fi
fi
if check_kernel_feature "NETFILTER_XT_MATCH_OWNER"; then
$cmd -t "$table" -A "APP_CHAIN$suffix" -m owner --uid-owner "$CORE_USER" --gid-owner "$CORE_GROUP" -j ACCEPT
log Info "Added bypass for core user $CORE_USER:$CORE_GROUP"
elif check_kernel_feature "NETFILTER_XT_MATCH_MARK" && [ -n "$ROUTING_MARK" ]; then
$cmd -t "$table" -A "APP_CHAIN$suffix" -m mark --mark "$ROUTING_MARK" -j ACCEPT
log Info "Added bypass for marked traffic with core mark $ROUTING_MARK"
else
log Warn "Core traffic bypass not configured, may cause traffic loop"
fi
if [ "$APP_PROXY_ENABLE" -eq 1 ]; then
if check_kernel_feature "NETFILTER_XT_MATCH_OWNER"; then
log Info "Setting up application filter rules in $APP_PROXY_MODE mode"
case "$APP_PROXY_MODE" in
blacklist)
if [ -n "$BYPASS_APPS_LIST" ]; then
uids=$(find_packages_uid "$BYPASS_APPS_LIST" || true)
for uid in $uids; do
if [ -n "$uid" ]; then
$cmd -t "$table" -A "APP_CHAIN$suffix" -m owner --uid-owner "$uid" -j ACCEPT
log Info "Added bypass for UID $uid"
fi
done
else
log Warn "App blacklist mode enabled but no bypass apps configured"
fi
$cmd -t "$table" -A "APP_CHAIN$suffix" -j RETURN
;;
whitelist)
if [ -n "$PROXY_APPS_LIST" ]; then
uids=$(find_packages_uid "$PROXY_APPS_LIST" || true)
for uid in $uids; do
if [ -n "$uid" ]; then
$cmd -t "$table" -A "APP_CHAIN$suffix" -m owner --uid-owner "$uid" -j RETURN
log Info "Added proxy for UID $uid"
fi
done
else
log Warn "App whitelist mode enabled but no proxy apps configured"
fi
$cmd -t "$table" -A "APP_CHAIN$suffix" -j ACCEPT
;;
esac
else
log Warn "Application filtering requires NETFILTER_XT_MATCH_OWNER kernel feature which is not available"
fi
fi
if [ "$GID_PROXY_ENABLE" -eq 1 ]; then
if check_kernel_feature "NETFILTER_XT_MATCH_OWNER"; then
log Info "Setting up GID filter rules in $GID_PROXY_MODE mode"
case "$GID_PROXY_MODE" in
blacklist)
if [ -n "$BYPASS_GIDS_LIST" ]; then
for gid in $BYPASS_GIDS_LIST; do
if [ -n "$gid" ]; then
$cmd -t "$table" -A "GID_CHAIN$suffix" -m owner --gid-owner "$gid" -j ACCEPT
log Info "Added bypass for GID $gid"
fi
done
else
log Warn "GID blacklist mode enabled but no bypass gids configured"
fi
$cmd -t "$table" -A "GID_CHAIN$suffix" -j RETURN
;;
whitelist)
if [ -n "$PROXY_GIDS_LIST" ]; then
for gid in $PROXY_GIDS_LIST; do
if [ -n "$gid" ]; then
$cmd -t "$table" -A "GID_CHAIN$suffix" -m owner --gid-owner "$gid" -j RETURN
log Info "Added proxy for GID $gid"
fi
done
else
log Warn "GID whitelist mode enabled but no proxy gids configured"
fi
$cmd -t "$table" -A "GID_CHAIN$suffix" -j ACCEPT
;;
esac
else
log Warn "GID filtering requires NETFILTER_XT_MATCH_OWNER kernel feature which is not available"
fi
fi
if [ "$DNS_HIJACK_ENABLE" -ne 0 ]; then
if [ "$mode" = "redirect" ]; then
setup_dns_hijack "$family" "redirect"
else
if [ "$DNS_HIJACK_ENABLE" -eq 2 ]; then
setup_dns_hijack "$family" "redirect2"
else
setup_dns_hijack "$family" "tproxy"
fi
fi
fi
if [ "$mode" = "tproxy" ]; then
$cmd -t "$table" -A "PROXY_PREROUTING$suffix" -p tcp -j TPROXY --on-port "$PROXY_TCP_PORT" --tproxy-mark "$mark"
$cmd -t "$table" -A "PROXY_PREROUTING$suffix" -p udp -j TPROXY --on-port "$PROXY_UDP_PORT" --tproxy-mark "$mark"
$cmd -t "$table" -A "PROXY_OUTPUT$suffix" -j MARK --set-mark "$mark"
log Info "TPROXY mode rules added"
else
$cmd -t "$table" -A "PROXY_PREROUTING$suffix" -j REDIRECT --to-ports "$PROXY_TCP_PORT"
$cmd -t "$table" -A "PROXY_OUTPUT$suffix" -j REDIRECT --to-ports "$PROXY_TCP_PORT"
log Info "REDIRECT mode rules added"
fi
# Add rules to main chains
if [ "$PROXY_UDP" -eq 1 ] || [ "$mode" = "redirect" ]; then
$cmd -t "$table" -I PREROUTING -p udp -j "PROXY_PREROUTING$suffix"
$cmd -t "$table" -I OUTPUT -p udp -j "PROXY_OUTPUT$suffix"
log Info "Added UDP rules to PREROUTING and OUTPUT chains"
fi
if [ "$PROXY_TCP" -eq 1 ]; then
$cmd -t "$table" -I PREROUTING -p tcp -j "PROXY_PREROUTING$suffix"
$cmd -t "$table" -I OUTPUT -p tcp -j "PROXY_OUTPUT$suffix"
log Info "Added TCP rules to PREROUTING and OUTPUT chains"
fi
log Info "$mode_name chains for IPv${family} setup completed"
}
setup_dns_hijack() {
local family="$1"
local mode="$2"
local suffix=""
local mark="$MARK_VALUE"
local cmd="iptables"
if [ "$family" = "6" ]; then
suffix="6"
mark="$MARK_VALUE6"
cmd="ip6tables"
fi
case "$mode" in
tproxy)
# Handle DNS from interfaces in PREROUTING chain (DNS_HIJACK_PRE)
$cmd -t mangle -A "DNS_HIJACK_PRE$suffix" -j RETURN
# Handle local DNS hijacking in OUTPUT chain (DNS_HIJACK_OUT)
$cmd -t mangle -A "DNS_HIJACK_OUT$suffix" -j RETURN
log Info "DNS hijack enabled using TPROXY mode"
;;
redirect)
# Handle DNS using REDIRECT method
$cmd -t nat -A "DNS_HIJACK_PRE$suffix" -p tcp --dport 53 -j REDIRECT --to-ports "$DNS_PORT"
$cmd -t nat -A "DNS_HIJACK_PRE$suffix" -p udp --dport 53 -j REDIRECT --to-ports "$DNS_PORT"
$cmd -t nat -A "DNS_HIJACK_OUT$suffix" -p tcp --dport 53 -j REDIRECT --to-ports "$DNS_PORT"
$cmd -t nat -A "DNS_HIJACK_OUT$suffix" -p udp --dport 53 -j REDIRECT --to-ports "$DNS_PORT"
log Info "DNS hijack enabled using REDIRECT mode to port $DNS_PORT"
;;
redirect2)
# Handle DNS using REDIRECT method
if [ "$family" = "6" ] && {
! check_kernel_feature "IP6_NF_NAT" || ! check_kernel_feature "IP6_NF_TARGET_REDIRECT"
}; then
log Warn "IPv6: Kernel does not support IPv6 NAT or REDIRECT, skipping IPv6 DNS hijack"
return 0
fi
safe_chain_create "$family" "nat" "NAT_DNS_HIJACK$suffix"
$cmd -t nat -A "NAT_DNS_HIJACK$suffix" -p tcp --dport 53 -j REDIRECT --to-ports "$DNS_PORT"
$cmd -t nat -A "NAT_DNS_HIJACK$suffix" -p udp --dport 53 -j REDIRECT --to-ports "$DNS_PORT"
[ "$PROXY_MOBILE" -eq 1 ] && $cmd -t nat -A PREROUTING -i "$MOBILE_INTERFACE" -j "NAT_DNS_HIJACK$suffix"
[ "$PROXY_WIFI" -eq 1 ] && $cmd -t nat -A PREROUTING -i "$WIFI_INTERFACE" -j "NAT_DNS_HIJACK$suffix"
[ "$PROXY_USB" -eq 1 ] && $cmd -t nat -A PREROUTING -i "$USB_INTERFACE" -j "NAT_DNS_HIJACK$suffix"
$cmd -t nat -A OUTPUT -p udp --dport 53 -m owner --uid-owner "$CORE_USER" --gid-owner "$CORE_GROUP" -j ACCEPT
$cmd -t nat -A OUTPUT -p tcp --dport 53 -m owner --uid-owner "$CORE_USER" --gid-owner "$CORE_GROUP" -j ACCEPT
$cmd -t nat -A OUTPUT -j "NAT_DNS_HIJACK$suffix"
log Info "DNS hijack enabled using REDIRECT mode to port $DNS_PORT"
;;
esac
}
setup_tproxy_chain4() {
setup_proxy_chain 4 "tproxy"
}
setup_redirect_chain4() {
log Warn "REDIRECT mode only supports TCP"
setup_proxy_chain 4 "redirect"
}
setup_tproxy_chain6() {
setup_proxy_chain 6 "tproxy"
}
setup_redirect_chain6() {
if ! check_kernel_feature "IP6_NF_NAT" || ! check_kernel_feature "IP6_NF_TARGET_REDIRECT"; then
log Warn "IPv6: Kernel does not support IPv6 NAT or REDIRECT, skipping IPv6 proxy setup"
return 0
fi
log Warn "REDIRECT mode only supports TCP"
setup_proxy_chain 6 "redirect"
}
setup_routing4() {
log Info "Setting up routing rules for IPv4"
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] ip rule add fwmark $MARK_VALUE lookup $TABLE_ID"
log Debug "[DRY-RUN] ip route add local 0.0.0.0/0 dev lo table $TABLE_ID"
log Debug "[DRY-RUN] echo 1 > /proc/sys/net/ipv4/ip_forward"
else
ip_rule del fwmark "$MARK_VALUE" lookup "$TABLE_ID" 2> /dev/null || true
ip_route del local 0.0.0.0/0 dev lo table "$TABLE_ID" 2> /dev/null || true
if ! ip_rule add fwmark "$MARK_VALUE" table "$TABLE_ID" pref "$TABLE_ID"; then
log Error "Failed to add IPv4 routing rule"
return 1
fi
if ! ip_route add local 0.0.0.0/0 dev lo table "$TABLE_ID"; then
log Error "Failed to add IPv4 route"
ip_rule del fwmark "$MARK_VALUE" table "$TABLE_ID" pref "$TABLE_ID" 2> /dev/null || true
return 1
fi
echo 1 > /proc/sys/net/ipv4/ip_forward
fi
log Info "IPv4 routing setup completed"
}
setup_routing6() {
log Info "Setting up routing rules for IPv6"
if [ "$DRY_RUN" -eq 1 ]; then
log Debug "[DRY-RUN] ip -6 rule add fwmark $MARK_VALUE6 lookup $TABLE_ID"
log Debug "[DRY-RUN] ip -6 route add local ::/0 dev lo table $TABLE_ID"
log Debug "[DRY-RUN] echo 1 > /proc/sys/net/ipv6/conf/all/forwarding"
else
ip6_rule del fwmark "$MARK_VALUE6" table "$TABLE_ID" pref "$TABLE_ID" 2> /dev/null || true
ip6_route del local ::/0 dev lo table "$TABLE_ID" 2> /dev/null || true
if ! ip6_rule add fwmark "$MARK_VALUE6" table "$TABLE_ID" pref "$TABLE_ID"; then
log Error "Failed to add IPv6 routing rule"
return 1
fi
if ! ip6_route add local ::/0 dev lo table "$TABLE_ID"; then
log Error "Failed to add IPv6 route"
ip6_rule del fwmark "$MARK_VALUE6" table "$TABLE_ID" pref "$TABLE_ID" 2> /dev/null || true
return 1
fi
echo 1 > /proc/sys/net/ipv6/conf/all/forwarding
fi
log Info "IPv6 routing setup completed"
}
# Unified cleanup function for both IPv4 and IPv6 with mode selection
cleanup_chain() {
local family="$1"
local mode="$2"
local suffix=""
local cmd="iptables"
if [ "$family" = "6" ]; then
suffix="6"
cmd="ip6tables"
fi
# Set mode name for logging
local mode_name="$mode"
if [ "$mode" = "tproxy" ]; then
mode_name="TPROXY"
else
mode_name="REDIRECT"
fi