forked from VPSDance/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.sh
executable file
·470 lines (455 loc) · 13.7 KB
/
tools.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
#!/usr/bin/env bash
# Usage:
# bash <(curl -Lso- https://sh.vps.dance/tools.sh) [snell|snell4|realm|gost|ss|nali|ddns-go|nexttrace|hy2]
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BOLD="\033[1m"
NC='\033[0m'
OS=$(uname -s) # Linux, FreeBSD, Darwin
ARCH=$(uname -m) # x86_64, arm64/aarch64, i386,
# DISTRO=$( [[ -e $(which lsb_release) ]] && (lsb_release -si) || echo 'unknown' ) which/lsb_release command not found
DISTRO=$( ([[ -e "/usr/bin/yum" ]] && echo 'CentOS') || ([[ -e "/usr/bin/apt" ]] && echo 'Debian') || echo 'unknown' )
name=$( tr '[:upper:]' '[:lower:]' <<<"$1" )
# prerelease=$( [[ "${2}" =~ ^(-p|prerelease)$ ]] && echo true || echo false )
prerelease=true
debug=$( [[ $OS == "Darwin" ]] && echo true || echo false )
ipv4="$(curl -m 5 -fsL4 http://ipv4.ip.sb)"
# ipv6="$(curl -m 5 -fsL6 http://ipv6.ip.sb)"
CURR_USER="$(whoami)"
prompt_yn () {
while true; do
read -p "$1 (y/N)" yn
case "${yn:-${2:-N}}" in
[Yy]* ) return 0;;
[Nn]* ) return 1;;
* ) echo "Please answer yes(y) or no(n).";;
esac
done
}
init () {
case "$name" in
snell | snell4)
app="$name"
file="/root/$app.conf"
# repo="surge-networks/snell"
case $ARCH in
aarch64 | armv8)
match="linux-aarch64"
;;
armv7 | armv6l)
match="linux-armv7l"
;;
*) #x86_64
match="linux-amd64"
;;
esac
;;
realm)
app="realm"
file="/root/realm.toml"
repo="zhboner/realm"
# match=""
case $ARCH in
aarch64)
match="aarch64.*linux-gnu.*.gz"
;;
*) #x86_64
match="x86_64.*linux-gnu.*.gz"
;;
esac
;;
gost)
app="gost"
file="/root/gost.json"
repo="ginuerzh/gost"
case $ARCH in
aarch64)
match="linux-armv8"
;;
*) #x86_64
match="linux-amd64"
;;
esac
;;
ss | shadowsocks)
app="ss"
file="/root/ss.json"
repo="shadowsocks/shadowsocks-rust"
case $ARCH in
aarch64)
match="aarch64.*linux-gnu"
;;
*)
match="x86_64.*linux-gnu"
;;
esac
;;
hy2)
app="hysteria-server"
file="/etc/hysteria/config.yaml"
;;
nali)
app="nali"
repo="zu1k/nali"
case $ARCH in
aarch64)
match="linux-armv8"
;;
*)
match="linux-amd64"
;;
esac
;;
# wtrace | worsttrace)
# app="wtrace"
# ;;
ddns-go)
app="ddns-go"
file="/root/ddns-go.yaml"
repo="jeessy2/ddns-go"
case $ARCH in
aarch64)
match="linux_.*arm64"
;;
*)
match="linux_.*x86_64"
;;
esac
;;
nexttrace)
app="nexttrace"
repo="nxtrace/Ntrace-core"
case $ARCH in
aarch64)
match="linux_arm64"
;;
*)
match="linux_amd64"
;;
esac
;;
*)
printf "${YELLOW}Please specify app_name (snell|realm|gost|ss|nali|ddns-go|nexttrace)\n\n${NC}";
exit
;;
esac
}
install_deps () {
case "${DISTRO}" in
Debian*|Ubuntu*)
apt install -y curl wget zip unzip tar xz-utils gzip;
;;
CentOS*|RedHat*)
yum install -y curl wget zip unzip tar xz gzip;
;;
*)
;;
esac
}
not_support_ipv6 () {
app="$1"
if [ -z "$ipv4" ]; then
printf "\n${RED}[x] $app does not support downloading over IPv6. ${NC}\n\n"; exit 1;
fi
}
download () {
local temp_dir=$(mktemp -d)
suffix=$( [[ "$prerelease" = true ]] && echo "releases" || echo "releases/latest" )
prefix=$( [ -z "$ipv4" ] && echo "https://sh.vps.dance" || echo "https://ghp.ci" )
if [[ -n "$repo" ]]; then
# api="https://api.github.com/repos/$repo/$suffix"
api="https://sh.vps.dance/api/repos/$repo/$suffix"
# curl -s https://api.github.com/repos/nxtrace/NTrace-core/releases | grep "browser_download_url.*$match" | head -1 | cut -d : -f 2,3 | xargs echo
url=$( curl -s $api | grep "browser_download_url.*$match" | head -1 | cut -d : -f 2,3 | xargs echo ) # xargs wget
url="$prefix/$url"
fi
# echo $api; echo $url;exit
# echo -e "\n${GREEN}$app${NC}"
case "$app" in
snell)
version="v3.0.1"
url="https://raw.githubusercontent.com/VPSDance/files/main/snell/${version}/snell-server-${version}-${match}.zip"
url="$prefix/$url"
;;
snell4)
not_support_ipv6 $app
# https://manual.nssurge.com/others/snell.html
version="v4.0.1"
# url="https://raw.githubusercontent.com/VPSDance/files/main/snell/${version}/snell-server-${version}-${match}.zip"
# url="$prefix/$url"
url="https://dl.nssurge.com/snell/snell-server-${version}-${match}.zip"
;;
realm)
if [[ "$debug" != true ]]; then
rm -rf /usr/bin/realm ./realm;
fi
;;
nexttrace)
rm -rf ./nexttrace_*
;;
# wtrace)
# not_support_ipv6 $app
# case $ARCH in
# aarch64)
# printf "${RED}[x] $ARCH not supported ${PLAIN}\n${NC}"; exit 1;
# ;;
# *)
# url="https://wtrace.app/packages/linux/worsttrace"
# ;;
# esac
# ;;
esac
echo -e "${GREEN}\n[Download]${NC}"
echo "$url"
if [[ "$debug" != true ]]; then
if [[ -n "$url" ]]; then
wget -q -P "$temp_dir" "$url"
fi
fi
# echo -e "\n[Extract files]"
# tar xJvf .tar.xz/.txz # apt install -y xz-utils
# tar xzvf .tar.gz
# tar xvf .tar
# unzip -o .zip # apt install -y unzip
# gzip -d .gz
if [[ "$debug" = true ]]; then return; fi
case "$app" in
snell)
unzip -o "$temp_dir"/snell*.zip -d "$temp_dir" && rm -f "$temp_dir"/snell-server-*.zip*
mv "$temp_dir"/snell-server /usr/bin/
;;
snell4)
unzip -o "$temp_dir"/snell*.zip -d "$temp_dir" && rm -f "$temp_dir"/snell-server-*.zip*
mv "$temp_dir"/snell-server /usr/bin/snell4-server
;;
realm)
# curl -s https://api.github.com/repos/zhboner/realm/releases/latest | grep "browser_download_url.*" | cut -d : -f 2,3 | xargs wget -O ./realm {}; chmod +x realm
if [[ `compgen -G "$temp_dir/realm*.tar.gz"` ]]; then
tar xzvf "$temp_dir"/realm*.tar.gz -C "$temp_dir" && rm -f "$temp_dir"/realm*.tar.gz
fi
mv "$temp_dir"/realm /usr/bin/realm && chmod +x /usr/bin/realm
;;
gost)
gzip -d "$temp_dir"/gost-*.gz
mv "$temp_dir"/gost-* /usr/bin/gost && chmod +x /usr/bin/gost
;;
ss)
tar xJf "$temp_dir"/shadowsocks-*.xz -C "$temp_dir" && rm -f "$temp_dir"/shadowsocks-*.xz*
mv "$temp_dir"/ssserver "$temp_dir"/sslocal "$temp_dir"/ssurl "$temp_dir"/ssmanager "$temp_dir"/ssservice /usr/bin/
;;
hysteria-server)
bash <(curl -fsSL https://get.hy2.sh/)
;;
nali)
gzip -d "$temp_dir"/nali-*.gz
mv "$temp_dir"/nali-* /usr/bin/nali && chmod +x /usr/bin/nali
nali update
;;
# wtrace)
# mv "$temp_dir"/worsttrace /usr/bin/worsttrace && chmod +x /usr/bin/worsttrace
# ;;
ddns-go)
tar xzf "$temp_dir"/ddns-go_*tar.gz -C "$temp_dir"
mv "$temp_dir"/ddns-go /usr/bin/ && rm -f "$temp_dir"/ddns-go_*tar.gz* "$temp_dir"/LICENSE "$temp_dir"/README.md
;;
nexttrace)
mv "$temp_dir"/nexttrace_* /usr/bin/nexttrace && chmod +x /usr/bin/nexttrace
;;
*);;
esac
rm -rf "$temp_dir"
}
gen_service () {
local service=""
case "$app" in
snell)
service='[Unit]\nDescription=Snell Service\nAfter=network.target\n[Service]\nType=simple\nLimitNOFILE=32768\nRestart=on-failure\nExecStart=/usr/bin/snell-server -c /root/snell.conf\nStandardOutput=syslog\nStandardError=syslog\nSyslogIdentifier=snell-server\n[Install]\nWantedBy=multi-user.target\n'
;;
snell4)
service='[Unit]\nDescription=Snell Service\nAfter=network.target\n[Service]\nType=simple\nLimitNOFILE=32768\nRestart=on-failure\nExecStart=/usr/bin/snell4-server -c /root/snell4.conf\nStandardOutput=syslog\nStandardError=syslog\nSyslogIdentifier=snell-server\n[Install]\nWantedBy=multi-user.target\n'
;;
realm)
service='[Unit]\nDescription=realm\nAfter=network-online.target\nWants=network-online.target systemd-networkd-wait-online.service\n[Service]\nType=simple\nUser=root\nRestart=on-failure\nRestartSec=5s\nExecStart=/usr/bin/realm -c /root/realm.toml\n[Install]\nWantedBy=multi-user.target'
;;
gost)
service='[Unit]\nDescription=gost\nAfter=network-online.target\nWants=network-online.target systemd-networkd-wait-online.service\n[Service]\nType=simple\nUser=root\nRestart=on-failure\nRestartSec=5s\nExecStart=/usr/bin/gost -C /root/gost.json\n[Install]\nWantedBy=multi-user.target'
;;
ss)
service='[Unit]\nDescription=Shadowsocks\nAfter=network.target\n[Service]\nType=simple\nRestart=on-failure\nExecStart=/usr/bin/ssserver -c /root/ss.json\n[Install]\nWantedBy=multi-user.target\n'
;;
ddns-go)
service='[Unit]\nDescription=ddns-go\n[Service]\nExecStart=/usr/bin/ddns-go "-l" ":9876" "-f" "120" "-c" "/root/ddns-go.yaml"\nStartLimitInterval=5\nStartLimitBurst=10\nRestart=always\nRestartSec=120\n[Install]\nWantedBy=multi-user.target\n'
;;
esac
if [[ -n "$service" ]]; then
echo -e "${GREEN}\n[Generate service]${NC}" >&2
echo -e "$service" >&2
echo -e "$service" > "/etc/systemd/system/$app.service"
systemctl daemon-reload
systemctl enable "$app"
echo "$service"
fi
}
gen_config () {
if ! [[ -n "$file" ]]; then return; fi # no config path
# if [[ -f "$file" ]]; then
# if ! prompt_yn "\"$file\" already exists, do you want to overwrite it?"; then return; fi
# else
# if ! prompt_yn "Do you want to create \"$file\"?"; then return; fi
# fi
# read -p "Specify port number: " port
# while [[ ! ${port} =~ ^[0-9]+$ ]]; do
# echo "Please enter number of port:"
# read port
# done
# port=$(( ${RANDOM:0:4} + 10000 )) # random 10000-20000
port="${port:-1234}"
pass=$(openssl rand -base64 32 | tr -dc A-Za-z0-9 | cut -b1-16)
# conf=('{'
# '}')
# conf="$(printf "%s\n" "${conf[@]}")"
# conf="$(
# echo '{'
# echo '}'
# )"
case "$app" in
snell)
conf="[snell-server]\nlisten = ::0:$port\nipv6 = false\npsk = $pass\nobfs = tls"
;;
snell4)
conf="[snell-server]\nlisten = ::0:$port\nipv6 = false\npsk = $pass" #v4
;;
realm)
conf=(''
'[log]'
'level = "warn"'
''
'[dns]'
'# mode = "ipv6_then_ipv4" # ipv4_then_ipv6, ipv6_then_ipv4'
''
'[network]'
'no_tcp = false'
'use_udp = true'
''
'[[endpoints]]'
'listen = "0.0.0.0:10001"'
'remote = "test.com:80"'
''
'[[endpoints]]'
'listen = "0.0.0.0:10002"'
'remote = "1.1.1.1:443"'
'')
conf="$(printf "%s\n" "${conf[@]}")"
;;
gost)
conf=('{'
' "Debug": true,'
' "Retries": 0,'
' "ServeNodes": ['
' "tcp://:10002/1.1.1.1:443"'
' ],'
' "ChainNodes": ['
' ],'
' "Routes": ['
' ]'
'}')
conf="$(printf "%s\n" "${conf[@]}")"
;;
ss)
conf=('{'
'"mode": "tcp_and_udp",'
'"fast_open": false,'
'"ipv6_first": true,'
'"servers": ['
'{'
' "address": "::",'
' "port": '$port','
' "password": "'$pass'",'
' "method": "chacha20-ietf-poly1305",'
' "timeout": 300'
'}'
']}')
conf="$(printf "%s\n" "${conf[@]}")"
;;
esac
# echo "--- file: $file, conf: $conf"
if [[ -n "$file" && -n "$conf" ]]; then
echo -e "${GREEN}\n[Create config file]${NC} \"$file\", for example:"
echo -e "$conf"
fi
if [[ -f "/root/realm.json" && "$app" == "realm" ]]; then
printf "\n%b\n" "${YELLOW}Convert Realm1 to Realm2 config${NC}: realm convert realm.json > realm.toml";
fi
}
finally () {
local service="$*"
local ip=`curl -Ls ip.sb || echo 'localhost'`;
# echo -e "finally: $service"
if [[ -n "$service" ]]; then
echo -e "${GREEN}\n[Enable and start service]${NC}"
else
echo -e "${GREEN}\n[Usage]${NC}"
fi
service_tips="${GREEN}systemctl enable $app; systemctl restart $app; systemctl status $app;${NC}"
case "$app" in
hysteria-server)
tips="Please modify the ${RED}listen${NC}, ${RED}acme.domains${NC}, ${RED}acme.email${NC}, and ${RED}masquerade.proxy.url${NC} in the config file."
tips+="\nDocs: https://v2.hysteria.network/en/docs/getting-started/Server/"
tips+="\n\n$service_tips"
;;
nali)
tips="nali update;\nping g.cn | nali;\ntraceroute 189.cn | nali"
;;
# wtrace)
# tips="worsttrace g.cn;"
# ;;
ddns-go)
systemctl restart $app;
tips="\nOpen http://$ip:9876 for configuration."
tips+="\n\n$service_tips"
;;
nexttrace)
tips="nexttrace -T -f"
;;
*)
tips="$service_tips"
;;
esac
# if [[ -n "$file" ]]; then
# tips+="\n\n${GREEN}[Config file]${NC} \"$file\"\n"
# fi
if [[ -n "$tips" ]]; then
echo -e "$tips\n"
fi
}
# echo "name: $name; repo=$repo; prerelease=$prerelease"
init
with_sudo() {
if ! command -v sudo >/dev/null 2>&1; then
warn "Error: sudo command not found"
return 1
fi
local cmd
if [[ "$(type -t "$1")" == "function" ]]; then
local declare_vars="$(declare -p CURR_USER OS ARCH DISTRO name prerelease debug ipv4 app file repo match RED GREEN YELLOW BLUE CYAN PURPLE BOLD NC 2>/dev/null)"
local declare_funcs="$(declare -f)"
cmd="$declare_vars; $declare_funcs; $1 "'"${@:2}"'
else
cmd="$1 "'"${@:2}"'
fi
if [[ $EUID -ne 0 ]]; then
sudo bash -c "$cmd" -- "$@" < /dev/tty
else
bash -c "$cmd" -- "$@" -- "$@"
fi
}
# with_sudo install_deps
with_sudo download
service=$(with_sudo gen_service)
# echo -e "gen_service:\n$service"
# with_sudo echo "-- $service"
# with_sudo echo -e "$service"
with_sudo gen_config
with_sudo finally "$service"