forked from jackyaz/connmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connmon.sh
4008 lines (3716 loc) · 134 KB
/
connmon.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/connmon ##
## ##
##############################################################
############## Shellcheck directives #############
# shellcheck disable=SC1090
# shellcheck disable=SC2009
# shellcheck disable=SC2012
# shellcheck disable=SC2016
# shellcheck disable=SC2018
# shellcheck disable=SC2019
# shellcheck disable=SC2039
# shellcheck disable=SC2059
# shellcheck disable=SC2086
# shellcheck disable=SC2155
# shellcheck disable=SC2181
# shellcheck disable=SC3003
##############################################################
### Start of script variables ###
readonly SCRIPT_NAME="connmon"
readonly SCRIPT_VERSION="v3.0.2"
SCRIPT_BRANCH="master"
SCRIPT_REPO="https://jackyaz.io/$SCRIPT_NAME/$SCRIPT_BRANCH"
readonly SCRIPT_DIR="/jffs/addons/$SCRIPT_NAME.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"
readonly EMAIL_DIR="/jffs/addons/amtm/mail"
readonly EMAIL_CONF="$EMAIL_DIR/email.conf"
readonly EMAIL_REGEX="^(([A-Za-z0-9]+((\.|\-|\_|\+)?[A-Za-z0-9]?)*[A-Za-z0-9]+)|[A-Za-z0-9]+)@(([A-Za-z0-9]+)+((\.|\-|\_)?([A-Za-z0-9]+)+)*)+\.([A-Za-z]{2,})+$"
[ -z "$(nvram get odmpid)" ] && ROUTER_MODEL="$(nvram get productid)" || ROUTER_MODEL="$(nvram get odmpid)"
[ -f /opt/bin/sqlite3 ] && SQLITE3_PATH=/opt/bin/sqlite3 || SQLITE3_PATH=/usr/sbin/sqlite3
### 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 UNDERLINE="\\e[4m"
readonly CLEARFORMAT="\\e[0m"
### End of output format variables ###
# $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"
}
Firmware_Version_Check(){
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="$(($(/bin/date "+%s") - $(/bin/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) - ping test likely currently running" "$ERR"
if [ -z "$1" ]; then
exit 1
else
if [ "$1" = "webui" ]; then
echo 'var connmonstatus = "LOCKED";' > "$SCRIPT_WEB_DIR/detect_connmon.js"
exit 1
fi
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 "connmon_version_local" $SETTINGSFILE)" -gt 0 ]; then
if [ "$2" != "$(grep "connmon_version_local" /jffs/addons/custom_settings.txt | cut -f2 -d' ')" ]; then
sed -i "s/connmon_version_local.*/connmon_version_local $2/" "$SETTINGSFILE"
fi
else
echo "connmon_version_local $2" >> "$SETTINGSFILE"
fi
else
echo "connmon_version_local $2" >> "$SETTINGSFILE"
fi
;;
server)
if [ -f "$SETTINGSFILE" ]; then
if [ "$(grep -c "connmon_version_server" $SETTINGSFILE)" -gt 0 ]; then
if [ "$2" != "$(grep "connmon_version_server" /jffs/addons/custom_settings.txt | cut -f2 -d' ')" ]; then
sed -i "s/connmon_version_server.*/connmon_version_server $2/" "$SETTINGSFILE"
fi
else
echo "connmon_version_server $2" >> "$SETTINGSFILE"
fi
else
echo "connmon_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})')"
Set_Version_Custom_Settings local "$localver"
/usr/sbin/curl -fsL --retry 3 --connect-timeout 15 "$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 --connect-timeout 15 "$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"
changelog="$(/usr/sbin/curl -fsL --retry 3 --connect-timeout 15 "$SCRIPT_REPO/files/CHANGELOG.md" | sed -n "/$serverver"'/,/##/p' | head -n -1 | sed 's/## //')"
echo 'var changelog = "<div style=\"width:350px;\"><b>Changelog</b><br />'"$(echo "$changelog" | tr '\n' '|' | sed 's/|/<br \/>/g')"'</div>"' > "$SCRIPT_WEB_DIR/detect_changelog.js"
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"
}
Update_Version(){
if [ -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"
changelog="$(/usr/sbin/curl -fsL --retry 3 --connect-timeout 15 "$SCRIPT_REPO/files/CHANGELOG.md" | sed -n "/$serverver"'/,/##/p' | head -n -1 | sed 's/## //')"
printf "${BOLD}${UNDERLINE}Changelog\\n${CLEARFORMAT}%s\\n\\n" "$changelog"
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)
printf "\\n"
Update_File CHANGELOG.md
Update_File README.md
Update_File LICENSE
Update_File shared-jy.tar.gz
Update_File connmonstats_www.asp
Download_File "$SCRIPT_REPO/update/$SCRIPT_NAME.sh" "/jffs/scripts/$SCRIPT_NAME" && Print_Output true "$SCRIPT_NAME successfully updated"
chmod 0755 "/jffs/scripts/$SCRIPT_NAME"
Set_Version_Custom_Settings local "$serverver"
Set_Version_Custom_Settings server "$serverver"
Clear_Lock
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 --connect-timeout 15 "$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"
Update_File CHANGELOG.md
Update_File README.md
Update_File LICENSE
Update_File shared-jy.tar.gz
Update_File connmonstats_www.asp
Download_File "$SCRIPT_REPO/update/$SCRIPT_NAME.sh" "/jffs/scripts/$SCRIPT_NAME" && Print_Output true "$SCRIPT_NAME successfully updated"
chmod 0755 "/jffs/scripts/$SCRIPT_NAME"
Set_Version_Custom_Settings local "$serverver"
Set_Version_Custom_Settings server "$serverver"
Clear_Lock
if [ -z "$2" ]; then
PressEnter
exec "$0"
elif [ "$2" = "unattended" ]; then
exec "$0" postupdate
fi
exit 0
fi
}
Update_File(){
if [ "$1" = "connmonstats_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" = "CHANGELOG.md" ]; 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" = "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"
else
return 1
fi
}
Validate_Number(){
if [ "$1" -eq "$1" ] 2>/dev/null; then
return 0
else
return 1
fi
}
Validate_IP(){
if expr "$1" : '[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 "$1" | cut -d. -f$i)" -gt 255 ]; then
Print_Output false "Octet $i ($(echo "$1" | cut -d. -f$i)) - is invalid, must be less than 255" "$ERR"
return 1
fi
done
else
Print_Output false "$1 - is not a valid IPv4 address, valid format is 1.2.3.4" "$ERR"
return 1
fi
}
Validate_Domain(){
if ! nslookup "$1" >/dev/null 2>&1; then
Print_Output false "$1 cannot be resolved by nslookup, please ensure you enter a valid domain name" "$ERR"
return 1
else
return 0
fi
}
Conf_FromSettings(){
SETTINGSFILE="/jffs/addons/custom_settings.txt"
TMPFILE="/tmp/connmon_settings.txt"
if [ -f "$SETTINGSFILE" ]; then
if [ "$(grep "connmon_" $SETTINGSFILE | grep -v "version" -c)" -gt 0 ]; then
Print_Output true "Updated settings from WebUI found, merging into $SCRIPT_CONF" "$PASS"
cp -a "$SCRIPT_CONF" "$SCRIPT_CONF.bak"
grep "connmon_" "$SETTINGSFILE" | grep -v "version" > "$TMPFILE"
sed -i "s/connmon_//g;s/ /=/g" "$TMPFILE"
while IFS='' read -r line || [ -n "$line" ]; do
SETTINGNAME="$(echo "$line" | cut -f1 -d'=' | awk '{print toupper($1)}')"
SETTINGVALUE="$(echo "$line" | cut -f2 -d'=')"
if [ "$SETTINGNAME" = "NOTIFICATIONS_PUSHOVER_LIST" ] || [ "$SETTINGNAME" = "NOTIFICATIONS_WEBHOOK_LIST" ] || [ "$SETTINGNAME" = "NOTIFICATIONS_EMAIL_LIST" ]; then
SETTINGVALUE="$(echo "$SETTINGVALUE" | sed 's~||||~,~g')"
fi
sed -i "s~$SETTINGNAME=.*~$SETTINGNAME=$SETTINGVALUE~" "$SCRIPT_CONF"
done < "$TMPFILE"
grep 'connmon_version' "$SETTINGSFILE" > "$TMPFILE"
sed -i "\\~connmon_~d" "$SETTINGSFILE"
mv "$SETTINGSFILE" "$SETTINGSFILE.bak"
cat "$SETTINGSFILE.bak" "$TMPFILE" > "$SETTINGSFILE"
rm -f "$TMPFILE"
rm -f "$SETTINGSFILE.bak"
ScriptStorageLocation "$(ScriptStorageLocation check)"
Create_Symlinks
if AutomaticMode check; then
if diff "$SCRIPT_CONF" "$SCRIPT_CONF.bak" | grep -q "^SCH"; then
Auto_Cron delete 2>/dev/null
Auto_Cron create 2>/dev/null
fi
else
Auto_Cron delete 2>/dev/null
fi
if diff "$SCRIPT_CONF" "$SCRIPT_CONF.bak" | grep -q "OUTPUTTIMEMODE=\|DAYSTOKEEP=\|LASTXRESULTS="; then
Generate_CSVs
fi
Print_Output true "Merge of updated settings from WebUI completed successfully" "$PASS"
else
Print_Output false "No updated settings from WebUI found, no merge into $SCRIPT_CONF necessary" "$PASS"
fi
fi
}
EmailConf_FromSettings(){
SETTINGSFILE="/jffs/addons/custom_settings.txt"
TMPFILE="/tmp/email_settings.txt"
if [ -f "$SETTINGSFILE" ]; then
Print_Output true "Updated email settings from WebUI found, merging into $EMAIL_CONF" "$PASS"
cp -a "$EMAIL_CONF" "$EMAIL_CONF.bak"
grep "email_" "$SETTINGSFILE" > "$TMPFILE"
sed -i "s/email_//g;s/ /=/g" "$TMPFILE"
while IFS='' read -r line || [ -n "$line" ]; do
SETTINGNAME="$(echo "$line" | cut -f1 -d'=' | awk '{print toupper($1)}')"
SETTINGVALUE="$(echo "$line" | cut -f2- -d'=' | sed 's/=/ /g')"
if [ "$SETTINGNAME" = "PASSWORD" ]; then
Email_Encrypt_Password "$SETTINGVALUE"
else
sed -i "s~$SETTINGNAME=.*~$SETTINGNAME=\"$SETTINGVALUE\"~" "$EMAIL_CONF"
fi
done < "$TMPFILE"
sed -i "\\~email_~d" "$SETTINGSFILE"
rm -f "$TMPFILE"
Print_Output true "Merge of updated email settings from WebUI completed successfully" "$PASS"
fi
}
Create_Dirs(){
if [ ! -d "$SCRIPT_DIR" ]; then
mkdir -p "$SCRIPT_DIR"
fi
if [ ! -d "$SCRIPT_STORAGE_DIR" ]; then
mkdir -p "$SCRIPT_STORAGE_DIR"
fi
if [ ! -d "$CSV_OUTPUT_DIR" ]; then
mkdir -p "$CSV_OUTPUT_DIR"
fi
if [ ! -d "$SHARED_DIR" ]; then
mkdir -p "$SHARED_DIR"
fi
if [ ! -d "$SCRIPT_WEBPAGE_DIR" ]; then
mkdir -p "$SCRIPT_WEBPAGE_DIR"
fi
if [ ! -d "$SCRIPT_WEB_DIR" ]; then
mkdir -p "$SCRIPT_WEB_DIR"
fi
if [ ! -d "$EMAIL_DIR" ]; then
mkdir -p "$EMAIL_DIR"
fi
if [ ! -d "$USER_SCRIPT_DIR" ]; then
mkdir -p "$USER_SCRIPT_DIR"
fi
}
Create_Symlinks(){
ln -sf "$SCRIPT_STORAGE_DIR/connstatstext.js" "$SCRIPT_WEB_DIR/connstatstext.js" 2>/dev/null
ln -sf "$SCRIPT_STORAGE_DIR/lastx.csv" "$SCRIPT_WEB_DIR/lastx.htm" 2>/dev/null
ln -sf "$EMAIL_CONF" "$SCRIPT_WEB_DIR/email_config.htm" 2>/dev/null
ln -sf "$SCRIPT_CONF" "$SCRIPT_WEB_DIR/config.htm" 2>/dev/null
ln -sf "$SCRIPT_DIR/CHANGELOG.md" "$SCRIPT_WEB_DIR/changelog.htm" 2>/dev/null
ln -sf "$SCRIPT_STORAGE_DIR/.cron" "$SCRIPT_WEB_DIR/cron.js" 2>/dev/null
ln -sf "$SCRIPT_STORAGE_DIR/.customactioninfo" "$SCRIPT_WEB_DIR/customactioninfo.htm" 2>/dev/null
ln -sf "$SCRIPT_STORAGE_DIR/.customactionlist" "$SCRIPT_WEB_DIR/customactionlist.htm" 2>/dev/null
ln -sf "$SCRIPT_STORAGE_DIR/.emailinfo" "$SCRIPT_WEB_DIR/emailinfo.htm" 2>/dev/null
ln -sf "$CSV_OUTPUT_DIR" "$SCRIPT_WEB_DIR/csv" 2>/dev/null
if [ ! -d "$SHARED_WEB_DIR" ]; then
ln -s "$SHARED_DIR" "$SHARED_WEB_DIR" 2>/dev/null
fi
}
Conf_Exists(){
if [ -f "$SCRIPT_CONF" ]; then
dos2unix "$SCRIPT_CONF"
chmod 0644 "$SCRIPT_CONF"
sed -i -e 's/"//g' "$SCRIPT_CONF"
if grep -q "SCHEDULESTART" "$SCRIPT_CONF"; then
if ! grep -q "AUTOMATED" "$SCRIPT_CONF"; then
echo "AUTOMATED=true" >> "$SCRIPT_CONF"
fi
if ! grep -q "SCHDAYS" "$SCRIPT_CONF"; then
echo "SCHDAYS=*" >> "$SCRIPT_CONF"
fi
echo "SCHHOURS=*" >> "$SCRIPT_CONF"
PINGFREQUENCY="$(Conf_Parameters check PINGFREQUENCY)"
echo "SCHMINS=*/$PINGFREQUENCY" >> "$SCRIPT_CONF"
sed -i '/SCHEDULESTART/d;/SCHEDULEEND/d;/PINGFREQUENCY/d;' "$SCRIPT_CONF"
fi
if grep -q "OUTPUTDATAMODE" "$SCRIPT_CONF"; then
sed -i '/OUTPUTDATAMODE/d;' "$SCRIPT_CONF"
fi
if ! grep -q "DAYSTOKEEP" "$SCRIPT_CONF"; then
echo "DAYSTOKEEP=30" >> "$SCRIPT_CONF"
fi
if ! grep -q "LASTXRESULTS" "$SCRIPT_CONF"; then
echo "LASTXRESULTS=10" >> "$SCRIPT_CONF"
fi
if ! grep -q "EXCLUDEFROMQOS" "$SCRIPT_CONF"; then
echo "EXCLUDEFROMQOS=true" >> "$SCRIPT_CONF"
fi
if ! grep -q "NOTIFICATIONS" "$SCRIPT_CONF"; then
{
echo "NOTIFICATIONS_EMAIL=false"
echo "NOTIFICATIONS_WEBHOOK=false"
echo "NOTIFICATIONS_PUSHOVER=false"
echo "NOTIFICATIONS_CUSTOM=false"
echo "NOTIFICATIONS_HEALTHCHECK=false"
echo "NOTIFICATIONS_INFLUXDB=false"
echo "NOTIFICATIONS_PINGTEST=None"
echo "NOTIFICATIONS_PINGTHRESHOLD=None"
echo "NOTIFICATIONS_JITTERTHRESHOLD=None"
echo "NOTIFICATIONS_LINEQUALITYTHRESHOLD=None"
echo "NOTIFICATIONS_PINGTHRESHOLD_VALUE=30"
echo "NOTIFICATIONS_JITTERTHRESHOLD_VALUE=15"
echo "NOTIFICATIONS_LINEQUALITYTHRESHOLD_VALUE=75"
echo "NOTIFICATIONS_EMAIL_LIST="
echo "NOTIFICATIONS_HEALTHCHECK_UUID="
echo "NOTIFICATIONS_WEBHOOK_LIST="
echo "NOTIFICATIONS_PUSHOVER_LIST="
echo "NOTIFICATIONS_PUSHOVER_API="
echo "NOTIFICATIONS_PUSHOVER_USERKEY="
echo "NOTIFICATIONS_INFLUXDB_HOST="
echo "NOTIFICATIONS_INFLUXDB_PORT=8086"
echo "NOTIFICATIONS_INFLUXDB_DB=connmon"
echo "NOTIFICATIONS_INFLUXDB_VERSION=1.8"
echo "NOTIFICATIONS_INFLUXDB_USERNAME="
echo "NOTIFICATIONS_INFLUXDB_PASSWORD="
echo "NOTIFICATIONS_INFLUXDB_APITOKEN="
} >> "$SCRIPT_CONF"
fi
return 0
else
{ echo "PINGSERVER=8.8.8.8"; echo "OUTPUTTIMEMODE=unix"; echo "STORAGELOCATION=jffs"; echo "PINGDURATION=60"; echo "AUTOMATED=true"; echo "SCHDAYS=*"; echo "SCHHOURS=*"; echo "SCHMINS=*/3"; echo "DAYSTOKEEP=30"; echo "LASTXRESULTS=10"; echo "EXCLUDEFROMQOS=true";
echo "NOTIFICATIONS_EMAIL=false"; echo "NOTIFICATIONS_WEBHOOK=false"; echo "NOTIFICATIONS_PUSHOVER=false"; echo "NOTIFICATIONS_CUSTOM=false"; echo "NOTIFICATIONS_HEALTHCHECK=false"; echo "NOTIFICATIONS_INFLUXDB=false";
echo "NOTIFICATIONS_PINGTEST=None"; echo "NOTIFICATIONS_PINGTHRESHOLD=None"; echo "NOTIFICATIONS_JITTERTHRESHOLD=None"; echo "NOTIFICATIONS_LINEQUALITYTHRESHOLD=None"; echo "NOTIFICATIONS_PINGTHRESHOLD_VALUE=30"; echo "NOTIFICATIONS_JITTERTHRESHOLD_VALUE=15"; echo "NOTIFICATIONS_LINEQUALITYTHRESHOLD_VALUE=75";
echo "NOTIFICATIONS_EMAIL_LIST="; echo "NOTIFICATIONS_HEALTHCHECK_UUID="; echo "NOTIFICATIONS_WEBHOOK_LIST="; echo "NOTIFICATIONS_PUSHOVER_LIST="; echo "NOTIFICATIONS_PUSHOVER_API="; echo "NOTIFICATIONS_PUSHOVER_USERKEY="; echo "NOTIFICATIONS_INFLUXDB_HOST="; echo "NOTIFICATIONS_INFLUXDB_PORT=8086"; echo "NOTIFICATIONS_INFLUXDB_DB=connmon";
echo "NOTIFICATIONS_INFLUXDB_VERSION=1.8"; echo "NOTIFICATIONS_INFLUXDB_USERNAME="; echo "NOTIFICATIONS_INFLUXDB_PASSWORD="; echo "NOTIFICATIONS_INFLUXDB_APITOKEN="; } > "$SCRIPT_CONF"
return 1
fi
}
PingServer(){
case "$1" in
update)
while true; do
ScriptHeader
printf "\\n${BOLD}Current ping destination: %s${CLEARFORMAT}\\n\\n" "$(PingServer check)"
printf "1. Enter IP Address\\n"
printf "2. Enter Domain\\n"
printf "\\ne. Go back\\n"
printf "\\n${BOLD}Choose an option:${CLEARFORMAT} "
read -r pingoption
case "$pingoption" in
1)
while true; do
printf "\\n${BOLD}Please enter an IP address, or enter e to go back:${CLEARFORMAT} "
read -r ipoption
if [ "$ipoption" = "e" ]; then
break
elif Validate_IP "$ipoption"; then
sed -i 's/^PINGSERVER=.*$/PINGSERVER='"$ipoption"'/' "$SCRIPT_CONF"
break
fi
done
;;
2)
while true; do
printf "\\n${BOLD}Please enter a domain name, or enter e to go back:${CLEARFORMAT} "
read -r domainoption
if [ "$domainoption" = "e" ]; then
break
elif Validate_Domain "$domainoption"; then
sed -i 's/^PINGSERVER=.*$/PINGSERVER='"$domainoption"'/' "$SCRIPT_CONF"
break
fi
done
;;
e)
printf "\\n"
break
;;
*)
printf "\\n${BOLD}${ERR}Please choose a valid option${CLEARFORMAT}\\n\\n"
PressEnter
;;
esac
done
;;
check)
PINGSERVER="$(Conf_Parameters check PINGSERVER)"
echo "$PINGSERVER"
;;
esac
}
PingDuration(){
case "$1" in
update)
pingdur=0
exitmenu=""
ScriptHeader
while true; do
printf "\\n${BOLD}Please enter the desired test duration (10-60 seconds):${CLEARFORMAT} "
read -r pingdur_choice
if [ "$pingdur_choice" = "e" ]; then
exitmenu="exit"
break
elif ! Validate_Number "$pingdur_choice"; then
printf "\\n${ERR}Please enter a valid number (10-60)${CLEARFORMAT}\\n"
elif [ "$pingdur_choice" -lt 10 ] || [ "$pingdur_choice" -gt 60 ]; then
printf "\\n${ERR}Please enter a number between 10 and 60${CLEARFORMAT}\\n"
else
pingdur="$pingdur_choice"
printf "\\n"
break
fi
done
if [ "$exitmenu" != "exit" ]; then
sed -i 's/^PINGDURATION=.*$/PINGDURATION='"$pingdur"'/' "$SCRIPT_CONF"
return 0
else
printf "\\n"
return 1
fi
;;
check)
PINGDURATION="$(Conf_Parameters check PINGDURATION)"
echo "$PINGDURATION"
;;
esac
}
DaysToKeep(){
case "$1" in
update)
daystokeep=30
exitmenu=""
ScriptHeader
while true; do
printf "\\n${BOLD}Please enter the desired number of days\\nto keep data for (30-365 days):${CLEARFORMAT} "
read -r daystokeep_choice
if [ "$daystokeep_choice" = "e" ]; then
exitmenu="exit"
break
elif ! Validate_Number "$daystokeep_choice"; then
printf "\\n${ERR}Please enter a valid number (30-365)${CLEARFORMAT}\\n"
elif [ "$daystokeep_choice" -lt 30 ] || [ "$daystokeep_choice" -gt 365 ]; then
printf "\\n${ERR}Please enter a number between 30 and 365${CLEARFORMAT}\\n"
else
daystokeep="$daystokeep_choice"
printf "\\n"
break
fi
done
if [ "$exitmenu" != "exit" ]; then
sed -i 's/^DAYSTOKEEP=.*$/DAYSTOKEEP='"$daystokeep"'/' "$SCRIPT_CONF"
return 0
else
printf "\\n"
return 1
fi
;;
check)
DAYSTOKEEP="$(Conf_Parameters check DAYSTOKEEP)"
echo "$DAYSTOKEEP"
;;
esac
}
LastXResults(){
case "$1" in
update)
lastxresults=10
exitmenu=""
ScriptHeader
while true; do
printf "\\n${BOLD}Please enter the desired number of results\\nto display in the WebUI (1-100):${CLEARFORMAT} "
read -r lastx_choice
if [ "$lastx_choice" = "e" ]; then
exitmenu="exit"
break
elif ! Validate_Number "$lastx_choice"; then
printf "\\n${ERR}Please enter a valid number (1-100)${CLEARFORMAT}\\n"
elif [ "$lastx_choice" -lt 1 ] || [ "$lastx_choice" -gt 100 ]; then
printf "\\n${ERR}Please enter a number between 1 and 100${CLEARFORMAT}\\n"
else
lastxresults="$lastx_choice"
printf "\\n"
break
fi
done
if [ "$exitmenu" != "exit" ]; then
sed -i 's/^LASTXRESULTS=.*$/LASTXRESULTS='"$lastxresults"'/' "$SCRIPT_CONF"
Generate_LastXResults
return 0
else
printf "\\n"
return 1
fi
;;
check)
LASTXRESULTS="$(Conf_Parameters check LASTXRESULTS)"
echo "$LASTXRESULTS"
;;
esac
}
Auto_ServiceEvent(){
case $1 in
create)
if [ -f /jffs/scripts/service-event ]; then
STARTUPLINECOUNT="$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/service-event)"
STARTUPLINECOUNTEX="$(grep -cx 'if echo "$2" | /bin/grep -q "'"$SCRIPT_NAME"'"; then { /jffs/scripts/'"$SCRIPT_NAME"' service_event "$@" & }; fi # '"$SCRIPT_NAME" /jffs/scripts/service-event)"
if [ "$STARTUPLINECOUNT" -gt 1 ] || { [ "$STARTUPLINECOUNTEX" -eq 0 ] && [ "$STARTUPLINECOUNT" -gt 0 ]; }; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/service-event
fi
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
echo 'if echo "$2" | /bin/grep -q "'"$SCRIPT_NAME"'"; then { /jffs/scripts/'"$SCRIPT_NAME"' service_event "$@" & }; fi # '"$SCRIPT_NAME" >> /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"'"; then { /jffs/scripts/'"$SCRIPT_NAME"' service_event "$@" & }; fi # '"$SCRIPT_NAME" >> /jffs/scripts/service-event
chmod 0755 /jffs/scripts/service-event
fi
;;
delete)
if [ -f /jffs/scripts/service-event ]; then
STARTUPLINECOUNT="$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/service-event)"
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/service-event
fi
fi
;;
esac
}
Auto_Startup(){
case $1 in
create)
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
if [ -f /jffs/scripts/post-mount ]; then
STARTUPLINECOUNT="$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/post-mount)"
STARTUPLINECOUNTEX="$(grep -cx "/jffs/scripts/$SCRIPT_NAME startup"' "$@" & # '"$SCRIPT_NAME" /jffs/scripts/post-mount)"
if [ "$STARTUPLINECOUNT" -gt 1 ] || { [ "$STARTUPLINECOUNTEX" -eq 0 ] && [ "$STARTUPLINECOUNT" -gt 0 ]; }; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/post-mount
fi
if [ "$STARTUPLINECOUNTEX" -eq 0 ]; then
echo "/jffs/scripts/$SCRIPT_NAME startup"' "$@" & # '"$SCRIPT_NAME" >> /jffs/scripts/post-mount
fi
else
echo "#!/bin/sh" > /jffs/scripts/post-mount
echo "" >> /jffs/scripts/post-mount
echo "/jffs/scripts/$SCRIPT_NAME startup"' "$@" & # '"$SCRIPT_NAME" >> /jffs/scripts/post-mount
chmod 0755 /jffs/scripts/post-mount
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
if [ -f /jffs/scripts/post-mount ]; then
STARTUPLINECOUNT="$(grep -c '# '"$SCRIPT_NAME" /jffs/scripts/post-mount)"
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
sed -i -e '/# '"$SCRIPT_NAME"'/d' /jffs/scripts/post-mount
fi
fi
;;
esac
}
Auto_Cron(){
case $1 in
create)
STARTUPLINECOUNT="$(cru l | grep -c "$SCRIPT_NAME")"
if [ "$STARTUPLINECOUNT" -eq 0 ]; then
CRU_DAYNUMBERS="$(Conf_Parameters check SCHDAYS | sed 's/Sun/0/;s/Mon/1/;s/Tues/2/;s/Wed/3/;s/Thurs/4/;s/Fri/5/;s/Sat/6/;')"
CRU_HOURS="$(Conf_Parameters check SCHHOURS)"
CRU_MINUTES="$(Conf_Parameters check SCHMINS)"
cru a "$SCRIPT_NAME" "$CRU_MINUTES $CRU_HOURS * * $CRU_DAYNUMBERS /jffs/scripts/$SCRIPT_NAME generate"
echo "$CRU_MINUTES $CRU_HOURS * * $CRU_DAYNUMBERS" > "$SCRIPT_STORAGE_DIR/.cron"
fi
;;
delete)
STARTUPLINECOUNT="$(cru l | grep -c "$SCRIPT_NAME")"
if [ "$STARTUPLINECOUNT" -gt 0 ]; then
cru d "$SCRIPT_NAME"
fi
;;
esac
}
Download_File(){
/usr/sbin/curl -fsL --retry 3 --connect-timeout 15 "$1" -o "$2"
}
Get_WebUI_Page(){
MyPage="none"
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
page="/www/user/user$i.asp"
if [ -f "$page" ] && [ "$(md5sum < "$1")" = "$(md5sum < "$page")" ]; then
MyPage="user$i.asp"
return
elif [ "$MyPage" = "none" ] && [ ! -f "$page" ]; then
MyPage="user$i.asp"
fi
done
}
### function based on @dave14305's FlexQoS webconfigpage function ###
Get_WebUI_URL(){
urlpage=""
urlproto=""
urldomain=""
urlport=""
urlpage="$(sed -nE "/$SCRIPT_NAME/ s/.*url\: \"(user[0-9]+\.asp)\".*/\1/p" /tmp/menuTree.js)"
if [ "$(nvram get http_enable)" -eq 1 ]; then
urlproto="https"
else
urlproto="http"
fi
if [ -n "$(nvram get lan_domain)" ]; then
urldomain="$(nvram get lan_hostname).$(nvram get lan_domain)"
else
urldomain="$(nvram get lan_ipaddr)"
fi
if [ "$(nvram get ${urlproto}_lanport)" -eq 80 ] || [ "$(nvram get ${urlproto}_lanport)" -eq 443 ]; then
urlport=""
else
urlport=":$(nvram get ${urlproto}_lanport)"
fi
if echo "$urlpage" | grep -qE "user[0-9]+\.asp"; then
echo "${urlproto}://${urldomain}${urlport}/${urlpage}" | tr "A-Z" "a-z"
else
echo "WebUI page not found"
fi
}
### ###
### locking mechanism code credit to Martineau (@MartineauUK) ###
Mount_WebUI(){
Print_Output true "Mounting WebUI tab for $SCRIPT_NAME" "$PASS"
LOCKFILE=/tmp/addonwebui.lock
FD=386
eval exec "$FD>$LOCKFILE"
flock -x "$FD"
Get_WebUI_Page "$SCRIPT_DIR/connmonstats_www.asp"
if [ "$MyPage" = "none" ]; then
Print_Output true "Unable to mount $SCRIPT_NAME WebUI page, exiting" "$CRIT"
flock -u "$FD"
return 1
fi
cp -f "$SCRIPT_DIR/connmonstats_www.asp" "$SCRIPT_WEBPAGE_DIR/$MyPage"
echo "$SCRIPT_NAME" > "$SCRIPT_WEBPAGE_DIR/$(echo $MyPage | cut -f1 -d'.').title"
if [ "$(/bin/uname -o)" = "ASUSWRT-Merlin" ]; then
if [ ! -f /tmp/index_style.css ]; then
cp -f /www/index_style.css /tmp/
fi
if ! grep -q '.menu_Addons' /tmp/index_style.css ; then
echo ".menu_Addons { background: url(ext/shared-jy/addons.png); }" >> /tmp/index_style.css
fi
umount /www/index_style.css 2>/dev/null
mount -o bind /tmp/index_style.css /www/index_style.css
if [ ! -f /tmp/menuTree.js ]; then
cp -f /www/require/modules/menuTree.js /tmp/
fi
sed -i "\\~$MyPage~d" /tmp/menuTree.js
if ! grep -q 'menuName: "Addons"' /tmp/menuTree.js ; then
lineinsbefore="$(( $(grep -n "exclude:" /tmp/menuTree.js | cut -f1 -d':') - 1))"
sed -i "$lineinsbefore"'i,\n{\nmenuName: "Addons",\nindex: "menu_Addons",\ntab: [\n{url: "javascript:var helpwindow=window.open('"'"'/ext/shared-jy/redirect.htm'"'"')", tabName: "Help & Support"},\n{url: "NULL", tabName: "__INHERIT__"}\n]\n}' /tmp/menuTree.js
fi
sed -i "/url: \"javascript:var helpwindow=window.open('\/ext\/shared-jy\/redirect.htm'/i {url: \"$MyPage\", tabName: \"$SCRIPT_NAME\"}," /tmp/menuTree.js
umount /www/require/modules/menuTree.js 2>/dev/null
mount -o bind /tmp/menuTree.js /www/require/modules/menuTree.js
if [ ! -f /tmp/start_apply.htm ]; then
cp -f /www/start_apply.htm /tmp/
fi
if ! grep -q 'addon_settings' /tmp/start_apply.htm ; then
sed -i "/}else if(action_script == \"start_sig_check\"){/i }else if(action_script.indexOf(\"addon_settings\") != -1){ \/\/ do nothing" /tmp/start_apply.htm
fi
umount /www/start_apply.htm 2>/dev/null
mount -o bind /tmp/start_apply.htm /www/start_apply.htm
fi
flock -u "$FD"
Print_Output true "Mounted $SCRIPT_NAME WebUI page as $MyPage" "$PASS"
}
ExcludeFromQoS(){
case "$1" in
enable)
sed -i 's/^EXCLUDEFROMQOS=.*$/EXCLUDEFROMQOS=true/' "$SCRIPT_CONF"
;;
disable)
sed -i 's/^EXCLUDEFROMQOS=.*$/EXCLUDEFROMQOS=false/' "$SCRIPT_CONF"
;;
check)
EXCLUDEFROMQOS="$(Conf_Parameters check EXCLUDEFROMQOS)"
echo "$EXCLUDEFROMQOS"
;;
esac
}
AutomaticMode(){
case "$1" in
enable)
sed -i 's/^AUTOMATED=.*$/AUTOMATED=true/' "$SCRIPT_CONF"
Auto_Cron create 2>/dev/null
;;
disable)
sed -i 's/^AUTOMATED=.*$/AUTOMATED=false/' "$SCRIPT_CONF"
Auto_Cron delete 2>/dev/null
;;
check)
AUTOMATED="$(Conf_Parameters check AUTOMATED)"
if [ "$AUTOMATED" = "true" ]; then return 0; else return 1; fi
;;
esac
}
TestSchedule(){
case "$1" in
update)
sed -i 's/^SCHDAYS=.*$/SCHDAYS='"$(echo "$2" | sed 's/0/Sun/;s/1/Mon/;s/2/Tues/;s/3/Wed/;s/4/Thurs/;s/5/Fri/;s/6/Sat/;')"'/' "$SCRIPT_CONF"
sed -i 's~^SCHHOURS=.*$~SCHHOURS='"$3"'~' "$SCRIPT_CONF"
sed -i 's~^SCHMINS=.*$~SCHMINS='"$4"'~' "$SCRIPT_CONF"
Auto_Cron delete 2>/dev/null
Auto_Cron create 2>/dev/null
;;
check)
SCHDAYS="$(Conf_Parameters check SCHDAYS)"
SCHHOURS="$(Conf_Parameters check SCHHOURS)"
SCHMINS="$(Conf_Parameters check SCHMINS)"
echo "$SCHDAYS|$SCHHOURS|$SCHMINS"
;;
esac
}
ScriptStorageLocation(){
case "$1" in
usb)
sed -i 's/^STORAGELOCATION=.*$/STORAGELOCATION=usb/' "$SCRIPT_CONF"
mkdir -p "/opt/share/$SCRIPT_NAME.d/"
mv "/jffs/addons/$SCRIPT_NAME.d/csv" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/config" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/config.bak" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/connstatstext.js" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/lastx.csv" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/connstats.db" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/.indexcreated" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/.newcolumns" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/.cron" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/.customactioninfo" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/.customactionlist" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
mv "/jffs/addons/$SCRIPT_NAME.d/.emailinfo" "/opt/share/$SCRIPT_NAME.d/" 2>/dev/null
SCRIPT_CONF="/opt/share/$SCRIPT_NAME.d/config"
ScriptStorageLocation load
;;
jffs)
sed -i 's/^STORAGELOCATION=.*$/STORAGELOCATION=jffs/' "$SCRIPT_CONF"
mkdir -p "/jffs/addons/$SCRIPT_NAME.d/"
mv "/opt/share/$SCRIPT_NAME.d/csv" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/config" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/config.bak" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/connstatstext.js" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/lastx.csv" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/connstats.db" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/.indexcreated" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/.newcolumns" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/.cron" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/.customactioninfo" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/.customactionlist" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
mv "/opt/share/$SCRIPT_NAME.d/.emailinfo" "/jffs/addons/$SCRIPT_NAME.d/" 2>/dev/null
SCRIPT_CONF="/jffs/addons/$SCRIPT_NAME.d/config"
ScriptStorageLocation load
;;
check)
STORAGELOCATION="$(Conf_Parameters check STORAGELOCATION)"
echo "$STORAGELOCATION"
;;
load)
STORAGELOCATION="$(Conf_Parameters check STORAGELOCATION)"
if [ "$STORAGELOCATION" = "usb" ]; then
SCRIPT_STORAGE_DIR="/opt/share/$SCRIPT_NAME.d"
elif [ "$STORAGELOCATION" = "jffs" ]; then
SCRIPT_STORAGE_DIR="/jffs/addons/$SCRIPT_NAME.d"
fi