-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver-proxy.sh
More file actions
executable file
·2194 lines (1928 loc) · 71.9 KB
/
Copy pathserver-proxy.sh
File metadata and controls
executable file
·2194 lines (1928 loc) · 71.9 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
#!/bin/bash
# 定义颜色代码
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
RESET='\033[0m'
# 定义常量
CONFIG_DIR="/etc/sing-box"
CONFIG_FILE="${CONFIG_DIR}/config.json"
SERVICE_NAME="sing-box"
DIRECT_CONFIG_FILE="${CONFIG_DIR}/direct_configs.conf"
# Snell 相关常量
SNELL_CONFIG_DIR="/etc/snell"
SNELL_CONFIG_FILE="${SNELL_CONFIG_DIR}/snell-server.conf"
SNELL_SERVICE_NAME="snell"
# 检查 root 权限
check_root() {
if [ "$(id -u)" != "0" ]; then
echo -e "${RED}请使用 root 权限执行此脚本!${RESET}"
exit 1
fi
}
# 检查 sing-box 是否已安装
is_sing_box_installed() {
if command -v sing-box &> /dev/null; then
return 0
else
return 1
fi
}
# 检查 sing-box 运行状态
is_sing_box_running() {
systemctl is-active --quiet "${SERVICE_NAME}"
return $?
}
# 检查 Snell 是否已安装
is_snell_installed() {
if command -v snell-server &> /dev/null; then
return 0
else
return 1
fi
}
# 检查 Snell 运行状态
is_snell_running() {
systemctl is-active --quiet "${SNELL_SERVICE_NAME}"
return $?
}
# 检查 ss 命令是否可用
check_ss_command() {
if ! command -v ss &> /dev/null; then
echo -e "${YELLOW}ss 命令未找到,正在尝试自动安装 iproute2 ${RESET}"
# 检测包管理器并安装
if command -v apt-get &> /dev/null; then
sudo apt-get update && sudo apt-get install -y iproute2
elif command -v yum &> /dev/null; then
sudo yum install -y iproute
elif command -v dnf &> /dev/null; then
sudo dnf install -y iproute
elif command -v pacman &> /dev/null; then
sudo pacman -Sy --noconfirm iproute2
elif command -v zypper &> /dev/null; then
sudo zypper install -y iproute2
else
echo -e "${RED}无法检测到支持的包管理器,请手动安装 iproute2 包${RESET}"
exit 1
fi
# 再次检查是否安装成功
if command -v ss &> /dev/null; then
echo -e "${GREEN}iproute2 安装成功,ss 命令已可用${RESET}"
else
echo -e "${RED}自动安装失败,请手动安装 iproute2 包${RESET}"
exit 1
fi
else
echo -e "${GREEN}ss 命令可用${RESET}"
fi
}
# 检查 jq 命令是否可用
check_jq_command() {
if ! command -v jq &> /dev/null; then
echo -e "${YELLOW}jq 命令未找到,正在尝试自动安装 jq ${RESET}"
# 检测包管理器并安装
if command -v apt-get &> /dev/null; then
sudo apt-get update && sudo apt-get install -y jq
elif command -v yum &> /dev/null; then
sudo yum install -y jq
elif command -v dnf &> /dev/null; then
sudo dnf install -y jq
elif command -v pacman &> /dev/null; then
sudo pacman -Sy --noconfirm jq
elif command -v zypper &> /dev/null; then
sudo zypper install -y jq
else
echo -e "${RED}无法检测到支持的包管理器,请手动安装 jq 包${RESET}"
exit 1
fi
# 再次检查是否安装成功
if command -v jq &> /dev/null; then
echo -e "${GREEN}jq 安装成功,jq 命令已可用${RESET}"
else
echo -e "${RED}自动安装失败,请手动安装 jq 包${RESET}"
exit 1
fi
fi
}
# 检查端口是否已被使用
is_port_available() {
local port=$1
if ss -tuln | grep -q ":$port "; then
return 1 # 端口已被使用
else
return 0 # 端口可用
fi
}
# 生成未被占用的端口
generate_unused_port() {
local port
while true; do
port=$(shuf -i 1025-65535 -n 1)
if is_port_available $port; then
echo $port
return
fi
done
}
# 安装 sing-box
install_sing_box() {
echo -e "${CYAN}正在安装 sing-box${RESET}"
# 下载并运行 sing-box 安装脚本
bash <(curl -fsSL https://sing-box.app/deb-install.sh) || {
echo -e "${RED}sing-box 安装失败!请检查网络连接或安装脚本来源。${RESET}"
exit 1
}
# 生成随机端口和密码
check_ss_command
vless_port=$(generate_unused_port)
hysteria_port=$(generate_unused_port)
anytls_port=$(generate_unused_port)
shadowsocks_port=$(generate_unused_port)
ss_password=$(sing-box generate rand --base64 32)
password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 12)
socks_port=$(generate_unused_port)
http_port=$(generate_unused_port)
socks_username=$(tr -dc a-z </dev/urandom | head -c 8)
socks_password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 12)
http_username=$(tr -dc a-z </dev/urandom | head -c 8)
http_password=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 12)
# 生成 UUID 和 Reality 密钥对
uuid=$(sing-box generate uuid)
reality_output=$(sing-box generate reality-keypair)
private_key=$(echo "${reality_output}" | grep -oP 'PrivateKey:\s*\K.*')
public_key=$(echo "${reality_output}" | grep -oP 'PublicKey:\s*\K.*')
# 生成自签名证书
mkdir -p "${CONFIG_DIR}"
openssl ecparam -genkey -name prime256v1 -out "${CONFIG_DIR}/private.key" || {
echo -e "${RED}生成私钥失败${RESET}"
exit 1
}
openssl req -new -x509 -days 3650 -key "${CONFIG_DIR}/private.key" -out "${CONFIG_DIR}/cert.pem" -subj "/CN=bing.com" || {
echo -e "${RED}生成证书失败${RESET}"
exit 1
}
# 获取本机 IP 地址和所在国家
host_ip=$(curl -s http://checkip.amazonaws.com)
ip_country=$(curl -s http://ipinfo.io/${host_ip}/country)
# 参数直接从 config.json 读取
# 生成最小配置文件(初次安装不包含节点信息)
cat > "${CONFIG_FILE}" << EOF
{
"log": {
"output": "stdout",
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"type": "local",
"tag": "local-ipv4"
}
]
},
"route": {
"default_domain_resolver": {
"server": "local-ipv4",
"strategy": "ipv4_only"
},
"rules": [
{
"ip_is_private": true,
"outbound": "block"
}
]
},
"outbounds": [
{
"tag": "direct",
"type": "direct",
"domain_resolver": {
"server": "local-ipv4",
"strategy": "ipv4_only"
}
},
{
"tag": "block",
"type": "block"
}
]
}
EOF
# 启用并启动 sing-box 服务
systemctl enable "${SERVICE_NAME}" || {
echo -e "${RED}无法启用 ${SERVICE_NAME} 服务!${RESET}"
exit 1
}
systemctl start "${SERVICE_NAME}" || {
echo -e "${RED}无法启动 ${SERVICE_NAME} 服务!${RESET}"
exit 1
}
# 检查服务状态
if ! is_sing_box_running; then
echo -e "${RED}${SERVICE_NAME} 服务未成功启动!${RESET}"
systemctl status "${SERVICE_NAME}"
exit 1
fi
echo -e "${GREEN}sing-box 安装成功!${RESET}"
echo -e "${YELLOW}提示:已使用最小配置安装,请使用菜单选项生成节点配置。${RESET}"
}
# 安装 Snell
install_snell() {
echo -e "${CYAN}正在安装 Snell${RESET}"
# 检查是否已安装
if is_snell_installed; then
echo -e "${YELLOW}Snell 已经安装!${RESET}"
return 0
fi
# 检查必要的包管理器函数
if ! command -v apt-get &> /dev/null && ! command -v yum &> /dev/null; then
echo -e "${RED}不支持的系统包管理器${RESET}"
return 1
fi
# 安装必要软件包
echo -e "${GREEN}安装必要软件包${RESET}"
if command -v apt-get &> /dev/null; then
apt update
apt install -y wget unzip curl
elif command -v yum &> /dev/null; then
yum -y update
yum -y install wget unzip curl
fi
# Snell 版本
local SNELL_VERSION="v5.0.1"
# 检测系统架构
local ARCH=$(arch)
local SNELL_URL
if [[ ${ARCH} == "aarch64" ]]; then
SNELL_URL="https://dl.nssurge.com/snell/snell-server-${SNELL_VERSION}-linux-aarch64.zip"
else
SNELL_URL="https://dl.nssurge.com/snell/snell-server-${SNELL_VERSION}-linux-amd64.zip"
fi
# 下载 Snell
echo -e "${GREEN}下载 Snell...${RESET}"
wget ${SNELL_URL} -O snell-server.zip || {
echo -e "${RED}下载 Snell 失败${RESET}"
return 1
}
# 解压安装
echo -e "${GREEN}安装 Snell...${RESET}"
unzip -o snell-server.zip -d /usr/local/bin || {
echo -e "${RED}解压缩 Snell 失败${RESET}"
return 1
}
rm snell-server.zip
chmod +x /usr/local/bin/snell-server
# 创建 Snell 用户
if ! id "snell" &>/dev/null; then
useradd -r -s /usr/sbin/nologin snell
fi
# 创建配置目录
mkdir -p "${SNELL_CONFIG_DIR}"
# 生成 systemd 服务文件
cat > /etc/systemd/system/snell.service << EOF
[Unit]
Description=Snell Proxy Service
After=network.target
[Service]
Type=simple
User=snell
Group=snell
ExecStart=/usr/local/bin/snell-server -c ${SNELL_CONFIG_FILE}
AmbientCapabilities=CAP_NET_BIND_SERVICE CAP_NET_ADMIN CAP_NET_RAW
CapabilityBoundingSet=CAP_NET_BIND_SERVICE CAP_NET_ADMIN CAP_NET_RAW
LimitNOFILE=32768
Restart=on-failure
StandardOutput=journal
StandardError=journal
SyslogIdentifier=snell-server
[Install]
WantedBy=multi-user.target
EOF
# 启用服务(但不启动,因为还没有配置)
systemctl daemon-reload
systemctl enable "${SNELL_SERVICE_NAME}"
echo -e "${GREEN}Snell 安装成功!${RESET}"
echo -e "${YELLOW}提示:Snell 已安装但未配置,请使用菜单选项生成配置。${RESET}"
}
# 生成 Snell 配置
generate_snell_config() {
echo -e "${CYAN}=== 生成 Snell 配置 ===${RESET}"
# 检查 Snell 是否已安装
if ! is_snell_installed; then
echo -e "${RED}Snell 尚未安装,请先安装!${RESET}"
return 1
fi
# 检查是否已有配置
if [ -f "${SNELL_CONFIG_FILE}" ]; then
echo -e "${YELLOW}Snell 配置已存在,是否重新生成?${RESET}"
read -p "$(echo -e "${RED}重新生成将覆盖现有配置!(y/N) ${RESET}")" confirm
confirm=${confirm:-N}
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}已取消操作${RESET}"
return 0
fi
fi
# 获取或生成端口
local port
while true; do
read -r -p "请输入 Snell 端口 (直接回车使用随机端口): " port
# 如果用户直接回车,使用随机端口
if [ -z "$port" ]; then
port=$(generate_unused_port)
echo -e "${CYAN}使用随机端口: ${port}${RESET}"
break
fi
# 验证端口格式
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1025 ] || [ "$port" -gt 65535 ]; then
echo -e "${RED}端口必须是 1025-65535 之间的数字!${RESET}"
continue
fi
# 检查端口是否可用
if ! is_port_available "$port"; then
echo -e "${RED}端口 $port 已被占用,请选择其他端口!${RESET}"
continue
fi
break
done
# 生成随机密钥
local psk=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 20)
# 获取本机 IP 和国家
local host_ip=$(curl -s http://checkip.amazonaws.com)
local ip_country=$(curl -s http://ipinfo.io/${host_ip}/country)
# 生成 Snell 配置文件
cat > "${SNELL_CONFIG_FILE}" << EOF
[snell-server]
listen = 0.0.0.0:${port}
psk = ${psk}
ipv6 = false
EOF
# 参数直接从 snell-server.conf 读取
# 生成客户端配置
cat > "${SNELL_CONFIG_DIR}/config.txt" << EOF
${ip_country}-snell = snell, ${host_ip}, ${port}, psk = ${psk}, version = 5, reuse = true
EOF
# 启动服务
echo -e "${YELLOW}正在启动 Snell 服务...${RESET}"
systemctl start "${SNELL_SERVICE_NAME}"
# 检查服务状态
if ! is_snell_running; then
echo -e "${RED}Snell 服务启动失败${RESET}"
systemctl status "${SNELL_SERVICE_NAME}"
return 1
fi
echo -e "${GREEN}Snell 配置生成成功!${RESET}"
echo -e "${CYAN}Snell 配置信息:${RESET}"
cat "${SNELL_CONFIG_DIR}/config.txt"
# 配置信息已保存在 snell-server.conf 中
}
# 删除 Snell 配置
delete_snell_config() {
echo -e "${CYAN}=== 删除 Snell 配置 ===${RESET}"
# 检查 Snell 是否已安装
if ! is_snell_installed; then
echo -e "${RED}Snell 尚未安装!${RESET}"
return 1
fi
# 检查是否有配置
if [ ! -f "${SNELL_CONFIG_FILE}" ]; then
echo -e "${YELLOW}Snell 配置不存在!${RESET}"
return 0
fi
read -r -p "$(echo -e "${RED}确定要删除 Snell 配置吗?这将停止服务并删除配置文件。(y/N) ${RESET}")" confirm
confirm=${confirm:-N}
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}已取消操作${RESET}"
return 0
fi
# 停止服务
systemctl stop "${SNELL_SERVICE_NAME}" 2>/dev/null
# 删除配置文件
rm -f "${SNELL_CONFIG_FILE}"
rm -f "${SNELL_CONFIG_DIR}/config.txt"
echo -e "${GREEN}Snell 配置已删除!${RESET}"
echo -e "${YELLOW}提示:Snell 服务已停止,但程序文件仍保留。如需重新配置,请使用菜单选项生成配置。${RESET}"
}
uninstall_sing_box() {
read -p "$(echo -e "${RED}确定要卸载 sing-box 吗? (Y/n) ${RESET}")" choice
choice=${choice:-Y} # 默认设置为 Y
case "${choice}" in
y|Y)
echo -e "${CYAN}正在卸载 sing-box${RESET}"
# 停止 sing-box 服务
systemctl stop "${SERVICE_NAME}" || {
echo -e "${RED}停止 sing-box 服务失败。${RESET}"
}
# 禁用 sing-box 服务
systemctl disable "${SERVICE_NAME}" || {
echo -e "${RED}禁用 sing-box 服务失败。${RESET}"
}
# 卸载 sing-box
dpkg --purge sing-box || {
echo -e "${YELLOW}无法通过 dpkg 卸载 sing-box,可能未通过 apt 安装。${RESET}"
}
# 删除配置目录和所有相关文件
if [ -d "${CONFIG_DIR}" ]; then
echo -e "${YELLOW}正在删除配置目录: ${CONFIG_DIR}${RESET}"
rm -rf "${CONFIG_DIR}" || {
echo -e "${YELLOW}无法删除配置目录 ${CONFIG_DIR}${RESET}"
}
fi
# 重新加载 systemd
systemctl daemon-reload || {
echo -e "${YELLOW}无法重新加载 systemd 守护进程。${RESET}"
}
# 删除 sing-box 可执行文件,如果存在
if [ -f "/usr/local/bin/sing-box" ]; then
rm /usr/local/bin/sing-box || {
echo -e "${YELLOW}无法删除 /usr/local/bin/sing-box。${RESET}"
}
fi
echo -e "${GREEN}sing-box 卸载成功${RESET}"
;;
*)
echo -e "${YELLOW}已取消卸载操作${RESET}"
;;
esac
}
# 卸载 Snell
uninstall_snell() {
read -p "$(echo -e "${RED}确定要卸载 Snell 吗? (Y/n) ${RESET}")" choice
choice=${choice:-Y} # 默认设置为 Y
case "${choice}" in
y|Y)
echo -e "${CYAN}正在卸载 Snell${RESET}"
# 停止 Snell 服务
systemctl stop "${SNELL_SERVICE_NAME}" || {
echo -e "${RED}停止 Snell 服务失败。${RESET}"
}
# 禁用 Snell 服务
systemctl disable "${SNELL_SERVICE_NAME}" || {
echo -e "${RED}禁用 Snell 服务失败。${RESET}"
}
# 删除服务文件
rm -f /etc/systemd/system/snell.service
# 重新加载 systemd
systemctl daemon-reload || {
echo -e "${YELLOW}无法重新加载 systemd 守护进程。${RESET}"
}
# 删除 Snell 可执行文件
if [ -f "/usr/local/bin/snell-server" ]; then
rm /usr/local/bin/snell-server || {
echo -e "${YELLOW}无法删除 /usr/local/bin/snell-server。${RESET}"
}
fi
# 删除配置目录
if [ -d "${SNELL_CONFIG_DIR}" ]; then
echo -e "${YELLOW}正在删除配置目录: ${SNELL_CONFIG_DIR}${RESET}"
rm -rf "${SNELL_CONFIG_DIR}" || {
echo -e "${YELLOW}无法删除配置目录 ${SNELL_CONFIG_DIR}${RESET}"
}
fi
# 删除 Snell 用户(如果存在且没有其他用途)
if id "snell" &>/dev/null; then
echo -e "${YELLOW}注意:保留 Snell 用户,如需删除请手动执行 'userdel snell'${RESET}"
fi
echo -e "${GREEN}Snell 卸载成功${RESET}"
;;
*)
echo -e "${YELLOW}已取消卸载操作${RESET}"
;;
esac
}
# 启动 sing-box
start_sing_box() {
systemctl start "${SERVICE_NAME}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}${SERVICE_NAME} 服务成功启动${RESET}"
else
echo -e "${RED}${SERVICE_NAME} 服务启动失败${RESET}"
fi
}
# 停止 sing-box
stop_sing_box() {
systemctl stop "${SERVICE_NAME}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}${SERVICE_NAME} 服务成功停止${RESET}"
else
echo -e "${RED}${SERVICE_NAME} 服务停止失败${RESET}"
fi
}
# 重启 sing-box
restart_sing_box() {
systemctl restart "${SERVICE_NAME}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}${SERVICE_NAME} 服务成功重启${RESET}"
else
echo -e "${RED}${SERVICE_NAME} 服务重启失败${RESET}"
fi
}
# 查看 sing-box 状态
status_sing_box() {
systemctl status "${SERVICE_NAME}"
}
# 查看 sing-box 日志
log_sing_box() {
sudo journalctl -u sing-box --output cat -f
}
# 启动 Snell
start_snell() {
systemctl start "${SNELL_SERVICE_NAME}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}${SNELL_SERVICE_NAME} 服务成功启动${RESET}"
else
echo -e "${RED}${SNELL_SERVICE_NAME} 服务启动失败${RESET}"
fi
}
# 停止 Snell
stop_snell() {
systemctl stop "${SNELL_SERVICE_NAME}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}${SNELL_SERVICE_NAME} 服务成功停止${RESET}"
else
echo -e "${RED}${SNELL_SERVICE_NAME} 服务停止失败${RESET}"
fi
}
# 重启 Snell
restart_snell() {
systemctl restart "${SNELL_SERVICE_NAME}"
if [ $? -eq 0 ]; then
echo -e "${GREEN}${SNELL_SERVICE_NAME} 服务成功重启${RESET}"
else
echo -e "${RED}${SNELL_SERVICE_NAME} 服务重启失败${RESET}"
fi
}
# 查看 Snell 状态
status_snell() {
systemctl status "${SNELL_SERVICE_NAME}"
}
# 查看 Snell 日志
log_snell() {
sudo journalctl -u snell --output cat -f
}
# 获取或生成端口(允许用户输入或使用随机端口)
get_or_generate_port() {
local protocol_name=$1
local default_port=$2
local port
while true; do
read -p "请输入 ${protocol_name} 端口 (直接回车使用随机端口 ${default_port}): " port
# 如果用户直接回车,使用默认随机端口
if [ -z "$port" ]; then
port=$default_port
echo -e "${CYAN}使用随机端口: ${port}${RESET}" >&2
echo "$port"
return 0
fi
# 验证端口格式
if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1025 ] || [ "$port" -gt 65535 ]; then
echo -e "${RED}端口必须是 1025-65535 之间的数字!${RESET}" >&2
continue
fi
# 检查端口是否可用
if ! is_port_available "$port"; then
echo -e "${RED}端口 $port 已被占用,请选择其他端口!${RESET}" >&2
continue
fi
echo "$port"
return 0
done
}
# 生成节点配置
generate_node_config() {
# 检查是否已安装
if ! is_sing_box_installed; then
echo -e "${RED}sing-box 未安装,请先安装!${RESET}"
return 1
fi
# 检查配置文件是否存在
if [ ! -f "${CONFIG_FILE}" ]; then
echo -e "${RED}配置文件不存在,请先完成初始安装!${RESET}"
return 1
fi
# 从配置文件读取必要参数
if ! parse_config_from_json; then
echo -e "${RED}无法从配置文件读取参数${RESET}"
return 1
fi
echo -e "${CYAN}请选择要生成的协议(可多选,用空格分隔):${RESET}"
echo "1. Hysteria2"
echo "2. Shadowsocks"
echo "3. VLESS+Vision+Reality"
echo "4. AnyTLS"
echo "5. SOCKS5代理"
echo "6. HTTP代理"
echo "7. 全部"
read -p "请输入选项编号 (1-7): " choices
# 检查必要的命令
check_ss_command
check_jq_command
# 读取当前配置
local current_config
current_config=$(cat "${CONFIG_FILE}") || {
echo -e "${RED}错误: 无法读取配置文件${RESET}"
return 1
}
# 检查当前配置是否有 inbounds 字段
if ! echo "$current_config" | jq -e '.inbounds' &>/dev/null; then
# 如果不存在 inbounds,创建一个空数组
current_config=$(echo "$current_config" | jq '.inbounds = []')
fi
local new_inbounds
new_inbounds=$(echo "$current_config" | jq '.inbounds')
if [[ "$choices" == *"7"* ]] || [[ "$choices" == *"1"* ]]; then
# 获取 Hysteria2 端口
echo -e "${CYAN}=== 配置 Hysteria2 ===${RESET}"
hysteria_port=$(get_or_generate_port "Hysteria2" "$hysteria_port")
# 添加 Hysteria2
local hysteria_config
hysteria_config=$(jq -n \
--arg port "$hysteria_port" \
--arg password "$password" \
--arg cert_path "${CONFIG_DIR}/cert.pem" \
--arg key_path "${CONFIG_DIR}/private.key" \
'{
type: "hysteria2",
tag: "hysteria-in",
listen: "0.0.0.0",
listen_port: ($port | tonumber),
users: [{password: $password}],
masquerade: "https://bing.com",
tls: {
enabled: true,
alpn: ["h3"],
certificate_path: $cert_path,
key_path: $key_path
}
}')
if [ -z "$hysteria_config" ] || [ "$hysteria_config" == "null" ]; then
echo -e "${RED}错误: Hysteria2 配置生成失败${RESET}"
return 1
fi
new_inbounds=$(jq --argjson item "$hysteria_config" '. += [$item]' <<< "$new_inbounds")
fi
if [[ "$choices" == *"7"* ]] || [[ "$choices" == *"2"* ]]; then
# 获取 Shadowsocks 端口
echo -e "${CYAN}=== 配置 Shadowsocks ===${RESET}"
shadowsocks_port=$(get_or_generate_port "Shadowsocks" "$shadowsocks_port")
# 添加 Shadowsocks
local shadowsocks_config
shadowsocks_config=$(jq -n \
--arg port "$shadowsocks_port" \
--arg ss_password "$ss_password" \
'{
type: "shadowsocks",
tag: "shadowsocks-in",
listen: "0.0.0.0",
listen_port: ($port | tonumber),
method: "2022-blake3-aes-256-gcm",
password: $ss_password,
multiplex: {enabled: true}
}')
if [ -z "$shadowsocks_config" ] || [ "$shadowsocks_config" == "null" ]; then
echo -e "${RED}错误: Shadowsocks 配置生成失败${RESET}"
return 1
fi
new_inbounds=$(jq --argjson item "$shadowsocks_config" '. += [$item]' <<< "$new_inbounds")
fi
if [[ "$choices" == *"7"* ]] || [[ "$choices" == *"3"* ]]; then
# 获取 VLESS 端口
echo -e "${CYAN}=== 配置 VLESS+Vision+Reality ===${RESET}"
vless_port=$(get_or_generate_port "VLESS" "$vless_port")
# 添加 VLESS+Reality
local vless_config
vless_config=$(jq -n \
--arg port "$vless_port" \
--arg uuid "$uuid" \
--arg private_key "$private_key" \
'{
type: "vless",
tag: "vless-in",
listen: "0.0.0.0",
listen_port: ($port | tonumber),
users: [{
uuid: $uuid,
flow: "xtls-rprx-vision"
}],
tls: {
enabled: true,
server_name: "www.tesla.com",
reality: {
enabled: true,
handshake: {
server: "www.tesla.com",
server_port: 443
},
private_key: $private_key,
short_id: ["123abc"]
}
}
}')
if [ -z "$vless_config" ] || [ "$vless_config" == "null" ]; then
echo -e "${RED}错误: VLESS 配置生成失败${RESET}"
return 1
fi
new_inbounds=$(jq --argjson item "$vless_config" '. += [$item]' <<< "$new_inbounds")
fi
if [[ "$choices" == *"7"* ]] || [[ "$choices" == *"4"* ]]; then
# 获取 AnyTLS 端口
echo -e "${CYAN}=== 配置 AnyTLS ===${RESET}"
anytls_port=$(get_or_generate_port "AnyTLS" "$anytls_port")
# 添加 AnyTLS
local anytls_config
anytls_config=$(jq -n \
--arg port "$anytls_port" \
--arg uuid "$uuid" \
--arg public_key "$public_key" \
--arg cert_path "${CONFIG_DIR}/cert.pem" \
--arg key_path "${CONFIG_DIR}/private.key" \
'{
type: "anytls",
tag: "anytls-in",
listen: "0.0.0.0",
listen_port: ($port | tonumber),
users: [{
name: $uuid,
password: $public_key
}],
tls: {
enabled: true,
certificate_path: $cert_path,
key_path: $key_path
}
}')
if [ -z "$anytls_config" ] || [ "$anytls_config" == "null" ]; then
echo -e "${RED}错误: AnyTLS 配置生成失败${RESET}"
return 1
fi
new_inbounds=$(jq --argjson item "$anytls_config" '. += [$item]' <<< "$new_inbounds")
fi
if [[ "$choices" == *"7"* ]] || [[ "$choices" == *"5"* ]]; then
# 获取 SOCKS5 端口
echo -e "${CYAN}=== 配置 SOCKS5 代理 ===${RESET}"
socks_port=$(get_or_generate_port "SOCKS5" "$socks_port")
# 添加 SOCKS5 配置
local socks_config
socks_config=$(jq -n \
--arg port "$socks_port" \
--arg username "$socks_username" \
--arg password "$socks_password" \
'{
type: "socks",
tag: "socks-in",
listen: "0.0.0.0",
listen_port: ($port | tonumber),
users: [{
username: $username,
password: $password
}]
}')
if [ -z "$socks_config" ] || [ "$socks_config" == "null" ]; then
echo -e "${RED}错误: SOCKS5 配置生成失败${RESET}"
return 1
fi
new_inbounds=$(jq --argjson item "$socks_config" '. += [$item]' <<< "$new_inbounds")
fi
if [[ "$choices" == *"7"* ]] || [[ "$choices" == *"6"* ]]; then
# 获取 HTTP 端口
echo -e "${CYAN}=== 配置 HTTP 代理 ===${RESET}"
http_port=$(get_or_generate_port "HTTP" "$http_port")
# 添加 HTTP 配置
local http_config
http_config=$(jq -n \
--arg port "$http_port" \
--arg username "$http_username" \
--arg password "$http_password" \
'{
type: "http",
tag: "http-in",
listen: "0.0.0.0",
listen_port: ($port | tonumber),
users: [{
username: $username,
password: $password
}]
}')
if [ -z "$http_config" ] || [ "$http_config" == "null" ]; then
echo -e "${RED}错误: HTTP 配置生成失败${RESET}"
return 1
fi
new_inbounds=$(jq --argjson item "$http_config" '. += [$item]' <<< "$new_inbounds")
fi
# 检查 inbounds 是否为空
if [ "$new_inbounds" == "[]" ]; then
echo -e "${RED}错误: 未选择任何协议配置!${RESET}"
return 1
fi
# 更新配置文件,添加 inbounds
local new_config
new_config=$(jq --argjson inbounds "$new_inbounds" '.inbounds = $inbounds' <<< "$current_config")
# 验证 jq 操作是否成功
if [ -z "$new_config" ] || [ "$new_config" == "null" ]; then
echo -e "${RED}错误: 配置生成失败,jq 操作返回空值!${RESET}"
return 1
fi
# 验证生成的配置是否为有效的 JSON
if ! echo "$new_config" | jq empty 2>/dev/null; then
echo -e "${RED}错误: 生成的配置不是有效的 JSON 格式!${RESET}"
return 1
fi
# 备份当前配置
cp "${CONFIG_FILE}" "${CONFIG_FILE}.backup" || {
echo -e "${RED}错误: 无法备份配置文件${RESET}"
return 1
}
# 保存新配置
echo "$new_config" > "${CONFIG_FILE}" || {
echo -e "${RED}错误: 无法写入配置文件${RESET}"
mv "${CONFIG_FILE}.backup" "${CONFIG_FILE}"
return 1
}
# 验证写入的文件是否为有效的 JSON
if ! jq empty "${CONFIG_FILE}" 2>/dev/null; then
echo -e "${RED}错误: 写入配置文件失败,正在恢复备份...${RESET}"
mv "${CONFIG_FILE}.backup" "${CONFIG_FILE}"
return 1
fi
# 删除备份文件
rm -f "${CONFIG_FILE}.backup"
echo -e "${GREEN}节点配置已生成并更新到 ${CONFIG_FILE}${RESET}"
# 重启服务
restart_sing_box
}
# 检查 BBR 状态
check_bbr_status() {
# 检查内核模块是否加载
if ! lsmod | grep -q "tcp_bbr"; then
return 1 # BBR 未启用(内核模块未加载)
fi
# 检查系统参数是否设置为 bbr
local congestion_control
if ! congestion_control=$(sysctl -n net.ipv4.tcp_congestion_control 2>/dev/null); then
# 如果读取失败,也认为没有开启 BBR
return 1
fi
if [ "$congestion_control" != "bbr" ]; then
return 1 # BBR 未启用(系统参数未设置)
fi
return 0 # BBR 已启用
}
# 启用 BBR
enable_bbr() {
echo -e "${CYAN}正在启用 TCP BBR...${RESET}"
# 检查内核版本
kernel_version=$(uname -r | cut -d. -f1-2)
required_version="4.9"
# 使用正确的版本比较方法
if ! printf "%s\n%s" "$required_version" "$kernel_version" | sort -V -C 2>/dev/null; then
echo -e "${RED}当前内核版本 $kernel_version 低于要求的 $required_version,无法启用 BBR${RESET}"
return 1
fi
# 加载 tcp_bbr 模块
if ! lsmod | grep -q "tcp_bbr"; then