-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnell-v6.sh
More file actions
executable file
·1426 lines (1184 loc) · 40 KB
/
Copy pathsnell-v6.sh
File metadata and controls
executable file
·1426 lines (1184 loc) · 40 KB
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
SCRIPT_NAME="$(basename "$0")"
INSTALL_ROOT="${INSTALL_ROOT:-/opt/snell}"
RELEASES_DIR="${RELEASES_DIR:-${INSTALL_ROOT}/releases}"
BIN_DIR="${BIN_DIR:-${INSTALL_ROOT}/bin}"
CONF_DIR="${CONF_DIR:-${INSTALL_ROOT}/conf}"
CURRENT_BIN="${CURRENT_BIN:-${BIN_DIR}/snell-server-v6}"
CONFIG_FILE="${CONFIG_FILE:-${CONF_DIR}/snell-server-v6.conf}"
SERVICE_NAME="${SERVICE_NAME:-snell-v6}"
SERVICE_FILE="${SERVICE_FILE:-/etc/systemd/system/${SERVICE_NAME}.service}"
RUN_USER="${RUN_USER:-snell}"
RUN_GROUP="${RUN_GROUP:-snell}"
RELEASE_NOTES_URL="${RELEASE_NOTES_URL:-https://kb.nssurge.com/surge-knowledge-base/release-notes/snell.md}"
DOWNLOAD_BASE_URL="${DOWNLOAD_BASE_URL:-https://dl.nssurge.com/snell}"
# Curated list of known Snell v6 builds, space separated, oldest to newest. The
# official release-notes page sometimes lags behind the download server after a
# new prerelease ships, so these entries let VERSION=auto/latest/arches resolve a
# published build before the notes page lists it. Every candidate is verified
# against the download server before use, so not-yet-published or removed builds
# are ignored. Append new builds here (and in the README) as upstream ships them.
SNELL_V6_KNOWN_VERSIONS="${SNELL_V6_KNOWN_VERSIONS:-v6.0.0b1 v6.0.0b2 v6.0.0b3 v6.0.0b4 v6.0.0rc v6.0.0rc2}"
# CPU arches probed when verifying curated builds against the download server.
SNELL_V6_SUPPORTED_ARCHES="${SNELL_V6_SUPPORTED_ARCHES:-amd64 i386 aarch64 armv7l}"
# Seconds to wait when checking whether a download URL exists.
URL_CHECK_TIMEOUT="${URL_CHECK_TIMEOUT:-15}"
VERSION="${VERSION:-auto}"
PORT_EXPLICIT=0
if [[ -n "${PORT+x}" && -n "${PORT:-}" ]]; then
PORT_EXPLICIT=1
fi
PORT="${PORT:-7177}"
LISTEN_EXPLICIT=0
if [[ -n "${LISTEN+x}" && -n "${LISTEN:-}" ]]; then
LISTEN_EXPLICIT=1
fi
LISTEN="${LISTEN:-}"
PSK="${PSK:-}"
DNS_IP_PREFERENCE="${DNS_IP_PREFERENCE:-}"
DNS_SERVERS="${DNS_SERVERS:-}"
EGRESS_INTERFACE="${EGRESS_INTERFACE:-}"
MODE_EXPLICIT=0
if [[ -n "${MODE+x}" && -n "${MODE:-}" ]]; then
MODE_EXPLICIT=1
fi
MODE="${MODE:-}"
AUTO_INSTALL_DEPS="${AUTO_INSTALL_DEPS:-1}"
MANAGE_FIREWALL="${MANAGE_FIREWALL:-auto}"
CONFIG_OVERWRITE="${CONFIG_OVERWRITE:-0}"
ALLOW_DOWNGRADE="${ALLOW_DOWNGRADE:-0}"
ASSUME_YES="${ASSUME_YES:-0}"
CONFIG_BACKUP_PATH=""
TARGET_VERSION="${TARGET_VERSION:-${VERSION}}"
ACTION="${1:-apply}"
log() {
printf '[%s] %s\n' "$SCRIPT_NAME" "$*"
}
die() {
printf '[%s] ERROR: %s\n' "$SCRIPT_NAME" "$*" >&2
exit 1
}
usage() {
cat <<'EOF'
Usage:
sudo ./snell-v6.sh [apply|latest|arches|installed|download-url|help]
Actions:
apply Install or update Snell v6. Default action.
latest Print the latest detected Snell v6 version for the current arch.
arches Print available CPU architectures for VERSION, or latest v6.
installed Print the currently installed version, if detected.
download-url Print the resolved download URL for VERSION/current arch.
help Show this help.
Environment variables:
VERSION=auto Detect the latest v6 release from official release notes.
VERSION=v6.0.0b4 Install a specific beta build.
VERSION=v6.0.0rc2 Install a numbered RC build. Legacy rc.2 input is normalized to rc2.
VERSION=v6.0.0 Install a specific stable build.
SNELL_V6_KNOWN_VERSIONS Space-separated curated builds the auto resolver verifies
against the download server when the release notes lag.
ARCH=auto Detect CPU arch automatically.
ARCH=amd64 Override CPU arch. Also accepts x86_64, i386, arm64, aarch64, armv7l.
PORT=7177 Listen port for first install, or with CONFIG_OVERWRITE=1.
LISTEN=0.0.0.0:7177 Override the generated listen line when writing config.
PSK=... Override the generated PSK for first install.
DNS_IP_PREFERENCE=default
DNS_SERVERS=1.1.1.1,8.8.8.8
EGRESS_INTERFACE=eth0
MODE=default Snell v6.0.0b3+ mode: default, unshaped, or unsafe-raw.
CONFIG_OVERWRITE=1 Rewrite the config file on update, preserving unspecified values.
ALLOW_DOWNGRADE=0 Refuse automatic downgrades by default.
ASSUME_YES=0 Prompt before install/update. Set to 1 for automation.
MANAGE_FIREWALL=auto Add an allow rule when ufw exists.
AUTO_INSTALL_DEPS=1 Install curl/unzip when apt/dnf/yum is available.
EOF
}
need_root() {
if [[ "${EUID}" -ne 0 ]]; then
die "please run as root or with sudo"
fi
}
need_linux() {
[[ "$(uname -s)" == "Linux" ]] || die "this script only supports Linux VPS hosts"
}
need_systemd() {
command -v systemctl >/dev/null 2>&1 || die "systemctl not found; this script expects systemd"
}
normalize_arch() {
local raw_arch="$1"
case "${raw_arch}" in
auto)
normalize_arch "$(uname -m)"
;;
x86_64|amd64)
printf 'amd64\n'
;;
aarch64|arm64)
printf 'aarch64\n'
;;
armv7l|armv7)
printf 'armv7l\n'
;;
i386|i486|i586|i686)
printf 'i386\n'
;;
*)
die "unsupported architecture: ${raw_arch}; supported values are amd64, i386, aarch64, and armv7l"
;;
esac
}
ARCH="$(normalize_arch "${ARCH:-auto}")"
is_truthy() {
case "${1:-}" in
1|true|yes|y|on)
return 0
;;
*)
return 1
;;
esac
}
can_prompt() {
[[ -r /dev/tty && -w /dev/tty ]]
}
is_valid_port() {
local port="$1"
[[ "${port}" =~ ^[0-9]+$ ]] || return 1
[[ "${#port}" -le 5 ]] || return 1
(( 10#${port} >= 1 && 10#${port} <= 65535 ))
}
validate_port() {
local port="$1"
is_valid_port "${port}" || die "invalid port '${port}'; expected an integer from 1 to 65535"
}
normalize_port_value() {
local port="$1"
validate_port "${port}"
printf '%s\n' "$((10#${port}))"
}
trim_value() {
sed 's/^[[:space:]]*//;s/[[:space:]]*$//' <<<"${1:-}"
}
listen_ports() {
local listen="$1"
local endpoint port
local IFS=','
local -a endpoints
[[ -n "${listen}" ]] || die "listen value cannot be empty"
IFS=',' read -r -a endpoints <<<"${listen}"
for endpoint in "${endpoints[@]}"; do
endpoint="$(trim_value "${endpoint}")"
[[ -n "${endpoint}" ]] || die "listen contains an empty endpoint: ${listen}"
if [[ "${endpoint}" =~ ^\[[^]]+\]:([0-9]+)$ ]]; then
port="${BASH_REMATCH[1]}"
elif [[ "${endpoint}" =~ :([0-9]+)$ ]]; then
port="${BASH_REMATCH[1]}"
else
die "invalid listen endpoint '${endpoint}'; expected host:port or [ipv6]:port"
fi
validate_port "${port}"
normalize_port_value "${port}"
done
}
ipv6_available() {
local disabled
[[ -e /proc/net/if_inet6 ]] || return 1
disabled="$(cat /proc/sys/net/ipv6/conf/all/disable_ipv6 2>/dev/null || printf '1')"
[[ "${disabled}" != "1" ]]
}
ensure_listen_ipv6_supported() {
local listen="$1"
if [[ "${listen}" == *'['*']'* ]] && ! ipv6_available; then
die "LISTEN includes IPv6, but IPv6 appears disabled on this host; use LISTEN=0.0.0.0:${PORT} or enable IPv6"
fi
}
validate_listen_value() {
local listen="$1"
listen_ports "${listen}" >/dev/null
ensure_listen_ipv6_supported "${listen}"
}
config_will_be_written() {
[[ ! -f "${CONFIG_FILE}" ]] || is_truthy "${CONFIG_OVERWRITE}"
}
planned_listen_value() {
local existing_listen
if [[ -f "${CONFIG_FILE}" ]] && ! is_truthy "${CONFIG_OVERWRITE}"; then
existing_listen="$(config_value "listen")"
if [[ -n "${existing_listen}" ]]; then
printf '%s\n' "${existing_listen}"
return 0
fi
fi
if [[ -n "${LISTEN}" ]]; then
printf '%s\n' "${LISTEN}"
return 0
fi
existing_listen="$(config_value "listen")"
if [[ -n "${existing_listen}" ]]; then
printf '%s\n' "${existing_listen}"
return 0
fi
printf '0.0.0.0:%s\n' "${PORT}"
}
is_valid_mode() {
case "$1" in
default|unshaped|unsafe-raw)
return 0
;;
*)
return 1
;;
esac
}
validate_mode() {
local mode="$1"
is_valid_mode "${mode}" || die "invalid MODE '${mode}'; expected default, unshaped, or unsafe-raw"
}
version_supports_mode() {
local version="$1"
local version_key minimum_key
version_key="$(version_sort_key "${version}")"
minimum_key="$(version_sort_key "v6.0.0b3")"
# Sort keys are fixed-width, zero-padded digits and dots, so lexical
# comparison matches numeric order regardless of locale; this is version >= minimum.
[[ ! "${version_key}" < "${minimum_key}" ]]
}
mode_default_migration_needed() {
local target_version="$1"
local existing_mode
[[ -f "${CONFIG_FILE}" ]] || return 1
is_truthy "${CONFIG_OVERWRITE}" && return 1
version_supports_mode "${target_version}" || return 1
existing_mode="$(config_value "mode")"
[[ -z "${existing_mode}" ]]
}
planned_mode_value() {
local target_version="$1"
local existing_mode
version_supports_mode "${target_version}" || return 0
if [[ -f "${CONFIG_FILE}" ]] && ! is_truthy "${CONFIG_OVERWRITE}"; then
existing_mode="$(config_value "mode")"
if [[ -n "${existing_mode}" ]]; then
printf '%s\n' "${existing_mode}"
return 0
fi
printf 'default\n'
return 0
fi
if [[ -n "${MODE}" ]]; then
printf '%s\n' "${MODE}"
return 0
fi
existing_mode="$(config_value "mode")"
if [[ -n "${existing_mode}" ]]; then
printf '%s\n' "${existing_mode}"
return 0
fi
printf 'default\n'
}
planned_mode_display() {
local target_version="$1"
local existing_mode planned_mode
if ! version_supports_mode "${target_version}"; then
if [[ -f "${CONFIG_FILE}" ]] && is_truthy "${CONFIG_OVERWRITE}" && [[ -n "$(config_value "mode")" ]]; then
printf 'removed on config rewrite; target does not support mode / 重写配置时移除;目标版本不支持 mode\n'
return 0
fi
printf 'not written; requires v6.0.0b3+ / 不写入;需要 v6.0.0b3+\n'
return 0
fi
if [[ -f "${CONFIG_FILE}" ]] && ! is_truthy "${CONFIG_OVERWRITE}"; then
existing_mode="$(config_value "mode")"
if [[ -n "${existing_mode}" ]]; then
printf '%s (preserved / 保留)\n' "${existing_mode}"
return 0
fi
printf 'default (new in v6.0.0b3; will be written / v6.0.0b3 新增;将写入)\n'
return 0
fi
planned_mode="$(planned_mode_value "${target_version}")"
if [[ "${planned_mode}" == "default" && -z "$(config_value "mode")" && -z "${MODE}" ]]; then
printf 'default (implicit) / 默认(隐式)\n'
return 0
fi
printf '%s\n' "${planned_mode}"
}
prompt_for_config_port() {
local existing_listen answer default_port
is_truthy "${ASSUME_YES}" && return 0
can_prompt || return 0
existing_listen="$(config_value "listen")"
default_port="$(normalize_port_value "${PORT}")"
if ! { exec 3<>/dev/tty; } 2>/dev/null; then
return 0
fi
while true; do
if [[ -f "${CONFIG_FILE}" && -n "${existing_listen}" ]]; then
printf 'Listen port / 监听端口 [keep current / 保持当前: %s]: ' "${existing_listen}" >&3
else
printf 'Listen port / 监听端口 [%s]: ' "${default_port}" >&3
fi
IFS= read -r answer <&3 || answer=""
answer="$(trim_value "${answer}")"
if [[ -z "${answer}" && -f "${CONFIG_FILE}" && -n "${existing_listen}" ]]; then
exec 3>&- 3<&-
return 0
fi
if [[ -z "${answer}" ]]; then
answer="${default_port}"
fi
if is_valid_port "${answer}"; then
PORT="$(normalize_port_value "${answer}")"
LISTEN="0.0.0.0:${PORT}"
exec 3>&- 3<&-
return 0
fi
printf 'Invalid port. Please enter an integer from 1 to 65535. / 端口无效,请输入 1 到 65535 之间的整数。\n' >&3
done
}
prompt_for_config_mode() {
local target_version="$1"
local existing_mode answer
version_supports_mode "${target_version}" || return 0
is_truthy "${ASSUME_YES}" && return 0
can_prompt || return 0
[[ "${MODE_EXPLICIT}" -eq 1 ]] && return 0
existing_mode="$(config_value "mode")"
if ! { exec 3<>/dev/tty; } 2>/dev/null; then
return 0
fi
{
printf '\nSnell v6 mode / Snell v6 模式:\n'
printf ' 1. default / 默认:obfuscation + AES / 启用混淆和 AES 加密\n'
printf ' 2. unshaped:AES only / 仅 AES,加密流量看起来完全随机\n'
printf ' 3. unsafe-raw:plaintext / 明文转发,仅限安全内网或安全隧道内\n'
} >&3
while true; do
if [[ -n "${existing_mode}" ]]; then
printf 'Mode / 模式 [keep current / 保持当前: %s]: ' "${existing_mode}" >&3
else
printf 'Mode / 模式 [1, default / 默认]: ' >&3
fi
IFS= read -r answer <&3 || answer=""
answer="$(trim_value "${answer}")"
if [[ -z "${answer}" && -n "${existing_mode}" ]]; then
exec 3>&- 3<&-
return 0
fi
case "${answer:-1}" in
1|default)
MODE="default"
;;
2|unshaped)
MODE="unshaped"
;;
3|unsafe-raw)
MODE="unsafe-raw"
;;
*)
printf 'Invalid mode. Choose 1/default, 2/unshaped, or 3/unsafe-raw. / 模式无效,请选择 1/default、2/unshaped 或 3/unsafe-raw。\n' >&3
continue
;;
esac
exec 3>&- 3<&-
return 0
done
}
port_list_contains() {
local needle="$1"
local haystack="$2"
grep -qx -- "${needle}" <<<"${haystack}"
}
service_is_active() {
command -v systemctl >/dev/null 2>&1 && systemctl is-active --quiet "${SERVICE_NAME}"
}
port_is_listening() {
local port="$1"
if command -v ss >/dev/null 2>&1; then
ss -H -ltn 2>/dev/null \
| awk -v port="${port}" '{ if ($4 ~ ":" port "$") found=1 } END { exit found ? 0 : 1 }'
return $?
fi
if command -v netstat >/dev/null 2>&1; then
netstat -ltn 2>/dev/null \
| awk -v port="${port}" '{ if ($4 ~ ":" port "$") found=1 } END { exit found ? 0 : 1 }'
return $?
fi
if command -v lsof >/dev/null 2>&1; then
lsof -nP -iTCP:"${port}" -sTCP:LISTEN >/dev/null 2>&1
return $?
fi
return 1
}
ensure_planned_ports_available() {
local listen="$1"
local ports existing_listen existing_ports port service_active=0
ports="$(listen_ports "${listen}")"
existing_listen="$(config_value "listen")"
existing_ports=""
if [[ -n "${existing_listen}" ]] && ! existing_ports="$(listen_ports "${existing_listen}" 2>/dev/null)"; then
existing_ports=""
fi
if service_is_active; then
service_active=1
fi
while IFS= read -r port; do
[[ -n "${port}" ]] || continue
if [[ "${service_active}" -eq 1 ]] && port_list_contains "${port}" "${existing_ports}"; then
continue
fi
if port_is_listening "${port}"; then
die "tcp/${port} already appears to be listening; choose another PORT or LISTEN"
fi
done <<<"${ports}"
}
prepare_config_inputs() {
local target_version="${1:-${TARGET_VERSION}}"
local listen existing_mode planned_mode
[[ -n "${target_version}" ]] || die "target version is required before preparing config"
existing_mode="$(config_value "mode")"
if [[ -n "${existing_mode}" ]]; then
if version_supports_mode "${target_version}"; then
validate_mode "${existing_mode}"
elif ! is_truthy "${CONFIG_OVERWRITE}"; then
die "existing config has mode=${existing_mode}, but ${target_version} does not support mode; downgrade with ALLOW_DOWNGRADE=1 CONFIG_OVERWRITE=1 to rewrite the config without mode, or remove mode manually"
fi
fi
if ! config_will_be_written; then
if mode_default_migration_needed "${target_version}"; then
if [[ "${MODE_EXPLICIT}" -eq 1 ]]; then
validate_mode "${MODE}"
if [[ "${MODE}" != "default" ]]; then
die "MODE=${MODE} changes an existing config and requires CONFIG_OVERWRITE=1; automatic v6.0.0b3 migration only writes mode=default"
fi
fi
return 0
fi
if [[ "${MODE_EXPLICIT}" -eq 1 ]]; then
die "MODE only changes an existing config when CONFIG_OVERWRITE=1; current config will otherwise be preserved"
fi
return 0
fi
if [[ "${LISTEN_EXPLICIT}" -eq 1 ]]; then
validate_listen_value "${LISTEN}"
elif [[ "${PORT_EXPLICIT}" -eq 1 ]]; then
PORT="$(normalize_port_value "${PORT}")"
LISTEN="0.0.0.0:${PORT}"
else
prompt_for_config_port
fi
if [[ -z "${LISTEN}" && ! -f "${CONFIG_FILE}" ]]; then
PORT="$(normalize_port_value "${PORT}")"
LISTEN="0.0.0.0:${PORT}"
fi
listen="$(planned_listen_value)"
validate_listen_value "${listen}"
ensure_planned_ports_available "${listen}"
if [[ "${MODE_EXPLICIT}" -eq 1 ]]; then
validate_mode "${MODE}"
if ! version_supports_mode "${target_version}"; then
die "MODE requires Snell v6.0.0b3 or newer; target version is ${target_version}"
fi
fi
prompt_for_config_mode "${target_version}"
planned_mode="$(planned_mode_value "${target_version}")"
if [[ -n "${planned_mode}" ]]; then
validate_mode "${planned_mode}"
fi
}
install_deps() {
local missing=0
command -v curl >/dev/null 2>&1 || missing=1
command -v unzip >/dev/null 2>&1 || missing=1
[[ "${missing}" -eq 1 ]] || return 0
[[ "${AUTO_INSTALL_DEPS}" == "1" ]] || die "curl and unzip are required"
if command -v apt-get >/dev/null 2>&1; then
log "installing curl and unzip via apt-get"
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y curl unzip
return 0
fi
if command -v dnf >/dev/null 2>&1; then
log "installing curl and unzip via dnf"
dnf install -y curl unzip
return 0
fi
if command -v yum >/dev/null 2>&1; then
log "installing curl and unzip via yum"
yum install -y curl unzip
return 0
fi
die "unable to install dependencies automatically; please install curl and unzip"
}
extract_version_from_url() {
local url="$1"
sed -n \
-e 's#.*snell-server-\(v[0-9][^-/]*\)-linux-.*#\1#p' \
-e 's#.*snell-server-\(v[0-9][^-/]*\)$#\1#p' \
<<<"${url}"
}
version_sort_key() {
local version="$1"
if [[ "${version}" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)b([0-9]+)$ ]]; then
printf '%09d.%09d.%09d.%01d.%09d' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[2]}" \
"${BASH_REMATCH[3]}" \
"0" \
"${BASH_REMATCH[4]}"
return 0
fi
if [[ "${version}" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)rc\.?([0-9]+)$ ]]; then
printf '%09d.%09d.%09d.%01d.%09d' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[2]}" \
"${BASH_REMATCH[3]}" \
"1" \
"${BASH_REMATCH[4]}"
return 0
fi
if [[ "${version}" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)rc$ ]]; then
printf '%09d.%09d.%09d.%01d.%09d' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[2]}" \
"${BASH_REMATCH[3]}" \
"1" \
"0"
return 0
fi
if [[ "${version}" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
printf '%09d.%09d.%09d.%01d.%09d' \
"${BASH_REMATCH[1]}" \
"${BASH_REMATCH[2]}" \
"${BASH_REMATCH[3]}" \
"2" \
"0"
return 0
fi
die "unsupported version format: ${version}; expected vX.Y.ZbN, vX.Y.Zrc, vX.Y.ZrcN, vX.Y.Zrc.N (legacy alias), or vX.Y.Z"
}
canonical_version() {
local version="$1"
if [[ "${version}" =~ ^(v[0-9]+\.[0-9]+\.[0-9]+rc)\.([0-9]+)$ ]]; then
printf '%s%s\n' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}"
return 0
fi
printf '%s\n' "${version}"
}
fetch_release_notes() {
curl -fsSL --connect-timeout 15 --max-time 60 --retry 3 --retry-delay 2 "${RELEASE_NOTES_URL}"
}
remote_url_exists() {
curl -fsIL -o /dev/null \
--connect-timeout 15 --max-time "${URL_CHECK_TIMEOUT}" --retry 2 --retry-delay 1 \
"$1" >/dev/null 2>&1
}
release_notes_v6_downloads() {
fetch_release_notes \
| grep -Eo 'https://dl\.nssurge\.com/snell/snell-server-v6[^[:space:]]*-linux-[[:alnum:]]+\.zip' \
| sort -u \
|| true
}
max_version_key_from_urls() {
local urls="$1"
local line version key max=""
[[ -n "${urls}" ]] || return 0
while IFS= read -r line; do
[[ -n "${line}" ]] || continue
version="$(extract_version_from_url "${line}")"
[[ -n "${version}" ]] || continue
key="$(version_sort_key "${version}" 2>/dev/null)" || continue
if [[ -z "${max}" || "${key}" > "${max}" ]]; then
max="${key}"
fi
done <<<"${urls}"
printf '%s' "${max}"
}
curated_v6_downloads() {
local notes_max_key="$1"
local version key arch url
local IFS=$' \t\n'
for version in ${SNELL_V6_KNOWN_VERSIONS}; do
key="$(version_sort_key "${version}" 2>/dev/null)" || continue
# Skip builds the release notes already cover; only probe newer ones.
if [[ -n "${notes_max_key}" ]] && ! [[ "${key}" > "${notes_max_key}" ]]; then
continue
fi
for arch in ${SNELL_V6_SUPPORTED_ARCHES}; do
url="${DOWNLOAD_BASE_URL}/snell-server-${version}-linux-${arch}.zip"
if remote_url_exists "${url}"; then
printf '%s\n' "${url}"
fi
done
done
}
available_v6_downloads() {
local notes notes_max curated
if [[ -z "${_AVAILABLE_V6_DOWNLOADS_CACHE+x}" ]]; then
notes="$(release_notes_v6_downloads)"
notes_max="$(max_version_key_from_urls "${notes}")"
curated="$(curated_v6_downloads "${notes_max}")"
_AVAILABLE_V6_DOWNLOADS_CACHE="$(
printf '%s\n%s\n' "${notes}" "${curated}" \
| grep -E '^https://' \
| sort -u \
|| true
)"
fi
[[ -n "${_AVAILABLE_V6_DOWNLOADS_CACHE}" ]] && printf '%s\n' "${_AVAILABLE_V6_DOWNLOADS_CACHE}"
return 0
}
extract_arch_from_url() {
local url="$1"
sed -n 's#.*-linux-\([[:alnum:]]*\)\.zip#\1#p' <<<"${url}"
}
latest_v6_url() {
local urls line version key ranked=""
urls="$(
available_v6_downloads \
| awk -v arch="${ARCH}" '$0 ~ "-linux-" arch "\\.zip$"'
)"
[[ -n "${urls}" ]] || die "no Snell v6 download found for arch ${ARCH}"
while IFS= read -r line; do
[[ -n "${line}" ]] || continue
version="$(extract_version_from_url "${line}")"
key="$(version_sort_key "${version}")" || return 1
ranked+="${key}"$'\t'"${version}"$'\t'"${line}"$'\n'
done <<<"${urls}"
sort <<<"${ranked}" | tail -n 1 | awk -F '\t' '{print $3}'
}
latest_v6_version() {
local urls line version key ranked=""
urls="$(available_v6_downloads)"
[[ -n "${urls}" ]] || die "no Snell v6 downloads found in release notes"
while IFS= read -r line; do
[[ -n "${line}" ]] || continue
version="$(extract_version_from_url "${line}")"
key="$(version_sort_key "${version}")" || return 1
ranked+="${key}"$'\t'"${version}"$'\n'
done <<<"${urls}"
sort -u <<<"${ranked}" | tail -n 1 | awk -F '\t' '{print $2}'
}
available_arches() {
local version="${VERSION}"
local arches
if [[ "${version}" == "auto" ]]; then
version="$(latest_v6_version)"
else
version="$(canonical_version "${version}")"
fi
arches="$(
available_v6_downloads \
| awk -v version="${version}" '$0 ~ "snell-server-" version "-linux-" { print }' \
| while IFS= read -r line; do
extract_arch_from_url "${line}"
done \
| sort -u \
| tr '\n' ' ' \
| sed 's/[[:space:]]*$//'
)"
[[ -n "${arches}" ]] || die "no downloads found for VERSION=${version}"
printf '%s: %s\n' "${version}" "${arches}"
}
resolve_download_url() {
local version
if [[ "${VERSION}" == "auto" ]]; then
latest_v6_url
return 0
fi
version="$(canonical_version "${VERSION}")"
version_sort_key "${version}" >/dev/null
printf '%s/snell-server-%s-linux-%s.zip\n' "${DOWNLOAD_BASE_URL}" "${version}" "${ARCH}"
}
resolve_version() {
local url="$1"
extract_version_from_url "${url}"
}
current_installed_version() {
local resolved=""
resolved="$(current_installed_path)"
[[ -n "${resolved}" ]] || return 0
extract_version_from_url "${resolved}"
}
current_installed_path() {
if [[ -e "${CURRENT_BIN}" ]]; then
readlink -f "${CURRENT_BIN}" 2>/dev/null || true
fi
}
version_relation() {
local installed_version="$1"
local target_version="$2"
local installed_key target_key
if [[ -z "${installed_version}" ]]; then
printf 'install\n'
return 0
fi
installed_key="$(version_sort_key "${installed_version}")"
target_key="$(version_sort_key "${target_version}")"
if [[ "${target_key}" < "${installed_key}" ]]; then
printf 'downgrade\n'
elif [[ "${target_key}" > "${installed_key}" ]]; then
printf 'update\n'
else
printf 'reinstall\n'
fi
}
ensure_not_downgrade() {
local target_version="$1"
local installed_version target_key installed_key
installed_version="$(current_installed_version)"
[[ -n "${installed_version}" ]] || return 0
target_key="$(version_sort_key "${target_version}")"
installed_key="$(version_sort_key "${installed_version}")"
if [[ "${target_key}" < "${installed_key}" ]] \
&& ! is_truthy "${ALLOW_DOWNGRADE}"; then
die "refusing to downgrade from ${installed_version} to ${target_version}; set ALLOW_DOWNGRADE=1 to override"
fi
}
display_relation() {
case "$1" in
install)
printf 'install / 安装\n'
;;
update)
printf 'update / 更新\n'
;;
reinstall)
printf 'reinstall / 重装\n'
;;
downgrade)
printf 'downgrade / 降级\n'
;;
unknown)
printf 'unknown / 未知\n'
;;
*)
printf '%s\n' "$1"
;;
esac
}
confirm_apply() {
local target_version="$1"
local url="$2"
local installed_path installed_version installed_display relation relation_display config_display listen_display mode_display answer
if is_truthy "${ASSUME_YES}"; then
return 0
fi
installed_path="$(current_installed_path)"
installed_version="$(current_installed_version)"
if [[ -n "${installed_version}" ]]; then
installed_display="${installed_version}"
elif [[ -n "${installed_path}" ]]; then
installed_display="unknown / 未知 (${installed_path})"
else
installed_display="not installed / 未安装"
fi
if [[ -n "${installed_version}" ]]; then
relation="$(version_relation "${installed_version}" "${target_version}")"
elif [[ -n "${installed_path}" ]]; then
relation="unknown"
else
relation="install"
fi
relation_display="$(display_relation "${relation}")"
if mode_default_migration_needed "${target_version}"; then
config_display="add mode=default for v6.0.0b3+, preserving other values / 为 v6.0.0b3+ 补写 mode=default,保留其他值"
elif [[ -f "${CONFIG_FILE}" ]] && is_truthy "${CONFIG_OVERWRITE}"; then
config_display="rewrite existing config, preserving unspecified values / 重写已有配置,保留未显式覆盖的值"
elif [[ -f "${CONFIG_FILE}" ]]; then
config_display="preserve existing config, normalize permissions / 保留已有配置,仅修正权限"
else
config_display="create new config / 创建新配置"
fi
listen_display="$(planned_listen_value)"
mode_display="$(planned_mode_display "${target_version}")"
if ! { exec 3<>/dev/tty; } 2>/dev/null; then
die "confirmation requires a TTY; set ASSUME_YES=1 for non-interactive use / 确认操作需要 TTY;非交互执行请设置 ASSUME_YES=1"
fi
{
printf '\nSnell v6 install plan / Snell v6 安装计划:\n'
printf ' Action / 操作: %s\n' "${relation_display}"
printf ' CPU arch / CPU 架构: %s\n' "${ARCH}"
printf ' Installed version / 已安装版本: %s\n' "${installed_display}"
printf ' Target version / 目标版本: %s\n' "${target_version}"
printf ' Download URL / 下载地址: %s\n' "${url}"
printf ' Binary symlink / 二进制软链接: %s\n' "${CURRENT_BIN}"
printf ' Config file / 配置文件: %s\n' "${CONFIG_FILE}"
printf ' Config action / 配置动作: %s\n' "${config_display}"
printf ' Listen / 监听地址: %s\n' "${listen_display}"
printf ' Mode / 模式: %s\n' "${mode_display}"
printf ' Service / 服务名: %s\n' "${SERVICE_NAME}"
printf '\nProceed? / 是否继续?[y/N] '
} >&3
IFS= read -r answer <&3 || answer=""
exec 3>&- 3<&-
if [[ "${answer}" != "y" && "${answer}" != "Y" && "${answer}" != "yes" && "${answer}" != "YES" ]]; then
die "cancelled by user / 用户已取消"
fi
}
validate_release_binary() {