-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathinstaller
executable file
·1999 lines (1921 loc) · 80.1 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
###################################################################################################################
# █████╗ ███████╗██╗ ██╗███████╗██╗ ██╗██████╗ ████████╗ ███╗ ███╗███████╗██████╗ ██╗ ██╗███╗ ██╗#
#██╔══██╗██╔════╝██║ ██║██╔════╝██║ ██║██╔══██╗╚══██╔══╝ ████╗ ████║██╔════╝██╔══██╗██║ ██║████╗ ██║#
#███████║███████╗██║ ██║███████╗██║ █╗ ██║██████╔╝ ██║ █████╗ ██╔████╔██║█████╗ ██████╔╝██║ ██║██╔██╗ ██║#
#██╔══██║╚════██║██║ ██║╚════██║██║███╗██║██╔══██╗ ██║ ╚════╝ ██║╚██╔╝██║██╔══╝ ██╔══██╗██║ ██║██║╚██╗██║#
#██║ ██║███████║╚██████╔╝███████║╚███╔███╔╝██║ ██║ ██║ ██║ ╚═╝ ██║███████╗██║ ██║███████╗██║██║ ╚████║#
#╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚══════╝ ╚══╝╚══╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚══════╝╚═╝╚═╝ ╚═══╝#
#██████╗ ███╗ ██╗███████╗ ██████╗██████╗ ██╗ ██╗██████╗ ████████╗ ██████╗ ██████╗ ██████╗ ██╗ ██╗██╗ ██╗#
#██╔══██╗████╗ ██║██╔════╝██╔════╝██╔══██╗╚██╗ ██╔╝██╔══██╗╚══██╔══╝ ██╔══██╗██╔══██╗██╔═══██╗╚██╗██╔╝╚██╗ ██╔╝#
#██║ ██║██╔██╗ ██║███████╗██║ ██████╔╝ ╚████╔╝ ██████╔╝ ██║█████╗██████╔╝██████╔╝██║ ██║ ╚███╔╝ ╚████╔╝ #
#██║ ██║██║╚██╗██║╚════██║██║ ██╔══██╗ ╚██╔╝ ██╔═══╝ ██║╚════╝██╔═══╝ ██╔══██╗██║ ██║ ██╔██╗ ╚██╔╝ #
#██████╔╝██║ ╚████║███████║╚██████╗██║ ██║ ██║ ██║ ██║ ██║ ██║ ██║╚██████╔╝██╔╝ ██╗ ██║ #
#╚═════╝ ╚═╝ ╚═══╝╚══════╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ #
#██╗███╗ ██╗███████╗████████╗ █████╗ ██╗ ██╗ ███████╗██████╗ Original Author: #
#██║████╗ ██║██╔════╝╚══██╔══╝██╔══██╗██║ ██║ ██╔════╝██╔══██╗ bigeyes0x0 #
#██║██╔██╗ ██║███████╗ ██║ ███████║██║ ██║ █████╗ ██████╔╝ Current Maintainer: #
#██║██║╚██╗██║╚════██║ ██║ ██╔══██║██║ ██║ ██╔══╝ ██╔══██╗ SomeWhereOverTheRainBow #
#██║██║ ╚████║███████║ ██║ ██║ ██║███████╗███████╗███████╗██║ ██║ v2.5.3 #
#╚═╝╚═╝ ╚═══╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝ #
###################################################################################################################
# shellcheck disable=SC2016
# shellcheck disable=SC2124
# shellcheck disable=SC3043
# shellcheck disable=SC3045
# shellcheck disable=SC3057
# shellcheck disable=SC3060
export LC_ALL=C
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:${PATH}"
DI_VERSION="v2.5.3"
export DI_VERSION
readonly LATEST_URL="https://api.github.com/repos/jedisct1/dnscrypt-proxy/releases/latest"
varcnt=0
until [ -n "${DNSCRYPT_VER}" ]; do
[ -z "${DNSCRYPT_VER}" ] && DNSCRYPT_VER="$(curl --retry 3 --connect-timeout 3 --retry-delay 1 --max-time $((3 * 5)) --retry-connrefused -sL "${LATEST_URL}" | grep "tag_name" | head -1 | 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 mannor." "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 DNSCRYPT_VER
readonly BASE_DIR="/jffs"
readonly TARG_DIR="${BASE_DIR}/dnscrypt"
readonly CONF_FILE="${TARG_DIR}/.config"
readonly TOML_FILE="${TARG_DIR}/dnscrypt-proxy.toml"
readonly TOML_BAK="${TARG_DIR}/dnscrypt-proxy.toml.bak"
readonly TOML_ERR="${TARG_DIR}/dnscrypt-proxy.toml.err"
readonly TOML_ORI="${TARG_DIR}/example-dnscrypt-proxy.toml"
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
}
backup_restore() {
if [ "$1" = "BACKUP" ] && [ -d "${TARG_DIR}" ] && [ -f "${TARG_DIR}/dnscrypt-proxy" ]; then
if [ -f "${BASE_DIR}/backup_dnscrypt.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_dnscrypt.tar.gz"
fi
fi
PTXT "${INFO} This operation will backup dnscrypt-proxy(<4MB)to jffs partition." \
"${INFO} Please wait a moment."
tar -czvf "${BASE_DIR}/backup_dnscrypt.tar.gz" -C "${TARG_DIR}" ../dnscrypt/ >/dev/null 2>&1
PTXT "${INFO} Backup complete"
[ -z "$2" ] && end_op_message 0
elif [ "$1" = "BACKUP" ] && [ ! -d "${TARG_DIR}" ] && [ ! -f "${TARG_DIR}/dnscrypt-proxy" ]; then
PTXT "${ERROR} No ${TARG_DIR}/dnscrypt-proxy to Backup!"
end_op_message 1
fi
if [ -f "${BASE_DIR}/backup_dnscrypt.tar.gz" ] && [ "$1" = "RESTORE" ]; then
PTXT "${INFO} Please wait a moment."
tar -xzvf "${BASE_DIR}/backup_dnscrypt.tar.gz" -C "${BASE_DIR}" >/dev/null 2>&1
chown "$(nvram get http_username)":root ${TARG_DIR}/*
chmod 755 "${TARG_DIR}/dnscrypt-proxy"
inst_dnscrypt "${1:-RESTORE}"
elif [ ! -f "${BASE_DIR}/backup_dnscrypt.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
}
########################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_dnscrypt_toml() {
[ ! -f "${TOML_FILE}" ] && return
PTXT "${INFO} Checking dnscrypt-proxy configuration..."
if ! ${TARG_DIR}/dnscrypt-proxy -check -config "${TOML_FILE}"; then
PTXT "${INFO} Move invalid configuration file to ${TOML_ERR}" \
"${INFO} Operation will continue with clean config file."
mv "${TOML_FILE}" "${TOML_ERR}"
return 1
fi
}
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/addons/AdGuardHome.d" ]; then
PTXT "${ERROR} Potential stubby or adguardhome 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 2>/dev/null; }
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 Dnscrypt-Proxy."
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 Dnscrypt-Proxy."
if read_yesno "Do you want to redirect only NON-CUSTOM DNS resolutions on your network through to Dnscrypt-Proxy?"; 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 Dnscrypt-Proxy, 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 Dnscrypt-Proxy."
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 DNSCRYPT_LOCAL "\"${LOCAL_CACHE}\""
;;
1)
LOCAL_CACHE="YES"
write_conf DNSCRYPT_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_anonymized_automatic() {
if [ "$1" -eq 0 ] && grep -q '^server_names = .*Static.*' "${TOML_FILE}"; then
PTXT "${INFO} Custom servers that are potentially not compatible with relays are detected!" \
"${WARNING} These servers might not work with relays." \
"${WARNING} Use at your own risk."
fi
local USE_BROKEN USE_WILDCARD
PTXT "${INFO} This allows for the use of server_name='*' as wildcard option for all servers compatible with relays." \
"${INFO} This will be the default route for all compatible servers." \
"${INFO} Additionally routes can be distinctly selected by using via=['*'] as relay wildcard."
if read_yesno "Do you want to use wildcard relay (via=['*']) option?"; then USE_WILDCARD="YES"; else USE_WILDCARD="NO"; fi
if [ "${USE_WILDCARD}" = "YES" ]; then choose_relays_automatic_wildcard; elif [ "${USE_WILDCARD}" = "NO" ]; then
PTXT "${INFO} You chose not to use wildcard for relay selection." "${INFO} Instead you will manually choose relays from a list."
choose_relays_automatic
fi
if read_yesno "Do you want to skip using resolvers that are incompatible with anonymization instead of using them directly?"; then USE_BROKEN="true"; else USE_BROKEN="false"; fi
toml_avars_prep skip_incompatible "${USE_BROKEN}"
}
check_anonymized_disabled() {
toml_avar_disable routes
toml_avars_prep skip_incompatible false
PTXT "${INFO} Continue without Relays Support"
}
check_opendns() {
if grep -q '^server_names = .*cisco.*' "${TOML_FILE}"; then
if [ -f "${CONF_FILE}" ]; then
local OPENDNS_USER OPENDNS_PASSWORD
OPENDNS_USER="$(awk -F'=' '/OPENDNS_USER/ {print $2}' "${CONF_FILE}")"
OPENDNS_PASSWORD="$(awk -F'=' '/OPENDNS_PASSWORD/ {print $2}' "${CONF_FILE}")"
if [ "${OPENDNS_USER}" ] && [ "${OPENDNS_PASSWORD}" ]; then
PTXT "${INFO} Found OpenDNS account ${BOLD}${OPENDNS_USER}" \
"${INFO} What do you want to do:" \
" 1) Use this account" \
" 2) Setup new account" \
" 3) Disable OpenDNS account authen"
read_input_num "Your choice" 1 3
case "${CHOSEN}" in
1)
PTXT "${INFO} Use previous account ${BOLD}${OPENDNS_USER}${NORM}"
;;
2)
opendns_authen 1
;;
3)
opendns_authen 0
;;
esac
else
if read_yesno "Do you want to set up OpenDNS account ip update?"; then opendns_authen 1; else opendns_authen 0; fi
fi
else
if read_yesno "Do you want to set up OpenDNS account ip update?"; then opendns_authen 1; else opendns_authen 0; fi
fi
else
opendns_authen 0
fi
}
check_relays() {
local DNSCRYPT_ARGS DNSCRYPT ODOH_ARGS ODOH FRAGSBLOCKED_ARGS FRAGSBLOCKED VARSARGS SERVER COUNT NUMFRAG NUMCRYPT CRYPT
FRAGSBLOCKED_ARGS="$(grep '^fragments_blocked =' "${TOML_ORI}" | cut -d'[' -f2- | sed "s/['\"\,]//g;s/]//g;s/^ [ t]*//;s/[ \t]*$//;s/ /|/g")"
NUMCRYPT="0"
while read -r CRYPT; do
CRYPT="$(PTXT "${CRYPT}" | cut -d',' -f1)"
if [ "${NUMCRYPT}" -eq 0 ]; then
NUMCRYPT="1"
continue
fi
if [ "${NUMCRYPT}" -eq 1 ]; then
if ! PTXT "${FRAGSBLOCKED_ARGS}" | grep -qoF "${CRYPT}"; then DNSCRYPT_ARGS="${CRYPT}"; fi
elif [ "${NUMCRYPT}" -gt 1 ]; then
if ! PTXT "${FRAGSBLOCKED_ARGS}" | grep -qoF "${CRYPT}"; then DNSCRYPT_ARGS="${DNSCRYPT_ARGS}|${CRYPT}"; fi
fi
NUMCRYPT="$((NUMCRYPT + 1))"
done <${TARG_DIR}/dnscrypt-resolvers.csv
ODOH_ARGS="$(awk -v PATT="odohrelay" '/^## / && ($0 !~ PATT) {printf "";printf ""$2"";getline;print}' "${TARG_DIR}/odoh-servers.md" | tr '\n' ' ' | sed 's/[ \t]*$//' | sed 's/ /|/g')"
if [ -n "${STAT_CRYPT}" ]; then
DNSCRYPT_ARGS="${DNSCRYPT_ARGS}|${STAT_CRYPT}"
fi
if [ -n "${STAT_ODOH}" ]; then
ODOH_ARGS="${ODOH_ARGS}|${STAT_ODOH}"
fi
VARSARGS="${DNSCRYPT_ARGS}|${ODOH_ARGS}|${FRAGSBLOCKED_ARGS}"
COUNT="0"
NUMFRAG="0"
[ "${NUMFRAG}" -eq 0 ] && toml_avars_prep skip_incompatible false
for SERVER in ${VARSARGS//|/ }; do
if [ "$(grep '^server_names = .*'"${SERVER}"'.*' "${TOML_FILE}" | grep -cF "'${SERVER}'")" -ne 0 ]; then
if PTXT "${DNSCRYPT_ARGS}" | grep -qoF "${SERVER}"; then DNSCRYPT="${SERVER}"; fi
if PTXT "${ODOH_ARGS}" | grep -qoF "${SERVER}"; then ODOH="${SERVER}"; fi
if PTXT "${FRAGSBLOCKED_ARGS}" | grep -qoF "${SERVER}"; then FRAGSBLOCKED="${SERVER}"; fi
case "${SERVER}" in
"${DNSCRYPT}")
if read_yesno "Do you want to add relays for ${SERVER}?"; then ADD_RELAYS="YES"; else ADD_RELAYS="NO"; fi
if [ "${ADD_RELAYS}" = "YES" ]; then PTXT "${INFO} You may manually choose relays for ${SERVER} or you may specify wildcard relay (via=['*'])."; fi
if [ "${COUNT}" -eq 0 ] && [ "${ADD_RELAYS}" = "YES" ]; then
toml_avar_enable routes
if read_yesno "Do you want to use wildcard relay (via=['*']) option for ${SERVER}?"; then USE_WILDCARD="YES"; else USE_WILDCARD="NO"; fi
if [ "${USE_WILDCARD}" = "YES" ]; then choose_relays_manual_wildcard; elif [ "${USE_WILDCARD}" = "NO" ]; then
PTXT "${INFO} You chose not to use wildcard for relay selection." "${INFO} Instead you will manually choose relays from a list."
choose_relays_manual
fi
COUNT="$((COUNT + 1))"
elif [ "${COUNT}" -gt 0 ] && [ "${ADD_RELAYS}" = "YES" ]; then
if read_yesno "Do you want to use wildcard relay (via=['*']) option for ${SERVER}?"; then USE_WILDCARD="YES"; else USE_WILDCARD="NO"; fi
if [ "${USE_WILDCARD}" = "YES" ]; then choose_relays_manual_wildcard; elif [ "${USE_WILDCARD}" = "NO" ]; then
PTXT "${INFO} You chose not to use wildcard for relay selection." "${INFO} Instead you will manually choose relays from a list."
choose_relays_manual
fi
elif [ "${ADD_RELAYS}" = "NO" ]; then
PTXT "${INFO} Skipping relays for ${SERVER}."
fi
;;
"${ODOH}")
PTXT "${INFO} Found ${SERVER}, Oblivious DNS-over-HTTPS relays are required for Oblivious DNS-over-HTTPS servers." \
"${INFO} You may manually choose relays for ${SERVER} server or you may specify wildcard relay (via=['*'])."
if [ "${COUNT}" -eq 0 ]; then
if read_yesno "Do you want to use wildcard relay (via=['*']) option for ${SERVER}?"; then USE_WILDCARD="YES"; else USE_WILDCARD="NO"; fi
if [ "${USE_WILDCARD}" = "YES" ]; then choose_relays_manual_wildcard; elif [ "${USE_WILDCARD}" = "NO" ]; then
PTXT "${INFO} You chose not to use wildcard for relay selection." "${INFO} Instead you will manually choose relays from a list."
choose_relays_manual_odoh
fi
COUNT="$((COUNT + 1))"
elif [ "${COUNT}" -gt 0 ]; then
if read_yesno "Do you want to use wildcard relay (via=['*']) option for ${SERVER}?"; then USE_WILDCARD="YES"; else USE_WILDCARD="NO"; fi
if [ "${USE_WILDCARD}" = "YES" ]; then choose_relays_manual_wildcard; elif [ "${USE_WILDCARD}" = "NO" ]; then
PTXT "${INFO} You chose not to use wildcard for relay selection." "${INFO} Instead you will manually choose relays from a list."
choose_relays_manual_odoh
fi
fi
;;
"${FRAGSBLOCKED}")
if [ "${NUMFRAG}" -eq 0 ] && [ "${COUNT}" -gt 0 ]; then
local USE_BROKEN
if read_yesno "Do you want to skip using resolvers that are incompatible with anonymization instead of using them directly?"; then USE_BROKEN="true"; else USE_BROKEN="false"; fi
toml_avars_prep skip_incompatible "${USE_BROKEN}"
NUMFRAG="$((NUMFRAG + 1))"
fi
;;
esac
fi
done
if [ "${COUNT}" -eq 0 ] && grep -q '^odoh_servers = .*false.*' "${TOML_FILE}" && grep -q '^dnscrypt_servers = .*true.*' "${TOML_FILE}"; then
if [ -n "${DNSCRYPT}" ] || { grep -q '^dnscrypt_servers = .*true.*' "${TOML_FILE}" && ! grep -q '^server_names' "${TOML_FILE}"; }; then
PTXT "${INFO} To continue, you may still define a default route for all compatible DNSCrypt servers and relays by selecting wildcard option for servers and relays."
if read_yesno "Do you still want to setup wildcard options for servers (server_name "*") and relays (via=['*']) for all compatible DNSCrypt servers and relays?"; then check_anonymized_automatic 0; else check_anonymized_disabled; fi
else
check_anonymized_disabled
fi
elif [ "${COUNT}" -eq 0 ] && grep -q '^odoh_servers = .*true.*' "${TOML_FILE}" && grep -q '^dnscrypt_servers = .*true.*' "${TOML_FILE}"; then
PTXT "${INFO} This option allows you to setup wildcard options for servers (server_name "*") and relays (via=['*']) for all compatible servers and relays."
if read_yesno "Do you only want to skip this option for Dnscrypt Servers (still required for ODOH)?"; then choose_relays_automatic_odoh; else COUNT="$((COUNT + 1))"; fi
if [ "${COUNT}" -gt 0 ]; then check_anonymized_automatic 0; fi
elif [ "${COUNT}" -eq 0 ] && grep -q '^odoh_servers = .*true.*' "${TOML_FILE}" && grep -q '^dnscrypt_servers = .*false.*' "${TOML_FILE}"; then
PTXT "${INFO} This option allows you to setup wildcard options for both servers (server_name "*") and relays (via=['*']) required for Oblivious DNS-over-HTTPS servers."
if read_yesno "Do you want to use wildcard relay (via=['*']) option for (server_name "*")?"; then USE_WILDCARD="YES"; else USE_WILDCARD="NO"; fi
if [ "${USE_WILDCARD}" = "YES" ]; then choose_relays_automatic_wildcard; elif [ "${USE_WILDCARD}" = "NO" ]; then
PTXT "${INFO} You chose not to use wildcard for relay selection." "${INFO} Instead you will manually choose relays from a list."
choose_relays_automatic_odoh
fi
elif [ "${COUNT}" -eq 0 ] && grep -q '^odoh_servers = .*false.*' "${TOML_FILE}" && grep -q '^dnscrypt_servers = .*false.*' "${TOML_FILE}"; then
check_anonymized_disabled
fi
}
check_swap() {
local SWAP_SIZE
SWAP_SIZE="$(awk '/SwapTotal/ {print $2}' /proc/meminfo)"
if [ "${SWAP_SIZE}" -gt 0 ]; then
PTXT "${INFO} Swap file is already setup"
end_op_message 0
return
fi
inst_swap
}
check_version() {
local RMNSTALL LINSTALL MD5SUM_L MD5SUM_R
if [ -f "${TARG_DIR}/installer" ] && [ -f "${TARG_DIR}/dnscrypt-proxy" ] && [ -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 "^DI_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 "^DI_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 mannor." "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 DI_VERSION=v${RMNSTALL} Available!" \
"${INFO} Run Option 1 of the Installer to upgrade DNScrypt Asuswrt Installer."
AUTO_UPDATE="update"
elif [ "${MD5SUM_R}" = "${MD5SUM_L}" ]; then
PTXT "${INFO} DI_VERSION=v${LINSTALL}"
else
PTXT "${INFO} DI_VERSION=v${LINSTALL}, but a New Minor Update is Available!" \
"${INFO} Run Option 1 of the Installer to upgrade DNScrypt Asuswrt Installer."
AUTO_UPDATE="update"
fi
local LVERSION
LVERSION="$("${TARG_DIR}/dnscrypt-proxy" -version)"
[ -z "${LVERSION}" ] && exit 1
[ -z "${DNSCRYPT_VER}" ] && exit 1
if [ "${DNSCRYPT_VER}" != "${LVERSION}" ]; then
PTXT "${INFO} New DNSCRYPT_VER=${DNSCRYPT_VER} Available!" \
"${INFO} Run Option 1 of the Installer to upgrade DNScrypt Proxy."
AUTO_UPDATE="update"
else
PTXT "${INFO} DNSCRYPT_VER=${LVERSION}"
fi
if [ -f "${TARG_DIR}/manager" ]; then
local MD5SUM_LM MD5SUM_M MURL
varcnt=0
MURL="${URL_GEN}/manager"
until [ -n "${MD5SUM_LM}" ] && [ -n "${MD5SUM_M}" ]; do
[ -z "${MD5SUM_LM}" ] && MD5SUM_LM="$(md5sum "${TARG_DIR}/manager" | cut -d' ' -f1 &)" || true
[ -z "${MD5SUM_M}" ] && MD5SUM_M="$(curl --retry 3 --connect-timeout 3 --retry-delay 1 --max-time $((3 * 5)) --retry-connrefused -fsL "${MURL}" | 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 mannor." "Please check your internet connection, or try again later." "The installer script will now exit."
sleep 3s
exit 1
fi
done
wait
if [ "${MD5SUM_M}" = "${MD5SUM_LM}" ]; then
PTXT "${INFO} Manager file is Up-To-Date!"
else
PTXT "${INFO} New Manager file is Available!" \
"${INFO} Run Option 1 of the Installer to upgrade the Manager File."
AUTO_UPDATE="update"
fi
fi
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_dnscrypt_server() {
local USE_IPV6
if [ "$(nvram get ipv6_service)" != "disabled" ]; then { if read_yesno "Do you want to use DNS server over IPv6 (yes only if your connection has IPv6)?"; then USE_IPV6="true"; else USE_IPV6="false"; fi; }; else USE_IPV6="false"; fi
toml_avars_prep ipv6_servers "${USE_IPV6}"
PTXT "${INFO} Choose DNS resolving load balancing strategy:" \
" 1) p2 (default)" \
" 2) ph" \
" 3) first" \
" 4) random"
read_input_num "Select your strategy" 1 4
case "${CHOSEN}" in
1)
toml_avars_prep lb_strategy "\'p2\'"
;;
2)
toml_avars_prep lb_strategy "\'ph\'"
;;
3)
toml_avars_prep lb_strategy "\'first\'"
;;
4)
toml_avars_prep lb_strategy "\'random\'"
;;
esac
if read_yesno "Do you want to use load balance estimator to adjust resolvers based on latency calculations?"; then USE_LBE="true"; else USE_LBE="false"; fi
toml_avars_prep lb_estimator "${USE_LBE}"
PTXT "${INFO} Choose how your DNS servers are selected:" \
" 1) Automatically (default)" \
" 2) Manually" \
" 3) Static"
read_input_num "Select your mode" 1 3
case "${CHOSEN}" in
1)
choose_dnscrypt_server_auto
if grep -q '^dnscrypt_servers = .*true.*' "${TOML_FILE}" && [ -z "${CRYPT_COUNT}" ]; then CRYPT_COUNT="1"; fi
if read_yesno "Do you want to choose which servers to disable (this can be a long process)?"; then CHOOSE_DISABLED="true"; else CHOOSE_DISABLED="false"; fi
if [ "${CHOOSE_DISABLED}" = "true" ]; then choose_dnscrypt_server_disabled; elif [ "${CHOOSE_DISABLED}" = "false" ]; then toml_avar_disable disabled_server_names; fi
;;
2)
toml_avar_disable disabled_server_names
if read_yesno "Do you only want to use the Oblivious DNS-over-HTTPS protocol?"; then ODOH_ONLY="true"; else ODOH_ONLY="false"; fi
if [ "${ODOH_ONLY}" = "true" ]; then choose_dnscrypt_server_odoh; elif [ "${ODOH_ONLY}" = "false" ]; then choose_dnscrypt_server_manual; fi
;;
3)
toml_avar_disable disabled_server_names
static_chosen 0
;;
esac
}
choose_dnscrypt_server_auto() {
toml_avar_disable server_names
if read_yesno "Use servers that support the DNSCrypt protocol"; then toml_avars_prep dnscrypt_servers true; else toml_avars_prep dnscrypt_servers false; fi
if read_yesno "Use servers that support the DNS-over-HTTPS protocol"; then toml_avars_prep doh_servers true; else toml_avars_prep doh_servers false; fi
if read_yesno "Use servers that support the Oblivious DNS-over-HTTPS protocol"; then toml_avars_prep odoh_servers true; else toml_avars_prep odoh_servers false; fi
if read_yesno "Use only servers that support DNSSEC"; then toml_avars_prep require_dnssec true; else toml_avars_prep require_dnssec false; fi
if read_yesno "Use only servers that do not log user's queries"; then toml_avars_prep require_nolog true; else toml_avars_prep require_nolog false; fi
if read_yesno "Use only servers that do not filter result"; then toml_avars_prep require_nofilter true; else toml_avars_prep require_nofilter false; fi
}
choose_dnscrypt_server_disabled() {
local INDEX
INDEX="$1"
if [ -z "${INDEX}" ]; then
if [ "${USE_IPV6}" = "true" ]; then USE_IPV6="NOMATCH"; else USE_IPV6="6"; fi
local RESOLVERS
PTXT "${INFO} Available DNS servers to disable: "
INDEX="$(awk -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT)' "${TARG_DIR}/public-resolvers.md" | wc -l)"
awk -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT) {printf " "; printf ++i") "$2": "; getline; getline; print}' "${TARG_DIR}/public-resolvers.md"
read_input_num "Please choose DNS server to disable" 1 "${INDEX}"
else
if ! read_input_num "Please choose next DNS server to disable or press n to stop" 1 "${INDEX}" n; then
if grep -q '^odoh_servers = .*true.*' "${TOML_FILE}"; then
if read_yesno "Do you want to choose which Oblivious DNS-over-HTTPS DNS servers to disable?"; then ODOH_DISABLED="true"; else ODOH_DISABLED="false"; fi
if [ "${ODOH_DISABLED}" = "true" ]; then choose_dnscrypt_server_disabled_odoh; fi
fi
toml_avars_prep disabled_server_names "\"[${RESOLVERS}]\""
return
fi
fi
local ITEM
ITEM="$(awk -v INDEX="${CHOSEN}" -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT) {i++} i==INDEX {print $2;exit}' "${TARG_DIR}/public-resolvers.md")"
if PTXT "${RESOLVERS}" | grep -qoF "'${ITEM}'"; then
PTXT "${INFO} ${ITEM} is already set."
else
if [ "${RESOLVERS}" ]; then
RESOLVERS="${RESOLVERS%?}', '${ITEM}'"
else
RESOLVERS="'${ITEM}'"
fi
fi
choose_dnscrypt_server_disabled "${INDEX}"
}
choose_dnscrypt_server_disabled_odoh() {
local INDEX
INDEX="$1"
if [ -z "${INDEX}" ]; then
local ORESOLVERS
PTXT "${INFO} Available DNS servers to disable: "
INDEX="$(awk -v PATT="odohrelay" '/^## / && ($0 !~ PATT)' "${TARG_DIR}/odoh-servers.md" | wc -l)"
awk -v PATT="odohrelay" '/^## / && ($0 !~ PATT) {printf " "; printf ++i") "$2": "; getline; getline; print}' "${TARG_DIR}/odoh-servers.md"
read_input_num "Please choose DNS server to disable" 1 "${INDEX}"
else
if ! read_input_num "Please choose next DNS server to disable or press n to stop" 1 "${INDEX}" n; then
RESOLVERS="${ORESOLVERS}, ${RESOLVERS}"
return
fi
fi
local OITEM
OITEM="$(awk -v INDEX="${CHOSEN}" -v PATT="odohrelay" '/^## / && ($0 !~ PATT) {i++} i==INDEX {print $2;exit}' "${TARG_DIR}/odoh-servers.md")"
if PTXT "${ORESOLVERS}" | grep -qoF "'${OITEM}'"; then
PTXT "${INFO} ${OITEM} is already set."
else
if [ "${ORESOLVERS}" ]; then
ORESOLVERS="${ORESOLVERS%?}', '${OITEM}'"
else
ORESOLVERS="'${OITEM}'"
fi
fi
choose_dnscrypt_server_disabled_odoh "${INDEX}"
}
choose_dnscrypt_server_manual() {
local INDEX
INDEX="$1"
if [ -z "${INDEX}" ]; then
[ "${USE_IPV6}" = "true" ] && USE_IPV6="NOMATCH" || USE_IPV6="6"
local RESOLVERS
toml_avars_prep dnscrypt_servers true doh_servers true require_dnssec false require_nolog false require_nofilter false
PTXT "${INFO} Available DNS servers: "
INDEX="$(awk -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT)' "${TARG_DIR}/public-resolvers.md" | wc -l)"
awk -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT) {printf " "; printf ++i") "$2": "; getline; getline; print}' "${TARG_DIR}/public-resolvers.md"
read_input_num "Please choose DNS server." 1 "${INDEX}"
else
if ! read_input_num "Please choose next DNS server or press n to stop." 1 "${INDEX}" n; then
if read_yesno "Do you want to choose which Oblivious DNS-over-HTTPS DNS servers to enable?"; then ODOH_ENABLE="true"; else ODOH_ENABLE="false"; fi
if [ "${ODOH_ENABLE}" = "true" ]; then choose_dnscrypt_server_odoh; elif [ "${ODOH_ENABLE}" = "false" ]; then toml_avars_prep odoh_servers "${ODOH_ENABLE}"; fi
if read_yesno "Do you want to add any static servers?"; then ADD_STATIC="YES"; else ADD_STATIC="NO"; fi
if [ "${ADD_STATIC}" = "YES" ]; then static_chosen 0; fi
toml_avars_prep server_names "\"[${RESOLVERS}]\""
return
fi
fi
local ITEM
ITEM="$(awk -v INDEX="${CHOSEN}" -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT) {i++} i==INDEX {print $2;exit}' "${TARG_DIR}/public-resolvers.md")"
if PTXT "${RESOLVERS}" | grep -qoF "'${ITEM}'"; then
PTXT "${INFO} ${ITEM} is already set"
else
if [ "${RESOLVERS}" ]; then
RESOLVERS="${RESOLVERS%?}', '${ITEM}'"
else
RESOLVERS="'${ITEM}'"
fi
fi
choose_dnscrypt_server_manual "${INDEX}"
}
choose_dnscrypt_server_odoh() {
local INDEX
INDEX="$1"
if [ -z "${INDEX}" ]; then
local ORESOLVERS
toml_avars_prep odoh_servers true
PTXT "${INFO} Available DNS servers: "
INDEX="$(awk -v PATT="odohrelay" '/^## / && ($0 !~ PATT)' "${TARG_DIR}/odoh-servers.md" | wc -l)"
awk -v PATT="odohrelay" '/^## / && ($0 !~ PATT) {printf " "; printf ++i") "$2": "; getline; getline; print}' "${TARG_DIR}/odoh-servers.md"
read_input_num "Please choose DNS server." 1 "${INDEX}"
else
if ! read_input_num "Please choose next DNS server or press n to stop." 1 "${INDEX}" n; then
if [ "${ODOH_ONLY}" = "true" ]; then toml_avars_prep server_names "\"[${ORESOLVERS}]\"" dnscrypt_servers false doh_servers false require_dnssec false require_nolog false require_nofilter false; elif [ "${ODOH_ONLY}" = "false" ]; then RESOLVERS="${ORESOLVERS}, ${RESOLVERS}"; fi
return
fi
fi
local OITEM
OITEM="$(awk -v INDEX="${CHOSEN}" -v PATT="odohrelay" '/^## / && ($0 !~ PATT) {i++} i==INDEX {print $2;exit}' "${TARG_DIR}/odoh-servers.md")"
if PTXT "${ORESOLVERS}" | grep -qoF "'${OITEM}'"; then
PTXT "${INFO} ${OITEM} is already set"
else
if [ "${ORESOLVERS}" ]; then
ORESOLVERS="${ORESOLVERS%?}', '${OITEM}'"
else
ORESOLVERS="'${OITEM}'"
fi
fi
choose_dnscrypt_server_odoh "${INDEX}"
}
choose_relays_automatic() {
local INDEX
INDEX="$1"
if [ -z "${INDEX}" ]; then
if [ "${USE_IPV6}" = "true" ]; then USE_IPV6="NOMATCH"; else USE_IPV6="6"; fi
local RELAYS
PTXT "${INFO} Available Relay servers: "
INDEX="$(awk -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT)' "${TARG_DIR}/relays.md" | wc -l)"
awk -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT) {printf " "; printf ++i") "$2": "; getline; getline; print}' "${TARG_DIR}/relays.md"
read_input_num "Please choose RELAY server" 1 "${INDEX}"
else
if ! read_input_num "Please choose next RELAY server or press n to stop" 1 "${INDEX}" n; then
if grep -q '^odoh_servers = .*true.*' "${TOML_FILE}"; then
PTXT "${INFO} Now to pick relays for Oblivious DNS-over-HTTPS DNS servers."
choose_relays_automatic_odoh
fi
if read_yesno "Do you want to add any static relays?"; then ADD_STATIC="YES"; else ADD_STATIC="NO"; fi
if [ "${ADD_STATIC}" = "YES" ]; then static_chosen_relays 0; fi
toml_avars_prep routes "\"[ { server_name='*', via=[${RELAYS}] } ]\""
return
fi
fi
local ITEM
ITEM="$(awk -v INDEX="${CHOSEN}" -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT) {i++} i==INDEX {print $2;exit}' "${TARG_DIR}/relays.md")"
if PTXT "${RELAYS}" | grep -qoF "'${ITEM}'"; then
PTXT "${INFO} ${ITEM} is already set."
else
if [ "${RELAYS}" ]; then
RELAYS="${RELAYS%?}', '${ITEM}'"
else
RELAYS="'${ITEM}'"
fi
fi
choose_relays_automatic "${INDEX}"
}
choose_relays_automatic_odoh() {
local INDEX
INDEX="$1"
if [ -z "${INDEX}" ]; then
local ORELAYS
PTXT "${INFO} Available Relay servers: "
INDEX="$(awk -v PATT="odoh-" '/^## / && ($0 !~ PATT)' "${TARG_DIR}/odoh-relays.md" | wc -l)"
awk -v PATT="odoh-" '/^## / && ($0 !~ PATT) {printf " "; printf ++i") "$2": "; getline; getline; print}' "${TARG_DIR}/odoh-relays.md"
read_input_num "Please choose RELAY server" 1 "${INDEX}"
else
if ! read_input_num "Please choose next RELAY server or press n to stop" 1 "${INDEX}" n; then
if grep -q '^dnscrypt_servers = .*false.*' "${TOML_FILE}" || [ "${COUNT}" -eq 0 ]; then toml_avars_prep routes "\"[ { server_name='*', via=[${ORELAYS}] } ]\""; else RELAYS="${ORELAYS}, ${RELAYS}"; fi
return
fi
fi
local OITEM
OITEM="$(awk -v INDEX="${CHOSEN}" -v PATT="odoh-" '/^## / && ($0 !~ PATT) {i++} i==INDEX {print $2;exit}' "${TARG_DIR}/odoh-relays.md")"
if PTXT "${ORELAYS}" | grep -qoF "'${OITEM}'"; then
PTXT "${INFO} ${OITEM} is already set."
else
if [ "${ORELAYS}" ]; then
ORELAYS="${ORELAYS%?}', '${OITEM}'"
else
ORELAYS="'${OITEM}'"
fi
fi
choose_relays_automatic_odoh "${INDEX}"
}
choose_relays_automatic_wildcard() {
toml_avars_prep routes "\"[ { server_name='*', via=['*'] } ]\""
}
choose_relays_manual() {
local INDEX
INDEX="$1"
if [ -z "${INDEX}" ]; then
if [ "${USE_IPV6}" = "true" ]; then USE_IPV6="NOMATCH"; else USE_IPV6="6"; fi
local RELAYS
PTXT "${INFO} Available Relay servers: "
INDEX="$(awk -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT)' "${TARG_DIR}/relays.md" | wc -l)"
awk -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT) {printf " "; printf ++i") "$2": "; getline; getline; print}' "${TARG_DIR}/relays.md"
read_input_num "Please choose RELAY server" 1 "${INDEX}"
else
if ! read_input_num "Please choose next RELAY server or press n to stop" 1 "${INDEX}" n; then
if read_yesno "Do you want to add any static relays?"; then ADD_STATIC="YES"; else ADD_STATIC="NO"; fi
if [ "${ADD_STATIC}" = "YES" ]; then static_chosen_relays 0; fi
if [ "${COUNT}" -eq 0 ]; then
toml_avars_prep routes "\"[ { server_name='${SERVER}', via=[${RELAYS}] } ]\""
else
toml_nvars_replace "} ]" "}, { server_name='${SERVER}', via=[${RELAYS}] } ]" "${TOML_FILE}"
fi
return
fi
fi
local ITEM
ITEM="$(awk -v INDEX="${CHOSEN}" -v PATT="${USE_IPV6}" '/^## / && ($0 !~ PATT) {i++} i==INDEX {print $2;exit}' "${TARG_DIR}/relays.md")"
if PTXT "${RELAYS}" | grep -qoF "'${ITEM}'"; then
PTXT "${INFO} ${ITEM} is already set."
else
if [ "${RELAYS}" ]; then
RELAYS="${RELAYS%?}', '${ITEM}'"
else
RELAYS="'${ITEM}'"
fi
fi
choose_relays_manual "${INDEX}"
}
choose_relays_manual_odoh() {
local INDEX
INDEX="$1"
if [ -z "${INDEX}" ]; then
local RELAYS
PTXT "${INFO} Available Relay servers: "
INDEX="$(awk -v PATT="odoh-" '/^## / && ($0 !~ PATT)' "${TARG_DIR}/odoh-relays.md" | wc -l)"
awk -v PATT="odoh-" '/^## / && ($0 !~ PATT) {printf " "; printf ++i") "$2": "; getline; getline; print}' "${TARG_DIR}/odoh-relays.md"
read_input_num "Please choose RELAY server" 1 "${INDEX}"
else
if ! read_input_num "Please choose next RELAY server or press n to stop" 1 "${INDEX}" n; then
if read_yesno "Do you want to add any static relays?"; then ADD_STATIC="YES"; else ADD_STATIC="NO"; fi
if [ "${ADD_STATIC}" = "YES" ]; then static_chosen_relays 0; fi
if [ "${COUNT}" -eq 0 ]; then
toml_avars_prep routes "\"[ { server_name='${SERVER}', via=[${RELAYS}] } ]\""
else
toml_nvars_replace "} ]" "}, { server_name='${SERVER}', via=[${RELAYS}] } ]" "${TOML_FILE}"
fi
return
fi
fi
local ITEM
ITEM="$(awk -v INDEX="${CHOSEN}" -v PATT="odoh-" '/^## / && ($0 !~ PATT) {i++} i==INDEX {print $2;exit}' "${TARG_DIR}/odoh-relays.md")"
if PTXT "${RELAYS}" | grep -qoF "'${ITEM}'"; then
PTXT "${INFO} ${ITEM} is already set."
else
if [ "${RELAYS}" ]; then
RELAYS="${RELAYS%?}', '${ITEM}'"
else
RELAYS="'${ITEM}'"
fi
fi
choose_relays_manual_odoh "${INDEX}"
}
choose_relays_manual_wildcard() {
if [ "${COUNT}" -eq 0 ]; then
toml_avars_prep routes "\"[ { server_name='${SERVER}', via=['*'] } ]\""
else
toml_nvars_replace "} ]" "}, { server_name='${SERVER}', via=['*'] } ]" "${TOML_FILE}"
fi
}
cleanup() {
rm -rf "${TARG_DIR}/dnscrypt-fw-rules" "${TARG_DIR}/dnscrypt-start" "${TARG_DIR}/dnsmasq-dnscrypt-reconfig" "${TARG_DIR}/fake-hwclock*" "${TARG_DIR}/init-start" "${TARG_DIR}/services-stop"
del_jffs_script /jffs/scripts/wan-start dnscrypt-start
del_jffs_script /jffs/scripts/openvpn-event
del_jffs_script /jffs/scripts/firewall-start
del_jffs_script /jffs/scripts/wan-start
}
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 ${TARG_DIR}/" "${TARG}" | grep -v "$(_quote "$2")" | cut -d':' -f1)"
else
LINE_NUM="$(grep -n -F "[ -x ${TARG_DIR}/" "${TARG}" | grep "$(_quote "$2")" | cut -d':' -f1)"
fi
else
LINE_NUM="$(grep -n -F "[ -x ${TARG_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