-
Notifications
You must be signed in to change notification settings - Fork 18
/
installer
1400 lines (1351 loc) · 57.7 KB
/
installer
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
#################################################################################################################
# █████╗ ███████╗██╗ ██╗███████╗██╗ ██╗██████╗ ████████╗ ███╗ ███╗███████╗██████╗ ██╗ ██╗███╗ ██╗#
#██╔══██╗██╔════╝██║ ██║██╔════╝██║ ██║██╔══██╗╚══██╔══╝ ████╗ ████║██╔════╝██╔══██╗██║ ██║████╗ ██║#
#███████║███████╗██║ ██║███████╗██║ █╗ ██║██████╔╝ ██║█████╗██╔████╔██║█████╗ ██████╔╝██║ ██║██╔██╗ ██║#
#██╔══██║╚════██║██║ ██║╚════██║██║███╗██║██╔══██╗ ██║╚════╝██║╚██╔╝██║██╔══╝ ██╔══██╗██║ ██║██║╚██╗██║#
#██║ ██║███████║╚██████╔╝███████║╚███╔███╔╝██║ ██║ ██║ ██║ ╚═╝ ██║███████╗██║ ██║███████╗██║██║ ╚████║#
#╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚══════╝ ╚══╝╚══╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚══════╝╚═╝╚═╝ ╚═══╝#
# █████╗ ██████╗ ██████╗ ██╗ ██╗ █████╗ ██████╗ ██████╗ ██╗ ██╗ ██████╗ ███╗ ███╗███████╗ #
#██╔══██╗██╔══██╗██╔════╝ ██║ ██║██╔══██╗██╔══██╗██╔══██╗██║ ██║██╔═══██╗████╗ ████║██╔════╝ #
#███████║██║ ██║██║ ███╗██║ ██║███████║██████╔╝██║ ██║███████║██║ ██║██╔████╔██║█████╗ #
#██╔══██║██║ ██║██║ ██║██║ ██║██╔══██║██╔══██╗██║ ██║██╔══██║██║ ██║██║╚██╔╝██║██╔══╝ #
#██║ ██║██████╔╝╚██████╔╝╚██████╔╝██║ ██║██║ ██║██████╔╝██║ ██║╚██████╔╝██║ ╚═╝ ██║███████╗ #
#╚═╝ ╚═╝╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ #
#██╗███╗ ██╗███████╗████████╗ █████╗ ██╗ ██╗ ███████╗██████╗ Author: #
#██║████╗ ██║██╔════╝╚══██╔══╝██╔══██╗██║ ██║ ██╔════╝██╔══██╗ SomeWhereOverTheRainBow #
#██║██╔██╗ ██║███████╗ ██║ ███████║██║ ██║ █████╗ ██████╔╝ Contributors: #
#██║██║╚██╗██║╚════██║ ██║ ██╔══██║██║ ██║ ██╔══╝ ██╔══██╗ JackYaz,gspannu,thelonelycoder #
#██║██║ ╚████║███████║ ██║ ██║ ██║███████╗███████╗███████╗██║ ██║ v1.7.4 #
#╚═╝╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝ #
#################################################################################################################
# shellcheck disable=SC2016
# shellcheck disable=SC3043
# shellcheck disable=SC3045
# shellcheck disable=SC3057
export LC_ALL=C
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:${PATH}"
AI_VERSION="v1.7.4"
export AI_VERSION
readonly LATEST_URL="https://api.github.com/repos/AdguardTeam/AdGuardHome/releases"
varcnt=0
until [ -n "${REMOTE_VER}" ] && [ -n "${REMOTE_BETA}" ]; do
[ -z "${REMOTE_VER}" ] && REMOTE_VER="$(curl --retry 3 --connect-timeout 3 --retry-delay 1 --max-time $((3 * 5)) --retry-connrefused -sL "${LATEST_URL}" | sed -n '/"prerelease": false,/q;p' | tail -4 | grep "tag_name" | cut -d \" -f 4 &)" || true
[ -z "${REMOTE_BETA}" ] && REMOTE_BETA="$(curl --retry 3 --connect-timeout 3 --retry-delay 1 --max-time $((3 * 5)) --retry-connrefused -sL "${LATEST_URL}" | sed -n '/"prerelease": true,/q;p' | tail -4 | grep "tag_name" | cut -d \" -f 4 &)" || true
varcnt="$((varcnt + 1))"
if [ "${varcnt}" -le 84 ]; then
wait
else
printf "%s/n" "One or more critical variables could not be set in a timely manner." "Please check your internet connection, or try again later." "The installer script will now exit."
sleep 3s
exit 1
fi
done
wait
unset varcnt
readonly REMOTE_VER REMOTE_BETA
readonly ADDON_DIR="/jffs/addons/AdGuardHome.d"
readonly BASE_DIR="/opt/etc"
readonly TARG_DIR="${BASE_DIR}/AdGuardHome"
readonly AGH_FILE="${TARG_DIR}/AdGuardHome"
readonly CONF_FILE="${TARG_DIR}/.config"
readonly YAML_FILE="${AGH_FILE}.yaml"
readonly YAML_BAK="${YAML_FILE}.bak"
readonly YAML_ERR="${YAML_FILE}.err"
readonly YAML_ORI="${TARG_DIR}/.AdGuardHome.yaml.ori"
SCRIPT_LOC="$(readlink -f "$0")" || true
readonly SCRIPT_LOC
BOLD="$(printf "\033[1m")" || true
readonly BOLD
NORM="$(printf "\033[0m")" || true
readonly NORM
INFO="$(printf "%s" "${BOLD} Info: ${NORM}")" || true
readonly INFO
ERROR="$(printf "%s" "${BOLD} *** Error: ${NORM}")" || true
readonly ERROR
WARNING="$(printf "%s" "${BOLD} * Warning: ${NORM}")" || true
readonly WARNING
INPUT="$(printf "%s" "${BOLD} => ${NORM}")" || true
readonly INPUT
_quote() {
printf "%s\n" "$1" | sed 's/[]\/()$*.^|[]/\\&/g'
}
PTXT() {
case "$1" in
-n)
shift
while [ $# -gt 0 ]; do
printf "%s" "$1"
shift
done
;;
*)
while [ $# -gt 0 ]; do
printf "%s\n" "$1"
shift
done
;;
esac
}
AdGuardHome_authen() {
if [ -z "${PW1}" ] || [ -z "${PW2}" ]; then
local USERNAME
PTXT -n "${INPUT} Please enter AdGuardHome username${NORM}: "
read -r USERNAME
fi
local PW1 PW2
PTXT -n "${INPUT} Please enter AdGuardHome password${NORM}: "
read -rs PW1
PTXT " "
PTXT -n "${INPUT} Please reenter AdGuardHome password${NORM}: "
read -rs PW2
PTXT " "
if [ -z "${PW1}" ] || [ -z "${PW2}" ] || [ "${PW1}" != "${PW2}" ]; then
PTXT "${ERROR} Password entered incorrectly!"
AdGuardHome_authen "$1"
fi
opkg install python3 python3-pip python3-bcrypt >/dev/null 2>&1
if opkg list-installed | grep -q apache; then
opkg flag user apache apache-utils >/dev/null 2>&1
opkg remove apache apache-utils --force-removal-of-dependent-packages >/dev/null 2>&1
fi
if ! opkg list-installed | grep -q python3-bcrypt; then
case "$(/bin/uname -m)" in
"aarch64" | "arm64")
opkg install go >/dev/null 2>&1
;;
"armv7l" | *)
if ! opkg list | grep -qw 'go_nohf'; then opkg install go >/dev/null 2>&1; else opkg install go_nohf >/dev/null 2>&1; fi
;;
esac
export PATH="${PATH}:/opt/bin/go/bin"
export GOROOT="/opt/bin/go"
export GOBIN="/opt/bin/"
go install gophers.dev/cmds/bcrypt-tool@latest >/dev/null 2>&1
rm -rf go
else
pip3 install bcrypt >/dev/null 2>&1
fi
local PW1_ENCRYPTED
if opkg list-installed | grep -q python3-bcrypt; then PW1_ENCRYPTED="$(python -c 'import bcrypt; password = b"'"${PW1}"'"; print(bcrypt.hashpw(password, bcrypt.gensalt(prefix=b"2a", rounds=10)).decode("ascii"))')"; elif [ -f "/opt/bin/bcrypt-tool" ]; then PW1_ENCRYPTED="$(/opt/bin/bcrypt-tool hash "${PW1}" 10)"; else
PTXT "${ERROR} Password could not be set!" "${ERROR} Please contact dev."
end_op_message 1
return
fi
if [ "$1" -eq 0 ]; then
yaml_nvars_delete "- name:" "${YAML_FILE}"
yaml_nvars_delete " password:" "${YAML_FILE}"
yaml_nvars_insert "users:" "\ password: ${PW1_ENCRYPTED}" "${YAML_FILE}"
yaml_nvars_insert "users:" "- name: ${USERNAME}" "${YAML_FILE}"
check_AdGuardHome_yaml "${YAML_FILE}"
PTXT "${INFO} Starting AdGuardHome..."
(
if [ -n "$(pidof AdGuardHome)" ]; then { if { service stop_AdGuardHome >/dev/null 2>&1; }; then sleep 1s; elif { /opt/etc/init.d/S99AdGuardHome stop; }; then sleep 1s; elif { /opt/etc/init.d/S99AdGuardHome kill; }; then sleep 1s; else
{ killall -q -9 AdGuardHome 2>/dev/null; }
rm -rf /opt/var/run/AdGuardHome.pid 2>/dev/null
sleep 1s
fi; }; fi
sleep 1s
) &
local PID="$!"
wait "${PID}" 2>/dev/null
({ until [ "$(pidof AdGuardHome S99AdGuardHome | wc -w)" -lt "1" ]; do sleep 1s; done; }) &
local PID="$!"
wait "${PID}" 2>/dev/null
sleep 1s
{ /opt/etc/init.d/S99AdGuardHome check; }
(
if [ -z "$(pidof AdGuardHome)" ]; then { if { service start_AdGuardHome >/dev/null 2>&1; }; then sleep 1s; elif { /opt/etc/init.d/S99AdGuardHome start; }; then sleep 1s; else
PTXT "${ERROR} Couldn't start AdGuardHome!" "${ERROR} Please send WebUI System Log to dev."
end_op_message 1
return
fi; }; fi
sleep 1s
) &
local PID="$!"
wait "${PID}" 2>/dev/null
({ until [ "$(pidof AdGuardHome S99AdGuardHome | wc -w)" -eq "2" ]; do sleep 1s; done; }) &
local PID="$!"
wait "${PID}" 2>/dev/null
sleep 1s
{ /opt/etc/init.d/S99AdGuardHome check; }
if [ -z "$(pidof AdGuardHome)" ]; then
PTXT "${ERROR} Couldn't start AdGuardHome!" \
"${ERROR} Please send WebUI System Log to dev."
end_op_message 1
return
fi
PTXT "${INFO} Please wait while we perform one last check."
(
if [ -n "$(pidof AdGuardHome)" ]; then { if { service restart_AdGuardHome >/dev/null 2>&1; }; then sleep 1s; elif { /opt/etc/init.d/S99AdGuardHome restart; }; then sleep 1s; else
PTXT "${ERROR} Couldn't start AdGuardHome!" "${ERROR} Please send WebUI System Log to dev."
end_op_message 1
return
fi; }; fi
sleep 1s
) &
local PID="$!"
wait "${PID}" 2>/dev/null
({ until { check_connection && [ "$(pidof AdGuardHome S99AdGuardHome | wc -w)" -eq "2" ]; }; do sleep 1s; done; }) &
local PID="$!"
wait "${PID}" 2>/dev/null
sleep 1s
{ /opt/etc/init.d/S99AdGuardHome check; }
PTXT "${INFO} AdGuardHome setup is complete."
end_op_message 0
else
PTXT "users:" \
"- name: ${USERNAME}" \
" password: ${PW1_ENCRYPTED}" >>"${YAML_ORI}"
fi
}
backup_restore() {
if [ "$1" = "BACKUP" ] && [ -d "${TARG_DIR}" ] && [ -f "${AGH_FILE}" ]; then
if [ -f "${BASE_DIR}/backup_AdGuardHome.tar.gz" ]; then
PTXT "${INFO} There is an old backup detected."
local USE_OLD
if read_yesno "Do you want to continue?(this will remove the old backup)"; then USE_OLD="NO"; else USE_OLD="YES"; fi
if [ "${USE_OLD}" = "YES" ]; then
PTXT "${INFO} Leaving Old Backup."
end_op_message 1
elif [ "${USE_OLD}" = "NO" ]; then
PTXT "${INFO} Removing Old Backup."
rm -rf "${BASE_DIR}/backup_AdGuardHome.tar.gz"
fi
fi
PTXT "${INFO} This operation will backup AdGuardHome(<4MB)to ENTWARE /opt/etc." \
"${INFO} Please wait a moment."
tar -czvf "${BASE_DIR}/backup_AdGuardHome.tar.gz" -C "${TARG_DIR}" ../AdGuardHome/ >/dev/null 2>&1
PTXT "${INFO} Backup complete"
[ -z "$2" ] && end_op_message 0 || return 0
elif [ "$1" = "BACKUP" ] && [ ! -d "${TARG_DIR}" ] && [ ! -f "${AGH_FILE}" ]; then
PTXT "${ERROR} No ${AGH_FILE} to Backup!"
end_op_message 1
fi
if [ -f "${BASE_DIR}/backup_AdGuardHome.tar.gz" ] && [ "$1" = "RESTORE" ]; then
PTXT "${INFO} Please wait a moment."
tar -xzvf "${BASE_DIR}/backup_AdGuardHome.tar.gz" -C "${BASE_DIR}" >/dev/null 2>&1
chown "$(nvram get http_username)":root ${TARG_DIR}/*
chmod 755 "${AGH_FILE}"
chmod 644 "${YAML_FILE}"
[ -f "/opt/sbin/AdGuardHome" ] && rm -rf /opt/sbin/AdGuardHome
ln -sf "${AGH_FILE}" /opt/sbin/AdGuardHome
inst_AdGuardHome "${1:-RESTORE}"
elif [ ! -f "${BASE_DIR}/backup_AdGuardHome.tar.gz" ] && [ "$1" = "RESTORE" ]; then
PTXT "${ERROR} No Backup found!" \
"${ERROR} Please make sure Backup Resides in ${BASE_DIR}"
end_op_message 1
return
fi
}
check_AdGuardHome_yaml() {
[ ! -f "${YAML_FILE}" ] && return
chmod 644 "${YAML_FILE}"
PTXT "${INFO} Checking AdGuardHome configuration..."
if ! "${AGH_FILE}" --check-config -c "${YAML_FILE}" --no-check-update -l "/dev/null"; then
PTXT "${INFO} Moving invalid configuration file to ${YAML_ERR}." \
"${INFO} Operation will continue with clean config file."
mv "${YAML_FILE}" "${YAML_ERR}"
return 1
fi
}
########################Modified Version of @Adamm Check_Connection####################################################
check_connection() {
local livecheck="0" i
while [ "${livecheck}" != "4" ]; do
for i in google.com github.com snbforums.com; do
if { ! nslookup "${i}" 127.0.0.1 >/dev/null 2>&1; } && { ping -q -w3 -c1 "${i}" >/dev/null 2>&1; }; then
if { ! curl --retry 3 --connect-timeout 3 --retry-delay 1 --max-time $((3 * 5)) --retry-connrefused -Is "http://${i}" | head -n 1 >/dev/null 2>&1; } || { ! wget --no-cache --no-cookies --tries=3 --timeout=3 --waitretry=1 --retry-connrefused -q --spider "http://${i}" >/dev/null 2>&1; }; then
sleep 1s
continue
fi
fi
return 0
done
livecheck="$((livecheck + 1))"
if [ "${livecheck}" != "4" ]; then
sleep 10s
continue
fi
return 1
done
}
########################################################################################################################
check_dns_environment() {
if [ -f "/opt/etc/init.d/S61stubby" ] || [ -f "/opt/sbin/stubby" ] || [ -f "/opt/bin/install_stubby" ] || [ -f "/jffs/scripts/install_stubby.sh" ] || [ -d "/jffs/dnscrypt" ] || [ -f "/opt/etc/init.d/S09dnscrypt-proxy" ]; then
PTXT "${ERROR} Potential stubby or dnscrypt-proxy installation detected." \
"${ERROR} Please remove before attempting to continue." \
"${ERROR} Exiting..."
exit 1
fi
local NVCHECK
NVCHECK="0"
if [ "$(nvram get dnspriv_enable)" != "0" ]; then
{ nvram set dnspriv_enable="0"; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(pidof stubby)" ]; then
{ killall -q -9 stubby; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dhcp_dns1_x)" ] && [ "${NVCHECK}" != "0" ]; then
{ nvram set dhcp_dns1_x=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dhcp_dns2_x)" ] && [ "${NVCHECK}" != "0" ]; then
{ nvram set dhcp_dns2_x=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dhcpd_dns_router)" != "1" ] && [ "${NVCHECK}" != "0" ]; then
{ nvram set dhcpd_dns_router="1"; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "${NVCHECK}" != "0" ]; then
{ nvram commit; }
{ service restart_dnsmasq >/dev/null 2>&1; }
(while { ! check_connection; }; do sleep 1s; done) &
local PID="$!"
wait "${PID}" 2>/dev/null
fi
PTXT "${INFO} DNS Environment is Ready."
}
check_dns_filter() {
local NVCHECK USE_SOME
NVCHECK="0"
if [ "$1" -eq 0 ]; then
if [ "$(nvram get dnsfilter_enable_x)" -ne 0 ]; then
{ nvram set dnsfilter_enable_x="0"; }
NVCHECK="$((NVCHECK + 1))"
fi
PTXT "${INFO} DNS will not be forced through to AdGuardHome."
fi
if [ "$1" -eq 1 ]; then
if [ "$(nvram get dnsfilter_enable_x)" -ne 1 ]; then
{ nvram set dnsfilter_enable_x="1"; }
NVCHECK="$((NVCHECK + 1))"
fi
PTXT "${INFO} You can choose to keep any custom dnsfilter values by only redirect non-custom traffic or send all traffic through to AdGuardHome."
if read_yesno "Do you want to redirect only NON-CUSTOM DNS resolutions on your network through to AdGuardHome?"; then USE_SOME="0"; else USE_SOME="1"; fi
if [ "$USE_SOME" -eq 0 ]; then
if [ "$(nvram get dnsfilter_mode)" != "11" ]; then
{ nvram set dnsfilter_mode="11"; }
NVCHECK="$((NVCHECK + 1))"
fi
PTXT "${INFO} DNSFilter is set to control DNS through to AdGuardHome, while leaving any Custom Rules and Values."
fi
if [ "$USE_SOME" -eq 1 ]; then
if [ "$(nvram get dnsfilter_custom1)" ]; then
{ nvram set dnsfilter_custom1=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dnsfilter_custom2)" ]; then
{ nvram set dnsfilter_custom2=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dnsfilter_custom3)" ]; then
{ nvram set dnsfilter_custom3=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dnsfilter_mode)" != "11" ]; then
{ nvram set dnsfilter_mode="11"; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dnsfilter_rulelist)" ]; then
{ nvram set dnsfilter_rulelist=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dnsfilter_rulelist1)" ]; then
{ nvram set dnsfilter_rulelist1=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dnsfilter_rulelist2)" ]; then
{ nvram set dnsfilter_rulelist2=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dnsfilter_rulelist3)" ]; then
{ nvram set dnsfilter_rulelist3=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dnsfilter_rulelist4)" ]; then
{ nvram set dnsfilter_rulelist4=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dnsfilter_rulelist5)" ]; then
{ nvram set dnsfilter_rulelist5=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dhcp_dns1_x)" ]; then
{ nvram set dhcp_dns1_x=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dhcp_dns2_x)" ]; then
{ nvram set dhcp_dns2_x=""; }
NVCHECK="$((NVCHECK + 1))"
fi
if [ "$(nvram get dhcpd_dns_router)" != "1" ]; then
{ nvram set dhcpd_dns_router="1"; }
NVCHECK="$((NVCHECK + 1))"
fi
PTXT "${INFO} DNS is set to redirect All DNS resolutions through to AdGuardHome."
fi
fi
if [ "${NVCHECK}" != "0" ]; then
{ nvram commit; }
{ service "restart_firewall;restart_dnsmasq" >/dev/null 2>&1; }
(while { ! check_connection; }; do sleep 1s; done) &
local PID="$!"
wait "${PID}" 2>/dev/null
fi
}
check_dns_local() {
local LOCAL_CACHE
case "$1" in
0)
LOCAL_CACHE="NO"
write_conf ADGUARD_LOCAL "\"${LOCAL_CACHE}\""
;;
1)
LOCAL_CACHE="YES"
write_conf ADGUARD_LOCAL "\"${LOCAL_CACHE}\""
;;
esac
}
check_jffs_enabled() {
if [ "$(nvram get jffs2_format)" = "1" ]; then
PTXT "${ERROR} JFFS partition is scheduled to be reformatted." \
"${ERROR} Please reboot to format or disable that setting and try again." \
"${ERROR} Exiting..."
exit 1
fi
local JFFS2_SCRIPTS JFFS2_ENABLED jffs2_on
JFFS2_SCRIPTS="$(nvram get jffs2_scripts)"
[ -z "$(nvram get jffs2_enable)" ] && JFFS2_ENABLED="$(nvram get jffs2_on)" || JFFS2_ENABLED="$(nvram get jffs2_enable)"
[ -z "$(nvram get jffs2_enable)" ] && jffs2_on="jffs2_on" || jffs2_on="jffs2_enable"
if [ "${JFFS2_ENABLED}" -ne 1 ] || [ "${JFFS2_SCRIPTS}" -ne 1 ]; then
PTXT "${INFO} JFFS custom scripts and configs are not enabled." \
"${INFO} Enabling them now!"
nvram set ${jffs2_on}="1"
nvram set jffs2_scripts="1"
nvram commit
else
PTXT "${INFO} JFFS custom scripts and configs are already enabled."
fi
}
check_version() {
local RMNSTALL LINSTALL MD5SUM_L MD5SUM_R
if [ -f "${TARG_DIR}/installer" ] && [ -f "${AGH_FILE}" ] && [ -z "$2" ]; then
if [ -n "$1" ] || { check_connection; }; then
local varcnt=0
until [ -n "${LINSTALL}" ] && [ -n "${RMNSTALL}" ] && [ -n "${MD5SUM_L}" ] && [ -n "${MD5SUM_R}" ]; do
[ -z "${LINSTALL}" ] && LINSTALL="$(awk '{ print }' "${TARG_DIR}/installer" | grep -m1 "^AI_VERSION=" | grep -oE '[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})' &)" || true
[ -z "${RMNSTALL}" ] && RMNSTALL="$(curl --retry 3 --connect-timeout 3 --retry-delay 1 --max-time $((3 * 5)) --retry-connrefused -sL "${RURL}/installer" | grep -m1 "^AI_VERSION=" | grep -oE '[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})' &)" || true
[ -z "${MD5SUM_L}" ] && MD5SUM_L="$(md5sum "${TARG_DIR}/installer" | cut -d' ' -f1 &)" || true
[ -z "${MD5SUM_R}" ] && MD5SUM_R="$(curl --retry 3 --connect-timeout 3 --retry-delay 1 --max-time $((3 * 5)) --retry-connrefused -fsL "${RURL}/installer" | md5sum | awk '{print $1}' &)" || true
varcnt="$((varcnt + 1))"
if [ "${varcnt}" -le 84 ]; then
wait
else
printf "%s/n" "One or more critical variables could not be set in a timely manner." "Please check your internet connection, or try again later." "The installer script will now exit."
sleep 3s
exit 1
fi
done
wait
if { [ -n "${LINSTALL}" ] && [ -n "${RMNSTALL}" ]; }; then
[ -z "${LINSTALL}" ] && exit 1
[ -z "${RMNSTALL}" ] && exit 1
if [ "${RMNSTALL}" != "${LINSTALL}" ]; then
PTXT "${INFO} New AI_VERSION=v${RMNSTALL} Available!" \
"${INFO} Run Option 1 of the Installer to upgrade Asuswrt-Merlin-AdGuardHome-Installer."
AUTO_UPDATE="update"
elif [ "${MD5SUM_R}" = "${MD5SUM_L}" ]; then
PTXT "${INFO} AI_VERSION=v${LINSTALL}"
else
PTXT "${INFO} AI_VERSION=v${LINSTALL}, but a New Minor Update is Available!" \
"${INFO} Run Option 1 of the Installer to upgrade Asuswrt-Merlin-AdGuardHome-Installer."
AUTO_UPDATE="update"
fi
local ADGUARD_BRANCH
ADGUARD_BRANCH="$(awk -F'=' '/ADGUARD_BRANCH/ {print $2}' "$CONF_FILE" | sed -e 's/^"//' -e 's/"$//')"
[ -z "$(${AGH_FILE} --version | cut -d" " -f4-)" ] && exit 1
[ -z "${REMOTE_VER}" ] && exit 1
[ -z "${REMOTE_BETA}" ] && exit 1
if { [ "${REMOTE_VER}" != "$(${AGH_FILE} --version | cut -d" " -f4-)" ] && [ "${ADGUARD_BRANCH}" = "release" ]; } || { [ "${REMOTE_BETA}" != "$(${AGH_FILE} --version | cut -d" " -f4-)" ] && [ "${ADGUARD_BRANCH}" = "beta" ]; }; then
case "${ADGUARD_BRANCH}" in
release)
PTXT "${INFO} New RELEASE ADGUARDHOME_VER=${REMOTE_VER} Available!"
;;
beta)
PTXT "${INFO} New BETA ADGUARDHOME_VER=${REMOTE_BETA} Available!"
;;
esac
AUTO_UPDATE="update"
else
case "${ADGUARD_BRANCH}" in
release)
PTXT "${INFO} ADGUARDHOME_BUILD=Release"
;;
beta)
PTXT "${INFO} ADGUARDHOME_BUILD=Beta"
;;
edge)
PTXT "${INFO} ADGUARDHOME_BUILD=Edge"
;;
esac
fi
PTXT "${INFO} ADGUARDHOME_VER=$(${AGH_FILE} --version | cut -d" " -f4-)" \
"${INFO} Run Option 1 to if you want to change builds or upgrade when updates are available." \
"${INFO} To visit AdGuardHome," \
"${INFO} please go to http://$(nvram get lan_ipaddr):$(awk -F: '/.*address\:.*/{ print $3 }' ${YAML_FILE})." \
"${INFO} You can use the WebUI to change things to your liking!" \
"${INFO} Stop by https://github.com/AdguardTeam/AdGuardHome/wiki " \
"${INFO} for any configuration needs. "
fi
elif [ -z "${LINSTALL}" ] && [ -z "${RMNSTALL}" ] && [ -z "$1" ]; then
while { ! check_connection; }; do sleep 1s; done && check_version x
else
check_version x x
fi
fi
}
choose_branch() {
if [ "$1" = "1" ]; then
del_conf ADGUARD_BRANCH
fi
local BUILD
if [ ! -f "${CONF_FILE}" ] || [ -z "$(awk -F'=' '/ADGUARD_BRANCH/ {print $2}' "${CONF_FILE}" | sed -e 's/^"//' -e 's/"$//')" ]; then
PTXT "${INFO} Choose which build of AdGuardHome to install:" \
" 1) Release" \
" 2) Beta" \
" 3) Edge"
read_input_num "Select your mode" 1 3
case "${CHOSEN}" in
1)
BUILD=release
write_conf ADGUARD_BRANCH "\"${BUILD}\""
;;
2)
BUILD=beta
write_conf ADGUARD_BRANCH "\"${BUILD}\""
;;
3)
BUILD=edge
write_conf ADGUARD_BRANCH "\"${BUILD}\""
;;
esac
else
if read_yesno "Do you want to switch AdGuardHome builds?"; then choose_branch 1; else PTXT "${INFO} continuing without changing builds."; fi
fi
}
cleanup() {
mv "/opt/etc/init.d/S61AdGuardHome" "/opt/etc/init.d/S99AdGuardHome" >/dev/null 2>&1
[ "$(cru l | grep -c "S99AdGuardHome")" -gt 0 ] && cru -d "S99AdGuardHome"
if [ -f "${TARG_DIR}/localtime" ]; then mv "${TARG_DIR}/localtime" "${ADDON_DIR}/localtime"; fi
if opkg list-installed | grep -q apache; then
opkg flag user apache apache-utils >/dev/null 2>&1
opkg remove apache apache-utils --force-removal-of-dependent-packages >/dev/null 2>&1
fi
}
create_dir() {
if ! mkdir -p "${1}"; then
PTXT "${ERROR} Unable to create ${1}!"
return 1
fi
}
del_between_magic() {
local TARG MAGIC BOUNDS
TARG="$1"
MAGIC="$2"
[ -f "${TARG}" ] || return
BOUNDS="$(awk -v PATT="${MAGIC}" '($0 ~ PATT) {printf NR","}' "${TARG}")"
if [ "${BOUNDS}" ]; then
sed -i "${BOUNDS%,}d" "${TARG}"
fi
}
del_conf() {
[ ! -f "${CONF_FILE}" ] && return
local KEY
for KEY in "$@"; do
sed -i "/^${KEY}=.*$/d" "${CONF_FILE}"
done
}
del_jffs_script() {
local TARG LINE_NUM LINE_ABOVE OP
TARG="$1"
[ -f "${TARG}" ] || return
if [ "$2" ]; then
OP="${2:0:1}"
if [ "$OP" = "!" ]; then
LINE_NUM="$(grep -n -F "[ -x ${ADDON_DIR}/" "${TARG}" | grep -v "$(_quote "$2")" | cut -d':' -f1)"
else
LINE_NUM="$(grep -n -F "[ -x ${ADDON_DIR}/" "${TARG}" | grep "$(_quote "$2")" | cut -d':' -f1)"
fi
else
LINE_NUM="$(grep -n -F "[ -x ${ADDON_DIR}/" "${TARG}" | cut -d':' -f1)"
fi
[ -z "${LINE_NUM}" ] && return
sed -i "${LINE_NUM}d" "${TARG}"
if [ "${LINE_NUM}" -gt 1 ]; then
LINE_NUM="$((LINE_NUM - 1))"
LINE_ABOVE="$(sed "${LINE_NUM}q;d" "${TARG}")"
[ -z "${LINE_ABOVE}" ] && sed -i "${LINE_NUM}d" "${TARG}"
fi
[ "$(awk '{ print }' "${TARG}")" = "#!/bin/sh" ] && rm -f "${TARG}"
}
download_file() {
local TARG PERM URL RET FILENAME MD5SUM_OLD MD5SUM_CURR
TARG="$1"
shift
PERM="$1"
shift
for URL in "$@"; do
FILENAME="$(basename "${URL}")"
local varcnt="0"
until [ -n "${MD5SUM_CURR}" ]; do
if [ -f "${TARG}/${FILENAME}" ]; then
[ -z "${MD5SUM_OLD}" ] && MD5SUM_OLD="$(md5sum "${TARG}/${FILENAME}" | cut -d' ' -f1 &)" || true
fi
[ -z "${MD5SUM_CURR}" ] && MD5SUM_CURR="$(curl --retry 3 --connect-timeout 3 --retry-delay 1 --max-time $((3 * 5)) --retry-connrefused -fsL "${URL}" | md5sum | awk '{print $1}' &)" || true
varcnt="$((varcnt + 1))"
if [ "${varcnt}" -eq 1 ]; then
wait
else
break
fi
done
wait
if [ "$(PTXT -n "${MD5SUM_CURR}" | wc -c)" -eq 32 ] && [ "${MD5SUM_CURR}" = "${MD5SUM_OLD}" ]; then
PTXT "${INFO} ${FILENAME} is up to date. Skipping..."
else
local COUNT
COUNT="0"
while [ "${COUNT}" -lt 3 ]; do
PTXT "${INFO} Downloading ${FILENAME}"
if curl --retry 3 --connect-timeout 3 --retry-delay 1 --max-time $((3 * 5)) --retry-connrefused -L -k -s "${URL}" -o "${TARG}/${FILENAME}"; then
chmod "${PERM}" "${TARG}/${FILENAME}"
break
fi
COUNT="$((COUNT + 1))"
done
if [ "${COUNT}" -eq 3 ]; then
PTXT "${ERROR} Unable to download ${BOLD}${URL}${NORM}"
if [ -z "${RET}" ]; then RET="1"; else RET="$((RET + 1))"; fi
fi
fi
done
if [ -z "${RET}" ]; then RET="0"; else PTXT "${ERROR} One or more download failures has occured." "${ERROR} It is recommended to rerun the installer, or restore from a backup!"; fi
return "${RET}"
}
end_op_message() {
case "${1:-0}" in
0)
PTXT "${INFO} Operation completed, returning to Main Menu. You can quit or continue."
;;
1)
PTXT "${INFO} Operation aborted, returning to Main Menu. You can quit or continue."
;;
2)
PTXT "${INFO} Abnormal operations, returning to Main Menu. You can quit or continue."
;;
esac
PTXT "====================================================="
PTXT " "
PTXT " "
sleep 3s && clear
if [ -f "${TARG_DIR}/installer" ]; then
chmod 755 "${TARG_DIR}/installer" >/dev/null 2>&1
exec "${TARG_DIR}/installer" "${BRANCH}" && exit
elif [ ! -f "${TARG_DIR}/installer" ] && [ -f "${SCRIPT_LOC}" ]; then
chmod 755 "${SCRIPT_LOC}" >/dev/null 2>&1
exec "${SCRIPT_LOC}" "${BRANCH}" && exit
elif [ -f "${HOME}/installer" ]; then
chmod 755 "${HOME}/installer"
exec "${HOME}/installer" "${BRANCH}" && exit
else
clear && { end_op_header 2>/dev/null; } && exit
fi
}
end_op_header() {
sed -n -e "1,$(($(grep -wn 'esac' "$0" | cut -d':' -f1 | tail -n1) + 1))p" "$0" >"${0}.tmp"
chmod 755 "${0}.tmp"
exec "${0}.tmp" "${BRANCH}" && rm -rf "${0}.tmp"
}
inst_AdGuardHome() {
local ADGUARD_TAR
ADGUARD_TAR="AdGuardHome_${ADGUARD_ARCH}.tar.gz"
if [ -z "$2" ]; then
if [ "${1:-RESTORE}" != "RESTORE" ]; then
if [ ! -d "${TARG_DIR}" ] && [ -f "${BASE_DIR}/backup_AdGuardHome.tar.gz" ]; then
PTXT "${INFO} Backup is detected."
local USE_OLD
if read_yesno "Do you want Restore instead?"; then USE_OLD="YES"; else USE_OLD="NO"; fi
if [ "${USE_OLD}" = "YES" ]; then
PTXT "${INFO} Installing from an old backup!"
backup_restore RESTORE
elif [ "${USE_OLD}" = "NO" ]; then
PTXT "${INFO} Continuing without restoring from backup!"
fi
elif [ -d "${TARG_DIR}" ] && [ -f "${AGH_FILE}" ] && [ ! -f "${BASE_DIR}/backup_AdGuardHome.tar.gz" ]; then
if read_yesno "Do you want create a backup before updating?"; then backup_restore BACKUP 0; else PTXT "${INFO} continuing without making a backup."; fi
fi
fi
if [ -z "${REMOTE_VER}" ] || { ! check_connection; }; then
PTXT "${ERROR} Unable to detect the Internet!"
end_op_message 1
return
fi
if ! create_dir "${TARG_DIR}"; then
end_op_message 1
return
fi
if ! download_file "${TARG_DIR}" 755 "${RURL}/installer" || ! awk '{ print }' "${TARG_DIR}/installer" | grep -m1 "^AI_VERSION=" | grep -qoE '[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})'; then
PTXT "${ERROR} Failed to download installer."
end_op_message 1
return
fi
if [ "${1:-RESTORE}" != "RESTORE" ]; then
if [ "${1:-install}" != "update" ]; then choose_branch; fi
local ADGUARD_BRANCH
ADGUARD_BRANCH="$(awk -F'=' '/ADGUARD_BRANCH/ {print $2}' "${CONF_FILE}" | sed -e 's/^"//' -e 's/"$//')"
if [ ! -f "${AGH_FILE}" ]; then
inst_AdGuardHome "${1:-install}" "$ADGUARD_BRANCH"
else
[ -z "$("${AGH_FILE}" --version | cut -d" " -f4-)" ] && exit 1
[ -z "${REMOTE_VER}" ] && exit 1
[ -z "${REMOTE_BETA}" ] && exit 1
if { [ "${REMOTE_VER}" != "$("${AGH_FILE}" --version | cut -d" " -f4-)" ] && [ "${ADGUARD_BRANCH}" = "release" ]; } || { [ "${REMOTE_BETA}" != "$("${AGH_FILE}" --version | cut -d" " -f4-)" ] && [ "${ADGUARD_BRANCH}" = "beta" ]; } || [ "${ADGUARD_BRANCH}" = "edge" ]; then
case "${ADGUARD_BRANCH}" in
release)
PTXT "${INFO} New RELEASE ADGUARDHOME_VER=${REMOTE_VER} Available!" \
"${INFO} Updating ADGUARDHOME_VER=$(${AGH_FILE} --version | cut -d" " -f4-) to ${REMOTE_VER}."
;;
beta)
PTXT "${INFO} New BETA ADGUARDHOME_VER=${REMOTE_BETA} Available!" \
"${INFO} Updating ADGUARDHOME_VER=$(${AGH_FILE} --version | cut -d" " -f4-) to ${REMOTE_BETA}."
;;
edge)
PTXT "${INFO} ADGUARDHOME_BUILD=Edge" \
"${INFO} Downloading the lastest Edge version to replace ADGUARDHOME_VER=$(${AGH_FILE} --version | cut -d" " -f4-)."
;;
esac
inst_AdGuardHome "${1:-update}" "${ADGUARD_BRANCH}"
else
case "${ADGUARD_BRANCH}" in
release)
PTXT "${INFO} ADGUARDHOME_BUILD=Release" \
"${INFO} No new release version available." \
"${INFO} ADGUARDHOME_VER=$(${AGH_FILE} --version | cut -d" " -f4-)"
;;
beta)
PTXT "${INFO} ADGUARDHOME_BUILD=Beta" \
"${INFO} No new beta version available." \
"${INFO} ADGUARDHOME_VER=$(${AGH_FILE} --version | cut -d" " -f4-)"
;;
esac
fi
fi
fi
else
if ! download_file "${BASE_DIR}" 644 "https://static.adguard.com/adguardhome/${2}/${ADGUARD_TAR}"; then
PTXT "${ERROR} Unable to download AdGuardHome package for your router"
end_op_message 1
return
fi
tar xzv -C "${BASE_DIR}" -f "${BASE_DIR}/${ADGUARD_TAR}" >/dev/null 2>&1
chown "$(nvram get http_username)":root ${TARG_DIR}/*
rm -r "${BASE_DIR:?}/${ADGUARD_TAR}"
chmod 755 "${AGH_FILE}"
[ -f "/opt/sbin/AdGuardHome" ] && rm -rf /opt/sbin/AdGuardHome
if ! ln -sf "${AGH_FILE}" /opt/sbin/AdGuardHome || [ -z "$("${AGH_FILE}" --version | cut -d" " -f4-)" ]; then
PTXT "${ERROR} Failed to download AdGuardHome package for your router"
end_op_message 1
return
fi
fi
create_dir "${ADDON_DIR}"
download_file "${ADDON_DIR}" 755 "${RURL}/AdGuardHome.sh"
download_file "/opt/etc/init.d" 755 "${RURL}/S99AdGuardHome"
download_file "/opt/etc/init.d" 644 "${RURL}/rc.func.AdGuardHome"
[ -f "/jffs/scripts/dnsmasq.postconf" ] && yaml_nvars_delete "#Asuswrt-Merlin AdGuardHome Installer" /jffs/scripts/dnsmasq.postconf
for i in init-start services-stop; do { if { ! grep -q "${ADDON_DIR}/AdGuardHome.sh $i &" "/jffs/scripts/${i}" && grep -q "${ADDON_DIR}/AdGuardHome.sh $i" "/jffs/scripts/${i}"; }; then del_jffs_script "/jffs/scripts/${i}"; fi; }; done
write_manager_script /jffs/scripts/init-start "init-start &"
write_manager_script /jffs/scripts/services-stop "services-stop &"
write_manager_script /jffs/scripts/dnsmasq.postconf dnsmasq
del_between_magic /jffs/scripts/service-event-end '# Asuswrt-Merlin-AdGuardHome-Installer'
write_command_script /jffs/scripts/service-event-end 'if printf "%s" "$@" | /bin/grep -qE "^(((start|stop|restart|kill|reload)_?.*AdGuardHome)$)"; then { sh /jffs/addons/AdGuardHome.d/AdGuardHome.sh "$(printf "%s" "$@" | /bin/grep -oE "(start|stop|restart|kill|reload)")" x & }; fi # Asuswrt-Merlin-AdGuardHome-Installer'
if [ ! -f "${ADDON_DIR}/localtime" ]; then
PTXT "${INFO} Before setting up AdGuardHome, please choose your right timezone!"
set_timezone
fi
if ! setup_AdGuardHome "" "${1:-install}"; then
end_op_message 1
return
fi
PTXT "${INFO} Starting AdGuardHome..."
(
if [ -n "$(pidof AdGuardHome)" ]; then { if { service stop_AdGuardHome >/dev/null 2>&1; }; then sleep 1s; elif { /opt/etc/init.d/S99AdGuardHome stop; }; then sleep 1s; elif { /opt/etc/init.d/S99AdGuardHome kill; }; then sleep 1s; else
{ killall -q -9 AdGuardHome 2>/dev/null; }
rm -rf /opt/var/run/AdGuardHome.pid 2>/dev/null
sleep 1s
fi; }; fi
sleep 1s
) &
local PID="$!"
wait "${PID}" 2>/dev/null
({ until [ "$(pidof AdGuardHome S99AdGuardHome | wc -w)" -lt "1" ]; do sleep 1s; done; }) &
local PID="$!"
wait "${PID}" 2>/dev/null
sleep 1s
{ /opt/etc/init.d/S99AdGuardHome check; }
(
if [ -z "$(pidof AdGuardHome)" ]; then { if { service start_AdGuardHome >/dev/null 2>&1; }; then sleep 1s; elif { /opt/etc/init.d/S99AdGuardHome start; }; then sleep 1s; else
PTXT "${ERROR} Couldn't start AdGuardHome!" "${ERROR} Please send WebUI System Log to dev."
end_op_message 1
return
fi; }; fi
sleep 1s
) &
local PID="$!"
wait "${PID}" 2>/dev/null
({ until [ "$(pidof AdGuardHome S99AdGuardHome | wc -w)" -eq "2" ]; do sleep 1s; done; }) &
local PID="$!"
wait "${PID}" 2>/dev/null
sleep 1s
{ /opt/etc/init.d/S99AdGuardHome check; }
if [ -z "$(pidof AdGuardHome)" ]; then
PTXT "${ERROR} Couldn't start AdGuardHome!" \
"${ERROR} Please send WebUI System Log to dev."
end_op_message 1
return
fi
PTXT "${INFO} Please wait while we perform one last check."
(
if [ -n "$(pidof AdGuardHome)" ]; then { if { service restart_AdGuardHome >/dev/null 2>&1; }; then sleep 1s; elif { /opt/etc/init.d/S99AdGuardHome restart; }; then sleep 1s; else
PTXT "${ERROR} Couldn't start AdGuardHome!" "${ERROR} Please send WebUI System Log to dev."
end_op_message 1
return
fi; }; fi
sleep 1s
) &
local PID="$!"
wait "${PID}" 2>/dev/null
({ until { check_connection && [ "$(pidof AdGuardHome S99AdGuardHome | wc -w)" -eq "2" ]; }; do sleep 1s; done; }) &
local PID="$!"
wait "${PID}" 2>/dev/null
sleep 1s
{ /opt/etc/init.d/S99AdGuardHome check; }
PTXT "${INFO} AdGuardHome setup is complete."
end_op_message 0
}
read_input_dns() {
PTXT -n "${INPUT} ${1} ${BOLD}${2}: ${NORM}"
local DNS_SERVER
read -r DNS_SERVER
[ -z "${DNS_SERVER}" ] && DNS_SERVER="$2"
if ! PTXT "${DNS_SERVER}" | grep -qoE "\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"; then
PTXT "${ERROR} Invalid DNS server address entered"
read_input_dns "$@"
fi
if [ "${DNS_SERVER1}" = "${DNS_SERVER}" ]; then
PTXT "${ERROR} ${DNS_SERVER} DNS server address already entered, please try again!"
read_input_dns "$@"
fi
case "$1" in
"Default is")
BOOTSTRAP1="${DNS_SERVER}"
DNS_SERVER1="${DNS_SERVER}"
;;
"2nd Default is")
BOOTSTRAP2="${DNS_SERVER}"
;;
esac
}
read_input_num() {
local RANGE
[ -z "$4" ] && [ -z "$5" ] && [ -z "$6" ] && RANGE="[${2}-${3}]"
[ -n "$4" ] && [ -z "$5" ] && [ -z "$6" ] && RANGE="[${2}-${3}/${4}]"
[ -n "$4" ] && [ -n "$5" ] && [ -z "$6" ] && RANGE="[${2}-${3}/${4}/${5}]"
[ -n "$4" ] && [ -n "$5" ] && [ -n "$6" ] && RANGE="[${2}-${3}/${4}/${5}/${6}]"
PTXT -n "${INPUT} ${1}, ${BOLD}${RANGE}${NORM}: "
read -r CHOSEN
case "$1" in
*)
if [ -z "${CHOSEN}" ]; then
PTXT "${ERROR} Invalid character(s) entered! Retrying..."
read_input_num "$@"
return
fi
;;
esac
case "${CHOSEN}" in
"$4" | "$5" | "$6")
return 1
;;
"$2" | "$3" | *)
if ! PTXT "${CHOSEN}" | grep -qE '^[0-9]+$'; then
PTXT "${ERROR} Invalid character(s) entered! Retrying..."
read_input_num "$@"
return
fi
if [ "${CHOSEN}" -lt "$2" ] || [ "${CHOSEN}" -gt "$3" ]; then
PTXT "${ERROR} Chosen number is not in range! Retrying..."
read_input_num "$@"
return
fi
;;
esac
}
read_input_port() {
PTXT -n "${INPUT} ${1} ${BOLD}${2}: ${NORM}"
read -r WEB_PORT
[ -z "${WEB_PORT}" ] && WEB_PORT="$2"
if ! PTXT "${WEB_PORT}" | grep -qoE '^(3000|[3-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$' || { [ "${WEB_PORT}" -lt 3000 ] || [ "${WEB_PORT}" -gt 65535 ]; }; then
PTXT "${ERROR} Invalid Web Port entered, only Web Ports residing within the port range 3000-65535 are selectable."
read_input_port "$@"
fi
if netstat -nlp 2>/dev/null | grep -qE "(:${WEB_PORT}[[:space:]])"; then
PTXT "${ERROR} ${WEB_PORT} already in use, please choose a different port!"
read_input_port "$@"
fi
}
read_yesno() {
PTXT -n "${INPUT} ${1} ${BOLD}[y/n]${NORM}: "
local YESNO
read -r YESNO
case "${YESNO}" in
y | Y)
return 0
;;
n | N)
return 1
;;
*)
PTXT "${ERROR} Invalid input!"
read_yesno "$@"
;;
esac
}
set_timezone() {
local TMP TZ_ARCH TZ_DATA INDEX TZ_FILE
TMP="/root"
TZ_ARCH="$(uname -m)"
case "${TZ_ARCH}" in
"aarch64" | "arm64")
TZ_ARCH="aarch64"
;;
"armv7l")
TZ_ARCH="arm"
;;
esac
TZ_DATA="tzdata-2021e-1-${TZ_ARCH}.pkg.tar.bz2"
opkg install column >/dev/null 2>&1
download_file "${TMP}" 644 "${RURL}/${TZ_DATA}"
INDEX="$(tar tjf "${TMP}/${TZ_DATA}" | awk -F'/' '!/\/$/ && /\/posix\//' | wc -l)"
PTXT "${INFO} Available timezones/locations:"
tar tjf "${TMP}/${TZ_DATA}" | awk -F'/' '!/\/$/ && /\/posix\//' | sort | cut -d'/' -f2- | awk -v INDEX=0 -F'/' '!/\/$/ {++INDEX;printf " " INDEX") ";for (i=5; i<NF; i++) printf $i "/"; print $NF}' | column
read_input_num "Select your timezone/location" 1 "${INDEX}"
TZ_FILE="$(tar tjf "${TMP}/${TZ_DATA}" | awk -F'/' '!/\/$/ && /\/posix\//' | sort | awk -v INDEX="$CHOSEN" '{++i}i==INDEX{print $0}')"
PTXT "${INFO} $(basename "${TZ_FILE}") selected"
tar xjf "${TMP}/${TZ_DATA}" -C "${TMP}" ./usr/share/zoneinfo/posix
if ! mv "${TMP}/${TZ_FILE}" "${ADDON_DIR}/localtime"; then
PTXT "${ERROR} Unable to set your timezone file"
end_op_message 1
return
fi
rm -rf "${TMP:?}/${TZ_DATA}" "${TMP:?}/usr"
}
setup_AdGuardHome() {
if [ ! -d "${TARG_DIR}" ] || [ ! -f "${AGH_FILE}" ]; then
PTXT "${ERROR} AdGuardHome is not installed. Aborting...."
return 1
fi
PTXT "${INFO} Configuring AdGuardHome..."
setup_AdGuardHome_impl "$@"
local RET="$?"
if [ "$1" = "reconfig" ]; then
if [ "$RET" -eq 0 ]; then
PTXT "${INFO} Restarting AdGuardHome with new config..."
(
if [ -n "$(pidof AdGuardHome)" ]; then { if { service restart_AdGuardHome >/dev/null 2>&1; }; then sleep 1s; elif { /opt/etc/init.d/S99AdGuardHome restart; }; then sleep 1s; else