forked from nextdns/diag
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.sh
executable file
·154 lines (142 loc) · 3.58 KB
/
run.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
#!/bin/sh
main() {
set -e
echo
echo "Welcome to NextDNS network diagnostic tool."
echo
echo "This tool will download a small binary to capture latency and routing information"
echo "regarding the connectivity of your network with NextDNS. In order to perform a"
echo "traceroute, root permission is required. You may therefore be asked to provide"
echo "your password for sudo."
echo
echo "The source code of this tool is available at https://github.com/nextdns/diag"
echo
printf "Do you want to continue? (press enter to accept)"
read -r _
GOARCH=$(detect_goarch)
GOOS=$(detect_goos)
RELEASE=$(get_release)
url="https://github.com/nextdns/diag/releases/download/v${RELEASE}/diag_${RELEASE}_${GOOS}_${GOARCH}"
bin_path=/tmp/nextdns-diag-$$
trap cleanup EXIT
curl -sL "$url" > "$bin_path"
chmod 755 "$bin_path"
asroot "$bin_path"
}
cleanup() {
rm -f "$bin_path"
}
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
}
get_release() {
out=$(curl -A curl -s "https://api.github.com/repos/nextdns/diag/releases/latest")
v=$(echo "$out" | grep '"tag_name":' | esed 's/.*"([^"]+)".*/\1/' | sed -e 's/^v//')
if [ -z "$v" ]; then
log_error "Cannot get latest version: $out"
fi
echo "$v"
}
esed() {
if (echo | sed -E '' >/dev/null 2>&1); then
sed -E "$@"
else
sed -r "$@"
fi
}
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
}
log_error() {
printf "\033[31mERROR: %s\033[0m\n" "$*" >&2
}
main