forked from nextdns/nextdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·1104 lines (1011 loc) · 27.6 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
main() {
OS=$(detect_os)
GOARCH=$(detect_goarch)
GOOS=$(detect_goos)
NEXTDNS_BIN=$(bin_location)
INSTALL_RELEASE=$(get_release)
export NEXTDNS_INSTALLER=1
log_info "OS: $OS"
log_info "GOARCH: $GOARCH"
log_info "GOOS: $GOOS"
log_info "NEXTDNS_BIN: $NEXTDNS_BIN"
log_info "INSTALL_RELEASE: $INSTALL_RELEASE"
if [ -z "$OS" ] || [ -z "$GOARCH" ] || [ -z "$GOOS" ] || [ -z "$NEXTDNS_BIN" ] || [ -z "$INSTALL_RELEASE" ]; then
log_error "Cannot detect running environment."
exit 1
fi
case "$RUN_COMMAND" in
install|upgrade|uninstall|configure) "$RUN_COMMAND"; exit ;;
esac
while true; do
CURRENT_RELEASE=$(get_current_release)
log_debug "Start install loop with CURRENT_RELEASE=$CURRENT_RELEASE"
if [ "$CURRENT_RELEASE" ]; then
if ! is_version_current; then
log_debug "NextDNS is out of date ($CURRENT_RELEASE != $INSTALL_RELEASE)"
menu \
u "Upgrade NextDNS from $CURRENT_RELEASE to $INSTALL_RELEASE" upgrade \
c "Configure NextDNS" configure \
r "Remove NextDNS" uninstall \
e "Exit" exit
else
log_debug "NextDNS is up to date ($CURRENT_RELEASE)"
menu \
c "Configure NextDNS" configure \
r "Remove NextDNS" uninstall \
e "Exit" exit
fi
else
log_debug "NextDNS is not installed"
menu \
i "Install NextDNS" install \
e "Exit" exit
fi
done
}
install() {
if [ "$(get_current_release)" ]; then
log_info "Already installed"
return
fi
if type=$(install_type); then
log_info "Installing NextDNS..."
log_debug "Using $type install type"
if "install_$type"; then
if [ ! -x "$NEXTDNS_BIN" ]; then
log_error "Installation failed: binary not installed in $NEXTDNS_BIN"
return 1
fi
configure
post_install
exit 0
fi
else
return $?
fi
}
upgrade() {
if [ "$(get_current_release)" = "$INSTALL_RELEASE" ]; then
log_info "Already on the latest version"
return
fi
if type=$(install_type); then
log_info "Upgrading NextDNS..."
log_debug "Using $type install type"
"upgrade_$type"
else
return $?
fi
}
uninstall() {
if type=$(install_type); then
log_info "Uninstalling NextDNS..."
log_debug "Using $type uninstall type"
"uninstall_$type"
else
return $?
fi
}
configure() {
log_debug "Start configure"
args=""
add_arg() {
for value in $2; do
log_debug "Add arg -$1=$value"
args="$args -$1=$value"
done
}
add_arg_bool_ask() {
arg=$1
msg=$2
default=$3
if [ -z "$default" ]; then
default=$(get_config_bool "$arg")
fi
# shellcheck disable=SC2046
add_arg "$arg" $(ask_bool "$msg" "$default")
}
add_arg config "$(get_config_id)"
doc "Sending your devices name lets you filter analytics and logs by device."
add_arg_bool_ask report-client-info 'Report device name?' true
case $(guess_host_type) in
router)
add_arg setup-router true
;;
unsure)
doc "Accept DNS request from other network hosts."
if [ "$(get_config_bool setup-router)" = "true" ]; then
router_default=true
fi
if [ "$(ask_bool 'Setup as a router?' $router_default)" = "true" ]; then
add_arg setup-router true
fi
;;
esac
doc "Make nextdns CLI cache responses. This improves latency and reduces the amount"
doc "of queries sent to NextDNS."
if [ "$(guess_host_type)" = "router" ]; then
doc "Note that enabling this feature will disable dnsmasq for DNS to avoid double"
doc "caching."
fi
if [ "$(get_config cache-size)" != "0" ]; then
cache_default=true
fi
if [ "$(ask_bool 'Enable caching?' $cache_default)" = "true" ]; then
add_arg cache-size "10MB"
doc "Instant refresh will force low TTL on responses sent to clients so they rely"
doc "on CLI DNS cache. This will allow changes on your NextDNS config to be applied"
doc "on you LAN hosts without having to wait for their cache to expire."
if [ "$(get_config max-ttl)" = "5s" ]; then
instant_refresh_default=true
fi
if [ "$(ask_bool 'Enable instant refresh?' $instant_refresh_default)" = "true" ]; then
add_arg max-ttl "5s"
fi
fi
if [ "$(guess_host_type)" != "router" ]; then
doc "Changes DNS settings of the host automatically when nextdns is started."
doc "If you say no here, you will have to manually configure DNS to 127.0.0.1."
add_arg_bool_ask auto-activate 'Automatically setup local host DNS?' true
fi
# shellcheck disable=SC2086
asroot "$NEXTDNS_BIN" install $args
}
post_install() {
println
println "Congratulations! NextDNS is now installed."
println
println "To upgrade/uninstall, run this command again and select the approriate option."
println
println "You can use the nextdns command to control the daemon."
println "Here is a few important commands to know:"
println
println "# Start, stop, restart the daemon:"
println "nextdns start"
println "nextdns stop"
println "nextdns restart"
println
println "# Configure the local host to point to NextDNS or not:"
println "nextdns activate"
println "nextdns deactivate"
println
println "# Explore daemon logs:"
println "nextdns log"
println
println "# For more commands, use:"
println "nextdns help"
println
}
install_bin() {
bin_path=$NEXTDNS_BIN
if [ "$1" ]; then
bin_path=$1
fi
log_debug "Installing $INSTALL_RELEASE binary for $GOOS/$GOARCH to $bin_path"
case "$INSTALL_RELEASE" in
*/*)
# Snapshot
branch=${INSTALL_RELEASE%/*}
hash=${INSTALL_RELEASE#*/}
url="https://snapshot.nextdns.io/${branch}/nextdns-${hash}_${GOOS}_${GOARCH}.tar.gz"
;;
*)
url="https://github.com/nextdns/nextdns/releases/download/v${INSTALL_RELEASE}/nextdns_${INSTALL_RELEASE}_${GOOS}_${GOARCH}.tar.gz"
;;
esac
log_debug "Downloading $url"
asroot mkdir -p "$(dirname "$bin_path")" &&
curl -sL "$url" | asroot sh -c "tar Ozxf - nextdns > \"$bin_path\"" &&
asroot chmod 755 "$bin_path"
}
upgrade_bin() {
tmp=$NEXTDNS_BIN.tmp
if install_bin "$tmp"; then
asroot "$NEXTDNS_BIN" uninstall
asroot mv "$tmp" "$NEXTDNS_BIN"
asroot "$NEXTDNS_BIN" install
fi
log_debug "Removing spurious temporary install file"
asroot rm -rf "$tmp"
}
uninstall_bin() {
asroot "$NEXTDNS_BIN" uninstall
asroot rm -f "$NEXTDNS_BIN"
}
install_rpm() {
asroot curl -Ls https://repo.nextdns.io/nextdns.repo -o /etc/yum.repos.d/nextdns.repo &&
asroot yum install -y nextdns
}
upgrade_rpm() {
asroot yum update -y nextdns
}
uninstall_rpm() {
asroot yum remove -y nextdns
}
install_zypper() {
if asroot zypper repos | grep -q nextdns >/dev/null; then
echo "Repository nextdns already exists. Skipping adding repository..."
else
asroot zypper ar -f -r https://repo.nextdns.io/nextdns.repo nextdns
fi
asroot zypper refresh && asroot zypper in -y nextdns
}
upgrade_zypper() {
asroot zypper up nextdns
}
uninstall_zypper() {
asroot zypper remove -y nextdns
case $(ask_bool 'Do you want to remove the repository from the repositories list?' true) in
true)
asroot zypper removerepo nextdns
;;
esac
}
install_deb() {
if [ -f /etc/default/ubnt-dpkg-cache ]; then
# On UnifiOS 2, make sure the package is persisted over upgrades
sed -e '/^DPKG_CACHE_UBNT_PKGS+=" nextdns"/{:a;n;ba;q}' \
-e '$aDPKG_CACHE_UBNT_PKGS+=" nextdns"' \
-i /etc/default/ubnt-dpkg-cache
fi
# Fallback on curl, some debian based distrib don't have wget while debian
# doesn't have curl by default.
( asroot wget -qO /usr/share/keyrings/nextdns.gpg https://repo.nextdns.io/nextdns.gpg ||
asroot curl -sfL https://repo.nextdns.io/nextdns.gpg -o /usr/share/keyrings/nextdns.gpg ) &&
asroot chmod 0644 /usr/share/keyrings/nextdns.gpg &&
asroot sh -c 'echo "deb [signed-by=/usr/share/keyrings/nextdns.gpg] https://repo.nextdns.io/deb stable main" > /etc/apt/sources.list.d/nextdns.list' &&
(dpkg --compare-versions $(dpkg-query --showformat='${Version}' --show apt) ge 1.1 ||
asroot ln -s /usr/share/keyrings/nextdns.gpg /etc/apt/trusted.gpg.d/.) &&
(test "$OS" = "debian" && asroot apt-get -y install apt-transport-https || true) &&
asroot apt-get update &&
asroot apt-get install -y nextdns
}
upgrade_deb() {
asroot apt-get update &&
asroot apt-get install -y nextdns
}
uninstall_deb() {
asroot apt-get remove -y nextdns
}
install_apk() {
repo=https://repo.nextdns.io/apk
asroot wget -O /etc/apk/keys/nextdns.pub https://repo.nextdns.io/nextdns.pub &&
(grep -v $repo /etc/apk/repositories; echo $repo) | asroot tee /etc/apk/repositories >/dev/null &&
asroot apk update &&
asroot apk add nextdns
}
upgrade_apk() {
asroot apk update && asroot apk upgrade nextdns
}
uninstall_apk() {
asroot apk del nextdns
}
install_arch() {
asroot pacman -Sy yay &&
yay -Sy nextdns
}
upgrade_arch() {
yay -Suy nextdns
}
uninstall_arch() {
asroot pacman -R nextdns
}
install_merlin_path() {
# Add next to Merlin's path
mkdir -p /tmp/opt/sbin
ln -sf "$NEXTDNS_BIN" /tmp/opt/sbin/nextdns
}
install_merlin() {
if install_bin; then
install_merlin_path
fi
}
uninstall_merlin() {
uninstall_bin
rm -f /tmp/opt/sbin/nextdns
}
upgrade_merlin() {
if upgrade_bin; then
install_merlin_path
fi
}
install_openwrt() {
opkg update &&
opkg install nextdns
rt=$?
if [ $rt -eq 0 ]; then
case $(ask_bool 'Install the GUI?' true) in
true)
opkg install luci-app-nextdns
rt=$?
;;
esac
fi
return $rt
}
upgrade_openwrt() {
opkg update &&
opkg upgrade nextdns
}
uninstall_openwrt() {
opkg remove nextdns
}
install_ddwrt() {
if [ "$(nvram get enable_jffs2)" = "0" ]; then
log_error "JFFS support not enabled"
log_info "To enabled JFFS:"
log_info " 1. On the router web page click on Administration."
log_info " 2. Scroll down until you see JFFS2 Support section."
log_info " 3. Click Enable JFFS."
log_info " 4. Click Save."
log_info " 5. Wait couple seconds, then click Apply."
log_info " 6. Wait again. Go back to the Enable JFFS section, and enable Clean JFFS."
log_info " 7. Do not click Save. Click Apply instead."
log_info " 8. Wait till you get the web-GUI back, then disable Clean JFFS again."
log_info " 9. Click Save."
log_info "10. Relaunch this installer."
exit 1
fi
mkdir -p /jffs/nextdns &&
openssl_get https://curl.haxx.se/ca/cacert.pem | http_body > /jffs/nextdns/ca.pem &&
install_bin
}
upgrade_ddwrt() {
upgrade_bin
}
uninstall_ddwrt() {
uninstall_bin
rm -rf /jffs/nextdns
}
install_brew() {
silent_exec brew install nextdns/tap/nextdns
}
upgrade_brew() {
silent_exec brew upgrade nextdns/tap/nextdns
asroot "$NEXTDNS_BIN" install
}
uninstall_brew() {
silent_exec brew uninstall nextdns/tap/nextdns
}
install_freebsd() {
# TODO: port install
install_bin
}
upgrade_freebsd() {
# TODO: port upgrade
upgrade_bin
}
uninstall_freebsd() {
# TODO: port uninstall
uninstall_bin
}
install_pfsense() {
# TODO: port install + UI
install_bin
}
upgrade_pfsense() {
# TODO: port upgrade
upgrade_bin
}
uninstall_pfsense() {
# TODO: port uninstall
uninstall_bin
}
install_opnsense() {
# TODO: port install + UI
install_bin
}
upgrade_opnsense() {
# TODO: port upgrade
upgrade_bin
}
uninstall_opnsense() {
# TODO: port uninstall
uninstall_bin
}
ubios_install_source() {
echo "deb [signed-by=/usr/share/keyrings/nextdns.gpg] https://repo.nextdns.io/deb stable main" > /data/nextdns.list
podman exec unifi-os mv /data/nextdns.list /etc/apt/sources.list.d/nextdns.list
rm -f /tmp/nextdns.list
podman exec unifi-os apt-get install -y gnupg1 curl
podman exec unifi-os curl -sfL https://repo.nextdns.io/nextdns.gpg -o /usr/share/keyrings/nextdns.gpg
podman exec unifi-os apt-get update -o Dir::Etc::sourcelist="sources.list.d/nextdns.list" -o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0"
}
install_ubios() {
ubios_install_source
podman exec unifi-os apt-get install -y nextdns
}
upgrade_ubios() {
ubios_install_source
podman exec unifi-os apt-get install --only-upgrade -y nextdns
}
uninstall_ubios() {
podman exec unifi-os apt-get remove -y nextdns
}
install_ubios_snapshot() {
branch=${INSTALL_RELEASE%/*}
hash=${INSTALL_RELEASE#*/}
url="https://snapshot.nextdns.io/${branch}/nextdns-${hash}_${GOOS}_${GOARCH}.tar.gz"
podman exec unifi-os sh -c "curl -o- $url | tar Ozxf - nextdns > /usr/bin/nextdns; /usr/bin/nextdns install"
}
upgrade_ubios_snapshot() {
/data/nextdns uninstall
install_ubios_snapshot
}
install_type() {
if [ "$FORCE_INSTALL_TYPE" ]; then
echo "$FORCE_INSTALL_TYPE"; return 0
fi
case "$INSTALL_RELEASE" in
*/*)
case $OS in
ubios)
echo "ubios_snapshot"; return 0
;;
*)
# Snapshot mode always use binary install
echo "bin"; return 0
;;
esac
esac
case $OS in
centos|fedora|rhel)
echo "rpm"
;;
opensuse-tumbleweed|opensuse-leap|opensuse)
echo "zypper"
;;
debian|ubuntu|elementary|raspbian|linuxmint|pop|neon|sparky|vyos|Deepin)
echo "deb"
;;
alpine)
echo "apk"
;;
arch|manjaro)
#echo "arch" # TODO: fix AUR install
echo "bin"
;;
openwrt)
# shellcheck disable=SC1091
. /etc/os-release
major=$(echo "$VERSION_ID" | cut -d. -f1)
case $major in
*[!0-9]*)
if [ "$VERSION_ID" = "19.07.0-rc1" ]; then
# No opkg support before 19.07.0-rc2
echo "bin"
else
# Likely 'snapshot' bulid in this case, but still > major version 19
echo "openwrt"
fi
;;
*)
if [ "$major" -lt 19 ]; then
# No opkg support before 19.07.0-rc2
echo "bin"
else
echo "openwrt"
fi
;;
esac
;;
asuswrt-merlin)
echo "merlin"
;;
edgeos|synology|clear-linux-os|solus|openbsd|netbsd|overthebox)
echo "bin"
;;
ddwrt)
echo "ddwrt"
;;
darwin)
if [ -x /usr/local/bin/brew ] || [ -x /opt/homebrew/bin/brew ]; then
echo "brew"
else
log_debug "Homebrew not installed, fallback on binary install"
echo "bin"
fi
;;
freebsd)
echo "freebsd"
;;
pfsense)
echo "pfsense"
;;
opnsense)
echo "opnsense"
;;
ubios)
echo "ubios"
;;
void)
# TODO: pkg for xbps
echo "bin"
;;
*)
log_error "Unsupported installation for $(detect_os)"
return 1
;;
esac
}
get_config() {
"$NEXTDNS_BIN" config | grep -E "^$1 " | cut -d' ' -f 2
}
get_config_bool() {
val=$(get_config "$1")
case $val in
true|false)
echo "$val"
;;
esac
echo "$2"
}
get_config_id() {
log_debug "Get configuration ID"
while [ -z "$CONFIG_ID" ]; do
default=
prev_id=$(get_config config)
if [ "$prev_id" ]; then
log_debug "Previous config ID: $prev_id"
default=" (default=$prev_id)"
fi
print "NextDNS Configuration ID%s: " "$default"
read -r id
if [ -z "$id" ]; then
id=$prev_id
fi
if echo "$id" | grep -qE '^[0-9a-f]{6}$'; then
CONFIG_ID=$id
break
else
log_error "Invalid configuration ID."
println
println "ID format is 6 alphanumerical lowercase characters (example: 123abc)."
println "Your ID can be found on the Setup tab of https://my.nextdns.io."
println
fi
done
echo "$CONFIG_ID"
}
log_debug() {
if [ "$DEBUG" = "1" ]; then
printf "\033[30;1mDEBUG: %s\033[0m\n" "$*" >&2
fi
}
log_info() {
printf "INFO: %s\n" "$*" >&2
}
log_error() {
printf "\033[31mERROR: %s\033[0m\n" "$*" >&2
}
print() {
format=$1
if [ $# -gt 0 ]; then
shift
fi
# shellcheck disable=SC2059
printf "$format" "$@" >&2
}
println() {
format=$1
if [ $# -gt 0 ]; then
shift
fi
# shellcheck disable=SC2059
printf "$format\n" "$@" >&2
}
doc() {
# shellcheck disable=SC2059
printf "\033[30;1m%s\033[0m\n" "$*" >&2
}
menu() {
while true; do
n=0
default=
for item in "$@"; do
case $((n%3)) in
0)
key=$item
if [ -z "$default" ]; then
default=$key
fi
;;
1)
echo "$key) $item"
;;
esac
n=$((n+1))
done
print "Choice (default=%s): " "$default"
read -r choice
if [ -z "$choice" ]; then
choice=$default
fi
n=0
for item in "$@"; do
case $((n%3)) in
0)
key=$item
;;
2)
if [ "$key" = "$choice" ]; then
if ! "$item"; then
log_error "$item: exit $?"
fi
break 2
fi
;;
esac
n=$((n+1))
done
echo "Invalid choice"
done
}
ask_bool() {
msg=$1
default=$2
case $default in
true)
msg="$msg [Y|n]: "
;;
false)
msg="$msg [y|N]: "
;;
*)
msg="$msg (y/n): "
esac
while true; do
print "%s" "$msg"
read -r answer
if [ -z "$answer" ]; then
answer=$default
fi
case $answer in
y|Y|yes|YES|true)
echo "true"
return 0
;;
n|N|no|NO|false)
echo "false"
return 0
;;
*)
echo "Invalid input, use yes or no"
;;
esac
done
}
detect_endiannes() {
if ! hexdump /dev/null 2>/dev/null; then
# Some firmware do not contain hexdump, for those, try to detect endiannes
# differently
case $(cat /proc/cpuinfo) in
*BCM5300*)
# RT-AC66U does not support merlin version over 380.70 which
# lack hexdump command.
echo "le"
;;
*)
log_error "Cannot determine endiannes"
return 1
;;
esac
return 0
fi
case $(hexdump -s 5 -n 1 -e '"%x"' /bin/sh | head -c1) in
1)
echo "le"
;;
2)
echo ""
;;
esac
}
detect_goarch() {
if [ "$FORCE_GOARCH" ]; then
echo "$FORCE_GOARCH"; return 0
fi
case $(uname -m) in
x86_64|amd64)
echo "amd64"
;;
i386|i686)
echo "386"
;;
arm)
# Freebsd does not include arm version
case "$(sysctl -b hw.model 2>/dev/null)" in
*A9*)
echo "armv7"
;;
*)
# Unknown version, fallback to the lowest
echo "armv5"
;;
esac
;;
armv5*)
echo "armv5"
;;
armv6*|armv7*)
if grep -q vfp /proc/cpuinfo 2>/dev/null; then
echo "armv$(uname -m | sed -e 's/[[:alpha:]]//g')"
else
# Soft floating point
echo "armv5"
fi
;;
aarch64)
case "$(uname -o 2>/dev/null)" in
ASUSWRT-Merlin*)
# XXX when using arm64 build on ASUS AC66U and ACG86U, we get Go error:
# "out of memory allocating heap arena metadata".
echo "armv7"
;;
*)
echo "arm64"
;;
esac
;;
armv8*|arm64)
echo "arm64"
;;
mips*)
# TODO: detect hardfloat
echo "$(uname -m)$(detect_endiannes)_softfloat"
;;
*)
log_error "Unsupported GOARCH: $(uname -m)"
return 1
;;
esac
}
detect_goos() {
if [ "$FORCE_GOOS" ]; then
echo "$FORCE_GOOS"; return 0
fi
case $(uname -s) in
Linux)
echo "linux"
;;
Darwin)
echo "darwin"
;;
FreeBSD)
echo "freebsd"
;;
NetBSD)
echo "netbsd"
;;
OpenBSD)
echo "openbsd"
;;
*)
log_error "Unsupported GOOS: $(uname -s)"
return 1
esac
}
detect_os() {
if [ "$FORCE_OS" ]; then
echo "$FORCE_OS"; return 0
fi
case $(uname -s) in
Linux)
case $(uname -o) in
GNU/Linux|Linux)
if grep -q -e '^EdgeRouter' -e '^UniFiSecurityGateway' /etc/version 2> /dev/null; then
echo "edgeos"; return 0
fi
if uname -u 2>/dev/null | grep -q '^synology'; then
echo "synology"; return 0
fi
# shellcheck disable=SC1091
dist=$(. /etc/os-release; echo "$ID")
case $dist in
ubios)
if [ -z "$(command -v podman)" ]; then
log_error "This version of UnifiOS is not supported. Make sure you run version 1.7.0 or above."
return 1
fi
echo "$dist"; return 0
;;
debian|ubuntu|elementary|raspbian|centos|fedora|rhel|arch|manjaro|openwrt|clear-linux-os|linuxmint|opensuse-tumbleweed|opensuse-leap|opensuse|solus|pop|neon|overthebox|sparky|vyos|void|alpine|Deepin)
echo "$dist"; return 0
;;
esac
# shellcheck disable=SC1091
for dist in $(. /etc/os-release; echo "$ID_LIKE"); do
case $dist in
debian|ubuntu|rhel|fedora|openwrt)
log_debug "Using ID_LIKE"
echo "$dist"; return 0
;;
esac
done
;;
ASUSWRT-Merlin*)
echo "asuswrt-merlin"; return 0
;;
DD-WRT)
echo "ddwrt"; return 0
esac
;;
Darwin)
echo "darwin"; return 0
;;
FreeBSD)
if [ -f /etc/platform ]; then
case $(cat /etc/platform) in
pfSense)
echo "pfsense"; return 0
;;
esac
fi
if [ -x /usr/local/sbin/opnsense-version ]; then
case $(/usr/local/sbin/opnsense-version -N) in
OPNsense)
echo "opnsense"; return 0
;;
esac
fi
echo "freebsd"; return 0
;;
NetBSD)
echo "netbsd"; return 0
;;
OpenBSD)
echo "openbsd"; return 0
;;
*)
esac
log_error "Unsupported OS: $(uname -o) $(grep ID "/etc/os-release" 2>/dev/null | xargs)"
return 1
}
guess_host_type() {
if [ -d /data/unifi ]; then
# Special case when installer is run from inside the ubios podman
echo "router"; return 0
fi
case $OS in
pfsense|opnsense|openwrt|asuswrt-merlin|edgeos|ddwrt|synology|overthebox|ubios)
echo "router"
;;
darwin)
echo "workstation"
;;
*)
echo "unsure"
;;
esac
}
asroot() {
# Some platform (merlin) do not have the "id" command and $USER report a non root username with uid 0.
if [ "$(grep '^Uid:' /proc/$$/status 2>/dev/null|cut -f2)" = "0" ] || [ "$USER" = "root" ] || [ "$(id -u 2>/dev/null)" = "0" ]; then
"$@"
elif [ "$(command -v sudo 2>/dev/null)" ]; then
sudo "$@"
else
echo "Root required"
su -m root -c "$*"
fi
}
silent_exec() {
if [ "$DEBUG" = 1 ]; then
"$@"
else
if ! out=$("$@" 2>&1); then
rt=$?
println "\033[30;1m%s\033[0m" "$out"
return $rt
fi
fi
}
bin_location() {
case $OS in
centos|fedora|rhel|debian|ubuntu|elementary|raspbian|arch|manjaro|clear-linux-os|linuxmint|opensuse-tumbleweed|opensuse-leap|opensuse|solus|pop|neon|sparky|vyos|void|alpine|Deepin)
echo "/usr/bin/nextdns"
;;
openwrt|overthebox)
echo "/usr/sbin/nextdns"
;;
synology)
echo "/usr/local/bin/nextdns"
;;
darwin)