-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
YazFi.sh
3484 lines (3091 loc) · 114 KB
/
YazFi.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
#############################################
## ##
## __ __ ______ _ ##
## \ \ / / | ____|(_) ##
## \ \_/ /__ _ ____| |__ _ ##
## \ // _ ||_ /| __| | | ##
## | || (_| | / / | | | | ##
## |_| \__,_|/___||_| |_| ##
## ##
## https://github.com/jackyaz/YazFi/ ##
## ##
#############################################
## Credit to @RMerlin for the original ##
## guest network DHCP script and for ##
## AsusWRT-Merlin firmware ##
#############################################
# Last Modified: 2023-Nov-18
#--------------------------------------------------
###### Shellcheck directives ######
# shellcheck disable=SC1003
# shellcheck disable=SC1090
# shellcheck disable=SC2005
# shellcheck disable=SC2016
# shellcheck disable=SC2018
# shellcheck disable=SC2019
# shellcheck disable=SC2034
# shellcheck disable=SC2039
# shellcheck disable=SC2059
# shellcheck disable=SC2086
# shellcheck disable=SC2140
# shellcheck disable=SC2155
# shellcheck disable=SC3003
# shellcheck disable=SC3043
#############################################
### Start of script variables ###
readonly SCRIPT_NAME="YazFi"
readonly SCRIPT_CONF="/jffs/addons/$SCRIPT_NAME.d/config"
readonly YAZFI_VERSION="v4.4.4"
readonly SCRIPT_VERSION="v4.4.4"
SCRIPT_BRANCH="master"
SCRIPT_REPO="https://jackyaz.io/$SCRIPT_NAME/$SCRIPT_BRANCH"
readonly SCRIPT_DIR="/jffs/addons/$SCRIPT_NAME.d"
readonly USER_SCRIPT_DIR="$SCRIPT_DIR/userscripts.d"
readonly SCRIPT_WEBPAGE_DIR="$(readlink /www/user)"
readonly SCRIPT_WEB_DIR="$SCRIPT_WEBPAGE_DIR/$SCRIPT_NAME"
readonly SHARED_DIR="/jffs/addons/shared-jy"
readonly SHARED_REPO="https://jackyaz.io/shared-jy/master"
readonly SHARED_WEB_DIR="$SCRIPT_WEBPAGE_DIR/shared-jy"
### End of script variables ###
### Start of output format variables ###
readonly CRIT="\\e[41m"
readonly ERR="\\e[31m"
readonly WARN="\\e[33m"
readonly PASS="\\e[32m"
readonly BOLD="\\e[1m"
readonly SETTING="${BOLD}\\e[36m"
readonly CLEARFORMAT="\\e[0m"
### End of output format variables ###
### Start of router environment variables ###
readonly LAN="$(nvram get lan_ipaddr)"
[ -z "$(nvram get odmpid)" ] && ROUTER_MODEL=$(nvram get productid) || ROUTER_MODEL=$(nvram get odmpid)
##-------------------------------------##
## Added by Martinski W. [2022-Nov-18] ##
##-------------------------------------##
_GetWiFiVirtualInterfaceNames_()
{
local IFListStr=""
for IFname in $(nvram get wl0_vifnames) $(nvram get wl1_vifnames) $(nvram get wl2_vifnames) $(nvram get wl3_vifnames)
do
IFname="$(echo "$IFname" | grep -E "^wl[0-3][.][1-3]$")"
[ -n "$IFname" ] && { [ -n "$IFListStr" ] && IFListStr="$IFListStr $IFname" || IFListStr="$IFname" ; }
done
echo "$IFListStr"
}
##----------------------------------------##
## Modified by Martinski W. [2022-Nov-18] ##
##----------------------------------------##
readonly IFACELIST_FULL="wl0.1 wl0.2 wl0.3 wl1.1 wl1.2 wl1.3 wl2.1 wl2.2 wl2.3 wl3.1 wl3.2 wl3.3"
IFACELIST="$(_GetWiFiVirtualInterfaceNames_)"
### End of router environment variables ###
### Start of path variables ###
readonly DNSCONF="$SCRIPT_DIR/.dnsmasq"
readonly TMPCONF="$SCRIPT_DIR/.dnsmasq.tmp"
### End of path variables ###
### Start of firewall variables ###
readonly INPT="${SCRIPT_NAME}INPUT"
readonly FWRD="${SCRIPT_NAME}FORWARD"
readonly LGRJT="${SCRIPT_NAME}REJECT"
readonly DNSFLTR="${SCRIPT_NAME}DNSFILTER"
readonly DNSFLTR_DOT="${SCRIPT_NAME}DNSFILTER_DOT"
readonly CHAINS="$INPT $FWRD $LGRJT $DNSFLTR_DOT"
readonly NATCHAINS="$DNSFLTR"
### End of firewall variables ###
### Start of VPN clientlist variables ###
VPN_IP_LIST_ORIG_1=""
VPN_IP_LIST_ORIG_2=""
VPN_IP_LIST_ORIG_3=""
VPN_IP_LIST_ORIG_4=""
VPN_IP_LIST_ORIG_5=""
VPN_IP_LIST_NEW_1=""
VPN_IP_LIST_NEW_2=""
VPN_IP_LIST_NEW_3=""
VPN_IP_LIST_NEW_4=""
VPN_IP_LIST_NEW_5=""
### End of VPN clientlist variables ###
##----------------------------------------------##
## Added/modified by Martinski W. [2023-Jan-28] ##
##----------------------------------------------##
# DHCP Lease Time: Min & Max Values in seconds.
# 2 minutes=120 to 90 days=7776000 (inclusive).
# Single '0' or 'I' indicates "infinite" value.
#------------------------------------------------#
readonly MinDHCPLeaseTime=120
readonly MaxDHCPLeaseTime=7776000
readonly InfiniteLeaseTimeTag="I"
readonly InfiniteLeaseTimeVal="infinite"
##-------------------------------------##
## Added by Martinski W. [2022-Dec-23] ##
##-------------------------------------##
readonly SUPPORTstr="$(nvram get rc_support)"
if echo "$SUPPORTstr" | grep -qw '2.4G'
then Band_24G_Support=true
else Band_24G_Support=false
fi
if echo "$SUPPORTstr" | grep -qw '5G'
then Band_5G_1_Support=true
else Band_5G_1_Support=false
fi
if echo "$SUPPORTstr" | grep -qw '5G-2'
then Band_5G_2_support=true
else Band_5G_2_support=false
fi
if echo "$SUPPORTstr" | grep -qw 'wifi6e'
then Band_6G_1_Support=true
else Band_6G_1_Support=false
fi
# $1 = print to syslog, $2 = message to print, $3 = log level
Print_Output(){
if [ "$1" = "true" ]; then
logger -t "$SCRIPT_NAME" "$2"
fi
printf "${BOLD}${3}%s${CLEARFORMAT}\\n\\n" "$2"
}
Generate_Random_String(){
PASSLENGTH=16
if Validate_Number "" "$1" silent; then
if [ "$1" -le 32 ] && [ "$1" -ge 8 ]; then
PASSLENGTH="$1"
else
printf "${BOLD}Number is not between 8 and 32, using default of 16 characters${CLEARFORMAT}\\n"
fi
else
printf "${BOLD}Invalid number provided, using default of 16 characters${CLEARFORMAT}\\n"
fi
< /dev/urandom tr -cd 'A-Za-z0-9' | head -c "$PASSLENGTH"
}
Escape_Sed(){
sed -e 's/</\\</g;s/>/\\>/g;s/ /\\ /g'
}
Get_Iface_Var(){
echo "$1" | sed -e 's/\.//g'
}
##-------------------------------------##
## Added by Martinski W. [2022-Dec-23] ##
##-------------------------------------##
GetIFaceUILabel()
{
if [ $# -eq 0 ] || [ -z "$1" ] ; then echo "" ; fi
theUILabel="*UNKNOWN*"
case "$1" in
wl0)
if [ "$ROUTER_MODEL" = "GT-AXE16000" ] && "$Band_5G_1_Support"
then theUILabel="5GHz"
elif "$Band_24G_Support" ; then theUILabel="2.4GHz"
fi
;;
wl1)
if [ "$ROUTER_MODEL" = "GT-AXE16000" ] && "$Band_5G_2_support"
then theUILabel="5GHz2"
elif "$Band_5G_1_Support" ; then theUILabel="5GHz"
fi
;;
wl2)
if [ "$ROUTER_MODEL" = "GT-AXE16000" ] || "$Band_6G_1_Support"
then theUILabel="6GHz"
elif "$Band_5G_2_support" ; then theUILabel="5GHz2"
fi
;;
wl3)
if [ "$ROUTER_MODEL" = "GT-AXE16000" ] && "$Band_24G_Support"
then theUILabel="2.4GHz"
fi
;;
esac
echo "$theUILabel"
}
##----------------------------------------##
## Modified by Martinski W. [2022-Dec-23] ##
##----------------------------------------##
Get_Guest_Name()
{
if [ $# -eq 0 ] || [ -z "$1" ] ; then echo "" ; fi
theIFprefix="$(echo "$1" | cut -d '.' -f1)"
theIFnumber="$(echo "$1" | cut -d '.' -f2)"
theIFlabel="$(GetIFaceUILabel "$theIFprefix")"
echo "YazFi $theIFlabel $theIFnumber"
}
##----------------------------------------##
## Modified by Martinski W. [2022-Dec-23] ##
##----------------------------------------##
Get_Guest_Name_Old()
{
if [ $# -eq 0 ] || [ -z "$1" ] ; then echo "" ; fi
theIFprefix="$(echo "$1" | cut -d '.' -f1)"
theIFnumber="$(echo "$1" | cut -d '.' -f2)"
theIFlabel="$(GetIFaceUILabel "$theIFprefix")"
if [ "$theIFlabel" = "5GHz" ]
then theIFlabel="5GHz1" ; fi
echo "$theIFlabel Guest $theIFnumber"
}
Set_WiFi_Passphrase(){
nvram set "${1}_wpa_psk"="$2"
nvram set "${1}_auth_mode_x"="psk2"
nvram set "${1}_akm"="psk2"
nvram commit
}
Iface_Manage(){
case $1 in
create)
ifconfig "$2" "$(eval echo '$'"$(Get_Iface_Var "$2")"_IPADDR | cut -f1-3 -d".").$(nvram get lan_ipaddr | cut -f4 -d".")" netmask 255.255.255.0
;;
delete)
ifconfig "$2" 0.0.0.0
;;
deleteall)
for IFACE in $IFACELIST; do
Iface_Manage delete "$IFACE"
done
;;
esac
}
Iface_BounceClients(){
Print_Output true "Forcing $SCRIPT_NAME Guest WiFi clients to reauthenticate" "$PASS"
for IFACE in $IFACELIST; do
wl -i "$IFACE" radio off >/dev/null 2>&1
done
sleep 10
for IFACE in $IFACELIST; do
wl -i "$IFACE" radio on >/dev/null 2>&1
done
ARPDUMP="$(arp -an)"
for IFACE in $IFACELIST; do
if [ "$(eval echo '$'"$(Get_Iface_Var "$IFACE")_ENABLED")" = "true" ]; then
IFACE_MACS="$(wl -i "$IFACE" assoclist)"
if [ "$IFACE_MACS" != "" ]; then
IFS=$'\n'
for GUEST_MAC in $IFACE_MACS; do
GUEST_MACADDR="${GUEST_MAC#* }"
GUEST_ARPINFO="$(arp -an | grep -i "$GUEST_MACADDR")"
for ARP_ENTRY in $GUEST_ARPINFO; do
GUEST_IPADDR="$(echo "$GUEST_ARPINFO" | awk '{print $2}' | sed -e 's/(//g;s/)//g')"
arp -d "$GUEST_IPADDR"
done
done
unset IFS
fi
fi
done
ip -s -s neigh flush all >/dev/null 2>&1
killall -q networkmap
sleep 5
if [ -z "$(pidof networkmap)" ]; then
networkmap >/dev/null 2>&1 &
fi
}
Auto_DNSMASQ(){
case $1 in
create)
if [ -f /jffs/scripts/dnsmasq.postconf ]; then
STARTUPLINECOUNT=$(grep -c "# $SCRIPT_NAME" /jffs/scripts/dnsmasq.postconf)
STARTUPLINECOUNTEX=$(grep -cx "cat $DNSCONF >> /etc/dnsmasq.conf # $SCRIPT_NAME" /jffs/scripts/dnsmasq.postconf)
if [ "$STARTUPLINECOUNT" -gt 1 ] || { [ "$STARTUPLINECOUNTEX" -eq 0 ] && [ "$STARTUPLINECOUNT" -gt 0 ]; }; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/dnsmasq.postconf
fi
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
echo "cat $DNSCONF >> /etc/dnsmasq.conf # $SCRIPT_NAME" >> /jffs/scripts/dnsmasq.postconf
fi
if [ "$(grep -c "NextDNS" /jffs/scripts/dnsmasq.postconf)" -gt 0 ]; then
sed -i '/exit 0/d' /jffs/scripts/dnsmasq.postconf
fi
else
echo "#!/bin/sh" > /jffs/scripts/dnsmasq.postconf
echo "" >> /jffs/scripts/dnsmasq.postconf
echo "cat $DNSCONF >> /etc/dnsmasq.conf # $SCRIPT_NAME" >> /jffs/scripts/dnsmasq.postconf
chmod 0755 /jffs/scripts/dnsmasq.postconf
fi
;;
delete)
if [ -f /jffs/scripts/dnsmasq.postconf ]; then
STARTUPLINECOUNT=$(grep -c "# $SCRIPT_NAME" /jffs/scripts/dnsmasq.postconf)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/dnsmasq.postconf
fi
fi
;;
esac
}
Auto_ServiceEvent(){
case $1 in
create)
if [ -f /jffs/scripts/service-event ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME Guest Networks" /jffs/scripts/service-event)
STARTUPLINECOUNTEX=$(grep -cx 'if echo "$2" | /bin/grep -q "'"$SCRIPT_NAME"'" || { \[ "$1" = "restart" \] && \[ "$2" = "wireless" \]; }; then { /jffs/scripts/'"$SCRIPT_NAME"' service_event "$@" & }; fi # '"$SCRIPT_NAME Guest Networks" /jffs/scripts/service-event)
if [ "$STARTUPLINECOUNT" -gt 1 ] || { [ "$STARTUPLINECOUNTEX" -eq 0 ] && [ "$STARTUPLINECOUNT" -gt 0 ]; }; then
sed -i -e '/# '"$SCRIPT_NAME"' Guest Networks/d' /jffs/scripts/service-event
fi
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
echo 'if echo "$2" | /bin/grep -q "'"$SCRIPT_NAME"'" || { [ "$1" = "restart" ] && [ "$2" = "wireless" ]; }; then { /jffs/scripts/'"$SCRIPT_NAME"' service_event "$@" & }; fi # '"$SCRIPT_NAME Guest Networks" >> /jffs/scripts/service-event
fi
else
echo "#!/bin/sh" > /jffs/scripts/service-event
echo "" >> /jffs/scripts/service-event
echo 'if echo "$2" | /bin/grep -q "'"$SCRIPT_NAME"'" || { [ "$1" = "restart" ] && [ "$2" = "wireless" ]; }; then { /jffs/scripts/'"$SCRIPT_NAME"' service_event "$@" & }; fi # '"$SCRIPT_NAME Guest Networks" >> /jffs/scripts/service-event
chmod 0755 /jffs/scripts/service-event
fi
;;
delete)
if [ -f /jffs/scripts/service-event ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME"' Guest Networks' /jffs/scripts/service-event)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"' Guest Networks/d' /jffs/scripts/service-event
fi
fi
;;
esac
}
Auto_Startup(){
case $1 in
create)
if [ -f /jffs/scripts/firewall-start ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME"' Guest Networks' /jffs/scripts/firewall-start)
STARTUPLINECOUNTEX=$(grep -cx "/jffs/scripts/$SCRIPT_NAME runnow & # $SCRIPT_NAME Guest Networks" /jffs/scripts/firewall-start)
if [ "$STARTUPLINECOUNT" -gt 1 ] || { [ "$STARTUPLINECOUNTEX" -eq 0 ] && [ "$STARTUPLINECOUNT" -gt 0 ]; }; then
sed -i -e '/# '"$SCRIPT_NAME"' Guest Networks/d' /jffs/scripts/firewall-start
fi
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
echo "/jffs/scripts/$SCRIPT_NAME runnow & # $SCRIPT_NAME Guest Networks" >> /jffs/scripts/firewall-start
fi
else
echo "#!/bin/sh" > /jffs/scripts/firewall-start
echo "" >> /jffs/scripts/firewall-start
echo "/jffs/scripts/$SCRIPT_NAME runnow & # $SCRIPT_NAME Guest Networks" >> /jffs/scripts/firewall-start
chmod 0755 /jffs/scripts/firewall-start
fi
;;
delete)
if [ -f /jffs/scripts/firewall-start ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME"' Guest Networks' /jffs/scripts/firewall-start)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"' Guest Networks/d' /jffs/scripts/firewall-start
fi
fi
;;
esac
}
Auto_ServiceEventEnd(){
case $1 in
create)
if [ -f /jffs/scripts/firewall-start ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME"' Guest Networks' /jffs/scripts/firewall-start)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"' Guest Networks/d' /jffs/scripts/firewall-start
fi
fi
if [ -f /jffs/scripts/service-event-end ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME Guest Networks" /jffs/scripts/service-event-end)
STARTUPLINECOUNTEX=$(grep -cx 'if { \[ "$1" = "start" \] || \[ "$1" = "restart" \]; } && \[ "$2" = "firewall" \]; then { /jffs/scripts/'"$SCRIPT_NAME"' runnow & }; fi # '"$SCRIPT_NAME Guest Networks" /jffs/scripts/service-event-end)
if [ "$STARTUPLINECOUNT" -gt 1 ] || { [ "$STARTUPLINECOUNTEX" -eq 0 ] && [ "$STARTUPLINECOUNT" -gt 0 ]; }; then
sed -i -e '/# '"$SCRIPT_NAME"' Guest Networks/d' /jffs/scripts/service-event-end
fi
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
echo 'if { [ "$1" = "start" ] || [ "$1" = "restart" ]; } && [ "$2" = "firewall" ]; then { /jffs/scripts/'"$SCRIPT_NAME"' runnow & }; fi # '"$SCRIPT_NAME Guest Networks" >> /jffs/scripts/service-event-end
fi
else
echo "#!/bin/sh" > /jffs/scripts/service-event-end
echo "" >> /jffs/scripts/service-event-end
echo 'if { [ "$1" = "start" ] || [ "$1" = "restart" ]; } && [ "$2" = "firewall" ]; then { /jffs/scripts/'"$SCRIPT_NAME"' runnow & }; fi # '"$SCRIPT_NAME Guest Networks" >> /jffs/scripts/service-event-end
chmod 0755 /jffs/scripts/service-event-end
fi
;;
delete)
if [ -f /jffs/scripts/firewall-start ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME"' Guest Networks' /jffs/scripts/firewall-start)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"' Guest Networks/d' /jffs/scripts/firewall-start
fi
fi
if [ -f /jffs/scripts/service-event ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME"' Guest Networks' /jffs/scripts/service-event)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"' Guest Networks/d' /jffs/scripts/service-event
fi
fi
;;
esac
}
Auto_ServiceStart(){
case $1 in
create)
if [ -f /jffs/scripts/services-start ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/services-start)
STARTUPLINECOUNTEX=$(grep -cx "/jffs/scripts/$SCRIPT_NAME startup & # $SCRIPT_NAME" /jffs/scripts/services-start)
if [ "$STARTUPLINECOUNT" -gt 1 ] || { [ "$STARTUPLINECOUNTEX" -eq 0 ] && [ "$STARTUPLINECOUNT" -gt 0 ]; }; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/services-start
fi
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
echo "/jffs/scripts/$SCRIPT_NAME startup & # $SCRIPT_NAME" >> /jffs/scripts/services-start
fi
else
echo "#!/bin/sh" > /jffs/scripts/services-start
echo "" >> /jffs/scripts/services-start
echo "/jffs/scripts/$SCRIPT_NAME startup & # $SCRIPT_NAME" >> /jffs/scripts/services-start
chmod 0755 /jffs/scripts/services-start
fi
;;
delete)
if [ -f /jffs/scripts/services-start ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/services-start)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/services-start
fi
fi
;;
esac
}
Auto_OpenVPNEvent(){
case $1 in
create)
if [ -f /jffs/scripts/openvpn-event ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/openvpn-event)
STARTUPLINECOUNTEX=$(grep -cx "/jffs/scripts/$SCRIPT_NAME openvpn "'$1 $script_type & # '"$SCRIPT_NAME" /jffs/scripts/openvpn-event)
if [ "$STARTUPLINECOUNT" -gt 1 ] || { [ "$STARTUPLINECOUNTEX" -eq 0 ] && [ "$STARTUPLINECOUNT" -gt 0 ]; }; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/openvpn-event
fi
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
sed -i '2 i /jffs/scripts/'"$SCRIPT_NAME"' openvpn $1 $script_type & # '"$SCRIPT_NAME" /jffs/scripts/openvpn-event
fi
else
echo "#!/bin/sh" > /jffs/scripts/openvpn-event
echo "" >> /jffs/scripts/openvpn-event
echo "/jffs/scripts/$SCRIPT_NAME openvpn "'$1 $script_type & # '"$SCRIPT_NAME" >> /jffs/scripts/openvpn-event
chmod 0755 /jffs/scripts/openvpn-event
fi
;;
delete)
if [ -f /jffs/scripts/openvpn-event ]; then
STARTUPLINECOUNT=$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/openvpn-event)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/openvpn-event
fi
fi
;;
esac
}
Auto_Cron(){
case $1 in
create)
STARTUPLINECOUNT=$(cru l | grep -c "$SCRIPT_NAME")
if [ "$STARTUPLINECOUNT" -eq 0 ]; then
cru a "$SCRIPT_NAME" "*/10 * * * * /jffs/scripts/$SCRIPT_NAME check"
fi
;;
delete)
STARTUPLINECOUNT=$(cru l | grep -c "$SCRIPT_NAME")
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
cru d "$SCRIPT_NAME"
fi
;;
esac
}
Avahi_Conf(){
case $1 in
create)
if [ -f /jffs/scripts/avahi-daemon.postconf ]; then
STARTUPLINECOUNT=$(grep -c "$SCRIPT_NAME" /jffs/scripts/avahi-daemon.postconf)
if [ "$STARTUPLINECOUNT" -eq 0 ]; then
{
echo 'echo "" >> "$1"'
echo 'echo "[reflector]" >> "$1" # '"$SCRIPT_NAME"
echo 'echo "enable-reflector=yes" >> "$1" # '"$SCRIPT_NAME"
echo "sed -i '/^\[Server\]/a cache-entries-max=0' "'"$1" # '"$SCRIPT_NAME"
} >> /jffs/scripts/avahi-daemon.postconf
service restart_mdns >/dev/null 2>&1
fi
else
{
echo '#!/bin/sh'
echo 'echo "" >> "$1"'
echo 'echo "[reflector]" >> "$1" # '"$SCRIPT_NAME"
echo 'echo "enable-reflector=yes" >> "$1" # '"$SCRIPT_NAME"
echo "sed -i '/^\[Server\]/a cache-entries-max=0' "'"$1" # '"$SCRIPT_NAME"
} > /jffs/scripts/avahi-daemon.postconf
chmod 0755 /jffs/scripts/avahi-daemon.postconf
service restart_mdns >/dev/null 2>&1
fi
;;
delete)
if [ -f /jffs/scripts/avahi-daemon.postconf ]; then
STARTUPLINECOUNT=$(grep -c "$SCRIPT_NAME" /jffs/scripts/avahi-daemon.postconf)
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/avahi-daemon.postconf
service restart_mdns >/dev/null 2>&1
fi
fi
;;
esac
}
### Code for this function courtesy of https://github.com/decoderman- credit to @thelonelycoder ###
Firmware_Version_Check(){
echo "$1" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
}
############################################################################
Firmware_Version_WebUI(){
if nvram get rc_support | grep -qF "am_addons"; then
return 0
else
return 1
fi
}
### Code for these functions inspired by https://github.com/Adamm00 - credit to @Adamm ###
Check_Lock(){
if [ -f "/tmp/$SCRIPT_NAME.lock" ]; then
ageoflock=$(($(date +%s) - $(date +%s -r /tmp/$SCRIPT_NAME.lock)))
if [ "$ageoflock" -gt 600 ]; then
Print_Output true "Stale lock file found (>600 seconds old) - purging lock" "$ERR"
kill "$(sed -n '1p' /tmp/$SCRIPT_NAME.lock)" >/dev/null 2>&1
Clear_Lock
echo "$$" > "/tmp/$SCRIPT_NAME.lock"
return 0
else
Print_Output true "Lock file found (age: $ageoflock seconds) - stopping to prevent duplicate runs" "$ERR"
if [ $# -eq 0 ] || [ -z "$1" ]; then
exit 1
else
return 1
fi
fi
else
echo "$$" > "/tmp/$SCRIPT_NAME.lock"
return 0
fi
}
Clear_Lock(){
rm -f "/tmp/$SCRIPT_NAME.lock" 2>/dev/null
return 0
}
############################################################################
Set_Version_Custom_Settings(){
SETTINGSFILE="/jffs/addons/custom_settings.txt"
case "$1" in
local)
if [ -f "$SETTINGSFILE" ]; then
if [ "$(grep -c "yazfi_version_local" $SETTINGSFILE)" -gt 0 ]; then
if [ "$SCRIPT_VERSION" != "$(grep "yazfi_version_local" /jffs/addons/custom_settings.txt | cut -f2 -d' ')" ]; then
sed -i "s/yazfi_version_local.*/yazfi_version_local $2/" "$SETTINGSFILE"
fi
else
echo "yazfi_version_local $2" >> "$SETTINGSFILE"
fi
else
echo "yazfi_version_local $2" >> "$SETTINGSFILE"
fi
;;
server)
if [ -f "$SETTINGSFILE" ]; then
if [ "$(grep -c "yazfi_version_server" $SETTINGSFILE)" -gt 0 ]; then
if [ "$2" != "$(grep "yazfi_version_server" /jffs/addons/custom_settings.txt | cut -f2 -d' ')" ]; then
sed -i "s/yazfi_version_server.*/yazfi_version_server $2/" "$SETTINGSFILE"
fi
else
echo "yazfi_version_server $2" >> "$SETTINGSFILE"
fi
else
echo "yazfi_version_server $2" >> "$SETTINGSFILE"
fi
;;
esac
}
Update_Check(){
echo 'var updatestatus = "InProgress";' > "$SCRIPT_WEB_DIR/detect_update.js"
doupdate="false"
localver=$(grep "SCRIPT_VERSION=" "/jffs/scripts/$SCRIPT_NAME" | grep -m1 -oE 'v[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})')
/usr/sbin/curl -fsL --retry 3 "$SCRIPT_REPO/404/$SCRIPT_NAME.sh" | grep -qF "jackyaz" || { Print_Output true "404 error detected - stopping update" "$ERR"; return 1; }
serverver=$(/usr/sbin/curl -fsL --retry 3 "$SCRIPT_REPO/version/$SCRIPT_NAME.sh" | grep "SCRIPT_VERSION=" | grep -m1 -oE 'v[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})')
if [ "$localver" != "$serverver" ]; then
doupdate="version"
Set_Version_Custom_Settings server "$serverver"
echo 'var updatestatus = "'"$serverver"'";' > "$SCRIPT_WEB_DIR/detect_update.js"
else
localmd5="$(md5sum "/jffs/scripts/$SCRIPT_NAME" | awk '{print $1}')"
remotemd5="$(curl -fsL --retry 3 "$SCRIPT_REPO/md5/$SCRIPT_NAME.sh" | md5sum | awk '{print $1}')"
if [ "$localmd5" != "$remotemd5" ]; then
doupdate="md5"
Set_Version_Custom_Settings server "$serverver-hotfix"
echo 'var updatestatus = "'"$serverver-hotfix"'";' > "$SCRIPT_WEB_DIR/detect_update.js"
fi
fi
if [ "$doupdate" = "false" ]; then
echo 'var updatestatus = "None";' > "$SCRIPT_WEB_DIR/detect_update.js"
fi
echo "$doupdate,$localver,$serverver"
}
##----------------------------------------##
## Modified by Martinski W. [2022-Dec-26] ##
##----------------------------------------##
Update_Version(){
if [ $# -eq 0 ] || [ -z "$1" ]; then
updatecheckresult="$(Update_Check)"
isupdate="$(echo "$updatecheckresult" | cut -f1 -d',')"
localver="$(echo "$updatecheckresult" | cut -f2 -d',')"
serverver="$(echo "$updatecheckresult" | cut -f3 -d',')"
if [ "$isupdate" = "version" ]; then
Print_Output true "New version of $SCRIPT_NAME available - $serverver" "$PASS"
elif [ "$isupdate" = "md5" ]; then
Print_Output true "MD5 hash of $SCRIPT_NAME does not match - hotfix available - $serverver" "$PASS"
fi
if [ "$isupdate" != "false" ]; then
printf "\\n${BOLD}Do you want to continue with the update? (y/n)${CLEARFORMAT} "
read -r confirm
case "$confirm" in
y|Y)
if Firmware_Version_WebUI ; then
Update_File shared-jy.tar.gz
Update_File YazFi_www.asp
else
Print_Output true "WebUI is only supported on firmware versions with addon support" "$WARN"
fi
Update_File README.md
Update_File LICENSE
##-------------------------------------##
## Added by Martinski W. [2022-Dec-26] ##
##-------------------------------------##
Update_File "$SCRIPT_CONF"
Download_File "$SCRIPT_REPO/update/$SCRIPT_NAME.sh" "/jffs/scripts/$SCRIPT_NAME" && Print_Output true "$SCRIPT_NAME successfully updated - restarting firewall to apply update"
chmod 0755 "/jffs/scripts/$SCRIPT_NAME"
Set_Version_Custom_Settings local "$serverver"
Set_Version_Custom_Settings server "$serverver"
Clear_Lock
service restart_firewall >/dev/null 2>&1
PressEnter
exec "$0"
exit 0
;;
*)
printf "\\n"
Clear_Lock
return 1
;;
esac
else
Print_Output true "No updates available - latest is $localver" "$WARN"
Clear_Lock
fi
fi
if [ "$1" = "force" ]; then
serverver=$(/usr/sbin/curl -fsL --retry 3 "$SCRIPT_REPO/version/$SCRIPT_NAME.sh" | grep "SCRIPT_VERSION=" | grep -m1 -oE 'v[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})')
Print_Output true "Downloading latest version ($serverver) of $SCRIPT_NAME" "$PASS"
if Firmware_Version_WebUI ; then
Update_File shared-jy.tar.gz
Update_File YazFi_www.asp
else
Print_Output true "WebUI is only supported on firmware versions with addon support" "$WARN"
fi
Update_File README.md
Update_File LICENSE
##-------------------------------------##
## Added by Martinski W. [2022-Dec-26] ##
##-------------------------------------##
Update_File "$SCRIPT_CONF"
Download_File "$SCRIPT_REPO/update/$SCRIPT_NAME.sh" "/jffs/scripts/$SCRIPT_NAME" && Print_Output true "$SCRIPT_NAME successfully updated - restarting firewall to apply update"
chmod 0755 "/jffs/scripts/$SCRIPT_NAME"
Set_Version_Custom_Settings local "$serverver"
Set_Version_Custom_Settings server "$serverver"
Clear_Lock
service restart_firewall >/dev/null 2>&1
if [ -z "$2" ]; then
PressEnter
exec "$0"
elif [ "$2" = "unattended" ]; then
exec "$0" postupdate
fi
exit 0
fi
}
##----------------------------------------##
## Modified by Martinski W. [2022-Dec-26] ##
##----------------------------------------##
Update_File(){
if [ "$1" = "YazFi_www.asp" ]; then
tmpfile="/tmp/$1"
Download_File "$SCRIPT_REPO/files/$1" "$tmpfile"
if ! diff -q "$tmpfile" "$SCRIPT_DIR/$1" >/dev/null 2>&1; then
if [ -f "$SCRIPT_DIR/$1" ]; then
Get_WebUI_Page "$SCRIPT_DIR/$1"
sed -i "\\~$MyPage~d" /tmp/menuTree.js
rm -f "$SCRIPT_WEBPAGE_DIR/$MyPage" 2>/dev/null
fi
Download_File "$SCRIPT_REPO/files/$1" "$SCRIPT_DIR/$1"
Print_Output true "New version of $1 downloaded" "$PASS"
Mount_WebUI
fi
rm -f "$tmpfile"
elif [ "$1" = "shared-jy.tar.gz" ]; then
if [ ! -f "$SHARED_DIR/$1.md5" ]; then
Download_File "$SHARED_REPO/$1" "$SHARED_DIR/$1"
Download_File "$SHARED_REPO/$1.md5" "$SHARED_DIR/$1.md5"
tar -xzf "$SHARED_DIR/$1" -C "$SHARED_DIR"
rm -f "$SHARED_DIR/$1"
Print_Output true "New version of $1 downloaded" "$PASS"
else
localmd5="$(cat "$SHARED_DIR/$1.md5")"
remotemd5="$(curl -fsL --retry 3 "$SHARED_REPO/$1.md5")"
if [ "$localmd5" != "$remotemd5" ]; then
Download_File "$SHARED_REPO/$1" "$SHARED_DIR/$1"
Download_File "$SHARED_REPO/$1.md5" "$SHARED_DIR/$1.md5"
tar -xzf "$SHARED_DIR/$1" -C "$SHARED_DIR"
rm -f "$SHARED_DIR/$1"
Print_Output true "New version of $1 downloaded" "$PASS"
fi
fi
elif [ "$1" = "README.md" ] || [ "$1" = "LICENSE" ]; then
tmpfile="/tmp/$1"
Download_File "$SCRIPT_REPO/files/$1" "$tmpfile"
if ! diff -q "$tmpfile" "$SCRIPT_DIR/$1" >/dev/null 2>&1; then
Download_File "$SCRIPT_REPO/files/$1" "$SCRIPT_DIR/$1"
fi
rm -f "$tmpfile"
elif [ "$1" = "$SCRIPT_CONF" ]
then
if ! grep -q -E "^wl31_|^wl32_|^wl33_" "$SCRIPT_CONF"
then
if Conf_ADD_Download "$SCRIPT_CONF"
then
cat "${SCRIPT_CONF}.ADD.txt" >> "$SCRIPT_CONF"
cp -fp "$SCRIPT_CONF" "${SCRIPT_CONF}.bak"
rm -f "${SCRIPT_CONF}.ADD.txt"
else
Print_Output true "Could not update configuration file: $SCRIPT_CONF" "$ERR"
fi
fi
else
return 1
fi
}
IP_Local(){
if echo "$1" | grep -qE '(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^192\.168\.)'; then
return 0
elif [ "$1" = "127.0.0.1" ]; then
return 0
else
return 1
fi
}
IP_Router(){
if [ "$1" = "$(nvram get lan_ipaddr)" ] || [ "$1" = "127.0.0.1" ]; then
return 0
elif [ "$1" = "$(eval echo '$'"$(Get_Iface_Var "$2")_IPADDR" | cut -f1-3 -d".").$(nvram get lan_ipaddr | cut -f4 -d".")" ]; then
return 0
else
return 1
fi
}
Validate_Enabled_IFACE(){
IFACE_TEST="$(nvram get "${1}_bss_enabled")"
if ! Validate_Number "" "$IFACE_TEST" silent; then IFACE_TEST=0; fi
if [ "$IFACE_TEST" -eq 0 ]; then
if [ -z "$2" ]; then
Print_Output false "$1 - Interface not enabled/configured in Web GUI (Guest Network menu)" "$ERR"
fi
return 1
else
return 0
fi
}
Validate_Exists_IFACE(){
validiface=""
for IFACE_EXIST in $IFACELIST; do
if [ "$1" = "$IFACE_EXIST" ]; then
validiface="true"
fi
done
if [ "$validiface" = "true" ]; then
return 0
else
if [ -z "$2" ]; then
Print_Output false "$1 - Interface not supported on this router" "$ERR"
fi
return 1
fi
}
Validate_IP(){
if expr "$2" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
for i in 1 2 3 4; do
if [ "$(echo "$2" | cut -d. -f$i)" -gt 255 ]; then
Print_Output false "$1 - Octet $i ($(echo "$2" | cut -d. -f$i)) - is invalid, must be less than 255" "$ERR"
return 1
fi
done
if [ "$3" != "DNS" ]; then
if IP_Local "$2"; then
return 0
else
Print_Output false "$1 - $2 - Non-local IP address block used" "$ERR"
return 1
fi
else
return 0
fi
else
Print_Output false "$1 - $2 - is not a valid IPv4 address, valid format is 1.2.3.4" "$ERR"
return 1
fi
}
Validate_Number(){
if [ "$2" -eq "$2" ] 2>/dev/null; then
return 0
else
formatted="$(echo "$1" | sed -e 's/|/ /g')"
if [ -z "$3" ]; then
Print_Output false "$formatted - $2 is not a number" "$ERR"
fi
return 1
fi
}
##----------------------------------------##
## Modified by Martinski W. [2022-Dec-05] ##
##----------------------------------------##
Validate_DHCP(){
if ! Validate_Number "$1" "$2"; then
return 1
elif ! Validate_Number "$1" "$3"; then
return 1
fi
if [ "$2" -gt "$3" ] || { [ "$2" -lt 2 ] || [ "$2" -gt 253 ]; } || { [ "$3" -lt 3 ] || [ "$3" -gt 254 ]; }; then
Print_Output false "$1 - $2 to $3 - both numbers must be between 2 and 254, $2 must be less than $3" "$ERR"
return 1
else
return 0
fi
}
##----------------------------------------------##
## Added/modified by Martinski W. [2023-Jan-30] ##
##----------------------------------------------##
# The DHCP Lease Time values can be given in:
# seconds, minutes, hours, days, or weeks.
# Single '0' or 'I' indicates "infinite" value.
#------------------------------------------------#
Validate_DHCP_LeaseTime()
{
timeUnits="X" timeFactor=1 timeNumber="$2"
if [ "$2" = "0" ] || [ "$2" = "$InfiniteLeaseTimeTag" ]
then return 0 ; fi
if echo "$2" | grep -q "^0.*"
then
Print_Output false "$1 - $2 is not a valid setting." "$ERR"
return 1
fi
if echo "$2" | grep -q "^[0-9]\{1,7\}$"
then
timeUnits="s"
timeNumber="$2"
elif echo "$2" | grep -q "^[0-9]\{1,6\}[smhdw]\{1\}$"
then
timeUnits="$(echo "$2" | awk '{print substr($0,length($0),1)}')"
timeNumber="$(echo "$2" | awk '{print substr($0,0,length($0)-1)}')"
fi
case "$timeUnits" in
s) timeFactor=1 ;;
m) timeFactor=60 ;;
h) timeFactor=3600 ;;
d) timeFactor=86400 ;;
w) timeFactor=604800 ;;
esac
if ! Validate_Number "$1" "$timeNumber"
then return 1 ; fi
timeValue="$((timeNumber * timeFactor))"
if [ "$timeValue" -lt "$MinDHCPLeaseTime" ] || [ "$timeValue" -gt "$MaxDHCPLeaseTime" ]; then
Print_Output false "$1 - $timeValue - must be between $MinDHCPLeaseTime and $MaxDHCPLeaseTime in seconds." "$ERR"
return 1
else
return 0
fi
}
Validate_VPNClientNo(){
if ! Validate_Number "$1" "$2"; then