forked from cmliu/socks5-for-serv00
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathvps_sb5in1.sh
More file actions
1982 lines (1790 loc) · 77.7 KB
/
Copy pathvps_sb5in1.sh
File metadata and controls
1982 lines (1790 loc) · 77.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
#!/bin/bash
# =========================
# 老王sing-box安装脚本 - 青云志精简版
# vless-version-reality|vmess-ws-tls(tunnel)|hysteria2|tuic5
# argo 端口默认为8001,可通过导入外部环境变量 ARGOPORT 修改
# nat小鸡安装后需要将端口修改为nat映射的端口
# 最后更新时间: 2026.5.30[新增Anytls,socks5,ss2022(有封ip风险,建议ipv6使用)等协议]
# 删除订阅相关功能,改为直接输出节点链接到url.txt文件,方便用户复制使用
# =========================
export LANG=en_US.UTF-8
# 定义颜色
re="\033[0m"
red="\033[1;91m"
green="\e[1;32m"
yellow="\e[1;33m"
purple="\e[1;35m"
skyblue="\e[1;36m"
red() { echo -e "\e[1;91m$1\033[0m"; }
green() { echo -e "\e[1;32m$1\033[0m"; }
yellow() { echo -e "\e[1;33m$1\033[0m"; }
purple() { echo -e "\e[1;35m$1\033[0m"; }
skyblue() { echo -e "\e[1;36m$1\033[0m"; }
reading() { read -p "$(red "$1")" "$2"; }
# 定义常量
server_name="sing-box"
work_dir="/etc/sing-box"
conf_dir="${work_dir}/conf"
client_dir="${work_dir}/url.txt"
export vless_port=${PORT:-$(shuf -i 1000-65000 -n 1)}
export CFIP=${CFIP:-'cdns.doon.eu.org'}
export CFPORT=${CFPORT:-'443'}
export ARGOPORT=${ARGOPORT:-'8001'}
# 检查是否为root下运行
[[ $EUID -ne 0 ]] && red "请在root用户下运行脚本" && exit 1
# 检查命令是否存在函数
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# 检查服务状态通用函数
check_service() {
local service_name=$1
local service_file=$2
[[ ! -f "${service_file}" ]] && { red "not installed"; return 2; }
if command_exists apk; then
rc-service "${service_name}" status | grep -q "started" && green "running" || yellow "not running"
else
systemctl is-active "${service_name}" | grep -q "^active$" && green "running" || yellow "not running"
fi
return $?
}
# 检查sing-box状态
check_singbox() {
check_service "sing-box" "${work_dir}/${server_name}"
}
# 检查argo状态
check_argo() {
check_service "argo" "${work_dir}/argo"
}
# 根据系统类型安装、卸载依赖
manage_packages() {
if [ $# -lt 2 ]; then
red "Unspecified package name or action"
return 1
fi
action=$1
shift
# work_dir 不存在说明是首次安装,需要更新系统
if [ "$action" == "install" ] && [ ! -d "$work_dir" ]; then
yellow "正在更新系统软件包...\n"
if command_exists apt; then
DEBIAN_FRONTEND=noninteractive apt update -y && DEBIAN_FRONTEND=noninteractive apt upgrade -y
elif command_exists dnf; then
dnf update -y
elif command_exists yum; then
yum update -y
elif command_exists apk; then
apk update && apk upgrade
else
yellow "Unknown system!\n"
fi
green "finished updated system\n"
fi
for package in "$@"; do
if [ "$action" == "install" ]; then
if command_exists "$package"; then
green "${package} already installed"
continue
fi
yellow "正在安装 ${package}...\n"
if command_exists apt; then
DEBIAN_FRONTEND=noninteractive apt install -y "$package"
elif command_exists dnf; then
dnf install -y "$package"
elif command_exists yum; then
yum install -y "$package"
elif command_exists apk; then
apk add "$package"
else
red "Unknown system!\n"
return 1
fi
elif [ "$action" == "uninstall" ]; then
if ! command_exists "$package"; then
yellow "${package} is not installed"
continue
fi
yellow "正在卸载 ${package}...\n"
if command_exists apt; then
apt remove -y "$package" && apt autoremove -y
elif command_exists dnf; then
dnf remove -y "$package" && dnf autoremove -y
elif command_exists yum; then
yum remove -y "$package" && yum autoremove -y
elif command_exists apk; then
apk del "$package"
else
red "Unknown system!\n"
return 1
fi
else
red "Unknown action: $action"
return 1
fi
done
return 0
}
# 获取ip
get_realip() {
ip=$(curl -4 -sm 2 ip.sb)
ipv6() { curl -6 -sm 2 ip.sb; }
if [ -z "$ip" ]; then
echo "[$(ipv6)]"
else
if curl -4 -sm 2 http://ipinfo.io/org | grep -qE 'Cloudflare|UnReal|AEZA|Andrei'; then
echo "[$(ipv6)]"
else
if grep -qE '^\s*precedence\s+::ffff:0:0/96\s+100' "/etc/gai.conf" 2>/dev/null; then
echo "$ip"
else
v6=$(ipv6)
[ -n "$v6" ] && echo "[$v6]" || echo "$ip"
fi
fi
fi
}
# 处理防火墙
allow_port() {
has_ufw=0
has_firewalld=0
has_iptables=0
has_ip6tables=0
command_exists ufw && has_ufw=1
command_exists firewall-cmd && systemctl is-active firewalld >/dev/null 2>&1 && has_firewalld=1
command_exists iptables && has_iptables=1
command_exists ip6tables && has_ip6tables=1
[ "$has_ufw" -eq 1 ] && ufw --force default allow outgoing >/dev/null 2>&1
[ "$has_firewalld" -eq 1 ] && firewall-cmd --permanent --zone=public --set-target=ACCEPT >/dev/null 2>&1
[ "$has_iptables" -eq 1 ] && {
iptables -C INPUT -i lo -j ACCEPT 2>/dev/null || iptables -I INPUT 3 -i lo -j ACCEPT
iptables -C INPUT -p icmp -j ACCEPT 2>/dev/null || iptables -I INPUT 4 -p icmp -j ACCEPT
iptables -P FORWARD DROP 2>/dev/null || true
iptables -P OUTPUT ACCEPT 2>/dev/null || true
}
[ "$has_ip6tables" -eq 1 ] && {
ip6tables -C INPUT -i lo -j ACCEPT 2>/dev/null || ip6tables -I INPUT 3 -i lo -j ACCEPT
ip6tables -C INPUT -p icmp -j ACCEPT 2>/dev/null || ip6tables -I INPUT 4 -p icmp -j ACCEPT
ip6tables -P FORWARD DROP 2>/dev/null || true
ip6tables -P OUTPUT ACCEPT 2>/dev/null || true
}
for rule in "$@"; do
port=${rule%/*}
proto=${rule#*/}
[ "$has_ufw" -eq 1 ] && ufw allow in ${port}/${proto} >/dev/null 2>&1
[ "$has_firewalld" -eq 1 ] && firewall-cmd --permanent --add-port=${port}/${proto} >/dev/null 2>&1
[ "$has_iptables" -eq 1 ] && (iptables -C INPUT -p ${proto} --dport ${port} -j ACCEPT 2>/dev/null || iptables -I INPUT 4 -p ${proto} --dport ${port} -j ACCEPT)
[ "$has_ip6tables" -eq 1 ] && (ip6tables -C INPUT -p ${proto} --dport ${port} -j ACCEPT 2>/dev/null || ip6tables -I INPUT 4 -p ${proto} --dport ${port} -j ACCEPT)
done
[ "$has_firewalld" -eq 1 ] && firewall-cmd --reload >/dev/null 2>&1
if command_exists rc-service 2>/dev/null; then
[ "$has_iptables" -eq 1 ] && iptables-save > /etc/iptables/rules.v4 2>/dev/null
[ "$has_ip6tables" -eq 1 ] && ip6tables-save > /etc/iptables/rules.v6 2>/dev/null
else
if ! command_exists netfilter-persistent; then
manage_packages install iptables-persistent || yellow "请手动安装netfilter-persistent或保存iptables规则"
netfilter-persistent save >/dev/null 2>&1
elif command_exists service; then
service iptables save 2>/dev/null
service ip6tables save 2>/dev/null
fi
fi
}
# 下载并安装 sing-box,cloudflared
install_singbox() {
clear
purple "正在安装sing-box中,请稍后...\n"
ARCH_RAW=$(uname -m)
case "${ARCH_RAW}" in
'x86_64' | 'amd64') ARCH='amd64' ;;
'x86' | 'i686' | 'i386') ARCH='386' ;;
'aarch64' | 'arm64') ARCH='arm64' ;;
'armv7l') ARCH='armv7' ;;
's390x') ARCH='s390x' ;;
*) red "不支持的架构: ${ARCH_RAW}"; exit 1 ;;
esac
[ ! -d "${work_dir}" ] && mkdir -p "${work_dir}" && chmod 777 "${work_dir}" && mkdir -p "${conf_dir}"
curl -sLo "${work_dir}/argo" "https://$ARCH.ssss.nyc.mn/bot"
curl -sLo "${work_dir}/sing-box" "https://$ARCH.ssss.nyc.mn/sb"
chown root:root ${work_dir} && chmod +x ${work_dir}/${server_name} ${work_dir}/argo
tuic_port=$(($vless_port + 2))
hy2_port=$(($vless_port + 3))
uuid=$(cat /proc/sys/kernel/random/uuid)
password=$(< /dev/urandom tr -dc 'A-Za-z0-9' | head -c 24)
output=$(/etc/sing-box/sing-box generate reality-keypair)
private_key=$(echo "${output}" | awk '/PrivateKey:/ {print $2}')
public_key=$(echo "${output}" | awk '/PublicKey:/ {print $2}')
allow_port $vless_port/tcp $tuic_port/udp $hy2_port/udp > /dev/null 2>&1
openssl ecparam -genkey -name prime256v1 -out "${work_dir}/private.key"
openssl req -new -x509 -days 3650 -key "${work_dir}/private.key" -out "${work_dir}/cert.pem" -subj "/CN=bing.com"
dns_strategy=$(ping -c 1 -W 3 8.8.8.8 >/dev/null 2>&1 && echo "prefer_ipv4" || \
(ping -c 1 -W 3 2001:4860:4860::8888 >/dev/null 2>&1 && echo "prefer_ipv6" || echo "prefer_ipv4"))
cat > "${conf_dir}/log.json" << EOF
{
"log": {
"disabled": false,
"level": "error",
"output": "$work_dir/sb.log",
"timestamp": true
}
}
EOF
cat > "${conf_dir}/dns.json" << EOF
{
"dns": {
"servers": [
{
"tag": "local",
"address": "local",
"strategy": "$dns_strategy"
}
]
}
}
EOF
cat > "${conf_dir}/inbounds.json" << EOF
{
"inbounds": [
{
"type": "vless",
"tag": "vless-reality",
"listen": "::",
"listen_port": $vless_port,
"users": [
{
"uuid": "$uuid",
"flow": "xtls-rprx-vision"
}
],
"tls": {
"enabled": true,
"server_name": "www.iij.ad.jp",
"reality": {
"enabled": true,
"handshake": {
"server": "www.iij.ad.jp",
"server_port": 443
},
"private_key": "$private_key",
"short_id": [""]
}
}
},
{
"type": "vmess",
"tag": "vmess-ws",
"listen": "::",
"listen_port": ${ARGOPORT},
"users": [
{
"uuid": "$uuid"
}
],
"transport": {
"type": "ws",
"path": "/vmess-argo",
"early_data_header_name": "Sec-WebSocket-Protocol"
}
},
{
"type": "hysteria2",
"tag": "hysteria2",
"listen": "::",
"listen_port": $hy2_port,
"users": [
{
"password": "$uuid"
}
],
"ignore_client_bandwidth": false,
"masquerade": "https://bing.com",
"tls": {
"enabled": true,
"alpn": ["h3"],
"min_version": "1.3",
"max_version": "1.3",
"certificate_path": "$work_dir/cert.pem",
"key_path": "$work_dir/private.key"
}
},
{
"type": "tuic",
"tag": "tuic",
"listen": "::",
"listen_port": $tuic_port,
"users": [
{
"uuid": "$uuid",
"password": "$uuid"
}
],
"congestion_control": "bbr",
"tls": {
"enabled": true,
"alpn": ["h3"],
"certificate_path": "$work_dir/cert.pem",
"key_path": "$work_dir/private.key"
}
}
]
}
EOF
cat > "${conf_dir}/outbounds.json" << EOF
{
"outbounds": [
{
"type": "direct",
"tag": "direct"
}
]
}
EOF
cat > "${conf_dir}/endpoints.json" << EOF
{
"endpoints": [
{
"type": "wireguard",
"tag": "wireguard-out",
"mtu": 1280,
"address": [
"172.16.0.2/32",
"2606:4700:110:8dfe:d141:69bb:6b80:925/128"
],
"private_key": "YFYOAdbw1bKTHlNNi+aEjBM3BO7unuFC5rOkMRAz9XY=",
"peers": [
{
"address": "engage.cloudflareclient.com",
"port": 2408,
"public_key": "bmXOC+F1FxEMF9dyiK2H5/1SUtzH0JuVo51h2wPfgyo=",
"allowed_ips": ["0.0.0.0/0", "::/0"],
"reserved": [78, 135, 76]
}
]
}
]
}
EOF
cat > "${conf_dir}/route.json" << EOF
{
"route": {
"rule_set": [
{"tag":"gemini","type":"remote","format":"binary","url":"https://main.ssss.nyc.mn/gemini.srs","download_detour":"direct"},
{"tag":"claude","type":"remote","format":"binary","url":"https://main.ssss.nyc.mn/claude.srs","download_detour":"direct"},
{"tag":"openai","type":"remote","format":"binary","url":"https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/sing/geo-lite/geosite/openai.srs","download_detour":"direct"},
{"tag":"tiktok","type":"remote","format":"binary","url":"https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/sing/geo-lite/geosite/tiktok.srs","download_detour":"direct"},
{"tag":"twitter","type":"remote","format":"binary","url":"https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/sing/geo-lite/geosite/twitter.srs","download_detour":"direct"},
{"tag":"google","type":"remote","format":"binary","url":"https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/sing/geo-lite/geosite/google.srs","download_detour":"direct"},
{"tag":"telegram","type":"remote","format":"binary","url":"https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/sing/geo-lite/geosite/telegram.srs","download_detour":"direct"},
{"tag":"youtube","type":"remote","format":"binary","url":"https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/sing/geo-lite/geosite/youtube.srs","download_detour":"direct"},
{"tag":"netflix","type":"remote","format":"binary","url":"https://raw.githubusercontent.com/MetaCubeX/meta-rules-dat/sing/geo-lite/geosite/netflix.srs","download_detour":"direct"}
],
"rules": [{"rule_set": []}],
"final": "direct"
}
}
EOF
}
# debian/ubuntu/centos 守护进程
main_systemd_services() {
cat > /etc/systemd/system/sing-box.service << EOF
[Unit]
Description=sing-box service
Documentation=https://sing-box.sagernet.org
After=network.target nss-lookup.target
[Service]
User=root
WorkingDirectory=/etc/sing-box
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
ExecStart=/etc/sing-box/sing-box run -C /etc/sing-box/conf
ExecReload=/bin/kill -HUP \$MAINPID
Restart=on-failure
RestartSec=10
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
EOF
cat > /etc/systemd/system/argo.service << EOF
[Unit]
Description=Cloudflare Tunnel
After=network.target
[Service]
Type=simple
NoNewPrivileges=yes
TimeoutStartSec=0
ExecStart=/bin/sh -c "/etc/sing-box/argo tunnel --url http://localhost:${ARGOPORT} --no-autoupdate --edge-ip-version auto --protocol http2 > /etc/sing-box/argo.log 2>&1"
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
EOF
if [ -f /etc/centos-release ]; then
yum install -y chrony
systemctl start chronyd
systemctl enable chronyd
chronyc -a makestep
yum update -y ca-certificates
bash -c 'echo "0 0" > /proc/sys/net/ipv4/ping_group_range'
fi
systemctl daemon-reload
systemctl enable sing-box
systemctl start sing-box
systemctl enable argo
systemctl start argo
}
# 适配alpine 守护进程
alpine_openrc_services() {
cat > /etc/init.d/sing-box << 'EOF'
#!/sbin/openrc-run
description="sing-box service"
command="/etc/sing-box/sing-box"
command_args="run -C /etc/sing-box/conf"
command_background=true
pidfile="/var/run/sing-box.pid"
EOF
cat > /etc/init.d/argo << EOF
#!/sbin/openrc-run
description="Cloudflare Tunnel"
command="/bin/sh"
command_args="-c '/etc/sing-box/argo tunnel --url http://localhost:${ARGOPORT} --no-autoupdate --edge-ip-version auto --protocol http2 > /etc/sing-box/argo.log 2>&1'"
command_background=true
pidfile="/var/run/argo.pid"
EOF
chmod +x /etc/init.d/sing-box
chmod +x /etc/init.d/argo
rc-update add sing-box default > /dev/null 2>&1
rc-update add argo default > /dev/null 2>&1
}
# 从已安装配置中获取UUID
get_current_uuid() {
local inbounds_file="${conf_dir}/inbounds.json"
if [ -f "$inbounds_file" ]; then
local uuid
uuid=$(jq -r '.inbounds[] | select(.type == "vless") | .users[0].uuid // empty' "$inbounds_file" 2>/dev/null | head -1)
[ -z "$uuid" ] && uuid=$(jq -r '.inbounds[] | select(.type == "vmess") | .users[0].uuid // empty' "$inbounds_file" 2>/dev/null | head -1)
[ -z "$uuid" ] && uuid=$(jq -r '.inbounds[] | select(.type == "hysteria2") | .users[0].password // empty' "$inbounds_file" 2>/dev/null | head -1)
echo "$uuid"
fi
}
# 生成节点链接
get_info() {
yellow "\nip检测中,请稍等...\n"
server_ip=$(get_realip)
clear
isp=$(curl -sm 3 -H "User-Agent: Mozilla/5.0" "https://api.ip.sb/geoip" | tr -d '\n' | \
awk -F\" '{c="";i="";for(x=1;x<=NF;x++){if($x=="country_code")c=$(x+2);if($x=="isp")i=$(x+2)};if(c&&i)print c"-"i}' | \
sed 's/ /_/g' || \
curl -sm 3 -H "User-Agent: Mozilla/5.0" "https://ipapi.co/json" | tr -d '\n' | \
awk -F\" '{c="";o="";for(x=1;x<=NF;x++){if($x=="country_code")c=$(x+2);if($x=="org")o=$(x+2)};if(c&&o)print c"-"o}' | \
sed 's/ /_/g' || echo "$hostname")
if [ -f "${work_dir}/argo.log" ]; then
for i in {1..5}; do
purple "第 $i 次尝试获取ArgoDomain中..."
argodomain=$(sed -n 's|.*https://\([^/]*trycloudflare\.com\).*|\1|p' "${work_dir}/argo.log")
[ -n "$argodomain" ] && break
sleep 2
done
else
restart_argo
sleep 6
argodomain=$(sed -n 's|.*https://\([^/]*trycloudflare\.com\).*|\1|p' "${work_dir}/argo.log")
fi
green "\nArgoDomain:${purple}$argodomain${re}\n"
VMESS="{ \"v\": \"2\", \"ps\": \"${isp}\", \"add\": \"${CFIP}\", \"port\": \"${CFPORT}\", \"id\": \"${uuid}\", \"aid\": \"0\", \"scy\": \"none\", \"net\": \"ws\", \"type\": \"none\", \"host\": \"${argodomain}\", \"path\": \"/vmess-argo?ed=2560\", \"tls\": \"tls\", \"sni\": \"${argodomain}\", \"alpn\": \"\", \"fp\": \"firefox\", \"allowInsecure\": \"false\"}"
cat > ${work_dir}/url.txt << EOF
vless://${uuid}@${server_ip}:${vless_port}?encryption=none&flow=xtls-rprx-vision&security=reality&sni=www.iij.ad.jp&fp=firefox&pbk=${public_key}&type=tcp&headerType=none#${isp}
vmess://$(echo "$VMESS" | base64 -w0)
hysteria2://${uuid}@${server_ip}:${hy2_port}/?sni=www.bing.com&insecure=1&alpn=h3&obfs=none#${isp}
tuic://${uuid}:${uuid}@${server_ip}:${tuic_port}?sni=www.bing.com&congestion_control=bbr&udp_relay_mode=native&alpn=h3&allow_insecure=1#${isp}
EOF
echo ""
while IFS= read -r line; do echo -e "${purple}$line"; done < ${work_dir}/url.txt
chmod 644 ${work_dir}/url.txt
yellow "\n温馨提醒:需打开V2rayN或其他软件里的 "跳过证书验证",或将节点的Insecure或TLS里设置为"true"\n"
}
# 通用服务管理函数
manage_service() {
local service_name="$1"
local action="$2"
if [ -z "$service_name" ] || [ -z "$action" ]; then
red "缺少服务名或操作参数\n"; return 1
fi
local status=$(check_service "$service_name" 2>/dev/null)
case "$action" in
"start")
[ "$status" == "running" ] && { yellow "${service_name} 正在运行\n"; return 0; }
[ "$status" == "not installed" ] && { yellow "${service_name} 尚未安装!\n"; return 1; }
yellow "正在启动 ${service_name} 服务\n"
if command_exists rc-service; then rc-service "$service_name" start
elif command_exists systemctl; then systemctl daemon-reload && systemctl start "$service_name"; fi
[ $? -eq 0 ] && green "${service_name} 服务已成功启动\n" || red "${service_name} 服务启动失败\n"
;;
"stop")
[ "$status" == "not installed" ] && { yellow "${service_name} 尚未安装!\n"; return 2; }
[ "$status" == "not running" ] && { yellow "${service_name} 未运行\n"; return 1; }
yellow "正在停止 ${service_name} 服务\n"
if command_exists rc-service; then rc-service "$service_name" stop
elif command_exists systemctl; then systemctl stop "$service_name"; fi
[ $? -eq 0 ] && green "${service_name} 服务已成功停止\n" || red "${service_name} 服务停止失败\n"
;;
"restart")
[ "$status" == "not installed" ] && { yellow "${service_name} 尚未安装!\n"; return 1; }
yellow "正在重启 ${service_name} 服务\n"
if command_exists rc-service; then rc-service "$service_name" restart
elif command_exists systemctl; then systemctl daemon-reload && systemctl restart "$service_name"; fi
[ $? -eq 0 ] && green "${service_name} 服务已成功重启\n" || red "${service_name} 服务重启失败\n"
;;
*)
red "无效的操作: $action\n"; return 1 ;;
esac
}
start_singbox() { manage_service "sing-box" "start"; }
stop_singbox() { manage_service "sing-box" "stop"; }
restart_singbox(){ manage_service "sing-box" "restart"; }
start_argo() { manage_service "argo" "start"; }
stop_argo() { manage_service "argo" "stop"; }
restart_argo() { manage_service "argo" "restart"; }
# 卸载 sing-box(交互式)
uninstall_singbox() {
reading "确定要卸载 sing-box 吗? (y/n): " choice
case "${choice}" in
y|Y)
yellow "正在卸载 sing-box"
if command_exists rc-service; then
rc-service sing-box stop; rc-service argo stop
rm -f /etc/init.d/sing-box /etc/init.d/argo
rc-update del sing-box default; rc-update del argo default
else
systemctl stop "${server_name}"; systemctl stop argo
systemctl disable "${server_name}"; systemctl disable argo
systemctl daemon-reload || true
fi
rm -rf "${work_dir}" || true
rm -f /etc/systemd/system/sing-box.service /etc/systemd/system/argo.service
green "\nsing-box 卸载成功\n\n" && exit 0
;;
*) purple "已取消卸载操作\n\n" ;;
esac
}
# 创建快捷指令
create_shortcut() {
cat > "$work_dir/sb.sh" << 'EOF'
#!/usr/bin/env bash
bash <(curl -Ls https://raw.githubusercontent.com/yutian81/Keepalive/main/vps_sb5in1.sh) $1
EOF
chmod +x "$work_dir/sb.sh"
ln -sf "$work_dir/sb.sh" /usr/bin/sb
[ -s /usr/bin/sb ] && green "\n快捷指令 sb 创建成功\n" || red "\n快捷指令创建失败\n"
}
# 适配alpine
change_hosts() {
sh -c 'echo "0 0" > /proc/sys/net/ipv4/ping_group_range'
sed -i '1s/.*/127.0.0.1 localhost/' /etc/hosts
sed -i '2s/.*/::1 localhost/' /etc/hosts
}
# 非交互静默安装(-i 参数)
auto_install() {
check_singbox &>/dev/null
if [ $? -eq 0 ]; then
yellow "sing-box 已经安装,跳过安装流程。"
exit 0
fi
green "开始无交互式安装 sing-box..."
manage_packages install jq tar openssl lsof coreutils
install_singbox
if command_exists systemctl; then
main_systemd_services
elif command_exists rc-update; then
alpine_openrc_services
change_hosts
rc-service sing-box restart
rc-service argo restart
else
red "不支持的 init 系统,安装中止。"
exit 1
fi
sleep 5
get_info
create_shortcut
green "\nsing-box 安装完成\n"
}
# 无交互静默卸载(-u 参数)
auto_uninstall() {
green "开始无交互式卸载sing-box..."
if command_exists rc-service; then
rc-service sing-box stop > /dev/null 2>&1
rc-service argo stop > /dev/null 2>&1
rc-update del sing-box default > /dev/null 2>&1
rc-update del argo default > /dev/null 2>&1
rm -f /etc/init.d/sing-box /etc/init.d/argo
elif command_exists systemctl; then
systemctl stop sing-box > /dev/null 2>&1
systemctl stop argo > /dev/null 2>&1
systemctl disable sing-box > /dev/null 2>&1
systemctl disable argo > /dev/null 2>&1
systemctl daemon-reload > /dev/null 2>&1
rm -f /etc/systemd/system/sing-box.service \
/etc/systemd/system/argo.service
fi
rm -rf "${work_dir}"
rm -f /usr/bin/sb
green "\nsing-box 已完全卸载!\n"
}
# 变更配置
change_config() {
local singbox_status=$(check_singbox 2>/dev/null)
local singbox_installed=$?
if [ $singbox_installed -eq 2 ]; then
yellow "sing-box 尚未安装!"; sleep 1; menu; return
fi
clear; echo ""
green "=== 修改节点配置 ===\n"
green "sing-box当前状态: $singbox_status\n"
green "1. 修改端口"
skyblue "------------"
green "2. 修改UUID"
skyblue "------------"
green "3. 修改Reality伪装域名"
skyblue "------------"
green "4. 添加hysteria2端口跳跃"
skyblue "------------"
green "5. 删除hysteria2端口跳跃"
skyblue "------------"
green "6. 修改vmess-argo优选域名"
skyblue "------------"
purple "0. 返回主菜单"
skyblue "------------"
reading "请输入选择: " choice
case "${choice}" in
1)
echo ""
green "1. 修改vless-reality端口"
skyblue "------------"
green "2. 修改hysteria2端口"
skyblue "------------"
green "3. 修改tuic端口"
skyblue "------------"
green "4. 修改vmess-argo端口"
skyblue "------------"
purple "0. 返回上一级菜单"
skyblue "------------"
reading "请输入选择: " choice
local inbounds_file="${conf_dir}/inbounds.json"
case "${choice}" in
1)
reading "\n请输入vless-reality端口 (回车跳过将使用随机端口): " new_port
[ -z "$new_port" ] && new_port=$(shuf -i 2000-65000 -n 1)
jq --arg port "$new_port" \
'(.inbounds[] | select(.type == "vless").listen_port) = ($port | tonumber)' \
"$inbounds_file" > "${inbounds_file}.tmp" && mv "${inbounds_file}.tmp" "$inbounds_file"
restart_singbox
allow_port $new_port/tcp > /dev/null 2>&1
sed -i 's/\(vless:\/\/[^@]*@[^:]*:\)[0-9]\{1,\}/\1'"$new_port"'/' $client_dir
while IFS= read -r line; do yellow "$line"; done < ${work_dir}/url.txt
green "\nvless-reality端口已修改成:${purple}$new_port${re}\n"
;;
2)
reading "\n请输入hysteria2端口 (回车跳过将使用随机端口): " new_port
[ -z "$new_port" ] && new_port=$(shuf -i 2000-65000 -n 1)
jq --arg port "$new_port" \
'(.inbounds[] | select(.type == "hysteria2").listen_port) = ($port | tonumber)' \
"$inbounds_file" > "${inbounds_file}.tmp" && mv "${inbounds_file}.tmp" "$inbounds_file"
restart_singbox
allow_port $new_port/udp > /dev/null 2>&1
sed -i 's/\(hysteria2:\/\/[^@]*@[^:]*:\)[0-9]\{1,\}/\1'"$new_port"'/' $client_dir
while IFS= read -r line; do yellow "$line"; done < ${work_dir}/url.txt
green "\nhysteria2端口已修改为:${purple}${new_port}${re}\n"
;;
3)
reading "\n请输入tuic端口 (回车跳过将使用随机端口): " new_port
[ -z "$new_port" ] && new_port=$(shuf -i 2000-65000 -n 1)
jq --arg port "$new_port" \
'(.inbounds[] | select(.type == "tuic").listen_port) = ($port | tonumber)' \
"$inbounds_file" > "${inbounds_file}.tmp" && mv "${inbounds_file}.tmp" "$inbounds_file"
restart_singbox
allow_port $new_port/udp > /dev/null 2>&1
sed -i 's/\(tuic:\/\/[^@]*@[^:]*:\)[0-9]\{1,\}/\1'"$new_port"'/' $client_dir
while IFS= read -r line; do yellow "$line"; done < ${work_dir}/url.txt
green "\ntuic端口已修改为:${purple}${new_port}${re}\n"
;;
4)
reading "\n请输入vmess-argo端口 (回车跳过将使用随机端口): " new_port
[ -z "$new_port" ] && new_port=$(shuf -i 2000-65000 -n 1)
jq --arg port "$new_port" \
'(.inbounds[] | select(.type == "vmess").listen_port) = ($port | tonumber)' \
"$inbounds_file" > "${inbounds_file}.tmp" && mv "${inbounds_file}.tmp" "$inbounds_file"
allow_port $new_port/tcp > /dev/null 2>&1
if command_exists rc-service; then
grep -q "localhost:" /etc/init.d/argo && \
sed -i 's/localhost:[0-9]\{1,\}/localhost:'"$new_port"'/' /etc/init.d/argo && \
get_quick_tunnel && change_argo_domain
else
grep -q "localhost:" /etc/systemd/system/argo.service && \
sed -i 's/localhost:[0-9]\{1,\}/localhost:'"$new_port"'/' /etc/systemd/system/argo.service && \
get_quick_tunnel && change_argo_domain
fi
restart_singbox
green "\nvmess-argo端口已修改为:${purple}${new_port}${re}\n"
;;
0) change_config ;;
*) red "无效的选项,请输入 1 到 4" ;;
esac
;;
2)
reading "\n请输入新的UUID(直接回车随机生成UUID): " new_uuid
[ -z "$new_uuid" ] && new_uuid=$(cat /proc/sys/kernel/random/uuid)
jq --arg uuid "$new_uuid" \
'(.inbounds[] | select(.users != null) | .users[] | select(.uuid != null).uuid) = $uuid |
(.inbounds[] | select(.users != null) | .users[] | select(.password != null).password) = $uuid' \
"${conf_dir}/inbounds.json" > "${conf_dir}/inbounds.json.tmp" && mv "${conf_dir}/inbounds.json.tmp" "${conf_dir}/inbounds.json"
restart_singbox
sed -i -E 's/(vless:\/\/|hysteria2:\/\/)[^@]*(@.*)/\1'"$new_uuid"'\2/' $client_dir
sed -i -E "s#tuic://[0-9a-f-]{36}:[0-9a-f-]{36}@#tuic://$new_uuid:$new_uuid@#g" $client_dir
isp=$(curl -sm 3 -H "User-Agent: Mozilla/5.0" "https://api.ip.sb/geoip" | tr -d '\n' | \
awk -F\" '{c="";i="";for(x=1;x<=NF;x++){if($x=="country_code")c=$(x+2);if($x=="isp")i=$(x+2)};if(c&&i)print c"-"i}' | sed 's/ /_/g' || echo "$hostname")
argodomain=$(grep -oE 'https://[[:alnum:]+\.-]+\.trycloudflare\.com' "${work_dir}/argo.log" | sed 's@https://@@')
VMESS="{ \"v\": \"2\", \"ps\": \"${isp}\", \"add\": \"${CFIP}\", \"port\": \"443\", \"id\": \"${new_uuid}\", \"aid\": \"0\", \"scy\": \"none\", \"net\": \"ws\", \"type\": \"none\", \"host\": \"${argodomain}\", \"path\": \"/vmess-argo?ed=2560\", \"tls\": \"tls\", \"sni\": \"${argodomain}\", \"alpn\": \"\", \"fp\": \"\", \"allowInsecure\": \"false\"}"
encoded_vmess=$(echo "$VMESS" | base64 -w0)
sed -i -E '/vmess:\/\//{s@vmess://.*@vmess://'"$encoded_vmess"'@}' $client_dir
while IFS= read -r line; do yellow "$line"; done < ${work_dir}/url.txt
green "\nUUID已修改为:${purple}${new_uuid}${re}\n"
;;
3)
clear
green "\n1. www.joom.com\n\n2. www.stengg.com\n\n3. www.wedgehr.com\n\n4. www.cerebrium.ai\n\n5. www.nazhumi.com\n"
reading "\n请输入新的Reality伪装域名(可自定义输入,回车留空将使用默认1): " new_sni
case "$new_sni" in
""|"1") new_sni="www.joom.com" ;;
"2") new_sni="www.stengg.com" ;;
"3") new_sni="www.wedgehr.com" ;;
"4") new_sni="www.cerebrium.ai" ;;
"5") new_sni="www.nazhumi.com" ;;
esac
jq --arg sni "$new_sni" \
'(.inbounds[] | select(.type == "vless") | .tls.server_name) = $sni |
(.inbounds[] | select(.type == "vless") | .tls.reality.handshake.server) = $sni' \
"${conf_dir}/inbounds.json" > "${conf_dir}/inbounds.json.tmp" && mv "${conf_dir}/inbounds.json.tmp" "${conf_dir}/inbounds.json"
restart_singbox
sed -i "s/\(vless:\/\/[^\?]*\?\([^\&]*\&\)*sni=\)[^&]*/\1$new_sni/" $client_dir
while IFS= read -r line; do yellow "$line"; done < ${work_dir}/url.txt
green "\nReality sni已修改为:${purple}${new_sni}${re}\n"
;;
4)
purple "端口跳跃需确保跳跃区间的端口没有被占用\n"
reading "请输入跳跃起始端口 (回车跳过将使用随机端口): " min_port
[ -z "$min_port" ] && min_port=$(shuf -i 50000-65000 -n 1)
yellow "你的起始端口为:$min_port"
reading "\n请输入跳跃结束端口 (需大于起始端口): " max_port
[ -z "$max_port" ] && max_port=$(($min_port + 100))
yellow "你的结束端口为:$max_port\n"
listen_port=$(jq -r '.inbounds[] | select(.type == "hysteria2").listen_port' "${conf_dir}/inbounds.json")
iptables -t nat -A PREROUTING -p udp --dport $min_port:$max_port -j DNAT --to-destination :$listen_port > /dev/null
command -v ip6tables &> /dev/null && ip6tables -t nat -A PREROUTING -p udp --dport $min_port:$max_port -j DNAT --to-destination :$listen_port > /dev/null
if command_exists rc-service 2>/dev/null; then
iptables-save > /etc/iptables/rules.v4
command -v ip6tables &> /dev/null && ip6tables-save > /etc/iptables/rules.v6
cat << 'IEOF' > /etc/init.d/iptables
#!/sbin/openrc-run
depend() { need net; }
start() {
[ -f /etc/iptables/rules.v4 ] && iptables-restore < /etc/iptables/rules.v4
command -v ip6tables &> /dev/null && [ -f /etc/iptables/rules.v6 ] && ip6tables-restore < /etc/iptables/rules.v6
}
IEOF
chmod +x /etc/init.d/iptables && rc-update add iptables default && /etc/init.d/iptables start
elif [ -f /etc/debian_version ]; then
DEBIAN_FRONTEND=noninteractive apt install -y iptables-persistent > /dev/null 2>&1 && netfilter-persistent save > /dev/null 2>&1
systemctl enable netfilter-persistent > /dev/null 2>&1 && systemctl start netfilter-persistent > /dev/null 2>&1
elif [ -f /etc/redhat-release ]; then
manage_packages install iptables-services > /dev/null 2>&1 && service iptables save > /dev/null 2>&1
systemctl enable iptables > /dev/null 2>&1 && systemctl start iptables > /dev/null 2>&1
command -v ip6tables &> /dev/null && service ip6tables save > /dev/null 2>&1
systemctl enable ip6tables > /dev/null 2>&1 && systemctl start ip6tables > /dev/null 2>&1
fi
restart_singbox
ip=$(get_realip)
uuid=$(sed -n 's/.*hysteria2:\/\/\([^@]*\)@.*/\1/p' $client_dir)
line_number=$(grep -n 'hysteria2://' $client_dir | cut -d':' -f1)
isp=$(curl -sm 3 -H "User-Agent: Mozilla/5.0" "https://api.ip.sb/geoip" | tr -d '\n' | \
awk -F\" '{c="";i="";for(x=1;x<=NF;x++){if($x=="country_code")c=$(x+2);if($x=="isp")i=$(x+2)};if(c&&i)print c"-"i}' | sed 's/ /_/g' || echo "$hostname")
sed -i.bak "/hysteria2:/d" $client_dir
sed -i "${line_number}i hysteria2://$uuid@$ip:$listen_port?peer=www.bing.com&insecure=1&alpn=h3&obfs=none&mport=$listen_port,$min_port-$max_port#$isp" $client_dir
while IFS= read -r line; do yellow "$line"; done < ${work_dir}/url.txt
green "\nhysteria2端口跳跃已开启:${purple}$min_port-$max_port${re}\n"
;;
5)
iptables -t nat -F PREROUTING > /dev/null 2>&1
command -v ip6tables &> /dev/null && ip6tables -t nat -F PREROUTING > /dev/null 2>&1
if command_exists rc-service 2>/dev/null; then
rc-update del iptables default && rm -rf /etc/init.d/iptables
elif [ -f /etc/debian_version ]; then
netfilter-persistent save > /dev/null 2>&1
elif [ -f /etc/redhat-release ]; then
service iptables save > /dev/null 2>&1
command -v ip6tables &> /dev/null && service ip6tables save > /dev/null 2>&1
fi
sed -i '/hysteria2/s/&mport=[^#&]*//g' /etc/sing-box/url.txt
green "\n端口跳跃已删除\n"
;;
6) change_cfip ;;
0) menu ;;
*) red "无效的选项!\n" ;;
esac
}
manage_singbox() {
local singbox_status=$(check_singbox 2>/dev/null)
clear; echo ""
green "=== sing-box 管理 ===\n"
green "sing-box当前状态: $singbox_status\n"
green "1. 启动sing-box服务"
skyblue "-------------------"
green "2. 停止sing-box服务"
skyblue "-------------------"
green "3. 重启sing-box服务"
skyblue "-------------------"
purple "0. 返回主菜单"
skyblue "------------"
reading "\n请输入选择: " choice
case "${choice}" in
1) start_singbox ;;
2) stop_singbox ;;
3) restart_singbox ;;
0) menu ;;
*) red "无效的选项!\n" && sleep 1 && manage_singbox ;;
esac
read -n 1 -s -r -p $'\n\033[1;91m按任意键返回...\033[0m\n'
}
# Argo 管理
manage_argo() {
local argo_status=$(check_argo 2>/dev/null)
clear; echo ""
green "=== Argo 隧道管理 ===\n"
green "Argo当前状态: $argo_status\n"
green "1. 启动Argo服务"
skyblue "------------"
green "2. 停止Argo服务"
skyblue "------------"
green "3. 重启Argo服务"
skyblue "------------"
green "4. 添加Argo固定隧道"
skyblue "----------------"
green "5. 切换回Argo临时隧道"
skyblue "------------------"
green "6. 重新获取Argo临时域名"
skyblue "-------------------"
purple "0. 返回主菜单"
skyblue "-----------"
reading "\n请输入选择: " choice
case "${choice}" in
1) start_argo ;;
2) stop_argo ;;
3)
clear
if command_exists rc-service 2>/dev/null; then
grep -Fq -- '--url http://localhost' /etc/init.d/argo && get_quick_tunnel && change_argo_domain || \
{ green "\n当前使用固定隧道,无需获取临时域名"; sleep 2; menu; }
else
grep -q 'ExecStart=.*--url http://localhost' /etc/systemd/system/argo.service && get_quick_tunnel && change_argo_domain || \
{ green "\n当前使用固定隧道,无需获取临时域名"; sleep 2; menu; }
fi
;;
4)
clear
yellow "\n固定隧道可为json或token,固定隧道端口为${ARGOPORT}, 使用token请在cloudflare里设置一致\njson获取地址:${purple}https://fscarmen.cloudflare.now.cc${re}\n"
reading "\n请输入你的argo域名: " argo_domain
ArgoDomain=$argo_domain
reading "\n请输入你的argo密钥(token或json): " argo_auth
if [[ $argo_auth =~ TunnelSecret ]]; then
echo $argo_auth > ${work_dir}/tunnel.json
cat > ${work_dir}/tunnel.yml << EOF
tunnel: $(cut -d\" -f12 <<< "$argo_auth")
credentials-file: ${work_dir}/tunnel.json
protocol: http2
ingress:
- hostname: $ArgoDomain
service: http://localhost:${ARGOPORT}
originRequest:
noTLSVerify: true
- service: http_status:404
EOF
if command_exists rc-service 2>/dev/null; then
sed -i '/^command_args=/c\command_args="-c '\''/etc/sing-box/argo tunnel --edge-ip-version auto --config /etc/sing-box/tunnel.yml run 2>&1'\''"' /etc/init.d/argo
else
sed -i '/^ExecStart=/c ExecStart=/bin/sh -c "/etc/sing-box/argo tunnel --edge-ip-version auto --config /etc/sing-box/tunnel.yml run 2>&1"' /etc/systemd/system/argo.service
fi
restart_argo; sleep 1; change_argo_domain
elif [[ $argo_auth =~ ^[A-Z0-9a-z=]{120,250}$ ]]; then
if command_exists rc-service 2>/dev/null; then
sed -i "/^command_args=/c\command_args=\"-c '/etc/sing-box/argo tunnel --edge-ip-version auto --no-autoupdate --protocol http2 run --token $argo_auth 2>&1'\"" /etc/init.d/argo
else
sed -i '/^ExecStart=/c ExecStart=/bin/sh -c "/etc/sing-box/argo tunnel --edge-ip-version auto --no-autoupdate --protocol http2 run --token '$argo_auth' 2>&1"' /etc/systemd/system/argo.service
fi
restart_argo; sleep 1; change_argo_domain
else
yellow "输入不匹配,请重新输入"; manage_argo
fi