-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMerlinAU.sh
6601 lines (5822 loc) · 238 KB
/
MerlinAU.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
###################################################################
# MerlinAU.sh (MerlinAutoUpdate)
#
# Original Creation Date: 2023-Oct-01 by @ExtremeFiretop.
# Official Co-Author: @Martinski W. - Date: 2023-Nov-01
# Last Modified: 2024-Jun-19
###################################################################
set -u
readonly SCRIPT_VERSION=1.3.0
readonly SCRIPT_NAME="MerlinAU"
##-------------------------------------##
## Added by Martinski W. [2023-Dec-01] ##
##-------------------------------------##
# Script URL Info #
readonly SCRIPT_BRANCH="master"
readonly SCRIPT_URL_BASE="https://raw.githubusercontent.com/ExtremeFiretop/MerlinAutoUpdate-Router/$SCRIPT_BRANCH"
# Firmware URL Info #
readonly FW_SFURL_BASE="https://sourceforge.net/projects/asuswrt-merlin/files"
readonly FW_SFURL_RELEASE_SUFFIX="Release"
readonly FW_GITURL_RELEASE="https://api.github.com/repos/gnuton/asuswrt-merlin.ng/releases/latest"
##----------------------------------------##
## Modified by Martinski W. [2024-May-31] ##
##----------------------------------------##
# Changelog Info #
readonly CL_URL_NG="${FW_SFURL_BASE}/Documentation/Changelog-NG.txt/download"
readonly CL_URL_386="${FW_SFURL_BASE}/Documentation/Changelog-386.txt/download"
readonly CL_URL_3006="${FW_SFURL_BASE}/Documentation/Changelog-3006.txt/download"
readonly high_risk_terms="factory default reset|features are disabled|break backward compatibility|must be manually|strongly recommended"
# For new script version updates from source repository #
DLRepoVersion=""
scriptUpdateNotify=0
# For supported version and model checks #
MinFirmwareCheckFailed=0
ModelCheckFailed=0
readonly ScriptFileName="${0##*/}"
readonly ScriptFNameTag="${ScriptFileName%%.*}"
readonly ScriptDirNameD="${ScriptFNameTag}.d"
##------------------------------------------##
## Modified by ExtremeFiretop [2024-Jan-21] ##
##------------------------------------------##
readonly ADDONS_PATH="/jffs/addons"
readonly SCRIPTS_PATH="/jffs/scripts"
readonly SETTINGS_DIR="${ADDONS_PATH}/$ScriptDirNameD"
readonly SETTINGSFILE="${SETTINGS_DIR}/custom_settings.txt"
readonly SCRIPTVERPATH="${SETTINGS_DIR}/version.txt"
##----------------------------------------##
## Modified by Martinski W. [2024-Jun-05] ##
##----------------------------------------##
ScriptsDirPath="$SCRIPTS_PATH"
ScriptFilePath="${SCRIPTS_PATH}/${SCRIPT_NAME}.sh"
if [ ! -f "$ScriptFilePath" ]
then
ScriptsDirPath="$(pwd)"
ScriptFilePath="$(pwd)/$ScriptFileName"
fi
##------------------------------------------##
## Modified by ExtremeFiretop [2024-Apr-02] ##
##------------------------------------------##
#-------------------------------------------------------#
# We'll use the built-in AMTM email configuration file
# to send email notifications *IF* enabled by the user.
#-------------------------------------------------------#
readonly FW_UpdateEMailFormatTypeDefault=HTML
readonly FW_UpdateEMailNotificationDefault=false
readonly amtmMailDirPath="/jffs/addons/amtm/mail"
readonly amtmMailConfFile="${amtmMailDirPath}/email.conf"
readonly amtmMailPswdFile="${amtmMailDirPath}/emailpw.enc"
readonly tempEMailContent="/tmp/var/tmp/tempEMailContent.$$.TXT"
readonly tempNodeEMailList="/tmp/var/tmp/tempNodeEMailList.$$.TXT"
readonly tempEMailBodyMsg="/tmp/var/tmp/tempEMailBodyMsg.$$.TXT"
readonly saveEMailInfoMsg="${SETTINGS_DIR}/savedEMailInfoMsg.SAVE.TXT"
readonly theEMailDateTimeFormat="%Y-%b-%d %a %I:%M:%S %p %Z"
if [ -z "$(which crontab)" ]
then cronListCmd="cru l"
else cronListCmd="crontab -l"
fi
##----------------------------------------##
## Modified by Martinski W. [2024-May-31] ##
##----------------------------------------##
inMenuMode=true
isInteractive=false
readonly mainLAN_IPaddr="$(nvram get lan_ipaddr)"
readonly fwInstalledBaseVers="$(nvram get firmver | sed 's/\.//g')"
readonly fwInstalledBuildVers="$(nvram get buildno)"
readonly fwInstalledExtendNum="$(nvram get extendno)"
if [ "$(nvram get sw_mode)" -eq 1 ]
then inRouterSWmode=true
else inRouterSWmode=false
fi
readonly mainMenuReturnPromptStr="Press <Enter> to return to the Main Menu..."
readonly advnMenuReturnPromptStr="Press <Enter> to return to the Advanced Options Menu..."
readonly logsMenuReturnPromptStr="Press <Enter> to return to the Log Options Menu..."
[ -t 0 ] && ! tty | grep -qwi "NOT" && isInteractive=true
##----------------------------------------##
## Modified by Martinski W. [2023-Dec-23] ##
##----------------------------------------##
userLOGFile=""
userTraceFile="${SETTINGS_DIR}/${ScriptFNameTag}_Trace.LOG"
userDebugFile="${SETTINGS_DIR}/${ScriptFNameTag}_Debug.LOG"
LOGdateFormat="%Y-%m-%d %H:%M:%S"
_LogMsgNoTime_() { _UserLogMsg_ "_NOTIME_" "$@" ; }
_UserLogMsg_()
{
if [ -z "$userLOGFile" ] || [ ! -f "$userLOGFile" ]
then return 1 ; fi
local logTime="$(date +"$LOGdateFormat")"
if [ $# -eq 0 ] || [ -z "$1" ]
then
echo >> "$userLOGFile"
elif [ $# -eq 1 ]
then
echo "$logTime" "$1" >> "$userLOGFile"
elif [ "$1" = "_NOTIME_" ]
then
echo "$2" >> "$userLOGFile"
else
echo "$logTime" "${1}: $2" >> "$userLOGFile"
fi
}
##----------------------------------------##
## Modified by Martinski W. [2023-Dec-21] ##
##----------------------------------------##
Say()
{
"$isInteractive" && printf "${1}\n"
# Clean out the "color escape sequences" from the log file #
local logMsg="$(echo "$1" | sed 's/\\\e\[0m//g ; s/\\\e\[[0-1];3[0-9]m//g')"
_UserLogMsg_ "$logMsg"
printf "$logMsg" | logger -t "[$(basename "$0")] $$"
}
##----------------------------------------------##
## Added/Modified by Martinski W. [2023-Nov-20] ##
##----------------------------------------------##
_WaitForEnterKey_()
{
! "$isInteractive" && return 0
local promptStr
if [ $# -gt 0 ] && [ -n "$1" ]
then promptStr="$1"
else promptStr="Press <Enter> to continue..."
fi
printf "\n$promptStr"
read -rs EnterKEY ; echo
}
##----------------------------------##
## Added Martinski W. [2023-Nov-28] ##
##----------------------------------##
_WaitForYESorNO_()
{
! "$isInteractive" && return 0
local promptStr
if [ $# -eq 0 ] || [ -z "$1" ]
then promptStr=" [yY|nN]? "
else promptStr="$1 [yY|nN]? "
fi
printf "$promptStr" ; read -r YESorNO
if echo "$YESorNO" | grep -qE "^([Yy](es)?|YES)$"
then echo "OK" ; return 0
else echo "NO" ; return 1
fi
}
##-------------------------------------##
## Added by Martinski W. [2023-Dec-26] ##
##-------------------------------------##
LockFilePath="/tmp/var/${ScriptFNameTag}.LOCK"
LockFileMaxSecs=600 #10-min "hold"#
_ReleaseLock_() { rm -f "$LockFilePath" ; }
_AcquireLock_()
{
if [ ! -f "$LockFilePath" ]
then
echo "$$" > "$LockFilePath"
return 0
fi
ageOfLockSecs="$(($(date +%s) - $(date +%s -r "$LockFilePath")))"
if [ "$ageOfLockSecs" -gt "$LockFileMaxSecs" ]
then
Say "Stale lock found (older than $LockFileMaxSecs secs.) Reset lock file."
oldPID="$(cat "$LockFilePath")"
if [ -n "$oldPID" ] && kill -EXIT "$oldPID" 2>/dev/null && \
pidof "$ScriptFileName" | grep -qow "$oldPID"
then
kill -TERM "$oldPID" ; wait "$oldPID"
fi
rm -f "$LockFilePath"
echo "$$" > "$LockFilePath"
return 0
else
Say "${REDct}**ERROR**${NOct}: The shell script '${ScriptFileName}' is already running [Lock file: $ageOfLockSecs secs.]"
return 1
fi
}
##-------------------------------------##
## Added by Martinski W. [2023-Dec-26] ##
##-------------------------------------##
_DoExit_()
{
local exitCode=0
[ $# -gt 0 ] && [ -n "$1" ] && exitCode="$1"
_ReleaseLock_ ; exit "$exitCode"
}
##----------------------------------------##
## Modified by Martinski W. [2024-May-31] ##
##----------------------------------------##
## To support new "3006" F/W Basecode ##
if [ "$fwInstalledBaseVers" -ge 3006 ]
then readonly nvramLEDsVar=AllLED
else readonly nvramLEDsVar=led_disable
fi
# Save initial LEDs state to put it back later #
readonly LEDsInitState="$(nvram get "$nvramLEDsVar")"
LEDsToggleState="$LEDsInitState"
Toggle_LEDs_PID=""
# To enable/disable the built-in "F/W Update Check" #
FW_UpdateCheckState="TBD"
FW_UpdateCheckScript="/usr/sbin/webs_update.sh"
##--------------------------------------##
## Added by Martinski W. [22023-Nov-24] ##
##--------------------------------------##
#---------------------------------------------------------#
# The USB-attached drives can have multiple partitions
# with different file systems (NTFS, ext3, ext4, etc.),
# which means that multiple mount points can be found.
# So for the purpose of choosing a default value here
# we will simply select the first mount point found.
# Users can later on change it by typing a different
# mount point path or directory using the Main Menu.
#---------------------------------------------------------#
_GetDefaultUSBMountPoint_()
{
local mounPointPath retCode=0
local mountPointRegExp="^/dev/sd.* /tmp/mnt/.*"
mounPointPath="$(grep -m1 "$mountPointRegExp" /proc/mounts | awk -F ' ' '{print $2}')"
[ -z "$mounPointPath" ] && retCode=1
echo "$mounPointPath" ; return "$retCode"
}
##----------------------------------------##
## Modified by Martinski W. [2024-May-31] ##
##----------------------------------------##
# Background function to create a blinking LED effect #
Toggle_LEDs()
{
if [ -z "$LEDsToggleState" ]
then
sleep 1
Toggle_LEDs_PID=""
return 1
fi
if [ $# -eq 0 ] || [ -z "$1" ] || \
! echo "$1" | grep -qE "^[2-5]$"
then blinkRateSecs=2
else blinkRateSecs="$1"
fi
while true
do
LEDsToggleState="$((! LEDsToggleState))"
nvram set ${nvramLEDsVar}="$LEDsToggleState"
/sbin/service restart_leds > /dev/null 2>&1
sleep "$blinkRateSecs"
done
return 0
}
##----------------------------------------##
## Modified by Martinski W. [2024-May-31] ##
##----------------------------------------##
_Reset_LEDs_()
{
local doTrace=false
[ $# -gt 0 ] && [ "$1" -eq 1 ] && doTrace=false
if "$doTrace"
then
Say "START _Reset_LEDs_"
echo "$(date +"$LOGdateFormat") START _Reset_LEDs_" >> "$userTraceFile"
fi
# Check if the process with that PID is still running #
if [ -n "$Toggle_LEDs_PID" ] && \
kill -EXIT "$Toggle_LEDs_PID" 2>/dev/null
then
kill -TERM $Toggle_LEDs_PID
wait $Toggle_LEDs_PID
# Set LEDs to their "initial state" #
nvram set ${nvramLEDsVar}="$LEDsInitState"
/sbin/service restart_leds >/dev/null 2>&1
sleep 2
fi
Toggle_LEDs_PID=""
if "$doTrace"
then
Say "EXIT _Reset_LEDs_"
echo "$(date +"$LOGdateFormat") EXIT _Reset_LEDs_" >> "$userTraceFile"
fi
}
##----------------------------------------##
## Modified by Martinski W. [2024-Apr-06] ##
##----------------------------------------##
_GetRouterURL_()
{
local urlProto urlDomain urlPort
if [ "$(nvram get http_enable)" = "1" ]
then urlProto="https"
else urlProto="http"
fi
urlDomain="$(nvram get lan_domain)"
if [ -z "$urlDomain" ]
then urlDomain="$mainLAN_IPaddr"
else urlDomain="$(nvram get lan_hostname).$urlDomain"
fi
urlPort="$(nvram get "${urlProto}_lanport")"
if [ "$urlPort" -eq 80 ] || [ "$urlPort" -eq 443 ]
then urlPort=""
else urlPort=":$urlPort"
fi
echo "${urlProto}://${urlDomain}${urlPort}"
}
##----------------------------------------------##
## Added/Modified by Martinski W. [2023-Nov-20] ##
##----------------------------------------------##
_GetRouterModelID_()
{
local retCode=1 routerModelID=""
local nvramModelKeys="odmpid wps_modelnum model build_name"
for nvramKey in $nvramModelKeys
do
routerModelID="$(nvram get "$nvramKey")"
[ -n "$routerModelID" ] && retCode=0 && break
done
echo "$routerModelID" ; return "$retCode"
}
##----------------------------------------------##
## Added/Modified by Martinski W. [2023-Nov-20] ##
##----------------------------------------------##
_GetRouterProductID_()
{
local retCode=1 routerProductID=""
local nvramProductKeys="productid build_name odmpid"
for nvramKey in $nvramProductKeys
do
routerProductID="$(nvram get "$nvramKey")"
[ -n "$routerProductID" ] && retCode=0 && break
done
echo "$routerProductID" ; return "$retCode"
}
##-------------------------------------##
## Added by Martinski W. [2023-Nov-28] ##
##-------------------------------------##
_ScriptVersionStrToNum_()
{
if [ $# -eq 0 ] || [ -z "$1" ] ; then echo 0 ; return 1 ; fi
local verNum verStr
verStr="$(echo "$1" | awk -F '_' '{print $1}')"
verNum="$(echo "$verStr" | awk -F '.' '{printf ("%d%03d%03d\n", $1,$2,$3);}')"
verNum="$(echo "$verNum" | sed 's/^0*//')"
echo "$verNum" ; return 0
}
##---------------------------------------##
## Added by ExtremeFiretop [2024-Jun-19] ##
##---------------------------------------##
_GetFirmwareVariantFromRouter_()
{
##DEFAULTS TO MERLIN##
local retCode=0 newVersionStr
buildInfoStr="$(nvram get buildinfo)"
innerverStr="$(nvram get innerver)"
##FOR TESTING/DEBUG ONLY##
if false # Change to true for forcing GNUton flag
then
isGNUtonFW=true
else
# Check if innerver contains "gnuton"
if echo "$innerverStr" | grep -iq "gnuton"
then
isGNUtonFW=true
# if the version string contain "merlin"
elif echo "$buildInfoStr" | grep -iq "merlin"
then
isGNUtonFW=false
else
isGNUtonFW=false
fi
fi
echo "$isGNUtonFW" ; return "$retCode"
}
##----------------------------------------##
## Modified by Martinski W. [2024-May-31] ##
##----------------------------------------##
_FWVersionStrToNum_()
{
if [ $# -lt 2 ] || [ -z "$1" ] || [ -z "$2" ]
then echo ; return 1 ; fi
USE_BETA_WEIGHT="$(Get_Custom_Setting FW_Allow_Beta_Production_Up)"
local verNum verStr="$1" nonProductionVersionWeight=0
local fwBasecodeVers="" numOfFields
#--------------------------------------------------------------
# Handle any 'alpha/beta' in the version string to be sure
# that we always get good numerical values for comparison.
#--------------------------------------------------------------
if echo "$verStr" | grep -qiE '(alpha|beta)'
then
# Adjust weight value if "Beta-to-Production" update is enabled #
[ "$USE_BETA_WEIGHT" = "ENABLED" ] && nonProductionVersionWeight=-100
# Replace '.alpha|.beta' and anything following it with ".0" #
verStr="$(echo "$verStr" | sed 's/[.][Aa]lpha.*/.0/ ; s/[.][Bb]eta.*/.0/')"
# Remove 'alpha|beta' and anything following it #
verStr="$(echo "$verStr" | sed 's/[_-]\?[Aa]lpha.*// ; s/[_-]\?[Bb]eta.*//')"
fi
numOfFields="$(echo "$verStr" | awk -F '.' '{print NF}')"
if [ "$numOfFields" -lt "$2" ]
then fwBasecodeVers="$fwInstalledBaseVers" ; fi
#-----------------------------------------------------------
# Temporarily remove Basecode version to avoid issues with
# integers greater than the maximum 32-bit signed integer
# when doing arithmetic computations with shell cmds.
#-----------------------------------------------------------
if [ "$numOfFields" -gt 3 ]
then
fwBasecodeVers="$(echo "$verStr" | cut -d'.' -f1)"
verStr="$(echo "$verStr" | cut -d'.' -f2-)"
fi
verNum="$(echo "$verStr" | awk -F '.' '{printf ("%d%02d%02d\n", $1,$2,$3);}')"
# Subtract non-production weight from the version number #
verNum="$((verNum + nonProductionVersionWeight))"
# Now prepend the F/W Basecode version #
[ -n "$fwBasecodeVers" ] && verNum="${fwBasecodeVers}$verNum"
echo "$verNum" ; return 0
}
##--------------------------------------------##
## Modified by ExtremeFiretop [2023-Nov-26] ##
##--------------------------------------------##
readonly NOct="\e[0m"
readonly REDct="\e[1;31m"
readonly GRNct="\e[1;32m"
readonly BLKct="\e[1;30m"
readonly YLWct="\e[1;33m"
readonly BLUEct="\e[1;34m"
readonly MAGENTAct="\e[1;35m"
readonly CYANct="\e[1;36m"
readonly WHITEct="\e[1;37m"
##------------------------------------------##
## Modified by ExtremeFiretop [2024-May-03] ##
##------------------------------------------##
if "$inRouterSWmode"
then
readonly FW_Update_CRON_DefaultSchedule="0 0 * * *"
else
readonly FW_Update_CRON_DefaultSchedule="15 0 * * *"
fi
readonly CRON_MINS_RegEx="([*0-9]|[1-5][0-9])([\/,-]([0-9]|[1-5][0-9]))*"
readonly CRON_HOUR_RegEx="([*0-9]|1[0-9]|2[0-3])([\/,-]([0-9]|1[0-9]|2[0-3]))*"
readonly CRON_DAYofMONTH_RegEx="([*1-9]|[1-2][0-9]|3[0-1])([\/,-]([1-9]|[1-2][0-9]|3[0-1]))*"
readonly CRON_DAYofWEEK_NAMES="(Sun|Mon|Tue|Wed|Thu|Fri|Sat)"
readonly CRON_DAYofWEEK_RegEx="$CRON_DAYofWEEK_NAMES([\/,-]$CRON_DAYofWEEK_NAMES)*|[*0-6]([\/,-][0-6])*"
readonly CRON_MONTH_NAMES="(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)"
readonly CRON_MONTH_RegEx="$CRON_MONTH_NAMES([\/,-]$CRON_MONTH_NAMES)*|([*1-9]|1[0-2])([\/,-]([1-9]|1[0-2]))*"
readonly CRON_UNKNOWN_DATE="**ERROR**: UNKNOWN Date Found"
##------------------------------------------##
## Modified by Martinski W. [2024-Jan-22] ##
##------------------------------------------##
# To postpone a firmware update for a few days #
readonly FW_UpdateMinimumPostponementDays=0
readonly FW_UpdateDefaultPostponementDays=15
readonly FW_UpdateMaximumPostponementDays=60
readonly FW_UpdateNotificationDateFormat="%Y-%m-%d_%H:%M:00"
readonly MODEL_ID="$(_GetRouterModelID_)"
readonly PRODUCT_ID="$(_GetRouterProductID_)"
##FOR TESTING/DEBUG ONLY##
#readonly PRODUCT_ID="TUF-AX3000_V2"
##FOR TESTING/DEBUG ONLY##
readonly FW_FileName="${PRODUCT_ID}_firmware"
readonly FW_SFURL_RELEASE="${FW_SFURL_BASE}/${PRODUCT_ID}/${FW_SFURL_RELEASE_SUFFIX}/"
readonly isGNUtonFW=$(_GetFirmwareVariantFromRouter_)
##------------------------------------------##
## Modified by ExtremeFiretop [2024-May-21] ##
##------------------------------------------##
logo() {
echo -e "${YLWct}"
echo -e " __ __ _ _ _ _ "
echo -e " | \/ | | (_) /\ | | | |"
echo -e " | \ / | ___ _ __| |_ _ __ / \ | | | |"
echo -e " | |\/| |/ _ | '__| | | '_ \ / /\ \| | | |"
echo -e " | | | | __| | | | | | | |/ ____ | |__| |"
echo -e " |_| |_|\___|_| |_|_|_| |_/_/ \_\____/ ${GRNct}v${SCRIPT_VERSION}"
echo -e "${NOct}"
}
##----------------------------------------##
## Modified by Martinski W. [2024-Jun-05] ##
##----------------------------------------##
_CheckForNewScriptUpdates_()
{
local DLRepoVersionNum ScriptVersionNum
echo ""
[ -s "$SCRIPTVERPATH" ] && DLRepoVersion="$(cat "$SCRIPTVERPATH")"
rm -f "$SCRIPTVERPATH"
# Download the latest version file from the source repository
curl -LSs --retry 4 --retry-delay 5 "${SCRIPT_URL_BASE}/version.txt" -o "$SCRIPTVERPATH"
if [ $? -ne 0 ] || [ ! -s "$SCRIPTVERPATH" ]
then scriptUpdateNotify=0 ; return 1 ; fi
# Read in its contents for the current version file
DLRepoVersion="$(cat "$SCRIPTVERPATH")"
if [ -z "$DLRepoVersion" ]; then
echo "Variable for downloaded version is empty."
scriptUpdateNotify=0
return 1
fi
DLRepoVersionNum="$(_ScriptVersionStrToNum_ "$DLRepoVersion")"
ScriptVersionNum="$(_ScriptVersionStrToNum_ "$SCRIPT_VERSION")"
# Version comparison
if [ "$DLRepoVersionNum" -gt "$ScriptVersionNum" ]
then
scriptUpdateNotify="New script update available.
${REDct}v$SCRIPT_VERSION${NOct} --> ${GRNct}v$DLRepoVersion${NOct}"
Say "$(date +'%b %d %Y %X') $(nvram get lan_hostname) ${ScriptFNameTag}_[$$] - INFO: A new script update (v$DLRepoVersion) is available to download."
else
scriptUpdateNotify=0
fi
}
##----------------------------------------##
## Modified by Martinski W. [2024-Jun-05] ##
##----------------------------------------##
#a function that provides a UI to check for script updates and allows you to install the latest version...
_SCRIPTUPDATE_()
{
local ScriptFileDL="${ScriptFilePath}.DL"
_CheckForNewScriptUpdates_
clear
logo
echo
echo -e "${YLWct}Update Utility${NOct}"
echo
echo -e "${CYANct}Current Version: ${YLWct}${SCRIPT_VERSION}${NOct}"
echo -e "${CYANct}Updated Version: ${YLWct}${DLRepoVersion}${NOct}"
echo
if [ "$SCRIPT_VERSION" = "$DLRepoVersion" ]
then
echo -e "${CYANct}You are on the latest version! Would you like to download anyways?${NOct}"
echo -e "${CYANct}This will overwrite your currently installed version.${NOct}"
if _WaitForYESorNO_
then
echo ; echo
echo -e "${CYANct}Downloading $SCRIPT_NAME ${CYANct}v$DLRepoVersion${NOct}"
curl -LSs --retry 4 --retry-delay 5 "${SCRIPT_URL_BASE}/version.txt" -o "$SCRIPTVERPATH"
curl -LSs --retry 4 --retry-delay 5 "${SCRIPT_URL_BASE}/${SCRIPT_NAME}.sh" -o "$ScriptFileDL"
if [ $? -eq 0 ] && [ -s "$ScriptFileDL" ]
then
mv -f "$ScriptFileDL" "$ScriptFilePath"
chmod 755 "$ScriptFilePath"
echo
echo -e "${CYANct}Download successful!${NOct}"
echo -e "$(date) - $SCRIPT_NAME - Successfully downloaded $SCRIPT_NAME v$DLRepoVersion"
echo
else
rm -f "$ScriptFileDL"
echo
echo -e "${REDct}Download failed.${NOct}"
fi
_WaitForEnterKey_
return
else
echo ; echo
echo -e "${GRNct}Exiting Update Utility...${NOct}"
sleep 1
return
fi
elif [ "$scriptUpdateNotify" != "0" ]
then
echo -e "${CYANct}Bingo! New version available! Would you like to update now?${NOct}"
if _WaitForYESorNO_
then
echo ; echo
echo -e "${CYANct}Downloading $SCRIPT_NAME ${CYANct}v$DLRepoVersion${NOct}"
curl -LSs --retry 4 --retry-delay 5 "${SCRIPT_URL_BASE}/version.txt" -o "$SCRIPTVERPATH"
curl -LSs --retry 4 --retry-delay 5 "${SCRIPT_URL_BASE}/${SCRIPT_NAME}.sh" -o "$ScriptFileDL"
if [ $? -eq 0 ] && [ -s "$ScriptFileDL" ]
then
mv -f "$ScriptFileDL" "$ScriptFilePath"
chmod 755 "$ScriptFilePath"
echo
echo -e "$(date) - $SCRIPT_NAME - Successfully downloaded $SCRIPT_NAME v$DLRepoVersion"
echo -e "${CYANct}Update successful! Restarting script...${NOct}"
_ReleaseLock_
exec "$ScriptFilePath" # Re-execute the updated script #
exit 0 # This line will not be executed due to above exec #
else
rm -f "$ScriptFileDL"
echo
echo -e "${REDct}Download failed.${NOct}"
_WaitForEnterKey_
return
fi
else
echo ; echo
echo -e "${GRNct}Exiting Update Utility...${NOct}"
sleep 1
return
fi
fi
}
##----------------------------------------##
## Modified by Martinski W. [2024-Mar-14] ##
##----------------------------------------##
#-------------------------------------------------------------#
# Since a list of current mount points can have a different
# order after each reboot, or when USB drives are unmounted
# (unplugged) & then mounted (plugged in) manually by users,
# to validate a given mount point path selection we have to
# go through the current list & check for the specific path.
# We also make a special case for Entware "/opt/" paths.
#-------------------------------------------------------------#
_ValidateUSBMountPoint_()
{
if [ $# -eq 0 ] || [ -z "$1" ] ; then return 1 ; fi
local mounPointPaths expectedPath mountPointList
local symblPath realPath1 realPath2 foundPathOK
local mountPointRegExp="^/dev/sd.* /tmp/mnt/.*"
mounPointPaths="$(grep "$mountPointRegExp" /proc/mounts | awk -F ' ' '{print $2}')"
[ -z "$mounPointPaths" ] && return 1
expectedPath="$1"
if echo "$1" | grep -qE "^(/opt/|/tmp/opt/)"
then
realPath1="$(readlink -f /tmp/opt)"
realPath2="$(ls -l /tmp/opt | awk -F ' ' '{print $11}')"
symblPath="$(ls -l /tmp/opt | awk -F ' ' '{print $9}')"
[ -L "$symblPath" ] && [ -n "$realPath1" ] && \
[ -n "$realPath2" ] && [ "$realPath1" = "$realPath2" ] && \
expectedPath="$(/usr/bin/dirname "$realPath1")"
fi
mountPointList=""
foundPathOK=false
for thePATH in $mounPointPaths
do
if echo "${expectedPath}/" | grep -qE "^${thePATH}/"
then foundPathOK=true ; break ; fi
mountPointList="$mountPointList $thePATH"
done
"$foundPathOK" && return 0
## Report found Mount Points on failure ##
if [ $# -gt 1 ] && [ "$2" -eq 1 ] && [ -n "$mountPointList" ]
then Say "Mount points found:\n$mountPointList" ; fi
return 1
}
##----------------------------------------##
## Modified by Martinski W. [2023-Nov-24] ##
##----------------------------------------##
if USBMountPoint="$(_GetDefaultUSBMountPoint_)"
then
USBConnected="${GRNct}True${NOct}"
readonly FW_Update_ZIP_DefaultSetupDIR="$USBMountPoint"
readonly FW_Update_LOG_BASE_DefaultDIR="$USBMountPoint"
else
USBConnected="${REDct}False${NOct}"
readonly FW_Update_ZIP_DefaultSetupDIR="/home/root"
readonly FW_Update_LOG_BASE_DefaultDIR="$ADDONS_PATH"
fi
##------------------------------------------##
## Modified by ExtremeFiretop [2024-May-25] ##
##------------------------------------------##
_Init_Custom_Settings_Config_()
{
[ ! -d "$SETTINGS_DIR" ] && mkdir -m 755 -p "$SETTINGS_DIR"
if [ ! -f "$SETTINGSFILE" ]
then
{
echo "FW_New_Update_Notification_Date TBD"
echo "FW_New_Update_Notification_Vers TBD"
echo "FW_New_Update_Postponement_Days=$FW_UpdateDefaultPostponementDays"
echo "FW_New_Update_EMail_Notification=$FW_UpdateEMailNotificationDefault"
echo "FW_New_Update_EMail_FormatType=\"${FW_UpdateEMailFormatTypeDefault}\""
echo "FW_New_Update_Cron_Job_Schedule=\"${FW_Update_CRON_DefaultSchedule}\""
echo "FW_New_Update_ZIP_Directory_Path=\"${FW_Update_ZIP_DefaultSetupDIR}\""
echo "FW_New_Update_LOG_Directory_Path=\"${FW_Update_LOG_BASE_DefaultDIR}\""
echo "FW_New_Update_LOG_Preferred_Path=\"${FW_Update_LOG_BASE_DefaultDIR}\""
echo "FW_New_Update_EMail_CC_Name=TBD"
echo "FW_New_Update_EMail_CC_Address=TBD"
echo "CheckChangeLog ENABLED"
echo "FW_New_Update_Changelog_Approval=TBD"
echo "FW_Allow_Beta_Production_Up ENABLED"
echo "FW_Auto_Backupmon ENABLED"
} > "$SETTINGSFILE"
return 1
fi
local retCode=0 prefPath
if ! grep -q "^FW_New_Update_Notification_Date " "$SETTINGSFILE"
then
sed -i "1 i FW_New_Update_Notification_Date TBD" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_Notification_Vers " "$SETTINGSFILE"
then
sed -i "2 i FW_New_Update_Notification_Vers TBD" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_Postponement_Days=" "$SETTINGSFILE"
then
sed -i "3 i FW_New_Update_Postponement_Days=$FW_UpdateDefaultPostponementDays" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_EMail_Notification=" "$SETTINGSFILE"
then
sed -i "4 i FW_New_Update_EMail_Notification=$FW_UpdateEMailNotificationDefault" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_EMail_FormatType=" "$SETTINGSFILE"
then
sed -i "5 i FW_New_Update_EMail_FormatType=\"${FW_UpdateEMailFormatTypeDefault}\"" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_Cron_Job_Schedule=" "$SETTINGSFILE"
then
sed -i "6 i FW_New_Update_Cron_Job_Schedule=\"${FW_Update_CRON_DefaultSchedule}\"" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_ZIP_Directory_Path=" "$SETTINGSFILE"
then
sed -i "7 i FW_New_Update_ZIP_Directory_Path=\"${FW_Update_ZIP_DefaultSetupDIR}\"" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_LOG_Directory_Path=" "$SETTINGSFILE"
then
sed -i "8 i FW_New_Update_LOG_Directory_Path=\"${FW_Update_LOG_BASE_DefaultDIR}\"" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_LOG_Preferred_Path=" "$SETTINGSFILE"
then
preferredPath="$(Get_Custom_Setting FW_New_Update_LOG_Directory_Path)"
sed -i "9 i FW_New_Update_LOG_Preferred_Path=\"${preferredPath}\"" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^CheckChangeLog " "$SETTINGSFILE"
then
sed -i "10 i CheckChangeLog ENABLED" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_Allow_Beta_Production_Up " "$SETTINGSFILE"
then
sed -i "11 i FW_Allow_Beta_Production_Up ENABLED" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_Auto_Backupmon " "$SETTINGSFILE"
then
sed -i "12 i FW_Auto_Backupmon ENABLED" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_Changelog_Approval=" "$SETTINGSFILE"
then
sed -i "13 i FW_New_Update_Changelog_Approval=TBD" "$SETTINGSFILE"
retCode=1
fi
return "$retCode"
}
##------------------------------------------##
## Modified by ExtremeFiretop [2024-May-06] ##
##------------------------------------------##
Get_Custom_Setting()
{
if [ $# -eq 0 ] || [ -z "$1" ]; then echo "**ERROR**"; return 1; fi
[ ! -d "$SETTINGS_DIR" ] && mkdir -m 755 -p "$SETTINGS_DIR"
local setting_value="" setting_type="$1" default_value="TBD"
[ $# -gt 1 ] && default_value="$2"
if [ -f "$SETTINGSFILE" ]; then
case "$setting_type" in
"ROGBuild" | "TUFBuild" | "credentials_base64" | \
"CheckChangeLog" | \
"FW_Allow_Beta_Production_Up" | \
"FW_Auto_Backupmon" | \
"FW_New_Update_Notification_Date" | \
"FW_New_Update_Notification_Vers")
setting_value="$(grep "^${setting_type} " "$SETTINGSFILE" | awk -F ' ' '{print $2}')"
;;
"FW_New_Update_Postponement_Days" | \
"FW_New_Update_Changelog_Approval" | \
"FW_New_Update_Expected_Run_Date" | \
"FW_New_Update_Cron_Job_Schedule" | \
"FW_New_Update_ZIP_Directory_Path" | \
"FW_New_Update_LOG_Directory_Path" | \
"FW_New_Update_LOG_Preferred_Path" | \
"FW_New_Update_EMail_Notification" | \
"FW_New_Update_EMail_FormatType" | \
"FW_New_Update_EMail_CC_Name" | \
"FW_New_Update_EMail_CC_Address")
grep -q "^${setting_type}=" "$SETTINGSFILE" && \
setting_value="$(grep "^${setting_type}=" "$SETTINGSFILE" | awk -F '=' '{print $2}' | sed "s/['\"]//g")"
;;
*)
setting_value="**ERROR**"
;;
esac
[ -z "$setting_value" ] && echo "$default_value" || echo "$setting_value"
else
echo "$default_value"
fi
}
##----------------------------------------##
## Modified by Martinski W. [2024-Apr-30] ##
##----------------------------------------##
_GetAllNodeSettings_()
{
if [ $# -lt 2 ] || [ -z "$1" ] || [ -z "$2" ]
then echo "**ERROR**" ; return 1; fi
## Node Setting KEY="Node_{MACaddress}_{keySuffix}" ##
local fullKeyName="Node_${1}_${2}"
local setting_value="TBD" matched_lines
# Ensure the settings directory exists #
[ ! -d "$SETTINGS_DIR" ] && mkdir -m 755 -p "$SETTINGS_DIR"
if [ -f "$SETTINGSFILE" ]
then
matched_lines="$(grep -E "^${fullKeyName}=.*" "$SETTINGSFILE")"
if [ -n "$matched_lines" ]
then
# Extract the value from the first matched line #
setting_value="$(echo "$matched_lines" | head -n 1 | awk -F '=' '{print $2}' | tr -d '"')"
fi
fi
echo "$setting_value"
}
##------------------------------------------##
## Modified by ExtremeFiretop [2024-May-06] ##
##------------------------------------------##
Update_Custom_Settings()
{
if [ $# -lt 2 ] || [ -z "$1" ] || [ -z "$2" ] ; then return 1 ; fi
local fixedVal oldVal=""
local setting_type="$1" setting_value="$2"
# Check if the directory exists, and if not, create it
[ ! -d "$SETTINGS_DIR" ] && mkdir -m 755 -p "$SETTINGS_DIR"
case "$setting_type" in
"ROGBuild" | "TUFBuild" | "credentials_base64" | \
"CheckChangeLog" | \
"FW_Allow_Beta_Production_Up" | \
"FW_Auto_Backupmon" | \
"FW_New_Update_Notification_Date" | \
"FW_New_Update_Notification_Vers")
if [ -f "$SETTINGSFILE" ]
then
if [ "$(grep -c "$setting_type" "$SETTINGSFILE")" -gt 0 ]
then
if [ "$setting_value" != "$(grep "^$setting_type" "$SETTINGSFILE" | cut -f2 -d' ')" ]; then
sed -i "s/$setting_type.*/$setting_type $setting_value/" "$SETTINGSFILE"
fi
else
echo "$setting_type $setting_value" >> "$SETTINGSFILE"
fi
else
echo "$setting_type $setting_value" > "$SETTINGSFILE"
fi
;;
"FW_New_Update_Postponement_Days" | \
"FW_New_Update_Changelog_Approval" | \
"FW_New_Update_Expected_Run_Date" | \
"FW_New_Update_Cron_Job_Schedule" | \
"FW_New_Update_ZIP_Directory_Path" | \
"FW_New_Update_LOG_Directory_Path" | \
"FW_New_Update_LOG_Preferred_Path" | \
"FW_New_Update_EMail_Notification" | \
"FW_New_Update_EMail_FormatType" | \
"FW_New_Update_EMail_CC_Name" | \
"FW_New_Update_EMail_CC_Address")
if [ -f "$SETTINGSFILE" ]
then
if grep -q "^${setting_type}=" "$SETTINGSFILE"
then
oldVal="$(grep "^${setting_type}=" "$SETTINGSFILE" | awk -F '=' '{print $2}' | sed "s/['\"]//g")"
if [ -z "$oldVal" ] || [ "$oldVal" != "$setting_value" ]
then
fixedVal="$(echo "$setting_value" | sed 's/[\/.,*-]/\\&/g')"
sed -i "s/${setting_type}=.*/${setting_type}=\"${fixedVal}\"/" "$SETTINGSFILE"
fi
else
echo "$setting_type=\"${setting_value}\"" >> "$SETTINGSFILE"
fi
else
echo "$setting_type=\"${setting_value}\"" > "$SETTINGSFILE"
fi
if [ "$setting_type" = "FW_New_Update_Postponement_Days" ]
then
FW_UpdatePostponementDays="$setting_value"
#
elif [ "$setting_type" = "FW_New_Update_EMail_Notification" ]
then
sendEMailNotificationsFlag="$setting_value"
#
elif [ "$setting_type" = "FW_New_Update_EMail_FormatType" ]
then
sendEMailFormaType="$setting_value"
[ "$sendEMailFormaType" = "HTML" ] && \
isEMailFormatHTML=true || isEMailFormatHTML=false
#
elif [ "$setting_type" = "FW_New_Update_EMail_CC_Name" ]
then
sendEMail_CC_Name="$setting_value"
#