-
Notifications
You must be signed in to change notification settings - Fork 1
/
MerlinAU.sh
8962 lines (7990 loc) · 321 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-Dec-01
###################################################################
set -u
## Set version for each Production Release ##
readonly SCRIPT_VERSION=1.3.8
readonly SCRIPT_NAME="MerlinAU"
## Set to "master" for Production Releases ##
SCRIPT_BRANCH="master"
##----------------------------------------##
## Modified by Martinski W. [2024-Jul-03] ##
##----------------------------------------##
# Script URL Info #
readonly SCRIPT_URL_BASE="https://raw.githubusercontent.com/ExtremeFiretop/MerlinAutoUpdate-Router"
SCRIPT_URL_REPO="${SCRIPT_URL_BASE}/$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
##------------------------------------------##
## Modified by ExtremeFiretop [2024-Oct-02] ##
##------------------------------------------##
# For minimum supported firmware version check #
MinFirmwareVerCheckFailed=false
MinSupportedFirmwareVers="3004.386.12.6"
# For router model check #
routerModelCheckFailed=false
offlineUpdateTrigger=false
##--------------------------------------------##
## 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"
readonly ScriptFileName="${0##*/}"
readonly ScriptFNameTag="${ScriptFileName%%.*}"
readonly ScriptDirNameD="${ScriptFNameTag}.d"
##----------------------------------------##
## Modified by Martinski W. [2024-Dec-01] ##
##----------------------------------------##
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"
# Give FIRST priority to built-in binaries over any other #
export PATH="/bin:/usr/bin:/sbin:/usr/sbin:$PATH"
##-------------------------------------##
## Added by Martinski W. [2024-Sep-15] ##
##-------------------------------------##
# For handling 3rd-party add-on cron jobs #
readonly USB_OPT_DIRPATH1="/opt"
readonly USB_OPT_DIRPATH2="/tmp/opt"
readonly USB_MNT_DIRPATH1="/mnt"
readonly USB_MNT_DIRPATH2="/tmp/mnt"
readonly cronJobsRegEx1="[[:blank:]]+${ADDONS_PATH}/.* "
readonly cronJobsRegEx2="[[:blank:]]+${SCRIPTS_PATH}/.* "
readonly cronJobsRegEx3="[[:blank:]]+${USB_OPT_DIRPATH1}/.* "
readonly cronJobsRegEx4="[[:blank:]]+${USB_OPT_DIRPATH2}/.* "
readonly cronJobsRegEx5="[[:blank:]]+${USB_MNT_DIRPATH1}/.* "
readonly cronJobsRegEx6="[[:blank:]]+${USB_MNT_DIRPATH2}/.* "
readonly addonCronJobList="/home/root/addonCronJobList_$$.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-Nov-27] ##
##----------------------------------------##
inMenuMode=true
isInteractive=false
FlashStarted=false
MerlinChangeLogURL=""
GnutonChangeLogURL=""
# Main LAN Network Info #
readonly myLAN_HostName="$(nvram get lan_hostname)"
readonly mainLAN_IFname="$(nvram get lan_ifname)"
readonly mainLAN_IPaddr="$(nvram get lan_ipaddr)"
readonly mainNET_IPaddr="$(ip route show | grep -E "[[:blank:]]+dev[[:blank:]]+${mainLAN_IFname}[[:blank:]]+proto[[:blank:]]+" | awk -F ' ' '{print $1}')"
# RegExp for IPv4 address #
readonly IPv4octet_RegEx="([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
readonly IPv4addrs_RegEx="(${IPv4octet_RegEx}\.){3}${IPv4octet_RegEx}"
readonly IPv4privt_RegEx="(^10\.|^172\.1[6-9]\.|^172\.2[0-9]\.|^172\.3[0-1]\.|^192\.168\.)"
##----------------------------------------##
## Modified by Martinski W. [2024-Oct-03] ##
##----------------------------------------##
readonly fwInstalledBaseVers="$(nvram get firmver | sed 's/\.//g')"
readonly fwInstalledBuildVers="$(nvram get buildno)"
readonly fwInstalledExtendNum="$(nvram get extendno)"
readonly fwInstalledInnerVers="$(nvram get innerver)"
readonly fwInstalledBranchVer="${fwInstalledBaseVers}.$(echo "$fwInstalledBuildVers" | awk -F'.' '{print $1}')"
##-------------------------------------##
## Added by Martinski W. [2024-Oct-03] ##
##-------------------------------------##
readonly MinSupportedFW_3004_386_Ver="3004.386.12.6"
readonly MinSupportedFW_3004_388_Ver="3004.388.6.2"
readonly MinSupportedFW_3006_102_Ver="3004.388.8.0"
case "$fwInstalledBranchVer" in
"3004.386") MinSupportedFirmwareVers="$MinSupportedFW_3004_386_Ver" ;;
"3004.388") MinSupportedFirmwareVers="$MinSupportedFW_3004_388_Ver" ;;
"3006.102") MinSupportedFirmwareVers="$MinSupportedFW_3006_102_Ver" ;;
esac
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..."
theMenuReturnPromptMsg="$mainMenuReturnPromptStr"
readonly SEPstr="----------------------------------------------------------"
##-------------------------------------##
## Added by Martinski W. [2024-Nov-24] ##
##-------------------------------------##
# menu setup variables #
readonly theExitStr="${GRNct}e${NOct}=Exit to Main Menu"
readonly theMUExitStr="${GRNct}e${NOct}=Exit"
readonly theADExitStr="${GRNct}e${NOct}=Exit to Advanced Options Menu"
readonly theLGExitStr="${GRNct}e${NOct}=Exit to Log Options Menu"
readonly menuCancelAndExitStr="${GRNct}e${NOct}=Exit Menu"
readonly menuSavedThenExitStr="${GRNct}s${NOct}=Save&Exit"
readonly menuReturnToBeginStr="${GRNct}b${NOct}=Back to Top"
##-------------------------------------##
## Added by Martinski W. [2024-Aug-15] ##
##-------------------------------------##
routerLoginFailureMsg="Please try the following:
1. Confirm that you are *not* already logged into the router webGUI using a web browser.
2. Check that the \"Enable Access Restrictions\" option from the webGUI is *not* set up
to restrict access to the router webGUI from the router's IP address [${GRNct}${mainLAN_IPaddr}${NOct}].
3. Confirm your password via the \"Set Router Login Credentials\" option from the Main 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_" "$@" ; }
_UserTraceLog_()
{
local logTime="$(date +"$LOGdateFormat")"
if [ $# -eq 0 ] || [ -z "$1" ]
then
echo >> "$userTraceFile"
elif [ $# -eq 1 ]
then
echo "$logTime" "$1" >> "$userTraceFile"
elif [ "$1" = "_NOTIME_" ]
then
echo "$2" >> "$userTraceFile"
else
echo "$logTime" "${1}: $2" >> "$userTraceFile"
fi
}
_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
}
##----------------------------------------##
## Modified by Martinski W. [2024-Nov-24] ##
##----------------------------------------##
readonly LockFilePath="/tmp/var/${ScriptFNameTag}.LOCK"
LockFileMaxAgeSecs=600 #10-minutes#
LockWaitTimeoutSecs=120
_ReleaseLock_() { rm -f "$LockFilePath" ; }
if [ $# -eq 0 ] || [ -z "$1" ] || \
[ "$1" = "resetLockFile" ]
then #Interactive Mode#
LockFileMaxAgeSecs=1200
LockWaitTimeoutSecs=2
elif [ "$1" = "addCronJob" ]
then #Special Case#
LockFileMaxAgeSecs=1200
LockWaitTimeoutSecs=600
fi
##----------------------------------------##
## Modified by Martinski W. [2024-Nov-17] ##
##----------------------------------------##
_AcquireLock_()
{
if [ ! -f "$LockFilePath" ]
then
echo "$$" > "$LockFilePath"
return 0
fi
local lockFileSecs ageOfLockSecs oldPID retCode waitTimeoutSecs
retCode=1
waitTimeoutSecs=0
while true
do
if [ -f "$LockFilePath" ]
then
oldPID="$(cat "$LockFilePath")"
lockFileSecs="$(date +%s -r "$LockFilePath")"
else
echo "$$" > "$LockFilePath"
retCode=0
break
fi
ageOfLockSecs="$(($(date +%s) - lockFileSecs))"
if [ "$ageOfLockSecs" -gt "$LockFileMaxAgeSecs" ]
then
Say "Stale Lock Found (older than $LockFileMaxAgeSecs secs.) Resetting lock file..."
if [ -n "$oldPID" ] && kill -EXIT "$oldPID" 2>/dev/null && \
pidof "$ScriptFileName" | grep -qow "$oldPID"
then
kill -TERM "$oldPID" ; wait "$oldPID"
fi
echo "$$" > "$LockFilePath"
retCode=0
break
elif [ "$waitTimeoutSecs" -le "$LockWaitTimeoutSecs" ]
then
if [ "$((waitTimeoutSecs % 10))" -eq 0 ]
then
Say "Lock Found [Age: $ageOfLockSecs secs.] Waiting for script [PID=$oldPID] to exit [Timer: $waitTimeoutSecs secs.]"
fi
sleep 2
waitTimeoutSecs="$((waitTimeoutSecs + 2))"
else
Say "${REDct}**ERROR**${NOct}: The shell script ${ScriptFileName} [PID=$oldPID] is already running [Lock Age: $ageOfLockSecs secs.]"
retCode=1
break
fi
done
return "$retCode"
}
##-------------------------------------##
## Added by Martinski W. [2023-Dec-26] ##
##-------------------------------------##
_DoExit_()
{
local exitCode=0
[ $# -gt 0 ] && [ -n "$1" ] && exitCode="$1"
_ReleaseLock_ ; exit "$exitCode"
}
##------------------------------------------##
## 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}"
}
##---------------------------------------##
## Added by ExtremeFiretop [2024-Jul-03] ##
##---------------------------------------##
_ShowAbout_()
{
clear
logo
cat <<EOF
About
$SCRIPT_NAME is a tool for automating firmware updates on AsusWRT Merlin,
ensuring your router stays up-to-date with the latest features and security
patches. It simplifies the update process by automatically checking for,
downloading, and applying new firmware versions.
Developed by ExtremeFiretop and Martinski W.
License
$SCRIPT_NAME is free to use under the GNU General Public License
version 3 (GPL-3.0) https://opensource.org/licenses/GPL-3.0
Help & Support
https://www.snbforums.com/threads/merlinau-the-ultimate-firmware-auto-updater-addon.88577/
Source code
https://github.com/ExtremeFiretop/MerlinAutoUpdate-Router
EOF
printf "\n"
_DoExit_ 0
}
##------------------------------------------##
## Modified by ExtremeFiretop [2024-Nov-18] ##
##------------------------------------------##
_ShowHelp_()
{
clear
logo
cat <<EOF
Available commands:
${SCRIPT_NAME}.sh about explains functionality
${SCRIPT_NAME}.sh help display available commands
${SCRIPT_NAME}.sh checkupdates check for available MerlinAU script updates
${SCRIPT_NAME}.sh forceupdate updates to latest version (force update)
${SCRIPT_NAME}.sh run_now run update process on router
${SCRIPT_NAME}.sh processNodes run update check on nodes
${SCRIPT_NAME}.sh develop switch to development branch
${SCRIPT_NAME}.sh stable switch to stable branch
${SCRIPT_NAME}.sh uninstall uninstalls script
EOF
printf "\n"
_DoExit_ 0
}
##----------------------------------------##
## 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 mountPointPath retCode=0
local mountPointRegExp="^/dev/sd.* /tmp/mnt/.*"
mountPointPath="$(grep -m1 "$mountPointRegExp" /proc/mounts | awk -F ' ' '{print $2}')"
[ -z "$mountPointPath" ] && retCode=1
echo "$mountPointPath" ; 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_"
_UserTraceLog_ "START _Reset_LEDs_"
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_"
_UserTraceLog_ "EXIT _Reset_LEDs_"
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="${myLAN_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
}
##----------------------------------------##
## Modified by Martinski W. [2024-Aug-11] ##
##----------------------------------------##
_GetFirmwareVariantFromRouter_()
{
local hasGNUtonFW=false
##FOR TESTING/DEBUG ONLY##
if false # Change to true for forcing GNUton flag #
then hasGNUtonFW=true ; return 0 ; fi
##FOR TESTING/DEBUG ONLY##
# Check if installed F/W NVRAM vars contain "gnuton" #
if echo "$fwInstalledInnerVers" | grep -iq "gnuton" || \
echo "$fwInstalledExtendNum" | grep -iq "gnuton"
then hasGNUtonFW=true ; fi
echo "$hasGNUtonFW" ; return 0
}
##----------------------------------------##
## 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 Martinski W. [2024-Nov-27] ##
##----------------------------------------##
if "$inRouterSWmode"
then
readonly FW_Update_CRON_DefaultSchedule="0 0 * * *"
else
## Recommended 20 minutes AFTER for AiMesh Nodes ##
readonly FW_Update_CRON_DefaultSchedule="20 0 * * *"
fi
## Recommended 15 minutes BEFORE the F/W Update ##
readonly ScriptAU_CRON_DefaultSchedule="45 23 * * *"
## For Automatic Script Updates Cron Schedule ##
readonly SW_Update_CRON_DefaultSchedDays="* x *"
readonly CRON_MINS_RegEx="([0-9]|[1-5][0-9])"
readonly CRON_HOUR_RegEx="([0-9]|1[0-9]|2[0-3])"
readonly CRON_DAYofMONTH_rexp1="([1-9]|[1-2][0-9]|3[0-1])"
readonly CRON_DAYofMONTH_rexp2="${CRON_DAYofMONTH_rexp1}[-]${CRON_DAYofMONTH_rexp1}"
readonly CRON_DAYofMONTH_rexp3="${CRON_DAYofMONTH_rexp2}[/][2-9]"
readonly CRON_DAYofMONTH_rexp4="${CRON_DAYofMONTH_rexp1}([,]${CRON_DAYofMONTH_rexp1})+"
readonly CRON_DAYofMONTH_RegEx="($CRON_DAYofMONTH_rexp1|$CRON_DAYofMONTH_rexp2|$CRON_DAYofMONTH_rexp3|$CRON_DAYofMONTH_rexp4)"
readonly CRON_DAYofWEEK_Names="([S|s]un|[M|m]on|[T|t]ue|[W|w]ed|[T|t]hu|[F|f]ri|[S|s]at)"
readonly CRON_DAYofWEEK_rexp1="[0-6][-][0-6][/][2-3]"
readonly CRON_DAYofWEEK_rexp2="${CRON_DAYofWEEK_Names}|[0-6]"
readonly CRON_DAYofWEEK_rexp3="${CRON_DAYofWEEK_Names}[-]${CRON_DAYofWEEK_Names}|[0-6][-][0-6]"
readonly CRON_DAYofWEEK_rexp4="${CRON_DAYofWEEK_Names}([,]${CRON_DAYofWEEK_Names})+|[0-6]([,][0-6])+"
readonly CRON_DAYofWEEK_RegEx="($CRON_DAYofWEEK_rexp1|$CRON_DAYofWEEK_rexp2|$CRON_DAYofWEEK_rexp3|$CRON_DAYofWEEK_rexp4)"
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-Aug-06] ##
##------------------------------------------##
# To postpone a firmware update for a few days #
readonly FW_UpdateMinimumPostponementDays=0
readonly FW_UpdateDefaultPostponementDays=15
readonly FW_UpdateMaximumPostponementDays=199
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"
##readonly MODEL_ID="$PRODUCT_ID"
##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 Martinski W. [2024-Jul-03] ##
##----------------------------------------##
_ChangeToDev_()
{
SCRIPT_BRANCH="dev"
SCRIPT_URL_REPO="${SCRIPT_URL_BASE}/$SCRIPT_BRANCH"
_SCRIPTUPDATE_ force
_DoExit_ 0
}
##----------------------------------------##
## Modified by Martinski W. [2024-Jul-03] ##
##----------------------------------------##
_ChangeToStable_()
{
SCRIPT_BRANCH="master"
SCRIPT_URL_REPO="${SCRIPT_URL_BASE}/$SCRIPT_BRANCH"
_SCRIPTUPDATE_ force
_DoExit_ 0
}
##----------------------------------------##
## Modified by Martinski W. [2024-Nov-15] ##
##----------------------------------------##
#-------------------------------------------------------------#
# 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/)" && [ -d /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 Martinski W. [2024-Nov-27] ##
##----------------------------------------##
_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 "Allow_Updates_OverVPN DISABLED"
echo "FW_New_Update_Changelog_Approval=TBD"
echo "FW_Allow_Beta_Production_Up ENABLED"
echo "FW_Auto_Backupmon ENABLED"
echo "Allow_Script_Auto_Update DISABLED"
echo "Script_Update_Cron_Job_SchedDays=\"${SW_Update_CRON_DefaultSchedDays}\""
} > "$SETTINGSFILE"
chmod 0664 "$SETTINGSFILE"
return 1
fi
local retCode=0 preferredPath
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 "^Allow_Updates_OverVPN " "$SETTINGSFILE"
then
sed -i "11 i Allow_Updates_OverVPN DISABLED" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_Allow_Beta_Production_Up " "$SETTINGSFILE"
then
sed -i "12 i FW_Allow_Beta_Production_Up ENABLED" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_Auto_Backupmon " "$SETTINGSFILE"
then
sed -i "13 i FW_Auto_Backupmon ENABLED" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^Allow_Script_Auto_Update " "$SETTINGSFILE"
then
sed -i "14 i Allow_Script_Auto_Update DISABLED" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^Script_Update_Cron_Job_SchedDays=" "$SETTINGSFILE"
then
sed -i "15 i Script_Update_Cron_Job_SchedDays=\"${SW_Update_CRON_DefaultSchedDays}\"" "$SETTINGSFILE"
retCode=1
fi
if ! grep -q "^FW_New_Update_Changelog_Approval=" "$SETTINGSFILE"
then
sed -i "16 i FW_New_Update_Changelog_Approval=TBD" "$SETTINGSFILE"
retCode=1
fi
dos2unix "$SETTINGSFILE"
chmod 0664 "$SETTINGSFILE"
return "$retCode"
}
##----------------------------------------##
## Modified by Martinski W. [2024-Nov-27] ##
##----------------------------------------##
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" | \
"Allow_Updates_OverVPN" | \
"FW_Allow_Beta_Production_Up" | \
"FW_Auto_Backupmon" | \
"Allow_Script_Auto_Update" | \
"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" | \
"Script_Update_Cron_Job_SchedDays" | \
"FW_New_Update_ZIP_Directory_Path" | \
"FW_New_Update_LOG_Directory_Path" | \
"FW_New_Update_LOG_Preferred_Path" | \
"FW_New_Update_EMail_Notification" | \