forked from thuantran/dnscrypt-asuswrt-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstaller
executable file
·1892 lines (1811 loc) · 74.3 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
########################################################################################################
# _____ _ _ _______ _______ _______ __ __ ______ _____ _ _____ _ _ #
# /\ / ____| | | |/ ____\ \ / | __ |__ __| | \/ | ____| __ \| | |_ _| \ | |#
# / \ | (___ | | | | (___ \ \ /\ / /| |__) | | |______| \ / | |__ | |__) | | | | | \| |#
# / /\ \ \___ \| | | |\___ \ \ \/ \/ / | _ / | |______| |\/| | __| | _ /| | | | | . ` |#
# / ____ \ ____) | |__| |____) | \ /\ / | | \ \ | | | | | | |____| | \ \| |____ _| |_| |\ |#
#/_/ \_|_____/ \____/|_____/ \/ \/ |_| \_\ |_| |_| |_|______|_| \_|______|_____|_| \_|#
# #
# _____ _ _ _____ _____ _______ _______ _______ _____ _____ ______ ___ __ #
#| __ \| \ | |/ ____|/ ____| __ \ \ / | __ |__ __| | __ \| __ \ / __ \ \ / \ \ / / #
#| | | | \| | (___ | | | |__) \ \_/ /| |__) | | |______| |__) | |__) | | | \ V / \ \_/ / #
#| | | | . ` |\___ \| | | _ / \ / | ___/ | |______| ___/| _ /| | | |> < \ / #
#| |__| | |\ |____) | |____| | \ \ | | | | | | | | | | \ \| |__| / . \ | | #
#|_____/|_| \_|_____/ \_____|_| \_\ |_| |_| |_| |_| |_| \_\\____/_/ \_\ |_| #
# #
# _____ _ _ _____ _______ _ _ ______ _____ Original Author: #
#|_ _| \ | |/ ____|__ __|/\ | | | | | ____| __ \ bigeyes0x0 #
# | | | \| | (___ | | / \ | | | | | |__ | |__) | Current Maintainer: #
# | | | . ` |\___ \ | | / /\ \ | | | | | __| | _ / SomeWhereOverTheRainBow #
# _| |_| |\ |____) | | |/ ____ \| |____| |____| |____| | \ \ #
#|_____|_| \_|_____/ |_/_/ \_|______|______|______|_| \_\ v2.3.4 #
# #
########################################################################################################
# shellcheck disable=SC1001
# shellcheck disable=SC1083
# shellcheck disable=SC2010
# shellcheck disable=SC2015
# shellcheck disable=SC2016
# shellcheck disable=SC2034
# shellcheck disable=SC2124
# shellcheck disable=SC2155
# shellcheck disable=SC2181
# shellcheck disable=SC3043
# shellcheck disable=SC3045
# shellcheck disable=SC3057
# shellcheck disable=SC3060
DI_VERSION="v2.3.4"
readonly LATEST_URL="https://api.github.com/repos/jedisct1/dnscrypt-proxy/releases/latest"
readonly DNSCRYPT_VER="$(curl -sL "$LATEST_URL" | grep "tag_name" | head -1 | cut -d \" -f 4)"
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"
readonly SCRIPT_LOC="$(readlink -f "$0")"
readonly BOLD="$(printf "\033[1m")"
readonly NORM="$(printf "\033[0m")"
readonly INFO="$(printf "%s" "${BOLD} Info: ${NORM}")"
readonly ERROR="$(printf "%s" "${BOLD} *** Error: ${NORM}")"
readonly WARNING="$(printf "%s" "${BOLD} * Warning: ${NORM}")"
readonly INPUT="$(printf "%s" "${BOLD} => ${NORM}")"
_quote() {
printf "%s\n" "$1" | sed 's/[]\/()$*.^|[]/\\&/g'
}
PTXT() {
case "$1" in
-n)
for i in "${@:2}"; do
printf "%s" "$i"
done
;;
*)
for i in "$@"; do
printf "%s\n" "$i"
done
;;
esac
}
backup_restore () {
if [ "$1" = "BACKUP" ] && [ -d "$TARG_DIR" ] && [ -f "${TARG_DIR}/dnscrypt-proxy" ]; then
PTXT "$INFO This operation will backup dnscrypt-proxy(<4MB)to jffs partition." \
"$INFO Please wait a moment."
if [ -f "${BASE_DIR}/backup_dnscrypt.tar.gz" ]; then
PTXT "$INFO There is an old backup detected."
local USE_OLD
read_yesno "Do you want to continue?(this will remove the old backup)" && USE_OLD="NO" || USE_OLD="YES"
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
tar -czvf "${BASE_DIR}/backup_dnscrypt.tar.gz" -C "$TARG_DIR" ../dnscrypt/ >/dev/null 2>&1
PTXT "$INFO Backup complete"
[ "$2" -ne 0 ] && 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"
chmod 644 "${TARG_DIR}/public-resolvers.md" \
"${TARG_DIR}/public-resolvers.md.minisig" \
"${TARG_DIR}/relays.md" \
"${TARG_DIR}/relays.md.minisig" \
"${TARG_DIR}/odoh-servers.md" \
"${TARG_DIR}/odoh-servers.md.minisig" \
"${TARG_DIR}/odoh-relays.md" \
"${TARG_DIR}/odoh-relays.md.minisig" \
"${TARG_DIR}/dnscrypt-resolvers.csv" \
"${TARG_DIR}/dnscrypt-resolvers.csv.minisig"
chown nobody:nobody "${TARG_DIR}/public-resolvers.md" \
"${TARG_DIR}/public-resolvers.md.minisig" \
"${TARG_DIR}/relays.md" \
"${TARG_DIR}/relays.md.minisig" \
"${TARG_DIR}/odoh-servers.md" \
"${TARG_DIR}/odoh-servers.md.minisig" \
"${TARG_DIR}/odoh-relays.md" \
"${TARG_DIR}/odoh-relays.md.minisig" \
"${TARG_DIR}/dnscrypt-resolvers.csv" \
"${TARG_DIR}/dnscrypt-resolvers.csv.minisig"
write_manager_script /jffs/scripts/dnsmasq.postconf dnsmasq
write_manager_script /jffs/scripts/init-start init-start
setup_dnscrypt
if [ "$?" -ne 0 ]; then
end_op_message 1
return
fi
PTXT "$INFO Starting dnscrypt-proxy..."
${TARG_DIR}/manager dnscrypt-start
sleep 1
if [ -z "$(pidof dnscrypt-proxy)" ]; then
PTXT "$ERROR Couldn't start dnscrypt-proxy" \
"$ERROR Please send WebUI System Log to dev"
end_op_message 1
return
fi
service restart_dnsmasq >/dev/null 2>&1
manager_monitor_restart
PTXT "$INFO Backup restored!" \
"$INFO - Add swap" \
"$INFO - Add a RNG" \
"$INFO - Set your timezone"
end_op_message 0
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
}
check_dnscrypt_toml () {
[ ! -f "$TOML_FILE" ] && return
PTXT "$INFO Checking dnscrypt-proxy configuration..."
${TARG_DIR}/dnscrypt-proxy -check -config "$TOML_FILE"
if [ "$?" -ne 0 ]; 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" ]; then
PTXT "$ERROR Potential stubby installation detected." \
"$ERROR Please remove before attempting to continue." \
"$ERROR Exiting..."
exit 1
fi
local DNSPRIV
DNSPRIV="$(nvram get dnspriv_enable)"
local DHCPDNS
DHCPDNS="$(nvram get dhcpd_dns_router)"
local DHCPDNS1
DHCPDNS1="$(nvram get nvram set dhcp_dns1_x)"
local DHCPDNS2
DHCPDNS1="$(nvram get nvram set dhcp_dns2_x)"
if [ "$DNSPRIV" -ne 0 ]; then
PTXT "$INFO Setting up Correct DNS Environment."
nvram set dnspriv_enable=0
nvram commit
killall -q -9 stubby
check_dns_environment x
elif [ "$DHCPDNS" -ne 1 ] || [ -n "$DHCPDNS1" ] || [ -n "$DHCPDNS2" ] || [ -n "$1" ]; then
nvram set dhcp_dns1_x=""
nvram set dhcp_dns2_x=""
nvram set dhcpd_dns_router="1"
nvram commit
service restart_dnsmasq >/dev/null 2>&1
while { [ "$(ping 1.1.1.1 -c1 -W2 >/dev/null 2>&1; printf "%s" "$?")" = "0" ] && [ "$(nslookup google.com 127.0.0.1 >/dev/null 2>&1; printf "%s" "$?")" != "0" ]; }; do sleep 1; done
fi
PTXT "$INFO DNS Environment is Ready."
}
check_dns_filter () {
local DNSFIL
DNSFIL="$(nvram get dnsfilter_enable_x)"
local USE_SOME
if [ "$1" -eq 0 ] && [ "$DNSFIL" -ne 0 ]; then
nvram set dnsfilter_enable_x="0"
nvram commit
service "restart_firewall;restart_dnsmasq" >/dev/null 2>&1
PTXT "$INFO DNS will not be forced through this proxy."
return
fi
if [ "$1" -eq 1 ] && [ "$DNSFIL" -ne 1 ]; then
nvram set dnsfilter_enable_x="1"
USE_SOME=1
elif [ "$1" -eq 1 ] && [ "$DNSFIL" -ne 0 ]; then
PTXT "$INFO DNSFilter is Already on." \
"$INFO You can choose to keep any custom dnsfilter values and only redirect non-custom traffic or send all traffic through this proxy."
read_yesno "Do you want to redirect only SOME DNS resolutions on your network through this proxy?" && USE_SOME="0" || USE_SOME="1"
fi
if [ "$USE_SOME" -eq 0 ]; then
nvram set dnsfilter_mode="11"
nvram commit
service "restart_firewall;restart_dnsmasq" >/dev/null 2>&1
PTXT "$INFO DNSFilter is set to control DNS through this proxy, while leaving any Custom Rules and Values."
return
fi
if [ "$USE_SOME" -eq 1 ]; then
nvram set dnsfilter_custom1=""
nvram set dnsfilter_custom2=""
nvram set dnsfilter_custom3=""
nvram set dnsfilter_mode="11"
nvram set dnsfilter_rulelist=""
nvram set dnsfilter_rulelist1=""
nvram set dnsfilter_rulelist2=""
nvram set dnsfilter_rulelist3=""
nvram set dnsfilter_rulelist4=""
nvram set dnsfilter_rulelist5=""
nvram set dhcp_dns1_x=""
nvram set dhcp_dns2_x=""
nvram set dhcpd_dns_router="1"
nvram commit
service "restart_firewall;restart_dnsmasq" >/dev/null 2>&1
PTXT "$INFO DNS is set to redirect All DNS resolutions through this proxy."
return
fi
}
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
local JFFS2_ENABLED
local 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
local 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."
read_yesno "Do you want to use wildcard relay (via=['*']) option?" && USE_WILDCARD="YES" || USE_WILDCARD="NO"
[ "$USE_WILDCARD" = "YES" ] && choose_relays_automatic_wildcard || PTXT "$INFO You chose not to use wildcard for relay selection." "$INFO Instead you will manually choose relays from a list."
[ "$USE_WILDCARD" = "NO" ] && choose_relays_automatic
read_yesno "Do you want to skip using resolvers that are incompatible with anonymization instead of using them directly?" && USE_BROKEN="true" || USE_BROKEN="false"
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_USER="$(awk -F'=' '/OPENDNS_USER/ {print $2}' "$CONF_FILE")"
local OPENDNS_PASSWORD
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
read_yesno "Do you want to set up OpenDNS account ip update?" && opendns_authen 1 || opendns_authen 0
fi
else
read_yesno "Do you want to set up OpenDNS account ip update?" && opendns_authen 1 || opendns_authen 0
fi
else
opendns_authen 0
fi
}
check_relays () {
local DNSCRYPT_ARGS
local DNSCRYPT
local ODOH_ARGS
local ODOH
local FRAGSBLOCKED_ARGS
local FRAGSBLOCKED
local VARSARGS
local SERVER
local COUNT
local NUMFRAG
local NUMCRYPT
local 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")
read_yesno "Do you want to add relays for $SERVER?" && ADD_RELAYS="YES" || ADD_RELAYS="NO"
[ "$ADD_RELAYS" = "YES" ] && PTXT "$INFO You may manually choose relays for $SERVER or you may specify wildcard relay (via=['*'])."
if [ "$COUNT" -eq 0 ] && [ "$ADD_RELAYS" = "YES" ]; then
toml_avar_enable routes
read_yesno "Do you want to use wildcard relay (via=['*']) option for $SERVER?" && USE_WILDCARD="YES" || USE_WILDCARD="NO"
[ "$USE_WILDCARD" = "YES" ] && choose_relays_manual_wildcard || PTXT "$INFO You chose not to use wildcard for relay selection." "$INFO Instead you will manually choose relays from a list."
[ "$USE_WILDCARD" = "NO" ] && choose_relays_manual
COUNT="$((COUNT + 1))"
elif [ "$COUNT" -gt 0 ] && [ "$ADD_RELAYS" = "YES" ]; then
read_yesno "Do you want to use wildcard relay (via=['*']) option for $SERVER?" && USE_WILDCARD="YES" || USE_WILDCARD="NO"
[ "$USE_WILDCARD" = "YES" ] && choose_relays_manual_wildcard || PTXT "$INFO You chose not to use wildcard for relay selection." "$INFO Instead you will manually choose relays from a list."
[ "$USE_WILDCARD" = "NO" ] && choose_relays_manual
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
read_yesno "Do you want to use wildcard relay (via=['*']) option for $SERVER?" && USE_WILDCARD="YES" || USE_WILDCARD="NO"
[ "$USE_WILDCARD" = "YES" ] && choose_relays_manual_wildcard || PTXT "$INFO You chose not to use wildcard for relay selection." "$INFO Instead you will manually choose relays from a list."
[ "$USE_WILDCARD" = "NO" ] && choose_relays_manual_odoh
COUNT="$((COUNT + 1))"
elif [ "$COUNT" -gt 0 ]; then
read_yesno "Do you want to use wildcard relay (via=['*']) option for $SERVER?" && USE_WILDCARD="YES" || USE_WILDCARD="NO"
[ "$USE_WILDCARD" = "YES" ] && choose_relays_manual_wildcard || PTXT "$INFO You chose not to use wildcard for relay selection." "$INFO Instead you will manually choose relays from a list."
[ "$USE_WILDCARD" = "NO" ] && choose_relays_manual_odoh
fi
;;
"$FRAGSBLOCKED")
if [ "$NUMFRAG" -eq 0 ] && [ "$COUNT" -gt 0 ]; then
local USE_BROKEN
read_yesno "Do you want to skip using resolvers that are incompatible with anonymization instead of using them directly?" && USE_BROKEN="true" || USE_BROKEN="false"
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" ]; then
PTXT "$INFO To continue, you may still define a default route for all compatible DNSCrypt servers by selecting wildcard option for servers and/or relays."
read_yesno "Do you still want to setup wildcard options for servers (server_name "*") and/or relays (via=['*']) for all compatible DNSCrypt servers?" && check_anonymized_automatic 0 || check_anonymized_disabled
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/or relays (via=['*']) for all compatible servers."
read_yesno "Do you only want relays for Oblivious DNS-over-HTTPS Servers?" && choose_relays_automatic_odoh || COUNT="$((COUNT + 1))"
[ "$COUNT" -gt 0 ] && check_anonymized_automatic 0
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 servers (server_name "*") and/or relays (via=['*']) required for Oblivious DNS-over-HTTPS servers."
read_yesno "Do you want to use wildcard relay (via=['*']) option?" && USE_WILDCARD=YES || USE_WILDCARD=NO
[ "$USE_WILDCARD" = "YES" ] && choose_relays_automatic_wildcard || PTXT "$INFO You chose not to use wildcard for relay selection." "$INFO Instead you will manually choose relays from a list."
[ "$USE_WILDCARD" = "NO" ] && choose_relays_automatic_odoh
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 () {
if [ -f "${TARG_DIR}/installer" ] && [ -f "${TARG_DIR}/dnscrypt-proxy" ]&& [ -z "$2" ]; then
local RMNSTALL
local LINSTALL
local MD5SUM_L
local MD5SUM_R
local NW_STATE
local RES_STATE
[ -z "$1" ] && NW_STATE="$(ping 1.1.1.1 -c1 -W2 >/dev/null 2>&1; printf "%s" "$?")"
[ -z "$1" ] && RES_STATE="$(nslookup google.com 127.0.0.1 >/dev/null 2>&1; printf "%s" "$?")"
LINSTALL="$(awk '{ print }' "${TARG_DIR}/installer" | grep -m1 "^DI_VERSION=" | grep -oE '[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})')"
RMNSTALL="$(curl -sL "${RURL}/installer" | grep -m1 "^DI_VERSION=" | grep -oE '[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})')"
MD5SUM_L="$(md5sum "${TARG_DIR}/installer" | cut -d' ' -f1)"
MD5SUM_R="$(curl -fsL "${RURL}/installer" | md5sum | awk '{print $1}')"
if { [ -n "$LINSTALL" ] && [ -n "$RMNSTALL" ]; } || { [ "$NW_STATE" = "0" ] && [ "$RES_STATE" = "0" ]; }; 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."
else
PTXT "$INFO DNSCRYPT_VER=${LVERSION}"
fi
if [ -f "${TARG_DIR}/manager" ]; then
local MD5SUM_LM
local MD5SUM_M
local L_GEN
MD5SUM_LM="$(md5sum "${TARG_DIR}/manager" | cut -d' ' -f1)"
MURL="${URL_GEN}/manager"
MD5SUM_M="$(curl -fsL "$MURL" | md5sum | awk '{print $1}')"
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."
fi
fi
elif { [ -z "$LINSTALL" ] && [ -z "$RMNSTALL" ]; } || { [ "$NW_STATE" = "0" ] && [ "$RES_STATE" != "0" ]; }; then
[ -z "$1" ] && while { [ "$NW_STATE" = "0" ] && [ "$RES_STATE" != "0" ]; }; do sleep 1; NW_STATE="$(ping 1.1.1.1 -c1 -W2 >/dev/null 2>&1; printf "%s" "$?")"; RES_STATE="$(nslookup google.com 127.0.0.1 >/dev/null 2>&1; printf "%s" "$?")"; done && check_version x
[ -n "$1" ] && check_version x x
fi
fi
}
choose_dnscrypt_server () {
local USE_IPV6
if [ "$(nvram get ipv6_service)" != "disabled" ]; then read_yesno "Do you want to use DNS server over IPv6 (yes only if your connection has IPv6)?" && USE_IPV6="true" || USE_IPV6="false"; 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
read_yesno "Do you want to use load balance estimator to adjust resolvers based on latency calculations?" && USE_LBE="true" || USE_LBE="false"
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"; then CRYPT_COUNT="1"; fi
read_yesno "Do you want to choose which servers to disable (this can be a long process)?" && CHOOSE_DISABLED="true" || CHOOSE_DISABLED="false"
[ "$CHOOSE_DISABLED" = "true" ] && choose_dnscrypt_server_disabled
[ "$CHOOSE_DISABLED" = "false" ] && toml_avar_disable disabled_server_names
;;
2)
toml_avar_disable disabled_server_names
read_yesno "Do you only want to use the Oblivious DNS-over-HTTPS protocol?" && ODOH_ONLY="true" || ODOH_ONLY="false"
[ "$ODOH_ONLY" = "true" ] && choose_dnscrypt_server_odoh
[ "$ODOH_ONLY" = "false" ] && choose_dnscrypt_server_manual
;;
3)
toml_avar_disable disabled_server_names
static_chosen 0
;;
esac
}
choose_dnscrypt_server_auto () {
toml_avar_disable server_names
read_yesno "Use servers that support the DNSCrypt protocol" && toml_avars_prep dnscrypt_servers true || toml_avars_prep dnscrypt_servers false
read_yesno "Use servers that support the DNS-over-HTTPS protocol" && toml_avars_prep doh_servers true || toml_avars_prep doh_servers false
read_yesno "Use servers that support the Oblivious DNS-over-HTTPS protocol" && toml_avars_prep odoh_servers true || toml_avars_prep odoh_servers false
read_yesno "Use only servers that support DNSSEC" && toml_avars_prep require_dnssec true || toml_avars_prep require_dnssec false
read_yesno "Use only servers that do not log user's queries" && toml_avars_prep require_nolog true || toml_avars_prep require_nolog false
read_yesno "Use only servers that do not filter result" && toml_avars_prep require_nofilter true || toml_avars_prep require_nofilter false
}
choose_dnscrypt_server_disabled () {
local INDEX
INDEX="$1"
if [ -z "$INDEX" ]; then
[ "$USE_IPV6" = "true" ] && USE_IPV6="NOMATCH" || USE_IPV6="6"
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
read_input_num "Please choose next DNS server to disable or press n to stop" 1 "$INDEX" n
if [ "$?" -eq 1 ]; then
if grep -q '^odoh_servers = .*true.*' "$TOML_FILE"; then
read_yesno "Do you want to choose which Oblivious DNS-over-HTTPS DNS servers to disable?" && ODOH_DISABLED="true" || ODOH_DISABLED="false"
[ "$ODOH_DISABLED" = "true" ] && choose_dnscrypt_server_disabled_odoh
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
read_input_num "Please choose next DNS server to disable or press n to stop" 1 "$INDEX" n
if [ "$?" -eq 1 ]; 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
read_input_num "Please choose next DNS server or press n to stop." 1 "$INDEX" n
if [ "$?" -eq 1 ]; then
read_yesno "Do you want to choose which Oblivious DNS-over-HTTPS DNS servers to enable?" && ODOH_ENABLE="true" || ODOH_ENABLE="false"
[ "$ODOH_ENABLE" = "true" ] && choose_dnscrypt_server_odoh
[ "$ODOH_ENABLE" = "false" ] && toml_avars_prep odoh_servers false
read_yesno "Do you want to add any static servers?" && ADD_STATIC="YES" || ADD_STATIC="NO"
[ "$ADD_STATIC" = "YES" ] && static_chosen 0
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
read_input_num "Please choose next DNS server or press n to stop." 1 "$INDEX" n
if [ "$?" -eq 1 ]; then
[ "$ODOH_ONLY" = "true" ] && toml_avars_prep server_names "\"[${ORESOLVERS}]\"" dnscrypt_servers false doh_servers false require_dnssec false require_nolog false require_nofilter false
[ "$ODOH_ONLY" = "false" ] && 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_odoh "$INDEX"
}
choose_relays_automatic () {
local INDEX
INDEX="$1"
if [ -z "$INDEX" ]; then
[ "$USE_IPV6" = "true" ] && USE_IPV6="NOMATCH" || USE_IPV6="6"
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
read_input_num "Please choose next RELAY server or press n to stop" 1 "$INDEX" n
if [ "$?" -eq 1 ]; 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
read_yesno "Do you want to add any static relays?" && ADD_STATIC="YES" || ADD_STATIC="NO"
[ "$ADD_STATIC" = "YES" ] && static_chosen_relays 0
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
read_input_num "Please choose next RELAY server or press n to stop" 1 "$INDEX" n
if [ "$?" -eq 1 ]; 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
[ "$USE_IPV6" = "true" ] && USE_IPV6="NOMATCH" || USE_IPV6="6"
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
read_input_num "Please choose next RELAY server or press n to stop" 1 "$INDEX" n
if [ "$?" -eq 1 ]; then
read_yesno "Do you want to add any static relays?" && ADD_STATIC="YES" || ADD_STATIC="NO"
[ "$ADD_STATIC" = "YES" ] && static_chosen_relays 0
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
read_input_num "Please choose next RELAY server or press n to stop" 1 "$INDEX" n
if [ "$?" -eq 1 ]; then
read_yesno "Do you want to add any static relays?" && ADD_STATIC="YES" || ADD_STATIC="NO"
[ "$ADD_STATIC" = "YES" ] && static_chosen_relays 0
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 () {
mkdir -p "$1"
if [ "$?" -ne 0 ]; then
PTXT "$ERROR Unable to create $1!"
return 1
fi
}
del_between_magic () {
local TARG
TARG="$1"
local MAGIC
MAGIC="$2"
[ -f "$TARG" ] || return
local BOUNDS
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
TARG="$1"
local LINE_NUM
local LINE_ABOVE
[ -f "$TARG" ] || return
if [ "$2" ]; then
local OP
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
TARG="$1"; shift
local PERM
PERM="$1"; shift
local URL
local FILENAME
local MD5SUM_OLD
local MD5SUM_CURR
for URL in "$@"; do
FILENAME="$(basename "$URL")"
MD5SUM_OLD="$([ -f "${TARG}/${FILENAME}" ] && md5sum "${TARG}/${FILENAME}" | cut -d' ' -f1)"
MD5SUM_CURR="$(curl -fsL "$URL" | md5sum | awk '{print $1}')"
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"
curl -L -k -s "$URL" -o "${TARG}/${FILENAME}"
if [ "$?" -eq 0 ]; then
chmod "$PERM" "${TARG}/${FILENAME}"
break
fi
COUNT="$((COUNT+1))"
done
if [ "$COUNT" -eq 3 ]; then
PTXT "$ERROR Unable to download ${BOLD}${URL}${NORM}"
fi
fi
done
}
end_op_message () {
[ "$1" = "0" ] && PTXT "$INFO Operation completed. You can quit or continue"
[ "$1" = "1" ] && PTXT "$INFO Operation aborted. You can quit or continue"
PTXT "====================================================="
PTXT " "
PTXT " "
sleep 3 && 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 && exit; fi
}
end_op_header () {
sed -n -e "1,$(($(grep -wn 'menu () {' "$0" | cut -d':' -f1)-1))p" "$0" > "${0}".tmp && sh "${0}".tmp && menu && rm -rf "${0}".tmp
}
inst_dnscrypt () {
local DNSCRYPT_TAR
DNSCRYPT_TAR=dnscrypt-proxy-${DNSCRYPT_ARCH}-${DNSCRYPT_VER}.tar.gz
local RESOLVERS_URL_PREFIX
RESOLVERS_URL_PREFIX="https://download.dnscrypt.info/resolvers-list/v3/"
local CRYPT_RESOLVERS
CRYPT_RESOLVERS="https://raw.githubusercontent.com/DNSCrypt/dnscrypt-resolvers/master/v1/"
if [ -z "$1" ]; then
if [ ! -d "$TARG_DIR" ] && [ -f "${BASE_DIR}/backup_dnscrypt.tar.gz" ]; then
PTXT "$INFO Backup is detected."
local USE_OLD
read_yesno "Do you want Restore instead?" && USE_OLD="YES" || USE_OLD="NO"
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 "${TARG_DIR}/dnscrypt-proxy" ] && [ ! -f "${BASE_DIR}/backup_dnscrypt.tar.gz" ]; then
read_yesno "Do you want create a backup before updating?" && backup_restore BACKUP 0 || PTXT "$INFO continuing without making a backup."
fi
local NW_STATE
local RES_STATE
NW_STATE="$(ping 1.1.1.1 -c1 -W2 >/dev/null 2>&1; printf "%s" "$?")"
RES_STATE="$(nslookup google.com 127.0.0.1 >/dev/null 2>&1; printf "%s" "$?")"
if [ -z "$DNSCRYPT_VER" ] || { [ "$NW_STATE" = "0" ] && [ "$RES_STATE" != "0" ]; }; then
PTXT "$ERROR Unable to detect the Internet!"
end_op_message 1
return
fi
create_dir "$TARG_DIR"