-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsupernova.sh
More file actions
executable file
·1601 lines (1359 loc) · 42.7 KB
/
Copy pathsupernova.sh
File metadata and controls
executable file
·1601 lines (1359 loc) · 42.7 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
#
# Supernova: Hysteria-only one-file installer
export LANG=en_US.UTF-8
CYAN="\033[36m\033[01m"
BLUE="\033[34m\033[01m"
PINK="\033[95m\033[01m"
GREEN="\033[32m\033[01m"
PLAIN="\033[0m"
RESET="\033[0m"
RED="\033[31m\033[01m"
YELLOW="\033[33m\033[01m"
SUPERNOVA_HOME="${SUPERNOVA_HOME:-/opt/supernova}"
HY_DIR="$SUPERNOVA_HOME/hysteria"
CERT_DIR="$SUPERNOVA_HOME/certs"
STATE_DIR="$SUPERNOVA_HOME/state"
HY_MASQ_DIR="$HY_DIR/masq"
HY_CONFIG="$HY_DIR/config.yaml"
HY_COMPOSE="$HY_DIR/compose.yaml"
HY_LINK_FILE="$STATE_DIR/hy.txt"
HY_RUNTIME_FILE="$STATE_DIR/runtime"
HY_MANAGER_BIN="/usr/local/bin/hy2"
SYSCTL_CONF_FILE="/etc/sysctl.d/90-supernova-hysteria.conf"
SUDO=""
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
SUDO="sudo"
fi
reset_terminal() {
printf "%b" "$RESET"
if [ -n "${TERM:-}" ] && [ "$TERM" != "dumb" ] && command -v tput >/dev/null 2>&1; then
tput sgr0 2>/dev/null || true
fi
}
handle_interrupt() {
reset_terminal
echo
exit 130
}
handle_terminate() {
reset_terminal
echo
exit 143
}
trap reset_terminal EXIT
trap handle_interrupt INT
trap handle_terminate TERM
cyan() { echo -e "${CYAN}$1${PLAIN}"; }
blue() { echo -e "${BLUE}$1${PLAIN}"; }
pink() { echo -e "${PINK}$1${PLAIN}"; }
red() { echo -e "${RED}$1${PLAIN}"; }
green() { echo -e "${GREEN}$1${PLAIN}"; }
yellow() { echo -e "${YELLOW}$1${PLAIN}"; }
is_yes() {
case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
y|yes) return 0 ;;
*) return 1 ;;
esac
}
confirm_yes() {
local answer
read -rp "$1 (y/N) : " answer
is_yes "$answer"
}
require_privileges() {
if [ -n "$SUDO" ] && ! command -v sudo >/dev/null 2>&1; then
red "This installer needs root privileges, but sudo is not installed."
red "Run it as root or install sudo first."
exit 1
fi
}
random_alnum() {
local length="$1"
LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c "$length"
echo
}
install_file() {
local source_file="$1"
local target_file="$2"
local mode="$3"
local owner="${4:-}"
if [ -n "$owner" ]; then
$SUDO install -D -m "$mode" -o "$owner" -g "$owner" "$source_file" "$target_file"
else
$SUDO install -D -m "$mode" "$source_file" "$target_file"
fi
}
sudo_cat() {
local file="$1"
if [ -r "$file" ]; then
cat "$file"
else
$SUDO cat "$file"
fi
}
ensure_runtime_dirs() {
require_privileges
$SUDO mkdir -p "$HY_DIR" "$CERT_DIR" "$STATE_DIR" "$HY_MASQ_DIR"
$SUDO chmod 755 "$SUPERNOVA_HOME" "$HY_DIR" "$CERT_DIR" "$STATE_DIR" "$HY_MASQ_DIR"
}
select_hysteria_obfs_mode() {
local answer
while true; do
echo
echo -e "Select obfuscation mode:"
echo -e " ${BLUE}[1]${PLAIN} Salamander ${GREEN}(default)${PLAIN}"
echo -e " ${BLUE}[2]${PLAIN} Gecko"
read -rp "Choice: " answer
[ -z "$answer" ] && answer=1
case "$answer" in
1)
hysteria_obfs_mode=salamander
return 0
;;
2)
hysteria_obfs_mode=gecko
return 0
;;
*)
red "Invalid obfuscation mode. Please select 1 or 2."
;;
esac
done
}
validate_cert_files() {
if [ ! -s "$CERT_DIR/cert.crt" ] || [ ! -f "$CERT_DIR/cert.crt" ]; then
red "Certificate generation failed: $CERT_DIR/cert.crt is missing or invalid."
exit 1
fi
if [ ! -s "$CERT_DIR/private.key" ] || [ ! -f "$CERT_DIR/private.key" ]; then
red "Certificate generation failed: $CERT_DIR/private.key is missing or invalid."
exit 1
fi
}
tcp_port_in_use() {
local port="$1"
if command -v ss >/dev/null 2>&1; then
ss -ltn "sport = :$port" 2>/dev/null | awk 'NR > 1 { found=1 } END { exit !found }'
return
fi
lsof -nP -iTCP:"$port" -sTCP:LISTEN >/dev/null 2>&1
}
print_docker_commands() {
green "Management commands for Hysteria:"
echo -e "${BLUE}Start:${PLAIN} hy2 start"
echo -e "${BLUE}Stop:${PLAIN} hy2 stop"
echo -e "${BLUE}Logs:${PLAIN} hy2 logs"
echo -e "${BLUE}Show:${PLAIN} hy2 show"
echo -e "${BLUE}Update:${PLAIN} hy2 update"
echo -e "${BLUE}Delete:${PLAIN} hy2 delete"
}
print_systemd_commands() {
green "Management commands for Hysteria:"
echo -e "${BLUE}Start:${PLAIN} hy2 start"
echo -e "${BLUE}Stop:${PLAIN} hy2 stop"
echo -e "${BLUE}Logs:${PLAIN} hy2 logs"
echo -e "${BLUE}Show:${PLAIN} hy2 show"
echo -e "${BLUE}Update:${PLAIN} hy2 update"
echo -e "${BLUE}Delete:${PLAIN} hy2 delete"
}
print_qr_code() {
local config_link="$1"
if command -v qrencode >/dev/null 2>&1; then
qrencode -m 2 -t utf8 <<< "$config_link"
else
yellow "qrencode is not installed; skipping QR code output."
fi
}
curl_without_proxy() {
env -u ALL_PROXY -u all_proxy -u HTTPS_PROXY -u https_proxy -u HTTP_PROXY -u http_proxy curl "$@"
}
proxy_env_is_set() {
[ -n "${ALL_PROXY:-}${all_proxy:-}${HTTPS_PROXY:-}${https_proxy:-}${HTTP_PROXY:-}${http_proxy:-}" ]
}
fetch_url() {
local url="$1"
local response
response=$(curl -fsSL --connect-timeout 4 --max-time 8 "$url" 2>/dev/null || true)
if [ -z "$response" ] && proxy_env_is_set; then
response=$(curl_without_proxy -fsSL --connect-timeout 4 --max-time 8 "$url" 2>/dev/null || true)
fi
printf '%s' "$response"
}
get_public_ip() {
local public_ip
public_ip=$(fetch_url https://api.ipify.org)
if [ -z "$public_ip" ]; then
public_ip=$(hostname -I 2>/dev/null | awk '{print $1}')
fi
printf '%s' "$public_ip"
}
normalize_country_code() {
printf '%s' "$1" | tr '[:lower:]' '[:upper:]' | tr -cd 'A-Z' | cut -c 1-2
}
is_valid_country_code() {
local code="$1"
[ "${#code}" -eq 2 ] && [ "$code" != "XX" ]
}
extract_country_code() {
local response="$1"
local code
code=$(printf '%s' "$response" | tr -d '\r\n[:space:]')
if is_valid_country_code "$(normalize_country_code "$code")" && [ "${#code}" -eq 2 ]; then
normalize_country_code "$code"
return
fi
code=$(printf '%s\n' "$response" | sed -nE 's/^loc=([A-Za-z]{2})$/\1/p' | head -1)
if is_valid_country_code "$(normalize_country_code "$code")"; then
normalize_country_code "$code"
return
fi
code=$(printf '%s' "$response" | grep -Eo '"(country_code|country_code2|countryCode|country|cc)"[[:space:]]*:[[:space:]]*"[A-Za-z]{2}"' | head -1 | sed -E 's/.*"([A-Za-z]{2})"$/\1/')
if is_valid_country_code "$(normalize_country_code "$code")"; then
normalize_country_code "$code"
fi
}
get_country_code() {
local lookup_ip="$1"
local detected_country
local response
local url
local geo_urls=()
if [ -n "$lookup_ip" ]; then
geo_urls=(
"https://ipinfo.io/$lookup_ip/country"
"https://get.geojs.io/v1/ip/country/$lookup_ip.json"
"https://api.iplocation.net/?ip=$lookup_ip"
"https://ipwho.is/$lookup_ip"
"https://ip.guide/$lookup_ip"
"https://ipapi.co/$lookup_ip/country/"
"http://ip-api.com/json/$lookup_ip?fields=status,countryCode"
)
else
geo_urls=(
"https://ipinfo.io/country"
"https://ifconfig.co/country-iso"
"https://www.cloudflare.com/cdn-cgi/trace"
"https://api.country.is"
"https://get.geojs.io/v1/ip/country.json"
"https://api.myip.com"
"https://ipwho.is/"
)
fi
for url in "${geo_urls[@]}"; do
response=$(fetch_url "$url")
detected_country=$(extract_country_code "$response")
if is_valid_country_code "$detected_country"; then
printf '%s' "$detected_country"
return
fi
done
printf 'XX'
}
country_code_to_flag() {
local code
local flag=""
local char
local ascii
local byte
local escape
local i
code=$(normalize_country_code "$1")
if ! is_valid_country_code "$code"; then
return 1
fi
for i in 0 1; do
char=${code:$i:1}
printf -v ascii '%d' "'$char"
byte=$((0xA6 + ascii - 65))
printf -v escape '\\xF0\\x9F\\x87\\x%02X' "$byte"
flag="$flag$(printf '%b' "$escape")"
done
printf '%s' "$flag"
}
country_code_to_label() {
local code
local flag
code=$(normalize_country_code "$1")
flag=$(country_code_to_flag "$code" || true)
if [ -n "$flag" ]; then
printf '%s' "$flag"
else
printf '%s' "${code:-XX}"
fi
}
install_hysteria_official_systemd() {
local installer_file
installer_file=$(mktemp)
if ! curl -fsSL --connect-timeout 10 --max-time 30 https://get.hy2.sh -o "$installer_file"; then
rm -f "$installer_file"
return 1
fi
if command -v timeout >/dev/null 2>&1; then
$SUDO env "ALL_PROXY=${ALL_PROXY:-}" "all_proxy=${all_proxy:-}" "HTTPS_PROXY=${HTTPS_PROXY:-}" "https_proxy=${https_proxy:-}" "HTTP_PROXY=${HTTP_PROXY:-}" "http_proxy=${http_proxy:-}" timeout 120s bash "$installer_file"
else
$SUDO env "ALL_PROXY=${ALL_PROXY:-}" "all_proxy=${all_proxy:-}" "HTTPS_PROXY=${HTTPS_PROXY:-}" "https_proxy=${https_proxy:-}" "HTTP_PROXY=${HTTP_PROXY:-}" "http_proxy=${http_proxy:-}" bash "$installer_file"
fi
local install_status=$?
rm -f "$installer_file"
return $install_status
}
install_hysteria_systemd_service_file() {
if ! id hysteria >/dev/null 2>&1; then
$SUDO useradd -r -d /var/lib/hysteria -m hysteria
fi
$SUDO mkdir -p /var/lib/hysteria
$SUDO chown hysteria:hysteria /var/lib/hysteria
local service_file
service_file=$(mktemp)
cat > "$service_file" <<'EOF'
[Unit]
Description=Hysteria Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/hysteria server --config /etc/hysteria/config.yaml
WorkingDirectory=/var/lib/hysteria
User=hysteria
Group=hysteria
Environment=HYSTERIA_LOG_LEVEL=info
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
NoNewPrivileges=true
Restart=on-failure
RestartSec=5s
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
EOF
install_file "$service_file" /etc/systemd/system/hysteria-server.service 0644
rm -f "$service_file"
}
validate_hysteria_binary() {
local hysteria_binary="$1"
if [ ! -f "$hysteria_binary" ]; then
return 1
fi
if [ ! -x "$hysteria_binary" ]; then
$SUDO chmod 755 "$hysteria_binary" || return 1
fi
"$hysteria_binary" version >/dev/null 2>&1
}
show_hysteria_manual_install_instructions() {
red "Failed to download or install the official Hysteria core for systemd."
yellow "Download the Hysteria Linux binary manually, install it at /usr/local/bin/hysteria, then restart this script and select the systemd method again."
yellow "Example for amd64:"
yellow " curl -fL -o hysteria-linux-amd64 https://github.com/apernet/hysteria/releases/download/app/v2.9.3/hysteria-linux-amd64"
yellow " sudo install -Dm755 hysteria-linux-amd64 /usr/local/bin/hysteria"
yellow " /usr/local/bin/hysteria version"
}
install_hysteria_systemd_runtime() {
if [ -f /usr/local/bin/hysteria ]; then
if ! validate_hysteria_binary /usr/local/bin/hysteria; then
red "/usr/local/bin/hysteria exists but does not look like a valid Hysteria binary."
show_hysteria_manual_install_instructions
exit 1
fi
install_hysteria_systemd_service_file
return
fi
if install_hysteria_official_systemd; then
install_hysteria_systemd_service_file
return
fi
show_hysteria_manual_install_instructions
exit 1
}
hy2_manager_is_managed() {
[ -f "$HY_MANAGER_BIN" ] && grep -q 'Managed by Supernova' "$HY_MANAGER_BIN" 2>/dev/null
}
ensure_hy2_manager_can_install() {
if [ -e "$HY_MANAGER_BIN" ] && ! hy2_manager_is_managed; then
red "$HY_MANAGER_BIN already exists and is not managed by Supernova."
red "Move or remove it before installing Hysteria with Supernova."
exit 1
fi
}
remove_hy2_manager() {
if hy2_manager_is_managed; then
$SUDO rm -f "$HY_MANAGER_BIN"
elif [ -e "$HY_MANAGER_BIN" ]; then
yellow "$HY_MANAGER_BIN exists but is not managed by Supernova; leaving it untouched."
fi
}
write_hysteria_runtime_state() {
local runtime="$1"
ensure_runtime_dirs
printf '%s\n' "$runtime" | $SUDO tee "$HY_RUNTIME_FILE" >/dev/null
$SUDO chmod 0644 "$HY_RUNTIME_FILE"
}
install_hy2_manager() {
local manager_tmp
ensure_hy2_manager_can_install
manager_tmp=$(mktemp)
cat > "$manager_tmp" <<EOF
#!/usr/bin/env bash
# Managed by Supernova. Do not edit manually.
SUPERNOVA_HOME="$SUPERNOVA_HOME"
HY_DIR="$HY_DIR"
CERT_DIR="$CERT_DIR"
STATE_DIR="$STATE_DIR"
HY_CONFIG="$HY_CONFIG"
HY_COMPOSE="$HY_COMPOSE"
HY_LINK_FILE="$HY_LINK_FILE"
HY_RUNTIME_FILE="$HY_RUNTIME_FILE"
HY_MANAGER_BIN="$HY_MANAGER_BIN"
SYSCTL_CONF_FILE="$SYSCTL_CONF_FILE"
SYSTEMD_SERVICE="hysteria-server.service"
EOF
cat >> "$manager_tmp" <<'MANAGER_EOF'
SUDO=""
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
SUDO="sudo"
fi
die() {
echo "hy2: $*" >&2
exit 1
}
info() {
echo "hy2: $*"
}
require_privileges() {
if [ -n "$SUDO" ] && ! command -v sudo >/dev/null 2>&1; then
die "this command needs root privileges, but sudo is not installed"
fi
}
docker_ps_names() {
command -v docker >/dev/null 2>&1 || return 1
if docker ps -a --format '{{.Names}}' 2>/dev/null; then
return 0
fi
if [ -n "$SUDO" ] && command -v sudo >/dev/null 2>&1; then
$SUDO docker ps -a --format '{{.Names}}' 2>/dev/null
return
fi
return 1
}
docker_container_exists() {
docker_ps_names | grep -qx hysteria
}
docker_runtime_valid_for_state() {
[ -f "$HY_COMPOSE" ] && [ -f "$HY_CONFIG" ]
}
docker_runtime_detected() {
docker_runtime_valid_for_state && docker_container_exists
}
systemd_runtime_detected() {
[ -f /etc/hysteria/.supernova-managed ] && return 0
[ -f /etc/systemd/system/hysteria-server.service ] && return 0
command -v systemctl >/dev/null 2>&1 && systemctl list-unit-files "$SYSTEMD_SERVICE" --no-legend 2>/dev/null | grep -q "$SYSTEMD_SERVICE"
}
state_runtime() {
local runtime
[ -f "$HY_RUNTIME_FILE" ] || return 1
runtime=$(cat "$HY_RUNTIME_FILE" 2>/dev/null | tr -d '[:space:]')
case "$runtime" in
docker)
docker_runtime_valid_for_state && printf '%s' docker && return 0
;;
systemd)
systemd_runtime_detected && printf '%s' systemd && return 0
;;
esac
return 1
}
detect_runtime() {
local runtime
local docker_detected=false
local systemd_detected=false
runtime=$(state_runtime || true)
if [ -n "$runtime" ]; then
printf '%s' "$runtime"
return 0
fi
if docker_runtime_detected; then
docker_detected=true
fi
if systemd_runtime_detected; then
systemd_detected=true
fi
if [ "$docker_detected" = true ] && [ "$systemd_detected" = true ]; then
printf '%s' both
return 0
fi
if [ "$docker_detected" = true ]; then
printf '%s' docker
return 0
fi
if [ "$systemd_detected" = true ]; then
printf '%s' systemd
return 0
fi
return 1
}
require_single_runtime() {
local action="$1"
local runtime
runtime=$(detect_runtime || true)
case "$runtime" in
docker|systemd)
printf '%s' "$runtime"
;;
both)
die "both Docker and systemd Hysteria installs were detected; cannot safely run '$action' without a valid $HY_RUNTIME_FILE"
;;
*)
die "no managed Hysteria install was found"
;;
esac
}
docker_cmd() {
command -v docker >/dev/null 2>&1 || die "docker is not installed"
if docker ps >/dev/null 2>&1; then
docker "$@"
else
require_privileges
$SUDO docker "$@"
fi
}
docker_compose() {
[ -f "$HY_COMPOSE" ] || die "Docker compose file not found at $HY_COMPOSE"
docker_cmd compose -f "$HY_COMPOSE" "$@"
}
systemctl_cmd() {
command -v systemctl >/dev/null 2>&1 || die "systemctl is not available"
require_privileges
$SUDO systemctl "$@"
}
start_hysteria() {
local runtime
runtime=$(require_single_runtime start) || exit $?
case "$runtime" in
docker) docker_compose up -d ;;
systemd) systemctl_cmd start "$SYSTEMD_SERVICE" ;;
esac
}
stop_docker() {
if docker_runtime_valid_for_state; then
docker_compose stop || true
elif docker_container_exists; then
docker_cmd stop hysteria || true
fi
}
stop_systemd() {
if systemd_runtime_detected; then
systemctl_cmd stop "$SYSTEMD_SERVICE" || true
fi
}
stop_hysteria() {
local runtime
runtime=$(state_runtime || true)
case "$runtime" in
docker)
stop_docker
return
;;
systemd)
stop_systemd
return
;;
esac
runtime=$(detect_runtime || true)
case "$runtime" in
docker) stop_docker ;;
systemd) stop_systemd ;;
both)
stop_docker
stop_systemd
;;
*) die "no managed Hysteria install was found" ;;
esac
}
logs_hysteria() {
local runtime
runtime=$(require_single_runtime logs) || exit $?
case "$runtime" in
docker) docker_compose logs -f ;;
systemd)
command -v journalctl >/dev/null 2>&1 || die "journalctl is not available"
require_privileges
$SUDO journalctl -u "$SYSTEMD_SERVICE" -f
;;
esac
}
read_link_file() {
[ -f "$HY_LINK_FILE" ] || die "no saved Hysteria configuration link was found at $HY_LINK_FILE"
if [ -r "$HY_LINK_FILE" ]; then
cat "$HY_LINK_FILE"
else
require_privileges
$SUDO cat "$HY_LINK_FILE"
fi
}
show_hysteria() {
require_single_runtime show >/dev/null
read_link_file
echo
if command -v qrencode >/dev/null 2>&1; then
read_link_file | head -1 | qrencode -m 2 -t utf8
fi
}
update_docker() {
info "Pulling latest Hysteria Docker image before touching the running container..."
docker_compose pull hysteria || die "Docker image pull failed; current Hysteria container was left untouched"
info "Image pull succeeded; recreating Hysteria container..."
docker_compose up -d --force-recreate
}
install_supernova_systemd_unit() {
local service_tmp
local override_tmp
require_privileges
if ! id hysteria >/dev/null 2>&1; then
$SUDO useradd -r -d /var/lib/hysteria -m hysteria
fi
$SUDO mkdir -p /var/lib/hysteria /etc/systemd/system/hysteria-server.service.d
$SUDO chown hysteria:hysteria /var/lib/hysteria
service_tmp=$(mktemp)
cat > "$service_tmp" <<'SERVICE_EOF'
[Unit]
Description=Hysteria Server Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/hysteria server --config /etc/hysteria/config.yaml
WorkingDirectory=/var/lib/hysteria
User=hysteria
Group=hysteria
Environment=HYSTERIA_LOG_LEVEL=info
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
NoNewPrivileges=true
Restart=on-failure
RestartSec=5s
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
SERVICE_EOF
$SUDO install -D -m 0644 "$service_tmp" /etc/systemd/system/hysteria-server.service
rm -f "$service_tmp"
override_tmp=$(mktemp)
cat > "$override_tmp" <<'OVERRIDE_EOF'
[Service]
Restart=on-failure
RestartSec=5s
OVERRIDE_EOF
$SUDO install -D -m 0644 "$override_tmp" /etc/systemd/system/hysteria-server.service.d/supernova.conf
rm -f "$override_tmp"
echo "managed_by=supernova" | $SUDO tee /etc/hysteria/.supernova-managed >/dev/null
$SUDO chown -R hysteria:hysteria /etc/hysteria
}
run_official_hysteria_installer() {
local installer_file
local install_status
command -v curl >/dev/null 2>&1 || die "curl is not installed"
installer_file=$(mktemp)
if ! curl -fsSL --connect-timeout 10 --max-time 30 https://get.hy2.sh -o "$installer_file"; then
rm -f "$installer_file"
die "failed to download official Hysteria installer; current service was left running"
fi
require_privileges
if command -v timeout >/dev/null 2>&1; then
$SUDO env "ALL_PROXY=${ALL_PROXY:-}" "all_proxy=${all_proxy:-}" "HTTPS_PROXY=${HTTPS_PROXY:-}" "https_proxy=${https_proxy:-}" "HTTP_PROXY=${HTTP_PROXY:-}" "http_proxy=${http_proxy:-}" FORCE_NO_SYSTEMD=2 timeout 120s bash "$installer_file"
else
$SUDO env "ALL_PROXY=${ALL_PROXY:-}" "all_proxy=${all_proxy:-}" "HTTPS_PROXY=${HTTPS_PROXY:-}" "https_proxy=${https_proxy:-}" "HTTP_PROXY=${HTTP_PROXY:-}" "http_proxy=${http_proxy:-}" FORCE_NO_SYSTEMD=2 bash "$installer_file"
fi
install_status=$?
rm -f "$installer_file"
return $install_status
}
update_systemd() {
info "Downloading and installing latest Hysteria core before restarting the service..."
if ! run_official_hysteria_installer; then
die "Hysteria core update failed; existing service was not restarted"
fi
install_supernova_systemd_unit
systemctl_cmd daemon-reload
systemctl_cmd enable "$SYSTEMD_SERVICE"
systemctl_cmd restart "$SYSTEMD_SERVICE"
}
update_hysteria() {
local runtime
runtime=$(require_single_runtime update) || exit $?
case "$runtime" in
docker) update_docker ;;
systemd) update_systemd ;;
esac
}
remove_docker() {
if docker_container_exists; then
docker_cmd rm -f hysteria >/dev/null 2>&1 || true
fi
}
remove_systemd() {
if [ -f /etc/hysteria/.supernova-managed ] || [ "$(cat "$HY_RUNTIME_FILE" 2>/dev/null | tr -d '[:space:]')" = "systemd" ]; then
require_privileges
$SUDO systemctl disable --now "$SYSTEMD_SERVICE" >/dev/null 2>&1 || true
$SUDO rm -rf /etc/hysteria
$SUDO rm -f /etc/systemd/system/hysteria-server.service
$SUDO rm -f /etc/systemd/system/hysteria-server@.service
$SUDO rm -rf /etc/systemd/system/hysteria-server.service.d
$SUDO rm -f /usr/local/bin/hysteria
$SUDO rm -rf /var/lib/hysteria
if id hysteria >/dev/null 2>&1; then
$SUDO userdel hysteria >/dev/null 2>&1 || true
fi
$SUDO systemctl daemon-reload >/dev/null 2>&1 || true
fi
}
delete_hysteria() {
require_privileges
remove_docker
remove_systemd
$SUDO rm -f "$SYSCTL_CONF_FILE"
$SUDO rm -rf "$HY_DIR" "$STATE_DIR"
if [ -f "$HY_MANAGER_BIN" ] && grep -q 'Managed by Supernova' "$HY_MANAGER_BIN" 2>/dev/null; then
$SUDO rm -f "$HY_MANAGER_BIN"
fi
}
usage() {
cat <<USAGE_EOF
Usage: hy2 <command>
Commands:
start Start Hysteria
stop Stop Hysteria
logs Follow Hysteria logs
show Show saved Hysteria client link
update Update Hysteria Docker image or native core
delete Delete the Supernova-managed Hysteria install
USAGE_EOF
}
case "${1:-}" in
start) start_hysteria ;;
stop) stop_hysteria ;;
logs) logs_hysteria ;;
show) show_hysteria ;;
update) update_hysteria ;;
delete|remove|uninstall) delete_hysteria ;;
-h|--help|help) usage ;;
*)
usage >&2
exit 1
;;
esac
MANAGER_EOF
install_file "$manager_tmp" "$HY_MANAGER_BIN" 0755
rm -f "$manager_tmp"
}
clients() {
echo -e "${BLUE}========================================================${PLAIN}"
echo -e "${RED}Recommended Hysteria clients${PLAIN}"
echo
echo -e "${GREEN}Android${PLAIN}:"
echo -e "${YELLOW}https://github.com/MatsuriDayo/NekoBoxForAndroid/releases${PLAIN}"
echo
echo -e "${GREEN}Windows/Linux/macOS${PLAIN}:"
echo -e "${YELLOW}https://github.com/MatsuriDayo/nekoray/releases${PLAIN}"
echo -e "${BLUE}========================================================${PLAIN}"
}
apt_install_if_available() {
local packages=("$@")
if command -v apt >/dev/null 2>&1; then
$SUDO apt update || yellow "apt update failed; continuing with available package metadata."
$SUDO apt install --no-upgrade "${packages[@]}" -y || yellow "Some dependencies could not be installed; verifying installed tools."
fi
}
install_hysteria_base_dependencies() {
local missing_packages=()
local required_cmd
for required_cmd in curl lsof openssl; do
if ! command -v "$required_cmd" >/dev/null 2>&1; then
missing_packages+=("$required_cmd")
fi
done
if [ ${#missing_packages[@]} -gt 0 ]; then
apt_install_if_available "${missing_packages[@]}"
fi
for required_cmd in curl lsof openssl; do
if ! command -v "$required_cmd" >/dev/null 2>&1; then
red "$required_cmd is required for Hysteria but is not installed."
exit 1
fi
done
if ! command -v qrencode >/dev/null 2>&1; then
yellow "qrencode is not installed; QR code output will be skipped."
fi
}
install_hysteria_docker_dependencies() {
install_hysteria_base_dependencies
if command -v docker >/dev/null 2>&1; then
cyan "Docker is installed. Continuing..."
else
if [ "${SUPERNOVA_SKIP_DOCKER_INSTALL:-}" = "1" ]; then
red "Docker is not installed and SUPERNOVA_SKIP_DOCKER_INSTALL=1 was set."
exit 1
fi
pink "Installing Docker..."
local installer_file
installer_file=$(mktemp)
curl -fsSL https://get.docker.com -o "$installer_file" || {
rm -f "$installer_file"
red "Failed to download Docker installer."
exit 1
}
$SUDO sh "$installer_file"
rm -f "$installer_file"
fi
if ! docker compose version >/dev/null 2>&1 && ! $SUDO docker compose version >/dev/null 2>&1; then
red "Docker Compose v2 is required but was not found."
exit 1
fi
}
get_cert() {
local private_key_tmp
local cert_tmp
ensure_runtime_dirs
if [ -s "$CERT_DIR/cert.crt" ] && [ -f "$CERT_DIR/cert.crt" ] && [ -s "$CERT_DIR/private.key" ] && [ -f "$CERT_DIR/private.key" ]; then
cyan "Found certificates. Continuing..."
return
fi
private_key_tmp=$(mktemp)
cert_tmp=$(mktemp)
if ! openssl ecparam -genkey -name prime256v1 -out "$private_key_tmp"; then
rm -f "$private_key_tmp" "$cert_tmp"
red "Failed to generate private key."
exit 1
fi
if ! openssl req -new -x509 -days 36500 -key "$private_key_tmp" -out "$cert_tmp" -subj "/CN=bing.com"; then
rm -f "$private_key_tmp" "$cert_tmp"
red "Failed to generate certificate."
exit 1
fi
install_file "$private_key_tmp" "$CERT_DIR/private.key" 0640
install_file "$cert_tmp" "$CERT_DIR/cert.crt" 0644
rm -f "$private_key_tmp" "$cert_tmp"
validate_cert_files
}