-
Notifications
You must be signed in to change notification settings - Fork 1
/
tailmon.sh
2850 lines (2501 loc) · 119 KB
/
tailmon.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
# TAILMON (TAILMON.SH) is an all-in-one script that is optimized to install, maintain and monitor a Tailscale service and
# connection from your Asus-Merlin FW router. It provides the basic steps needed to install and implement a successful
# connection to your tailnet. It allows for 2 different modes of operation: Kernel and Userspace modes. Depending on your
# needs, you can also enable exit node and subnet route advertisements. Separately, TAILMON functions as a Tailscale
# monitor application that will sit in the background (using the -screen utility), and will restart the Tailscale service
# should it happen to go down. Many thanks to: @jksmurf, @ColinTaylor, @Aiadi, and @kuki68ster for all their help, input
# and testing of this script!
#Preferred standard router binaries path
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:$PATH"
#Static Variables - please do not change
version="1.0.20"
beta=0
apppath="/jffs/scripts/tailmon.sh" # Static path to the app
config="/jffs/addons/tailmon.d/tailmon.cfg" # Static path to the config file
dlverpath="/jffs/addons/tailmon.d/version.txt" # Static path to the version file
logfile="/jffs/addons/tailmon.d/tailmon.log" # Static path to the log
routerboot=0
tsinstalled=0
keepalive=0
timerloop=60
logsize=2000
autostart=0
amtmemailsuccess=0
amtmemailfailure=0
exitnode=0
advroutes=1
accroutes=0
persistentsettings=0
tsoperatingmode="Userspace"
precmd=""
args="--tun=userspace-networking --state=/opt/var/tailscaled.state --statedir=/opt/var/lib/tailscale"
preargs="nohup"
routes="$(nvram get lan_ipaddr | cut -d"." -f1-3).0/24"
customcmdline=""
#AMTM Email Notification Variables
readonly scriptFileName="${0##*/}"
readonly scriptFileNTag="${scriptFileName%.*}"
readonly CEM_LIB_TAG="master"
readonly CEM_LIB_URL="https://raw.githubusercontent.com/Martinski4GitHub/CustomMiscUtils/${CEM_LIB_TAG}/EMail"
readonly CUSTOM_EMAIL_LIBDir="/jffs/addons/shared-libs"
readonly CUSTOM_EMAIL_LIBName="CustomEMailFunctions.lib.sh"
readonly CUSTOM_EMAIL_LIBFile="${CUSTOM_EMAIL_LIBDir}/$CUSTOM_EMAIL_LIBName"
# Color variables
CBlack="\e[1;30m"
InvBlack="\e[1;40m"
CRed="\e[1;31m"
InvRed="\e[1;41m"
CGreen="\e[1;32m"
InvGreen="\e[1;42m"
CDkGray="\e[1;90m"
InvDkGray="\e[1;100m"
InvLtGray="\e[1;47m"
CYellow="\e[1;33m"
InvYellow="\e[1;43m"
CBlue="\e[1;34m"
InvBlue="\e[1;44m"
CMagenta="\e[1;35m"
CCyan="\e[1;36m"
InvCyan="\e[1;46m"
CWhite="\e[1;37m"
InvWhite="\e[1;107m"
CClear="\e[0m"
# -------------------------------------------------------------------------------------------------------------------------
# FUNCTIONS BEGIN
# -------------------------------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------------------------------
# LogoNM is a function that displays the BACKUPMON script name in a cool ASCII font without menu options
logoNM ()
{
clear
echo ""
echo ""
echo ""
echo -e "${CDkGray} _________ ______ __ _______ _ __"
echo -e " /_ __/ | / _/ / / |/ / __ \/ | / /"
echo -e " / / / /| | / // / / /|_/ / / / / |/ /"
echo -e " / / / ___ |_/ // /___/ / / / /_/ / /| /"
echo -e " /_/ /_/ |_/___/_____/_/ /_/\____/_/ |_/ v$version"
echo ""
echo ""
printf "\r ${CGreen} [ INITIALIZING ] ${CClear}"
sleep 1
clear
echo ""
echo ""
echo ""
echo -e "${CYellow} _________ ______ __ _______ _ __"
echo -e " /_ __/ | / _/ / / |/ / __ \/ | / /"
echo -e " / / / /| | / // / / /|_/ / / / / |/ /"
echo -e " / / / ___ |_/ // /___/ / / / /_/ / /| /"
echo -e " /_/ /_/ |_/___/_____/_/ /_/\____/_/ |_/ v$version"
echo ""
echo ""
printf "\r ${CGreen}[ INITIALIZING ... DONE ]${CClear}"
sleep 1
printf "\r ${CGreen} [ LOADING... ] ${CClear}"
sleep 1
}
logoNMexit ()
{
clear
echo ""
echo ""
echo ""
echo -e "${CYellow} _________ ______ __ _______ _ __"
echo -e " /_ __/ | / _/ / / |/ / __ \/ | / /"
echo -e " / / / /| | / // / / /|_/ / / / / |/ /"
echo -e " / / / ___ |_/ // /___/ / / / /_/ / /| /"
echo -e " /_/ /_/ |_/___/_____/_/ /_/\____/_/ |_/ v$version"
echo ""
echo ""
printf "\r ${CGreen} [ SHUTTING DOWN ] ${CClear}"
sleep 1
clear
echo ""
echo ""
echo ""
echo -e "${CDkGray} _________ ______ __ _______ _ __"
echo -e " /_ __/ | / _/ / / |/ / __ \/ | / /"
echo -e " / / / /| | / // / / /|_/ / / / / |/ /"
echo -e " / / / ___ |_/ // /___/ / / / /_/ / /| /"
echo -e " /_/ /_/ |_/___/_____/_/ /_/\____/_/ |_/ v$version"
echo ""
echo ""
printf "\r ${CGreen} [ SHUTTING DOWN ] ${CClear}"
sleep 1
printf "\r ${CDkGray} [ GOODBYE... ] ${CClear}\n\n"
sleep 1
}
# -------------------------------------------------------------------------------------------------------------------------
# Promptyn is a simple function that accepts y/n input
promptyn()
{ # No defaults, just y or n
while true; do
read -p "$1" -n 1 -r yn
case "${yn}" in
[Yy]* ) return 0 ;;
[Nn]* ) return 1 ;;
* ) echo -e "\nPlease answer y or n.";;
esac
done
}
# -------------------------------------------------------------------------------------------------------------------------
# Spinner is a script that provides a small indicator on the screen to show script activity
spinner()
{
spins=$1
spin=0
totalspins=$((spins / 4))
while [ $spin -le $totalspins ]; do
for spinchar in / - \\ \|; do
printf "\r$spinchar"
sleep 1
done
spin=$((spin+1))
done
printf "\r"
}
# -------------------------------------------------------------------------------------------------------------------------
# Preparebar and Progressbar is a script that provides a nice progressbar to show script activity
preparebar()
{
barlen=$1
barspaces=$(printf "%*s" "$1")
barchars=$(printf "%*s" "$1" | tr ' ' "$2")
}
progressbaroverride()
{
insertspc=" "
bypasswancheck=0
if [ $1 -eq -1 ]; then
printf "\r $barspaces\r"
else
if [ ! -z $7 ] && [ $1 -ge $7 ]; then
barch=$(($7*barlen/$2))
barsp=$((barlen-barch))
progr=$((100*$1/$2))
else
barch=$(($1*barlen/$2))
barsp=$((barlen-barch))
progr=$((100*$1/$2))
fi
if [ ! -z $6 ]; then AltNum=$6; else AltNum=$1; fi
if [ "$5" == "Standard" ]; then
printf " ${CWhite}${InvDkGray}$AltNum${4} / ${progr}%%${CClear} [${CGreen}e${CClear}=Exit] [Selection? ${InvGreen} ${CClear}${CGreen}]\r${CClear}" "$barchars" "$barspaces"
fi
fi
# Borrowed this wonderful keypress capturing mechanism from @Eibgrad... thank you! :)
key_press=''; read -rsn1 -t 1 key_press < "$(tty 0>&2)"
if [ $key_press ]; then
case $key_press in
[Aa]) vconfig;;
[Cc]) vsetup;;
[Dd]) tsdown;;
[Ee]) logoNMexit; echo -e "${CClear}\n"; exit 0;;
[Kk]) vconfig;;
[Ll]) vlogs;;
[Mm]) timerloopconfig;;
[Oo]) if [ "$tsoperatingmode" == "Custom" ]; then customconfig; fi;;
[Rr]) restarttsc;;
[Ss]) startts;;
[Tt]) stopts;;
[Uu]) tsup;;
*) timer=$timerloop;;
esac
fi
}
# -------------------------------------------------------------------------------------------------------------------------
# Initial setup menu
initialsetup()
{
clear
echo -e "${InvGreen} ${InvDkGray}${CWhite} TAILMON Initial Setup ${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} TAILMON has not been configured yet, and Tailscale will need to be installed and${CClear}"
echo -e "${InvGreen} ${CClear} configured. You can choose between 'Express Install' and 'Advanced Install'.${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} 1) Express Install will automatically download and install Tailscale, choosing the${CClear}"
echo -e "${InvGreen} ${CClear} 'Userspace' mode of operation and configures it to advertise routes of your local${CClear}"
echo -e "${InvGreen} ${CClear} subnet by default. A URL prompt will appear which will require you to copy this link"
echo -e "${InvGreen} ${CClear} into your browser to connect this device to your tailnet."
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} 2) Advanced Install will launch the TAILMON Setup/Configuration Menu, and allows${CClear}"
echo -e "${InvGreen} ${CClear} you to manually choose your preferred settings, such as 'Kernel' vs. 'Userspace'${CClear}"
echo -e "${InvGreen} ${CClear} mode, and letting you pick the exit node option along with additional subnets."
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} Before starting, please familiarize yourself with how Tailscale works. Please use${CClear}"
echo -e "${InvGreen} ${CClear} @ColinTaylor's Wiki available here:${CClear}"
echo -e "${InvGreen} ${CClear} https://github.com/RMerl/asuswrt-merlin.ng/wiki/Installing-Tailscale-through-Entware${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} It is also advised to have an account set and ready to go on https://tailscale.com${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear}${CDkGray}---------------------------------------------------------------------------------------${CClear}"
echo ""
read -p "Please select? (1=Express Install, 2=Advanced Install, e=Exit): " SelectSetup
case $SelectSetup in
1)
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: TAILMON Express Install initiated." >> $logfile
expressinstall;;
2)
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: TAILMON Advanced Install initiated." >> $logfile
exec sh /jffs/scripts/tailmon.sh -setup;;
[Ee]) echo -e "${CClear}"; echo ""; exit 0;;
esac
}
# -------------------------------------------------------------------------------------------------------------------------
# Expressinstall script
expressinstall()
{
echo ""
echo -e "Ready to Express Install Tailscale?"
if promptyn "[y/n]: "
then
if [ -d "/opt" ]; then # Does entware exist? If yes proceed, if no error out.
echo ""
echo -e "\n${CGreen}Updating Entware Packages...${CClear}"
echo ""
opkg update
echo ""
echo -e "Installing Entware ${CGreen}CoreUtils-Timeout${CClear} Package...${CClear}"
echo ""
opkg install coreutils-timeout
echo ""
echo -e "Installing Entware ${CGreen}Screen${CClear} Package...${CClear}"
echo ""
opkg install screen
echo ""
echo -e "${CGreen}Installing Tailscale Package(s)...${CClear}"
echo ""
archker=$(opkg print-architecture | grep "armv7-2.6")
if [ -z "$archker" ]; then
opkg install tailscale
else
opkg install tailscale_nohf #install special tailscale package for arm7 kernel 2.6
fi
echo ""
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Entware package installed." >> $logfile
else
clear
echo -e "${CRed}ERROR: Entware was not found on this router...${CClear}"
echo -e "Please install Entware using the AMTM utility before proceeding..."
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - ERROR: Entware was not found installed on router. Please investigate." >> $logfile
echo ""
read -rsp $'Press any key to continue...\n' -n1 key
exit 1
fi
else
echo ""
echo -e "${CClear}"
exit 0
fi
echo ""
echo -e "${CGreen}Applying settings for Userspace mode of operation...${CClear}"
tsoperatingmode="Userspace"
precmd=""
args="--tun=userspace-networking --state=/opt/var/tailscaled.state --statedir=/opt/var/lib/tailscale"
preargs="nohup"
saveconfig
echo ""
echo -e "${CGreen}Applying settings to Tailscale service and connection...${CClear}"
if [ -f "/opt/bin/tailscale" ]; then
#make mods to the S06tailscaled service for Userspace mode
if [ "$tsoperatingmode" == "Userspace" ]; then
sed -i "s/^ARGS=.*/ARGS=\"--tun=userspace-networking\ --state=\/opt\/var\/tailscaled.state\ --statedir=\/opt\/var\/lib\/tailscale\"/" "/opt/etc/init.d/S06tailscaled"
sed -i "s/^PREARGS=.*/PREARGS=\"nohup\"/" "/opt/etc/init.d/S06tailscaled"
sed -i -e '/^PRECMD=/d' "/opt/etc/init.d/S06tailscaled"
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Userspace Mode settings have been applied." >> $logfile
#remove firewall-start entry if found
if [ -f /jffs/scripts/firewall-start ]; then
if grep -q -F "if [ -x /opt/bin/tailscale ]; then tailscale down; tailscale up; fi" /jffs/scripts/firewall-start; then
sed -i -e '/tailscale down/d' /jffs/scripts/firewall-start
fi
fi
fi
else
echo ""
echo -e "${CRed}ERROR: Tailscale binary was not found. Please check Entware and router/drive for errors.${CClear}"
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - ERROR: Tailscale binaries not found on router. Please investigate." >> $logfile
exit 1
fi
echo ""
echo -e "${CGreen}Starting Tailscale service...${CClear}"
echo ""
/opt/etc/init.d/S06tailscaled start
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Service started." >> $logfile
echo ""
echo ""
echo -e "${CGreen}Starting Tailscale connection...${CClear}"
echo ""
echo -e "${CGreen}Please be prepared to copy and paste the link below into your browser, and connect this device"
echo -e "to your tailnet (Tailscale Network)${CClear}"
echo ""
advroutescmd="--advertise-routes=$routes"
echo -e "${CGreen}Executing: tailscale up $advroutescmd${CClear}"
echo ""
tailscale up $advroutescmd
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Connection started." >> $logfile
echo ""
echo ""
echo -e "${CGreen}Express Install Completed Successfully!${CClear}"
echo ""
read -rsp $'Press any key to continue...\n' -n1 key
exec sh /jffs/scripts/tailmon.sh -noswitch
echo -e "${CClear}"
exit 0
}
# -------------------------------------------------------------------------------------------------------------------------
# Install script
installts()
{
clear
echo -e "${InvGreen} ${InvDkGray}${CWhite} Install Tailscale ${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} This installer will download and install Tailscale from the Entware respository."
echo -e "${InvGreen} ${CClear} It will also check for any prerequisites before install commences."
echo -e "${InvGreen} ${CClear}${CDkGray}---------------------------------------------------------------------------------------${CClear}"
echo ""
echo -e "Install Tailscale?"
if promptyn "[y/n]: "
then
if [ -d "/opt" ]; then # Does entware exist? If yes proceed, if no error out.
echo ""
echo -e "\n${CGreen}Updating Entware Packages...${CClear}"
echo ""
opkg update
echo ""
echo -e "${CGreen}Installing Tailscale Package(s)...${CClear}"
echo ""
archker=$(opkg print-architecture | grep "armv7-2.6")
if [ -z "$archker" ]; then
opkg install tailscale
else
opkg install tailscale_nohf #install special tailscale package for arm7 kernel 2.6
fi
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Entware package installed." >> $logfile
echo ""
read -rsp $'Press any key to continue...\n' -n1 key
else
clear
echo -e "${CRed}ERROR: Entware was not found on this router...${CClear}"
echo -e "Please install Entware using the AMTM utility before proceeding..."
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - ERROR: Entware was not found on router. Please investigate." >> $logfile
echo ""
read -rsp $'Press any key to continue...\n' -n1 key
exit 1
fi
fi
resettimer=1
}
# -------------------------------------------------------------------------------------------------------------------------
# Uninstall script
uninstallts()
{
clear
echo -e "${InvGreen} ${InvDkGray}${CWhite} Uninstall Tailscale ${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} This uninstaller utility will remove Tailscale Entware packages from your router"
echo -e "${InvGreen} ${CClear} along with all files and modifications made to Tailscale."
echo -e "${InvGreen} ${CClear}${CDkGray}---------------------------------------------------------------------------------------${CClear}"
echo ""
echo -e "Uninstall Tailscale?"
if promptyn "[y/n]: "
then
if [ -f /opt/bin/tailscale ]; then
if [ -d "/opt" ]; then # Does entware exist? If yes proceed, if no error out.
echo ""
echo -e "\n${CGreen}Shutting down Tailscale...${CClear}"
tailscale logout
tailscale down
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Connection shut down and logged out." >> $logfile
/opt/etc/init.d/S06tailscaled stop
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Service shut down." >> $logfile
echo ""
echo -e "\n${CGreen}Removing firewall-start entries...${CClear}"
#remove firewall-start entry if found
if [ -f /jffs/scripts/firewall-start ]; then
if grep -q -F "if [ -x /opt/bin/tailscale ]; then tailscale down; tailscale up; fi" /jffs/scripts/firewall-start; then
sed -i -e '/tailscale down/d' /jffs/scripts/firewall-start
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: firewall-start entries removed." >> $logfile
fi
fi
echo ""
echo -e "\n${CGreen}Updating Entware Packages...${CClear}"
echo ""
opkg update
echo ""
echo -e "${CGreen}Uninstalling Entware Tailscale Package(s)...${CClear}"
echo ""
archker=$(opkg print-architecture | grep "armv7-2.6")
if [ -z "$archker" ]; then
opkg remove tailscale
else
opkg remove tailscale_nohf #remove special tailscale package for arm7 kernel 2.6
fi
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Entware package removed." >> $logfile
# Removed the various folders tailscale could hide
rm -f /opt/var/tailscaled.state >/dev/null 2>&1
rm -r /opt/var/lib/tailscale >/dev/null 2>&1
rm -r /opt/var/run/tailscale >/dev/null 2>&1
rm -r /var/run/tailscale >/dev/null 2>&1
rm -r /var/lib/tailscale >/dev/null 2>&1
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale files and folders removed." >> $logfile
echo ""
read -rsp $'Press any key to continue...\n' -n1 key
else
clear
echo -e "${CRed}ERROR: Entware was not found on this router...${CClear}"
echo -e "Please install Entware using the AMTM utility before proceeding..."
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - ERROR: Entware not found on router. Please investigate." >> $logfile
echo ""
read -rsp $'Press any key to continue...\n' -n1 key
exit 1
fi
else
echo ""
echo -e "\n${CGreen}Tailscale was not found installed on this router.${CClear}"
echo ""
read -rsp $'Press any key to continue...\n' -n1 key
fi
fi
resettimer=1
}
# -------------------------------------------------------------------------------------------------------------------------
# start service script
startts()
{
printf "\33[2K\r"
printf "${CGreen}\r[Starting Tailscale Service]"
sleep 1
printf "\33[2K\r"
echo -e "${CGreen}Messages:"
echo ""
/opt/etc/init.d/S06tailscaled start
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Service started." >> $logfile
echo ""
resettimer=1
}
# -------------------------------------------------------------------------------------------------------------------------
# stop service script
stopts()
{
printf "\33[2K\r"
printf "${CGreen}\r[Stopping Tailscale Service]"
sleep 1
printf "\33[2K\r"
echo -e "${CGreen}Messages:"
echo ""
/opt/etc/init.d/S06tailscaled stop
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Service stopped." >> $logfile
echo ""
resettimer=1
}
# -------------------------------------------------------------------------------------------------------------------------
# restart service and connection
restarttsc()
{
printf "\33[2K\r"
printf "${CGreen}\r[Restarting Tailscale Service/Connection]${CClear}"
sleep 1
tsdown
stopts
#make mods to the S06tailscaled service for Userspace mode
if [ "$tsoperatingmode" == "Userspace" ]; then
applyuserspacemode
#make mods to the S06tailscaled service for Kernel mode
elif [ "$tsoperatingmode" == "Kernel" ]; then
applykernelmode
#make mods to the S06tailscaled service for Custom mode
elif [ "$tsoperatingmode" == "Custom" ]; then
applycustomchanges
fi
startts
tsup
echo ""
printf "\33[2K\r"
printf "${CGreen}\r[Tailscale Service/Connection Successfully Restarted]${CClear}"
echo -e "\n"
read -rsp $'Press any key to continue...\n' -n1 key
}
# -------------------------------------------------------------------------------------------------------------------------
# Tailscale reset connection routine
tsreset()
{
printf "\33[2K\r"
printf "${CGreen}\r[Initiating Forced Tailscale Connection Reset]"
sleep 1
echo -e "\n"
echo -e "${CRed}WARNING:${CClear} Executing this function will send a 'tailscale up --reset' command which "
echo -e "will reset any default switches that are configured on your Tailscale connection. "
echo -e "This action may be necessary at times when these switches are inadvertently set and "
echo -e "registered with Tailscale, or due to switch functionality being altered or changed "
echo -e "by the Tailscale developers themselves. Once the '--reset' switch has been sent, "
echo -e "TAILMON will reinitialize the connection back to its regular defaults."
echo ""
echo -e "${CRed}PLEASE NOTE:${CClear} If you have configured any custom commandline switches that you want "
echo -e "to reset, you would need to run your own custom Tailscale command in a separate "
echo -e "prompt to disable the switch that is currently enabled. Please know that the switch "
echo -e "itself is not removed, but basically disabled. Please consider finding more info at "
echo -e "the https://tailscale.com/kb site for other references. Examples: "
echo -e "${CGreen}tailscale up --accept-routes=false${CClear} -or-"
echo -e "${CGreen}tailscale up --advertise-routes=${CClear}"
echo ""
echo -e "Reset Tailscale Connection?"
if promptyn "[y/n]: "
then
echo -e "\n"
tsdown
echo "Executing: tailscale up --reset"
echo ""
tailscale up --reset
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Connection Reset using --reset switch." >> $logfile
echo ""
tsdown
tsup
echo ""
printf "\33[2K\r"
printf "${CGreen}\r[Tailscale Connection Successfully Reset]${CClear}"
echo -e "\n"
read -rsp $'Press any key to continue...\n' -n1 key
fi
}
# -------------------------------------------------------------------------------------------------------------------------
# Tailscale connection reset
tsresetc()
{
printf "\33[2K\r"
printf "${CGreen}\r[Resetting Tailscale Connection]"
sleep 1
printf "\33[2K\r"
echo -e "${CGreen}Messages:${CClear}"
echo ""
echo "Executing: tailscale up --reset"
echo ""
tailscale up --reset
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Connection Reset using --reset switch." >> $logfile
resettimer=1
echo -e "\n"
read -rsp $'Press any key to continue...\n' -n1 key
}
# -------------------------------------------------------------------------------------------------------------------------
# Tailscale connection up
tsup()
{
printf "\33[2K\r"
printf "${CGreen}\r[Activating Tailscale Connection]"
sleep 1
printf "\33[2K\r"
if [ $exitnode -eq 1 ]; then exitnodecmd="--advertise-exit-node "; else exitnodecmd=""; fi
if [ $advroutes -eq 1 ]; then advroutescmd="--advertise-routes=$routes "; else advroutescmd=""; fi
if [ $accroutes -eq 1 ]; then accroutescmd="--accept-routes"; else accroutescmd=""; fi
echo -e "${CGreen}Messages:${CClear}"
echo ""
if [ "$tsoperatingmode" == "Custom" ]; then
echo "Executing: tailscale up $customcmdline"
echo ""
tailscale up $customcmdline
else
echo "Executing: tailscale up $exitnodecmd$advroutescmd$accroutescmd"
echo ""
tailscale up $exitnodecmd$advroutescmd$accroutescmd
fi
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Connection started." >> $logfile
resettimer=1
}
# -------------------------------------------------------------------------------------------------------------------------
# Tailscale connection down
tsdown()
{
printf "\33[2K\r"
printf "${CGreen}\r[Bringing Tailscale Connection Down]"
sleep 1
printf "\33[2K\r"
echo -e "${CGreen}Messages:${CClear}"
echo ""
echo "Executing: tailscale down"
echo ""
tailscale down
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale Connection stopped." >> $logfile
resettimer=1
}
# -------------------------------------------------------------------------------------------------------------------------
# Force Tailscale Binary update
tsupdate()
{
printf "\33[2K\r"
printf "${CGreen}\r[Updating Tailscale Binary]"
sleep 1
printf "\33[2K\r"
echo -e "${CGreen}Messages:${CClear}"
echo ""
echo "Executing: tailscale update"
echo ""
tailscale update
echo ""
echo -e "Restart Tailscale?"
if promptyn "[y/n]: "
then
restarttsc
fi
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Tailscale binary updated to latest available version." >> $logfile
resettimer=1
}
# -------------------------------------------------------------------------------------------------------------------------
# autostart lets you enable the ability for tailmon to autostart after a router reboot
autostart()
{
while true; do
clear
echo -e "${InvGreen} ${InvDkGray}${CWhite} Reboot Protection ${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} Please indicate below if you would like to enable TAILMON to autostart after a"
echo -e "${InvGreen} ${CClear} router reboot. This will ensure continued, uninterrupted Tailscale monitoring."
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} (Default = Disabled)"
echo -e "${InvGreen} ${CClear}${CDkGray}---------------------------------------------------------------------------------------${CClear}"
echo -e "${InvGreen} ${CClear}"
if [ "$autostart" == "0" ]; then
echo -e "${InvGreen} ${CClear} Current: ${CRed}Disabled${CClear}"
elif [ "$autostart" == "1" ]; then
echo -e "${InvGreen} ${CClear} Current: ${CGreen}Enabled${CClear}"
fi
echo ""
read -p 'Enable Reboot Protection? (0=No, 1=Yes, e=Exit): ' autostart1
if [ "$autostart1" == "" ] || [ -z "$autostart1" ]; then autostart=0; else autostart="$autostart1"; fi # Using default value on enter keypress
if [ "$autostart" == "0" ]; then
if [ -f /jffs/scripts/post-mount ]; then
sed -i -e '/tailmon.sh/d' /jffs/scripts/post-mount
autostart=0
echo ""
echo -e "${CGreen}[Modifying POST-MOUNT file]..."
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Reboot Protection Disabled" >> $logfile
saveconfig
sleep 1
timer=$timerloop
break
fi
elif [ "$autostart" == "1" ]; then
if [ -f /jffs/scripts/post-mount ]; then
if ! grep -q -F "(sleep 30 && /jffs/scripts/tailmon.sh -screen) & # Added by tailmon" /jffs/scripts/post-mount; then
echo "(sleep 30 && /jffs/scripts/tailmon.sh -screen) & # Added by tailmon" >> /jffs/scripts/post-mount
autostart=1
echo ""
echo -e "${CGreen}[Modifying POST-MOUNT file]..."
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Reboot Protection Enabled" >> $logfile
saveconfig
sleep 1
timer=$timerloop
break
else
autostart=1
saveconfig
sleep 1
fi
else
echo "#!/bin/sh" > /jffs/scripts/post-mount
echo "" >> /jffs/scripts/post-mount
echo "(sleep 30 && /jffs/scripts/tailmon.sh -screen) & # Added by tailmon" >> /jffs/scripts/post-mount
chmod 755 /jffs/scripts/post-mount
autostart=1
echo ""
echo -e "${CGreen}[Modifying POST-MOUNT file]..."
echo -e "$(date +'%b %d %Y %X') $($timeoutcmd$timeoutsec nvram get lan_hostname) TAILMON[$$] - INFO: Reboot Protection Enabled" >> $logfile
saveconfig
sleep 1
timer=$timerloop
break
fi
elif [ "$autostart" == "e" ]; then
timer=$timerloop
break
else
autostart=0
saveconfig
fi
done
}
# -------------------------------------------------------------------------------------------------------------------------
# timerloopconfig lets you configure how long you want the timer cycle to last between tailscale checks
timerloopconfig()
{
while true; do
clear
echo -e "${InvGreen} ${InvDkGray}${CWhite} Timer Loop Configuration ${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} Please indicate how long the timer cycle should take between Tailscale Service and.${CClear}"
echo -e "${InvGreen} ${CClear} Connection checks."
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} (Default = 60 seconds)${CClear}"
echo -e "${InvGreen} ${CClear}${CDkGray}---------------------------------------------------------------------------------------${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} Current: ${CGreen}$timerloop sec${CClear}"
echo ""
read -p "Please enter value (1-999)? (e=Exit): " EnterTimerLoop
case $EnterTimerLoop in
[1-9])
timerloop=$EnterTimerLoop
saveconfig
timer=$timerloop
;;
[1-9][0-9])
timerloop=$EnterTimerLoop
saveconfig
timer=$timerloop
;;
[1-9][0-9][0-9])
timerloop=$EnterTimerLoop
saveconfig
timer=$timerloop
;;
*)
echo ""
echo -e "${CClear}[Exiting]"
timer=$timerloop
break
;;
esac
done
}
# -------------------------------------------------------------------------------------------------------------------------
# customconfig lets you edit the args and settings for tailscale
customconfig()
{
restartts=0
while true; do
clear
echo -e "${InvGreen} ${InvDkGray}${CWhite} Custom Tailscale Configuration ${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} This functionality allows you to choose your own Tailscale ARGS, PREARGS and PRECMD${CClear}"
echo -e "${InvGreen} ${CClear} entries, and allows you to modify the Tailscale connection commandline options.${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} ${CYellow}Proceed at your own risk!${CClear}"
echo -e "${InvGreen} ${CClear}${CDkGray}---------------------------------------------------------------------------------------${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} Current Operating Mode: ${CGreen}$tsoperatingmode${CClear}"
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} Current values in Tailscale Service (/opt/etc/init.d/S06tailscaled):${CClear}"
s06args=$(cat /opt/etc/init.d/S06tailscaled | grep ^ARGS= | cut -d '=' -f 2-) 2>/dev/null
if [ -z "$s06args" ]; then
echo -e "${InvGreen} ${CClear} ${InvDkGray}${CWhite}(1)${CClear} ${CGreen}ARGS=\"\"${CClear}"
else
echo -e "${InvGreen} ${CClear} ${InvDkGray}${CWhite}(1)${CClear} ${CGreen}ARGS=$s06args${CClear}"
fi
s06preargs=$(cat /opt/etc/init.d/S06tailscaled | grep ^PREARGS= | cut -d '=' -f 2-) 2>/dev/null
if [ -z "$s06preargs" ]; then
echo -e "${InvGreen} ${CClear} ${InvDkGray}${CWhite}(2)${CClear} ${CGreen}PREARGS=\"\"${CClear}"
else
echo -e "${InvGreen} ${CClear} ${InvDkGray}${CWhite}(2)${CClear} ${CGreen}PREARGS=$s06preargs${CClear}"
fi
s06precmd=$(cat /opt/etc/init.d/S06tailscaled | grep ^PRECMD= | cut -d '=' -f 2-) 2>/dev/null
if [ -z "$s06precmd" ]; then
echo -e "${InvGreen} ${CClear} ${InvDkGray}${CWhite}(3)${CClear} ${CGreen}PRECMD=\"\"${CClear}"
else
echo -e "${InvGreen} ${CClear} ${InvDkGray}${CWhite}(3)${CClear} ${CGreen}PRECMD=$s06precmd${CClear}"
fi
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear} Current custom values being used for Tailscale Connection commandline:${CClear}"
if [ -z "$customcmdline" ]; then
echo -e "${InvGreen} ${CClear} ${InvDkGray}${CWhite}(4)${CClear} ${CGreen}CMD=\"\"${CClear}"
else
echo -e "${InvGreen} ${CClear} ${InvDkGray}${CWhite}(4)${CClear} ${CGreen}CMD=\"$customcmdline\"${CClear}"
fi
echo -e "${InvGreen} ${CClear}"
echo -e "${InvGreen} ${CClear}${CDkGray}---------------------------------------------------------------------------------------${CClear}"
echo ""
read -p "Please enter item to modify (1-4)? (e=Exit): " EnterTimerLoop
case $EnterTimerLoop in
1)
echo ""
echo -e "${CClear}When entering a custom statement, please do not use quotes or other abnormal characters."
echo -e "${CClear}Example: --tun=userspace-networking --state=/opt/var/tailscaled.state --statedir=/opt/var/lib/tailscale"
echo ""
read -p "Enter new ARGS= " EnterNewArgs
tsoperatingmode="Custom"
args=$EnterNewArgs
args_regexp="$(printf '%s' "$args" | sed -e 's/[]\/$*.^|[]/\\&/g' | sed ':a;N;$!ba;s,\n,\\n,g')"
sed -i "s/^ARGS=.*/ARGS=\"$args_regexp\"/" "/opt/etc/init.d/S06tailscaled"
saveconfig
timer=$timerloop
restartts=1
;;
2)
echo ""
echo -e "${CClear}When entering a custom statement, please do not use quotes or other abnormal characters."
echo -e "${CClear}Example: nohup"
echo ""
read -p "Enter new PREARGS= " EnterNewPreArgs
tsoperatingmode="Custom"
preargs=$EnterNewPreArgs
preargs_regexp="$(printf '%s' "$preargs" | sed -e 's/[]\/$*.^|[]/\\&/g' | sed ':a;N;$!ba;s,\n,\\n,g')"
sed -i "s/^PREARGS=.*/PREARGS=\"$preargs_regexp\"/" "/opt/etc/init.d/S06tailscaled"
saveconfig
timer=$timerloop
restartts=1
;;
3)
echo ""
echo -e "${CClear}When entering a custom statement, please do not use quotes or other abnormal characters."
echo -e "${CClear}Example: modprobe tun"
echo ""
read -p "Enter new PRECMD= " EnterNewPreCmd
tsoperatingmode="Custom"
precmd=$EnterNewPreCmd
precmd_regexp="$(printf '%s' "$precmd" | sed -e 's/[]\/$*.^|[]/\\&/g' | sed ':a;N;$!ba;s,\n,\\n,g')"
if ! grep -q -F "PRECMD=" /opt/etc/init.d/S06tailscaled; then
sed '5 i PRECMD=\"'"$precmd_regexp"'\"' /opt/etc/init.d/S06tailscaled > /opt/etc/init.d/S06tailscaled2
rm -f /opt/etc/init.d/S06tailscaled
mv /opt/etc/init.d/S06tailscaled2 /opt/etc/init.d/S06tailscaled
chmod 755 /opt/etc/init.d/S06tailscaled
else
sed -i "s/^PRECMD=.*/PRECMD=\"$precmd_regexp\"/" "/opt/etc/init.d/S06tailscaled"
fi
saveconfig
timer=$timerloop
restartts=1
;;
4)
echo ""
echo -e "${CClear}When entering a custom statement, please do not use quotes or other abnormal characters."
echo -e "${CClear}Example: --advertise-exit-node --advertise-routes=192.168.50.0/24,192.168.87.0/24"
echo ""
read -p "Enter new Commandline Options: " EnterNewCmdOptions
tsoperatingmode="Custom"
customcmdline=$EnterNewCmdOptions
saveconfig
timer=$timerloop
restartts=1
;;
*)
if [ -f "/opt/bin/tailscale" ]; then
if [ $restartts -eq 1 ]; then
echo ""
echo -e "Changing custom configuration options will require a restart of Tailscale. Restart now?"
if promptyn "[y/n]: "
then
echo ""
echo -e "\n${CGreen}Restarting Tailscale Service and Connection...${CClear}"