-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathopenclash
More file actions
3827 lines (3474 loc) · 183 KB
/
Copy pathopenclash
File metadata and controls
3827 lines (3474 loc) · 183 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
#!/bin/sh /etc/rc.common
# Copyright (c) 2019-2026 vernesong
START=99
STOP=15
USE_PROCD=1
. $IPKG_INSTROOT/usr/share/openclash/openclash_ps.sh
. $IPKG_INSTROOT/usr/share/openclash/ruby.sh
. $IPKG_INSTROOT/usr/share/openclash/log.sh
. $IPKG_INSTROOT/usr/share/openclash/uci.sh
. $IPKG_INSTROOT/usr/share/openclash/openclash_curl.sh
[ -f /etc/openwrt_release ] && {
FW4=$(command -v fw4)
DEFAULT_DNSMASQ_CFGID="$(uci -q show "dhcp.@dnsmasq[0]" | awk 'NR==1 {split($0, conf, /[.=]/); print conf[2]}')"
if [ -f "/tmp/etc/dnsmasq.conf.$DEFAULT_DNSMASQ_CFGID" ]; then
DNSMASQ_CONF_DIR="$(awk -F '=' '/^conf-dir=/ {print $2}' "/tmp/etc/dnsmasq.conf.$DEFAULT_DNSMASQ_CFGID")"
else
DNSMASQ_CONF_DIR="/tmp/dnsmasq.d"
fi
DNSMASQ_CONF_DIR=${DNSMASQ_CONF_DIR%*/}
}
CLASH="/etc/openclash/clash"
CLASH_CONFIG="/etc/openclash"
CRON_FILE="/etc/crontabs/root"
CACHE_PATH="/etc/openclash/cache.db"
LOG_FILE="/tmp/openclash.log"
START_LOG="/tmp/openclash_start.log"
PROXY_FWMARK="0x162"
PROXY_ROUTE_TABLE="0x162"
QUICK_START_CHECK=false
QUICK_START=true
add_cron()
{
[ "$(tail -n1 /etc/crontabs/root | wc -l)" -eq 0 ] && [ -n "$(cat /etc/crontabs/root 2>/dev/null)" ] && echo >> /etc/crontabs/root
[ -z "$(grep "openclash.sh" "$CRON_FILE" 2>/dev/null)" ] && {
[ "$(uci_get_config "auto_update")" -eq 1 ] && [ "$(uci_get_config "config_auto_update_mode")" -ne 1 ] && echo "0 $(uci_get_config "auto_update_time" || 1) * * $(uci_get_config "config_update_week_time" || 0) /usr/share/openclash/openclash.sh #openclash-cron-task" >> $CRON_FILE
}
[ -z "$(grep "openclash_geo.sh" "$CRON_FILE" 2>/dev/null)" ] && {
[ "$(uci_get_config "geo_auto_update")" -eq 1 ] && echo "0 $(uci_get_config "geo_update_day_time" || 1) * * $(uci_get_config "geo_update_week_time" || 0) /usr/share/openclash/openclash_geo.sh ipdb #openclash-cron-task" >> $CRON_FILE
[ "$(uci_get_config "geosite_auto_update")" -eq 1 ] && echo "0 $(uci_get_config "geosite_update_day_time" || 1) * * $(uci_get_config "geosite_update_week_time" || 0) /usr/share/openclash/openclash_geo.sh geosite #openclash-cron-task" >> $CRON_FILE
[ "$(uci_get_config "geoip_auto_update")" -eq 1 ] && echo "0 $(uci_get_config "geoip_update_day_time" || 1) * * $(uci_get_config "geoip_update_week_time" || 0) /usr/share/openclash/openclash_geo.sh geoip #openclash-cron-task" >> $CRON_FILE
[ "$(uci_get_config "geoasn_auto_update")" -eq 1 ] && echo "0 $(uci_get_config "geoasn_update_day_time" || 1) * * $(uci_get_config "geoasn_update_week_time" || 0) /usr/share/openclash/openclash_geo.sh geoasn #openclash-cron-task" >> $CRON_FILE
}
[ -z "$(grep "openclash_chnroute.sh" "$CRON_FILE" 2>/dev/null)" ] && {
[ "$(uci_get_config "chnr_auto_update")" -eq 1 ] && echo "0 $(uci_get_config "chnr_update_day_time" || 1) * * $(uci_get_config "chnr_update_week_time" || 0) /usr/share/openclash/openclash_chnroute.sh #openclash-cron-task" >> $CRON_FILE
}
[ -z "$(grep "/etc/init.d/openclash" "$CRON_FILE" 2>/dev/null)" ] && {
[ "$(uci_get_config "auto_restart")" -eq 1 ] && echo "0 $(uci_get_config "auto_restart_day_time" || 1) * * $(uci_get_config "auto_restart_week_time" || 0) /etc/init.d/openclash restart #openclash-cron-task" >> $CRON_FILE
}
config_load "openclash"
config_foreach add_overwrite_cron "config_overwrite"
crontab $CRON_FILE
start_watchdog
}
del_cron()
{
sed -i '/#openclash-cron-task/d' $CRON_FILE
sed -i '/#openclash-overwrite-download/d' $CRON_FILE
/etc/init.d/cron restart
} >/dev/null 2>&1
save_dnsmasq_server() {
if [ -z "$1" ] || [ "$1" == "127.0.0.1#${dns_port}" ]; then
return
fi
uci -q add_list openclash.config.dnsmasq_server="$1"
}
set_dnsmasq_server() {
if [ -z "$1" ] || [ "$1" == "127.0.0.1#${dns_port}" ]; then
return
fi
uci -q add_list dhcp.@dnsmasq[0].server="$1"
}
load_ip_route_pass() {
local settype nftflag
if dnsmasq --version | grep -q 'Compile time options:.* nftset'; then
settype="nftset"
nftflag="inet#fw4#"
else
settype="ipset"
[ -n "$FW4" ] && LOG_WARN "Dnsmasq not Support nftset, Use ipset..."
fi
if [ -n "$FW4" ]; then
if [ "$china_ip_route" != "0" ] || [ "$disable_udp_quic" = "1" ]; then
if [ "$enable_redirect_dns" != "2" ]; then
mkdir -p ${DNSMASQ_CONF_DIR}
if [ "$settype" = "nftset" ]; then
nft add set inet fw4 china_ip_route_pass '{ type ipv4_addr; flags interval; auto-merge; }'
else
ipset -! create china_ip_route_pass hash:net family inet hashsize 1024 maxelem 1000000
fi
awk '!/^$/&&!/^#/&&!/(^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])((\/[0-9][0-9])?)$/{printf("'${settype}'=/%s/'${nftflag}'china_ip_route_pass'" "'\n",$0)}' /etc/openclash/custom/openclash_custom_chnroute_pass.list >>${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf
for ip in $(uci_get_config "china_ip_route_pass"); do
[ -z "$ip" ] && continue
echo "$ip" | awk '!/^$/&&!/^#/&&!/(^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])((\/[0-9][0-9])?)$/{printf("'${settype}'=/%s/'${nftflag}'china_ip_route_pass'" "'\n",$0)}'
done >>${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf
fi
fi
if [ "$ipv6_enable" -eq 1 ]; then
if [ "$china_ip6_route" != "0" ] || [ "$disable_udp_quic" = "1" ]; then
if [ "$enable_redirect_dns" != "2" ]; then
mkdir -p ${DNSMASQ_CONF_DIR}
if [ "$settype" = "nftset" ]; then
nft add set inet fw4 china_ip6_route_pass '{ type ipv6_addr; flags interval; auto-merge; }'
else
ipset -! create china_ip6_route_pass hash:net family inet6 hashsize 1024 maxelem 1000000
fi
awk '!/^$/&&!/^#/&&/([0-9a-zA-Z-]{1,}\.)+([a-zA-Z]{2,})/{printf("'${settype}'=/%s/'${nftflag}'china_ip_route_pass'" "'\n",$0)}' /etc/openclash/custom/openclash_custom_chnroute6_pass.list >>${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
for ip in $(uci_get_config "china_ip6_route_pass"); do
[ -z "$ip" ] && continue
echo "$ip" | awk '!/^$/&&!/^#/&&/([0-9a-zA-Z-]{1,}\.)+([a-zA-Z]{2,})/{printf("'${settype}'=/%s/'${nftflag}'china_ip_route_pass'" "'\n",$0)}'
done >>${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
#Prevent domain repeat
for i in `grep -wf ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf`
do
if [ -n "$nftflag" ]; then
sed -i "s:${i}:${i},6#${nftflag}china_ip6_route_pass:g" ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf
else
sed -i "s:${i}:${i},china_ip6_route_pass:g" ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf
fi
sed -i 's:'$i':EXCLUSIVE:;/EXCLUSIVE/d' ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
done
if [ -n "$nftflag" ]; then
sed -i "s/\/${nftflag}/\/4#${nftflag}/g" ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf
sed -i "s/${nftflag}china_ip_route_pass/6#${nftflag}china_ip6_route_pass/g" ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
else
sed -i "s/china_ip_route_pass/china_ip6_route_pass/g" ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
fi
fi
fi
fi
else
if [ "$china_ip_route" != "0" ] || [ "$disable_udp_quic" = "1" ]; then
if [ "$enable_redirect_dns" != "2" ]; then
mkdir -p ${DNSMASQ_CONF_DIR}
ipset -! create china_ip_route_pass hash:net family inet hashsize 1024 maxelem 1000000
awk '!/^$/&&!/^#/&&!/(^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])((\/[0-9][0-9])?)$/{printf("ipset=/%s/china_ip_route_pass'" "'\n",$0)}' /etc/openclash/custom/openclash_custom_chnroute_pass.list >>${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf
for ip in $(uci_get_config "china_ip_route_pass"); do
[ -z "$ip" ] && continue
echo "$ip" | awk '!/^$/&&!/^#/&&!/(^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-4])((\/[0-9][0-9])?)$/{printf("ipset=/%s/china_ip_route_pass'" "'\n",$0)}'
done >>${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf
fi
fi
if [ "$ipv6_enable" -eq 1 ]; then
if [ "$china_ip6_route" != "0" ] || [ "$disable_udp_quic" = "1" ]; then
if [ "$enable_redirect_dns" != "2" ]; then
mkdir -p ${DNSMASQ_CONF_DIR}
ipset -! create china_ip6_route_pass hash:net family inet6 hashsize 1024 maxelem 1000000
awk '!/^$/&&!/^#/&&/([0-9a-zA-Z-]{1,}\.)+([a-zA-Z]{2,})/{printf("ipset=/%s/china_ip_route_pass'" "'\n",$0)}' /etc/openclash/custom/openclash_custom_chnroute6_pass.list >>${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
for ip in $(uci_get_config "china_ip6_route_pass"); do
[ -z "$ip" ] && continue
echo "$ip" | awk '!/^$/&&!/^#/&&/([0-9a-zA-Z-]{1,}\.)+([a-zA-Z]{2,})/{printf("ipset=/%s/china_ip_route_pass'" "'\n",$0)}'
done >>${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
#Prevent domain repeat
for i in `grep -wf ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf`
do
sed -i "s:${i}:${i},china_ip6_route_pass:g" ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf
sed -i 's:'$i':EXCLUSIVE:;/EXCLUSIVE/d' ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
done
sed -i "s/china_ip_route_pass/china_ip6_route_pass/g" ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
fi
fi
fi
fi
}
change_dnsmasq() {
if ! /etc/init.d/dnsmasq enabled; then
return
fi
# 区域绕过黑名单
load_ip_route_pass
# 第二 DNS 服务
/usr/share/openclash/openclash_custom_domain_dns.sh
if [ "$1" -eq 1 ]; then
if [ "$(uci_get_config "redirect_dns")" != "1" ]; then
uci -q del openclash.config.dnsmasq_server
config_load "dhcp"
config_list_foreach "$(uci -q show dhcp.@dnsmasq[0].server |awk -F '.' '{print $2}')" "server" save_dnsmasq_server
uci -q set openclash.config.dnsmasq_noresolv="$(uci -q get dhcp.@dnsmasq[0].noresolv)"
uci -q set openclash.config.dnsmasq_resolvfile="$(uci -q get dhcp.@dnsmasq[0].resolvfile)"
fi
uci -q del dhcp.@dnsmasq[-1].server
uci -q add_list dhcp.@dnsmasq[0].server=127.0.0.1#"$dns_port"
uci -q delete dhcp.@dnsmasq[0].resolvfile
uci -q set dhcp.@dnsmasq[0].noresolv=1
uci -q set dhcp.@dnsmasq[0].localuse=1
uci -q set openclash.config.redirect_dns=1
uci -q set openclash.config.dnsmasq_cachesize="$(uci -q get dhcp.@dnsmasq[0].cachesize)"
uci -q set dhcp.@dnsmasq[0].cachesize=0
uci -q set openclash.config.cachesize_dns=1
else
uci -q set openclash.config.redirect_dns=0
uci -q set openclash.config.cachesize_dns=0
fi
if [ "$1" -eq 1 ] && [ "$ipv6_dns" -eq 1 ] && [ -n "$(ip6tables -t mangle -L 2>&1 | grep -o 'Chain')" ]; then
#dnsmasq answer ipv6
uci -q set openclash.config.dnsmasq_filter_aaaa="$(uci -q get dhcp.@dnsmasq[0].filter_aaaa)"
uci -q set dhcp.@dnsmasq[0].filter_aaaa=0
uci -q set openclash.config.filter_aaaa_dns=1
else
uci -q set openclash.config.filter_aaaa_dns=0
fi
uci -q commit openclash
uci -q commit dhcp
/etc/init.d/dnsmasq restart
} >/dev/null 2>&1
revert_dnsmasq()
{
if ! /etc/init.d/dnsmasq enabled; then
return
fi
redirect_dns=$(uci_get_config "redirect_dns")
dnsmasq_server=$(uci_get_config "dnsmasq_server")
dnsmasq_noresolv=$(uci_get_config "dnsmasq_noresolv")
dnsmasq_resolvfile=$(uci_get_config "dnsmasq_resolvfile")
cachesize_dns=$(uci_get_config "cachesize_dns")
dnsmasq_cachesize=$(uci_get_config "dnsmasq_cachesize")
filter_aaaa_dns=$(uci_get_config "filter_aaaa_dns")
dnsmasq_filter_aaaa=$(uci_get_config "dnsmasq_filter_aaaa")
default_resolvfile=$(uci_get_config "default_resolvfile")
rm -rf ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_custom_domain.conf
rm -rf ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute_pass.conf
rm -rf ${DNSMASQ_CONF_DIR}/dnsmasq_openclash_chnroute6_pass.conf
[ "$redirect_dns" -eq 1 ] && {
uci -q del dhcp.@dnsmasq[-1].server
[ -n "$dnsmasq_server" ] && {
config_load "openclash"
config_list_foreach "config" "dnsmasq_server" set_dnsmasq_server
}
if [ "$dnsmasq_noresolv" == "0" ] || [ -z "$dnsmasq_noresolv" ] || [ -z "$(uci -q show dhcp.@dnsmasq[0].server)" ]; then
uci -q set dhcp.@dnsmasq[0].noresolv=0
if [ -n "$dnsmasq_resolvfile" ] && [ -n "$(grep nameserver $dnsmasq_resolvfile)" ]; then
uci -q set dhcp.@dnsmasq[0].resolvfile="$dnsmasq_resolvfile"
elif [ -n "$default_resolvfile" ] && [ -n "$(grep nameserver $default_resolvfile)" ]; then
uci -q set dhcp.@dnsmasq[0].resolvfile="$default_resolvfile"
elif [ -s "/tmp/resolv.conf.d/resolv.conf.auto" ] && [ -n "$(grep "nameserver" /tmp/resolv.conf.d/resolv.conf.auto)" ]; then
uci -q set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.d/resolv.conf.auto
uci -q set openclash.config.default_resolvfile=/tmp/resolv.conf.d/resolv.conf.auto
elif [ -s "/tmp/resolv.conf.auto" ] && [ -n "$(grep "nameserver" /tmp/resolv.conf.auto)" ]; then
uci -q set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.auto
uci -q set openclash.config.default_resolvfile=/tmp/resolv.conf.auto
else
uci -q set dhcp.@dnsmasq[0].resolvfile=/tmp/resolv.conf.d/resolv.conf.auto
uci -q set openclash.config.default_resolvfile=/tmp/resolv.conf.d/resolv.conf.auto
fi
uci -q set dhcp.@dnsmasq[0].localuse=1
fi
}
[ "$cachesize_dns" -eq 1 ] && {
uci -q set dhcp.@dnsmasq[0].cachesize="$dnsmasq_cachesize"
uci -q set openclash.config.cachesize_dns=0
uci -q delete openclash.config.dnsmasq_cachesize
}
[ "$filter_aaaa_dns" -eq 1 ] && {
uci -q set dhcp.@dnsmasq[0].filter_aaaa="$dnsmasq_filter_aaaa"
uci -q set openclash.config.filter_aaaa_dns=0
uci -q delete openclash.config.dnsmasq_filter_aaaa
}
[ "$redirect_dns" -eq 1 ] && {
uci -q set openclash.config.redirect_dns=0
uci -q del openclash.config.dnsmasq_server
}
uci -q commit dhcp
uci -q commit openclash
masq_port=$(uci -q get dhcp.@dnsmasq[0].port)
if [ "$(nslookup www.apple.com 127.0.0.1:${masq_port} >/dev/null 2>&1 || echo $?)" = "1" ]; then
resolv_file=$(uci -q get dhcp.@dnsmasq[0].resolvfile)
wan_dns=$(/usr/share/openclash/openclash_get_network.lua "dns")
wan6_dns=$(/usr/share/openclash/openclash_get_network.lua "dns6")
mkdir -p "$(dirname "$resolv_file")"
touch "$resolv_file"
if [ -n "$wan_dns" ]; then
echo "# Interface lan" > "$resolv_file"
for dns in $wan_dns; do
echo "nameserver $dns" >> "$resolv_file"
done
fi
if [ -n "$wan6_dns" ]; then
echo "# Interface LAN6" > "$resolv_file"
for dns6 in $wan6_dns; do
echo "nameserver $dns6" >> "$resolv_file"
done
fi
if [ -z "$wan_dns" ] && [ -z "$wan6_dns" ]; then
cat > "$resolv_file" <<-EOF
# Interface lan
nameserver 119.29.29.29
nameserver 8.8.8.8
EOF
fi
fi
/etc/init.d/dnsmasq restart
} >/dev/null 2>&1
start_fail()
{
uci -q set openclash.config.enable=0
uci -q commit openclash
stop
exit 0
}
sub_info_set()
{
local section="$1" name
config_get "name" "$section" "name" ""
if [ -z "$name" ]; then
return
fi
if [ "$name" == "$2" ] && [ -n "$3" ]; then
if [ "$sub_info_setted" != "1" ]; then
uci -q delete openclash.$section.url
fi
uci -q add_list openclash.$section.url="$3"
uci -q commit openclash
sub_info_setted=1
fi
}
#获取订阅配置
sub_info_get()
{
local section="$1" address enabled name
config_get_bool "enabled" "$section" "enabled" "1"
config_get "address" "$section" "address" ""
config_get "name" "$section" "name" ""
if [ "$subscribe_enable" = "1" ]; then
return
fi
if [ "$enabled" -eq 0 ]; then
return
fi
if [ -z "$address" ]; then
return
fi
if [ -z "$name" ]; then
SUB_CONFIG_FILE="/etc/openclash/config/config.yaml"
else
SUB_CONFIG_FILE="/etc/openclash/config/$name.yaml"
fi
if [ "$SUB_CONFIG_FILE" != "$2" ]; then
return
fi
subscribe_enable=1
}
#配置文件选择
config_choose()
{
if [ ! -f "$RAW_CONFIG_FILE" ]; then
config_load "openclash"
config_foreach sub_info_get "config_subscribe" "$RAW_CONFIG_FILE"
if [ "$subscribe_enable" = "1" ]; then
LOG_OUT "【$RAW_CONFIG_FILE】Config File Does Not Exist, You Have Set Subscription Information, Ready To Download..."
/usr/share/openclash/openclash.sh "$RAW_CONFIG_FILE" &
exit 0
fi
fi
if [ -z "$RAW_CONFIG_FILE" ] || [ ! -f "$RAW_CONFIG_FILE" ]; then
for file_name in /etc/openclash/config/*
do
if [ -f "$file_name" ]; then
CONFIG_NAME=$(echo "$file_name" |awk -F '/' '{print $5}' 2>/dev/null)
uci -q set openclash.config.config_path="/etc/openclash/config/$CONFIG_NAME"
uci -q commit openclash
RAW_CONFIG_FILE="/etc/openclash/config/$CONFIG_NAME"
CONFIG_FILE="/etc/openclash/$CONFIG_NAME"
TMP_CONFIG_FILE="/tmp/$CONFIG_NAME"
LOG_ERROR "Config Not Found, Switch Config File to【$RAW_CONFIG_FILE】"
break
fi
done
fi
if [ ! -f "$RAW_CONFIG_FILE" ]; then
LOG_ERROR "Config Not Found"
exit 0
fi
CONFIG_NAME=$(echo "$RAW_CONFIG_FILE" |awk -F '/' '{print $5}' 2>/dev/null)
HISTORY_PATH="/etc/openclash/history/${CONFIG_NAME%.*}.db"
} >/dev/null 2>&1
config_check()
{
#创建启动配置
#rm -rf "/etc/openclash/*.y*" 2>/dev/null
cp "$RAW_CONFIG_FILE" "$TMP_CONFIG_FILE"
ruby -ryaml -rYAML -I "/usr/share/openclash" -E UTF-8 -e "
begin
YAML.load_file('$RAW_CONFIG_FILE');
rescue Exception => e
YAML.LOG_ERROR('Unable To Parse Config File,【' + e.message + '】');
system 'rm -rf ${TMP_CONFIG_FILE}';
end
" 2>/dev/null >> $LOG_FILE
if [ $? -ne 0 ]; then
LOG_ERROR "Ruby Works Abnormally, Please Check The Ruby Library Depends!"
start_fail
elif [ ! -f "$TMP_CONFIG_FILE" ] || [ ! -s "$TMP_CONFIG_FILE" ]; then
LOG_ERROR "Config File Format Validation Failed..."
start_fail
fi
}
check_run_quick()
{
if $QUICK_START_CHECK; then
return
fi
QUICK_START_CHECK=true
cat "/tmp/openclash.change" | while read -r i; do
file_path=$(echo "$i" |awk -F ' #edited time# ' '{print $1}')
if [ -z "$(grep "$file_path #edited time# $(date -r "$file_path")$" "/tmp/openclash.change")" ]; then
LOG_TIP "Because of the file【 $file_path 】modificated, Pause quick start..."
rm -rf /tmp/openclash.change
break
fi
done
if [ ! -f "/tmp/openclash.change" ]; then
QUICK_START=false
fi
} >/dev/null 2>&1
write_run_quick()
{
: > "/tmp/openclash.change"
{
echo "/etc/config/openclash"
echo "$RAW_CONFIG_FILE"
echo "$CONFIG_FILE"
ls -d /etc/openclash/custom/* 2>/dev/null
ls -d /etc/openclash/overwrite/* 2>/dev/null
} | while read -r file; do
echo "$file #edited time# $(date -r "$file")" >> "/tmp/openclash.change"
done
} >/dev/null 2>&1
#运行模式处理
do_run_mode()
{
en_mode=$(uci_get_config "en_mode")
if [ "$en_mode" = "fake-ip-tun" ]; then
en_mode_tun="1"
en_mode="fake-ip"
fi
if [ "$en_mode" = "redir-host-tun" ]; then
en_mode_tun="1"
en_mode="redir-host"
fi
if [ "$en_mode" = "redir-host-mix" ]; then
en_mode_tun="2"
en_mode="redir-host"
fi
if [ "$en_mode" = "fake-ip-mix" ]; then
en_mode_tun="2"
en_mode="fake-ip"
fi
}
do_run_file()
{
#Some MIPS devices file system cound not use db
source "/etc/openwrt_release"
[ "$small_flash_memory" == "1" ] || [ -n "$(echo $core_version |grep mips)" ] || [ -n "$(echo $DISTRIB_ARCH |grep mips)" ] || [ -n "$(opkg status libc 2>/dev/null |grep 'Architecture' |awk -F ': ' '{print $2}' |grep mips)" ] || [ -n "$(apk list libc 2>/dev/null |grep mips)" ] && mkdir -p /tmp/etc/openclash && CACHE_PATH="/tmp/etc/openclash/cache.db"
[ -f "/etc/openclash/geosite.dat" ] && {
mv "/etc/openclash/geosite.dat" "/etc/openclash/GeoSite.dat"
}
[ -f "/etc/openclash/geoip.dat" ] && {
mv "/etc/openclash/geoip.dat" "/etc/openclash/GeoIP.dat"
}
if [ "$small_flash_memory" != "1" ]; then
meta_core_path="/etc/openclash/core/clash_meta"
ipdb_path="/etc/openclash/Country.mmdb"
chnr_path="/etc/openclash/china_ip_route.ipset"
chnr6_path="/etc/openclash/china_ip6_route.ipset"
geosite_path="/etc/openclash/GeoSite.dat"
geoip_path="/etc/openclash/GeoIP.dat"
asn_path="/etc/openclash/ASN.mmdb"
lgbm_path="/etc/openclash/Model.bin"
mv "/tmp/etc/openclash/Country.mmdb" "$ipdb_path"
mv "/tmp/etc/openclash/china_ip_route.ipset" "$chnr_path"
mv "/tmp/etc/openclash/china_ip6_route.ipset" "$chnr6_path"
mv "/tmp/etc/openclash/GeoSite.dat" "$geosite_path"
mv "/tmp/etc/openclash/GeoIP.dat" "$geoip_path"
mv "/tmp/etc/openclash/ASN.mmdb" "$asn_path"
mv "/tmp/etc/openclash/Model.bin" "$lgbm_path"
mv "/tmp/etc/openclash/core/" "/etc/openclash"
if [ "$CACHE_PATH" != "/tmp/etc/openclash/cache.db" ]; then
rm -rf "/tmp/etc/openclash"
fi
else
meta_core_path="/tmp/etc/openclash/core/clash_meta"
ipdb_path="/tmp/etc/openclash/Country.mmdb"
chnr_path="/tmp/etc/openclash/china_ip_route.ipset"
chnr6_path="/tmp/etc/openclash/china_ip6_route.ipset"
geosite_path="/tmp/etc/openclash/GeoSite.dat"
geoip_path="/tmp/etc/openclash/GeoIP.dat"
asn_path="/tmp/etc/openclash/ASN.mmdb"
lgbm_path="/tmp/etc/openclash/Model.bin"
[ ! -h "/etc/openclash/Country.mmdb" ] && mv "/etc/openclash/Country.mmdb" "$ipdb_path"
[ ! -h "/etc/openclash/china_ip_route.ipset" ] && mv "/etc/openclash/china_ip_route.ipset" "$chnr_path"
[ ! -h "/etc/openclash/china_ip6_route.ipset" ] && mv "/etc/openclash/china_ip6_route.ipset" "$chnr6_path"
[ ! -h "/etc/openclash/GeoSite.dat" ] && mv "/etc/openclash/GeoSite.dat" "$geosite_path"
[ ! -h "/etc/openclash/GeoIP.dat" ] && mv "/etc/openclash/GeoIP.dat" "$geoip_path"
[ ! -h "/etc/openclash/ASN.mmdb" ] && mv "/etc/openclash/ASN.mmdb" "$asn_path"
[ ! -h "/etc/openclash/Model.bin" ] && mv "/etc/openclash/Model.bin" "$lgbm_path"
mv "/etc/openclash/core/" "/tmp/etc/openclash"
fi
rm -rf "/etc/openclash/cache.db"
rm -rf "/etc/openclash/clash"
ln -s "$meta_core_path" /etc/openclash/clash
if [ -n "$OIX_TOKEN" ]; then
core_type="Oix"
elif [ "$smart_enable" -eq 1 ] || [ "$core_type" == "Smart" ]; then
core_type="Smart"
else
core_type="Meta"
fi
if [ -f "$CLASH" ] && [ ! -x "$CLASH" ]; then
chmod 4755 "$CLASH"
chown root:root "$CLASH"
fi
[ ! -f "$CLASH" ] || { [ "$core_type" = "Smart" ] && [ -z "$($CLASH -v | grep 'smart')" ]; } || { [ "$core_type" = "Oix" ] && [ -z "$($CLASH -v | grep 'oix')" ]; } && {
LOG_TIP "【$core_type】Core is not Detected installed, Ready to Download..."
rm -rf "/tmp/clash_last_version"
/usr/share/openclash/openclash_core.sh "$core_type"
if [ ! -f "$meta_core_path" ]; then
start_fail
fi
}
if [ "$china_ip_route" != "0" ] || [ "$china_ip6_route" != "0" ] || [ "$disable_udp_quic" = "1" ]; then
if [ ! -f "$chnr_path" ] || [ ! -f "$chnr6_path" ]; then
LOG_TIP "Detected that the Chnroute Cidr is not Installed, Ready to Download..."
/usr/share/openclash/openclash_chnroute.sh
fi
if [ -n "$FW4" ]; then
if [ -z "$(cat "$chnr_path" |grep "define china_ip_route")" ] || [ -z "$(cat "$chnr6_path" |grep "define china_ip6_route")" ]; then
LOG_TIP "Detected that the Chnroute Cidr List Format is wrong, Ready to Reformat..."
/usr/share/openclash/openclash_chnroute.sh
if [ -z "$(cat "$chnr_path" |grep "define china_ip_route")" ] || [ -z "$(cat "$chnr6_path" |grep "define china_ip6_route")" ]; then
start_fail
fi
fi
else
if [ -n "$(cat "$chnr_path" |grep "define china_ip_route")" ] || [ -n "$(cat "$chnr6_path" |grep "define china_ip6_route")" ]; then
LOG_TIP "Detected that the Chnroute Cidr List Format is wrong, Ready to Reformat..."
/usr/share/openclash/openclash_chnroute.sh
if [ -n "$(cat "$chnr_path" |grep "define china_ip_route")" ] || [ -n "$(cat "$chnr6_path" |grep "define china_ip6_route")" ]; then
start_fail
fi
fi
fi
if [ ! -f "$chnr_path" ] || [ ! -f "$chnr6_path" ]; then
start_fail
fi
fi
[ ! -x "$meta_core_path" ] && chmod 4755 "$meta_core_path"
[ -f "$ipdb_path" ] && [ "$small_flash_memory" = "1" ] && {
ln -s "$ipdb_path" /etc/openclash/Country.mmdb
}
[ -f "$geosite_path" ] && [ "$small_flash_memory" = "1" ] && {
ln -s "$geosite_path" /etc/openclash/GeoSite.dat
}
[ -f "$geoip_path" ] && [ "$small_flash_memory" = "1" ] && {
ln -s "$geoip_path" /etc/openclash/GeoIP.dat
}
[ -f "$lgbm_path" ] && [ "$small_flash_memory" = "1" ] && {
ln -s "$lgbm_path" /etc/openclash/Model.bin
}
[ -f "$chnr_path" ] && [ "$small_flash_memory" = "1" ] && {
ln -s "$chnr_path" /etc/openclash/china_ip_route.ipset
}
[ -f "$chnr6_path" ] && [ "$small_flash_memory" = "1" ] && {
ln -s "$chnr6_path" /etc/openclash/china_ip6_route.ipset
}
[ -f "$asn_path" ] && [ "$small_flash_memory" = "1" ] && {
ln -s "$asn_path" /etc/openclash/ASN.mmdb
}
#Restore history cache
if [ -f "$HISTORY_PATH" ]; then
cmp -s "$CACHE_PATH" "$HISTORY_PATH"
if [ "$?" -ne "0" ]; then
if [ "$CACHE_PATH" != "/tmp/etc/openclash/cache.db" ]; then
ln -s "$HISTORY_PATH" "$CACHE_PATH"
else
cp "$HISTORY_PATH" "$CACHE_PATH"
fi
fi
fi
if [ "$CACHE_PATH" == "/tmp/etc/openclash/cache.db" ]; then
[ ! -f "$CACHE_PATH" ] && touch "$CACHE_PATH"
ln -s "$CACHE_PATH" /etc/openclash/cache.db
else
[ ! -f "$CACHE_PATH" ] && touch "$HISTORY_PATH"
ln -s "$HISTORY_PATH" "$CACHE_PATH"
fi
#保存启动内核类型
uci -q set openclash.config.core_type="$core_type"
uci -q commit openclash
} >/dev/null 2>&1
container() {
[ -f "/proc/1/cgroup" ] && grep -qiE "(docker|containerd|lxc|podman|kubepods|container)" /proc/1/cgroup 2>/dev/null && return 0
[ -f "/proc/1/environ" ] && grep -qiE "(docker|containerd|lxc|podman|kubepods|container)" /proc/1/environ 2>/dev/null && return 0
[ -f "/.dockerenv" ] && return 0
env | grep -qiE "(docker|kubernetes|container)" && return 0
return 1
}
check_mod()
{
if container; then
return 0
fi
# Convert module name to uppercase using POSIX-compatible syntax
# BusyBox tr doesn't support [:lower:]/[:upper:], use a-z/A-Z instead
module_upper=$(echo "$1" | tr a-z A-Z)
if [ -f /proc/config.gz ] && zcat /proc/config.gz | grep -q "CONFIG_${module_upper}=y"; then
return 0
fi
if lsmod | grep -q "^$1 "; then
return 0
fi
modprobe $1 2>/dev/null
if [ $? -eq 0 ]; then
return 0
fi
LOG_ERROR "【$1】module not found, please check your system depends if something abnormal!"
} >/dev/null 2>&1
check_core_status()
{
TUN_WAIT=0
TUN_RESTART=1
CORE_WAIT=0
CORE_HTTP_CODE=0
while ( [ -z "$(pidof clash)" ] && [ "$CORE_WAIT" -le 10 ] )
do
sleep 1
let CORE_WAIT++
done
if [ -n "$en_mode_tun" ] || [ "$ipv6_mode" -eq 2 ] || [ "$ipv6_mode" -eq 3 ]; then
check_mod "tun"
if [ -n "$en_mode_tun" ]; then
ip_="ip"
else
ip_="ip -6"
fi
#wait 300s most for core start
while ( [ -n "$(pidof clash)" ] && [ -z "$($ip_ route list |grep utun)" ] && [ "$TUN_WAIT" -le 300 ] )
do
$ip_ link set utun up
let TUN_WAIT++
sleep 1
done
if [ -n "$(pidof clash)" ] && [ -z "$($ip_ route list |grep utun)" ] && [ "$TUN_WAIT" -gt 300 ]; then
while ( [ -n "$(pidof clash)" ] && [ -z "$($ip_ route list |grep utun)" ] && [ "$TUN_RESTART" -le 3 ] )
do
LOG_WARN "TUN Interface Start Failed, Try to Restart Again..."
start_run_core
let TUN_RESTART++
sleep 300
done
if [ -n "$(pidof clash)" ] && [ -z "$($ip_ route list |grep utun)" ] && [ "$TUN_RESTART" -gt 3 ]; then
LOG_ERROR "TUN Interface Start Failed, Please Check The Dependence or Try to Restart Again!"
LOG_ERROR "Core Initial Configuration Timeout, Please Check The Log Infos!"
start_fail
fi
fi
if [ -n "$(pidof clash)" ]; then
while ( [ -n "$(pidof clash)" ] && [ -n "$(ip -6 rule show |grep 2022)" ] && [ "$CORE_WAIT" -le 10 ] )
do
ip -6 rule del oif utun table 2022
ip -6 route del default dev utun table 2022
let CORE_WAIT++
done
if [ "$ipv6_mode" -eq 2 ] || [ "$ipv6_mode" -eq 3 ]; then
ip -6 route add default dev utun table "$PROXY_ROUTE_TABLE"
ip -6 rule add fwmark "$PROXY_FWMARK" table "$PROXY_ROUTE_TABLE" pref 1888
fi
if [ -n "$en_mode_tun" ]; then
ip route add default dev utun table "$PROXY_ROUTE_TABLE"
ip rule add fwmark "$PROXY_FWMARK" table "$PROXY_ROUTE_TABLE" pref 1888
fi
fi
else
reg4='^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$'
while ( [ -n "$(pidof clash)" ] && [ "$CORE_HTTP_CODE" != "200" ] && [ "$TUN_WAIT" -le 300 ] && [ -n "$(echo ${lan_ip} | grep -Eo ${reg4})" ] )
do
CORE_HTTP_CODE=$(curl -m 5 -o /dev/null -s -w '%{http_code}' -H 'Content-Type: application/json' -H "Authorization: Bearer ${da_password}" -XGET http://${lan_ip}:${cn_port}/group)
let TUN_WAIT++
sleep 1
done
if [ -z "$(echo ${lan_ip} | grep -Eo ${reg4})" ]; then
LOG_ERROR "LAN IP Address Get Error, Please Check The LAN Interface Setting or Choose the Correct Interface in the Setting!"
start_fail
fi
if [ -n "$(pidof clash)" ] && [ "$CORE_HTTP_CODE" != "200" ]; then
LOG_ERROR "Core Initial Configuration Timeout, Please Check The Log Infos!"
start_fail
fi
fi
if [ -z "$(pidof clash)" ]; then
LOG_ERROR "Core Start Failed, Please Check The Log Infos!"
start_fail
fi
# redirect dns setting after core started, prevent core dns lookup failure
if [ "$1" == "start" ]; then
change_dnsmasq "$enable_redirect_dns"
set_firewall
LOG_TIP "OpenClash Start Successful!"
else
set_firewall
LOG_TIP "Firewall Reload Successful!"
fi
write_run_quick
SLOG_CLEAN
} >/dev/null 2>&1
start_run_core()
{
ulimit -SHn 1000000
ulimit -v unlimited
ulimit -u unlimited
if ! $QUICK_START; then
mv "$TMP_CONFIG_FILE" "$CONFIG_FILE"
rm -rf "$TMP_CONFIG_FILE"
fi
chown root:root "$CLASH"
procd_open_instance "openclash"
procd_set_param env SAFE_PATHS=/usr/share/openclash:/etc/ssl
procd_append_param env CLASH_AGE_SECRET_KEY="$SECRET_KEY"
procd_append_param env OIX_TOKEN="$OIX_TOKEN"
procd_set_param command /bin/sh -c "$CLASH -d $CLASH_CONFIG -f \"$CONFIG_FILE\" >> $LOG_FILE 2>&1"
procd_set_param user "root"
procd_set_param group "nogroup"
procd_set_param limits nproc="unlimited" as="unlimited" memlock="unlimited" nofile="1000000 1000000"
procd_set_param respawn 300 5 3
procd_set_param stderr 1
procd_set_param no_new_privs 1
procd_close_instance
} >/dev/null 2>&1
#防火墙设置部分
nft_ac_add()
{
if [ -z "$1" ]; then
return
fi
nft add element inet fw4 "$2" { "$1" }
[ -n "$3" ] && nft add element inet fw4 "$3" { "$1" }
} >/dev/null 2>&1
ac_add()
{
if [ -z "$1" ]; then
return
fi
ipset add "$2" "$1"
[ -n "$3" ] && ipset add "$3" "$1"
} >/dev/null 2>&1
wan_name_add()
{
if [ -z "$1" ]; then
return
fi
if [ -n "$wan_ints" ]; then
wan_ints="$wan_ints $1"
else
wan_ints="$1"
fi
}
wan6_name_add()
{
if [ -z "$1" ]; then
return
fi
if [ -n "$wan6_ints" ]; then
wan6_ints="$wan6_ints $1"
else
wan6_ints="$1"
fi
}
upnp_exclude()
{
if [ -s "$upnp_lease_file" ]; then
cat "$upnp_lease_file" |while read -r line
do
if [ -n "$line" ]; then
upnp_ip=$(echo "$line" |awk -F ':' '{print $3}')
upnp_dp=$(echo "$line" |awk -F ':' '{print $4}')
upnp_type=$(echo "$line" |awk -F ':' '{print $1}' |tr '[A-Z]' '[a-z]')
if [ -n "$upnp_ip" ] && [ -n "$upnp_dp" ] && [ -n "$upnp_type" ]; then
if [ -n "$FW4" ]; then
if [ -z "$(nft list chain inet fw4 openclash_upnp |grep "$upnp_ip" |grep "$upnp_dp" |grep "$upnp_type")" ]; then
nft add rule inet fw4 openclash_upnp ip saddr { "$upnp_ip" } "$upnp_type" sport "$upnp_dp" counter return
fi
else
if [ -z "$(iptables -t mangle -nL openclash_upnp |grep "$upnp_ip" |grep "$upnp_dp" |grep "$upnp_type")" ]; then
iptables -t mangle -A openclash_upnp -p "$upnp_type" -s "$upnp_ip" --sport "$upnp_dp" -j RETURN
fi
fi
fi
fi
done
fi
} >/dev/null 2>&1
ipv6_suffix_to_nft_format()
{
local ipv6_with_prefix="$1"
if ! echo "$ipv6_with_prefix" | grep -q '/'; then
echo "{ $ipv6_with_prefix }"
return
fi
local addr="${ipv6_with_prefix%%/*}"
local suffix="${ipv6_with_prefix##*/}"
if echo "$suffix" | grep -qE '^[0-9]+$'; then
echo "${addr}/${suffix}"
return
fi
echo "& ${suffix} == ${addr}"
} 2>/dev/null
firewall_lan_ac_traffic()
{
local src_port sport_rule dscp_rule sport_ipt dscp_ipt src_ip src_ip_v6 proto target target_ enabled family dscp rule output_rule comment
config_get "src_port" "$section" "src_port" "0-65535"
config_get "src_ip" "$section" "src_ip" ""
config_get "proto" "$section" "proto" "both"
config_get "target" "$section" "target" "return"
config_get_bool "enabled" "$section" "enabled" "0"
config_get "family" "$section" "family" "both"
config_get "dscp" "$section" "dscp" ""
config_get "interface" "$section" "interface" ""
config_get "user" "$section" "user" ""
config_get "comment" "$section" "comment" "lan_ac_traffic"
if [ "${enabled}" == "0" ]; then
return
fi
local e_udp=false
local e_tcp=false
if [ "${proto}" == "tcp" ]; then e_tcp=true; fi
if [ "${proto}" == "udp" ]; then e_udp=true; fi
if [ "${proto}" == "both" ]; then e_tcp=true; e_udp=true; fi
if [ -n "$FW4" ]; then
if [ "${src_ip}" == "localnetwork" ]; then
src_ip="ip saddr @localnetwork"
src_ip_v6="ip6 saddr @localnetwork6"
else
if [ -n "${src_ip}" ]; then
src_ip_v6="ip6 saddr $(ipv6_suffix_to_nft_format "${src_ip}")"
src_ip="ip saddr { ${src_ip} }"
fi
fi
if [ -n "${src_port}" ]; then
sport_rule="sport ${src_port}"
fi
if [ -n "${interface}" ]; then
interface_rule="iifname \"${interface}\""
fi
if [ -n "${user}" ]; then
user_rule="meta skuid ${user}"
fi
if [ -n "${dscp}" ]; then
dscp_rule="ip dscp ${dscp}"
fi
if [ "${target}" == "drop" ]; then
target_="return"
else
target_="${target}"
fi
rule_target_v4_="${sport_rule} ${interface_rule} ${dscp_rule} meta nfproto {ipv4} ip daddr != { ${fakeip_range} } ${src_ip} counter ${target_} comment ${comment}"
output_rule_target_v4_="${sport_rule} ${user_rule} ${interface_rule} ${dscp_rule} ip daddr != { ${fakeip_range} } ${src_ip} counter ${target_} comment ${comment}"
rule_target_v4="${sport_rule} ${interface_rule} ${dscp_rule} meta nfproto {ipv4} ip daddr != { ${fakeip_range} } ${src_ip} counter ${target} comment ${comment}"
output_rule_target_v4="${sport_rule} ${user_rule} ${interface_rule} ${dscp_rule} ip daddr != { ${fakeip_range} } ${src_ip} counter ${target} comment ${comment}"
rule_target_v6_="${sport_rule} ${interface_rule} ${dscp_rule} meta nfproto {ipv6} ip6 daddr != { ${fakeip_range6} } ${src_ip_v6} counter ${target_} comment ${comment}"
output_rule_target_v6_="${sport_rule} ${user_rule} ${interface_rule} ${dscp_rule} ip6 daddr != { ${fakeip_range6} } ${src_ip_v6} counter ${target_} comment ${comment}"
rule_target_v6="${sport_rule} ${interface_rule} ${dscp_rule} meta nfproto {ipv6} ip6 daddr != { ${fakeip_range6} } ${src_ip_v6} counter ${target} comment ${comment}"
output_rule_target_v6="${sport_rule} ${user_rule} ${interface_rule} ${dscp_rule} ip6 daddr != { ${fakeip_range6} } ${src_ip_v6} counter ${target} comment ${comment}"
if [ "${family}" == "both" ] || [ "${family}" == "ipv4" ]; then
if [ -z "${en_mode_tun}" ] || [ "${en_mode_tun}" -eq 2 ]; then
if $e_tcp ; then
nft insert rule inet fw4 openclash_output position 0 tcp ${output_rule_target_v4_}
[ -z "${user_rule}" ] && nft insert rule inet fw4 openclash position 0 tcp ${rule_target_v4_}
fi
if $e_udp ; then
nft insert rule inet fw4 openclash_mangle_output position 0 udp ${output_rule_target_v4}
[ -z "${user_rule}" ] && nft insert rule inet fw4 openclash_mangle position 0 udp ${rule_target_v4}
fi
elif [ "${en_mode_tun}" -eq 1 ]; then
if $e_tcp ; then
nft insert rule inet fw4 openclash_mangle_output position 0 tcp ${output_rule_target_v4}
[ -z "${user_rule}" ] && nft insert rule inet fw4 openclash_mangle position 0 tcp ${rule_target_v4}