forked from mack-a/v2ray-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
3783 lines (3306 loc) · 135 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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
# 检测区
# -------------------------------------------------------------
# 检查系统
export LANG=en_US.UTF-8
echoContent() {
case $1 in
# 红色
"red")
# shellcheck disable=SC2154
${echoType} "\033[31m${printN}$2 \033[0m"
;;
# 天蓝色
"skyBlue")
${echoType} "\033[1;36m${printN}$2 \033[0m"
;;
# 绿色
"green")
${echoType} "\033[32m${printN}$2 \033[0m"
;;
# 白色
"white")
${echoType} "\033[37m${printN}$2 \033[0m"
;;
"magenta")
${echoType} "\033[31m${printN}$2 \033[0m"
;;
# 黄色
"yellow")
${echoType} "\033[33m${printN}$2 \033[0m"
;;
esac
}
# 初始化全局变量
initVar() {
installType='yum -y install'
removeType='yum -y remove'
upgrade="yum -y update"
echoType='echo -e'
# 核心支持的cpu版本
xrayCoreCPUVendor=""
# 伪装域名
domain=
# 反代路径
path=
# UUID
UUID=
# 默认监听端口
Port=
# 安装总进度
totalProgress=1
# xray安装是否完成安装
coreInstallType=
# 当前的个性化安装方式 01234
currentInstallProtocolType=
# 前置类型
frontingType=
# centos version
centosVersion=
# nginx配置文件路径
nginxConfigPath=/etc/nginx/conf.d/
# xray配置文件路径
configPath=/etc/xray-agent/xray/conf/
# xray核心位置
ctlPath=/etc/xray-agent/xray/xray
# 是否为预览版
prereleaseStatus=false
# ssl申请的服务商
sslType=
# Xray中的TLS证书域名(用于解密TLS流量)
TLSDomain=
# Reality
RealityfrontingType=
RealityPrivateKey=
RealityPublicKey=
RealityServerNames=
RealityDestDomain=
RealityPort=
#共用443端口
reuse443=
}
checkSystem() {
if [[ -n $(find /etc -name "redhat-release") ]] || grep </proc/version -q -i "centos"; then
mkdir -p /etc/yum.repos.d
if [[ -f "/etc/centos-release" ]]; then
centosVersion=$(rpm -q centos-release | awk -F "[-]" '{print $3}' | awk -F "[.]" '{print $1}')
if [[ -z "${centosVersion}" ]] && grep </etc/centos-release -q -i "release 8"; then
centosVersion=8
fi
fi
release="centos"
installType='yum -y install'
removeType='yum -y remove'
upgrade="yum update -y --skip-broken"
elif grep </etc/issue -q -i "debian" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "debian" && [[ -f "/proc/version" ]]; then
release="debian"
installType='apt -y install'
upgrade="apt update"
updateReleaseInfoChange='apt-get --allow-releaseinfo-change update'
removeType='apt -y autoremove'
elif grep </etc/issue -q -i "ubuntu" && [[ -f "/etc/issue" ]] || grep </etc/issue -q -i "ubuntu" && [[ -f "/proc/version" ]]; then
release="ubuntu"
installType='apt -y install'
upgrade="apt update"
updateReleaseInfoChange='apt-get --allow-releaseinfo-change update'
removeType='apt -y autoremove'
if grep </etc/issue -q -i "16."; then
release=
fi
fi
if [[ -z ${release} ]]; then
echoContent red "\n本脚本不支持此系统,请将下方日志反馈给开发者\n"
echoContent yellow "$(cat /etc/issue)"
echoContent yellow "$(cat /proc/version)"
exit 0
fi
}
# 检查CPU提供商
checkCPUVendor() {
if [[ -n $(which uname) ]]; then
if [[ "$(uname)" == "Linux" ]]; then
case "$(uname -m)" in
'amd64' | 'x86_64')
xrayCoreCPUVendor="Xray-linux-64"
;;
'armv8' | 'aarch64')
xrayCoreCPUVendor="Xray-linux-arm64-v8a"
;;
*)
echo " 不支持此CPU架构--->"
exit 1
;;
esac
fi
else
echoContent red " 无法识别此CPU架构,默认amd64、x86_64--->"
xrayCoreCPUVendor="Xray-linux-64"
fi
}
# 检测xray是否完成安装
readInstallType() {
coreInstallType=
reuse443=
# 1.检测安装目录
if [[ -d "/etc/xray-agent" ]]; then
if [[ -d "/etc/xray-agent/xray" && -f "${ctlPath}" ]]; then
if [[ -d "/etc/xray-agent/xray/conf" ]] && [[ -f "${configPath}02_VLESS_TCP_inbounds.json" ]] && [[ -f "${configPath}07_VLESS_Reality_TCP_inbounds.json" ]]; then
# xray-core
coreInstallType=3
elif [[ -d "/etc/xray-agent/xray/conf" ]] && [[ -f "${configPath}02_VLESS_TCP_inbounds.json" ]]; then
# xray-core
coreInstallType=1
elif [[ -d "/etc/xray-agent/xray/conf" ]] && [[ -f "${configPath}07_VLESS_Reality_TCP_inbounds.json" ]]; then
# xray-core
coreInstallType=2
fi
if [[ -f "${nginxConfigPath}alone.stream" ]]; then
reuse443="y"
fi
fi
fi
}
# 读取协议类型
readInstallProtocolType() {
currentInstallProtocolType=
frontingType=
RealityfrontingType=
while read -r row; do
if echo "${row}" | grep -q VLESS_TCP_inbounds; then
currentInstallProtocolType=${currentInstallProtocolType}'0'
frontingType=02_VLESS_TCP_inbounds
fi
if echo "${row}" | grep -q VLESS_WS_inbounds; then
currentInstallProtocolType=${currentInstallProtocolType}'1'
fi
if echo "${row}" | grep -q VMess_WS_inbounds; then
currentInstallProtocolType=${currentInstallProtocolType}'2'
fi
if echo "${row}" | grep -q VLESS_Reality_TCP_inbounds; then
currentInstallProtocolType=${currentInstallProtocolType}'7'
RealityfrontingType=07_VLESS_Reality_TCP_inbounds
fi
if echo "${row}" | grep -q VLESS_XHTTP_inbounds; then
currentInstallProtocolType=${currentInstallProtocolType}'8'
fi
done < <(find ${configPath} -name "*inbounds.json" | awk -F "[.]" '{print $1}')
}
# 检查文件目录以及path路径
readConfigHostPathUUID() {
path=
Port=
UUID=
domain=
TLSDomain=
RealityPort=
RealityPublicKey=
RealityServerNames=
RealityDestDomain=
# 读取path
if [[ -f "${configPath}${frontingType}.json" ]]; then
local fallback
fallback=$(jq -r -c '.inbounds[0].settings.fallbacks[]|select(.path)' ${configPath}${frontingType}.json | head -1)
path=$(echo "${fallback}" | jq -r .path | awk -F "[/]" '{print $2}' | awk -F "[w][s]" '{print $1}')
if [[ -z "${path}" ]]; then
path=$(echo "${fallback}" | jq -r .path | awk -F "[/]" '{print $2}' | awk -F "[v][w][s]" '{print $1}')
fi
Port=$(jq -r .inbounds[0].port ${configPath}${frontingType}.json)
#domain=$(jq -r .inbounds[0].settings.clients[0].add ${configPath}${frontingType}.json | awk -F "@" '{print $1}')
#从nginx的回落配置中读取伪装域名
domain=$(grep "server_name" ${nginxConfigPath}alone.conf | awk '$2 ~ /\./ {gsub(";","",$2); print $2; exit}')
#UUID=$(jq -r .inbounds[0].settings.clients[0].id ${configPath}${frontingType}.json)
UUID=$(jq -r '.inbounds[0].settings.clients[] | .id' ${configPath}${frontingType}.json | paste -sd, -)
TLSDomain=$(jq -r .inbounds[0].streamSettings.tlsSettings.certificates[0].certificateFile ${configPath}${frontingType}.json | awk -F "[/]" '{print $5}' | awk -F "[.][c][r][t]" '{print $1}')
fi
if [[ -f "${configPath}${RealityfrontingType}.json" ]]; then
#UUID=$(jq -r .inbounds[0].settings.clients[0].id ${configPath}${RealityfrontingType}.json)
if [[ -z "${path}" ]]; then
UUID=$(jq -r '.inbounds[0].settings.clients[] | .id' ${configPath}${RealityfrontingType}.json | paste -sd, -)
fi
RealityServerNames=$(jq -r '.inbounds[0].streamSettings.realitySettings.serverNames | join(",")' ${configPath}${RealityfrontingType}.json)
RealityPublicKey=$(jq -r .inbounds[0].streamSettings.realitySettings.publicKey ${configPath}${RealityfrontingType}.json)
RealityPort=$(jq -r .inbounds[0].port ${configPath}${RealityfrontingType}.json)
RealityDestDomain=$(jq -r .inbounds[0].streamSettings.realitySettings.dest ${configPath}${RealityfrontingType}.json)
RealityPrivateKey=$(jq -r .inbounds[0].streamSettings.realitySettings.privateKey ${configPath}${RealityfrontingType}.json)
if [[ -z "${path}" ]] && [[ -f "${configPath}08_VLESS_XHTTP_inbounds.json" ]]; then
path=$(jq -r .inbounds[0].streamSettings.xhttpSettings.path ${configPath}08_VLESS_XHTTP_inbounds.json | awk -F "[/]" '{print $2}')
fi
fi
}
# 检查是否安装宝塔
checkBTPanel() {
if pgrep -f "BT-Panel"; then
nginxConfigPath=/www/server/panel/vhost/nginx/
fi
}
# 状态展示
showInstallStatus() {
if [[ -n "${coreInstallType}" ]]; then
if [[ -n $(pgrep -f xray/xray) ]]; then
echoContent yellow "\n核心: Xray-core[运行中]"
else
echoContent yellow "\n核心: Xray-core[未运行]"
fi
# 读取协议类型
readInstallProtocolType
if [[ -n ${currentInstallProtocolType} ]]; then
echoContent yellow "已安装协议: \c"
fi
if echo ${currentInstallProtocolType} | grep -q 0; then
echoContent yellow "VLESS+TCP[TLS] \c"
fi
if echo ${currentInstallProtocolType} | grep -q 1; then
echoContent yellow "VLESS+WS[TLS] \c"
fi
if echo ${currentInstallProtocolType} | grep -q 2; then
echoContent yellow "VMess+WS[TLS] \c"
fi
if echo ${currentInstallProtocolType} | grep -q 7; then
echoContent yellow "VLESS+TCP[Reality] \c"
fi
if echo ${currentInstallProtocolType} | grep -q 8; then
echoContent yellow "VLESS+XHTTP \c"
fi
fi
}
# 初始化安装目录
mkdirTools() {
mkdir -p /etc/xray-agent/tls
mkdir -p /etc/xray-agent/xray/conf
mkdir -p /etc/systemd/system/
}
# 脚本快捷方式
aliasInstall() {
if [[ -f "$HOME/install.sh" ]] && [[ -d "/etc/xray-agent" ]] && grep <"$HOME/install.sh" -q "作者:mack-a"; then
mv "$HOME/install.sh" /etc/xray-agent/install.sh
local vasmaType=
if [[ -d "/usr/bin/" ]]; then
if [[ ! -f "/usr/bin/vasma" ]]; then
ln -s /etc/xray-agent/install.sh /usr/bin/vasma
chmod 700 /usr/bin/vasma
vasmaType=true
fi
rm -rf "$HOME/install.sh"
elif [[ -d "/usr/sbin" ]]; then
if [[ ! -f "/usr/sbin/vasma" ]]; then
ln -s /etc/xray-agent/install.sh /usr/sbin/vasma
chmod 700 /usr/sbin/vasma
vasmaType=true
fi
rm -rf "$HOME/install.sh"
fi
if [[ "${vasmaType}" == "true" ]]; then
echoContent green "快捷方式创建成功,可执行[vasma]重新打开脚本"
fi
fi
}
# 安装Nginx
installNginxTools() {
if [[ "${release}" == "debian" ]]; then
sudo apt install gnupg2 ca-certificates lsb-release -y >/dev/null 2>&1
echo "deb http://nginx.org/packages/mainline/debian $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list >/dev/null 2>&1
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx >/dev/null 2>&1
curl -o /tmp/nginx_signing.key https://nginx.org/keys/nginx_signing.key >/dev/null 2>&1
# gpg --dry-run --quiet --import --import-options import-show /tmp/nginx_signing.key
sudo mv /tmp/nginx_signing.key /etc/apt/trusted.gpg.d/nginx_signing.asc
sudo apt update >/dev/null 2>&1
elif [[ "${release}" == "ubuntu" ]]; then
sudo apt install gnupg2 ca-certificates lsb-release -y >/dev/null 2>&1
echo "deb http://nginx.org/packages/mainline/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list >/dev/null 2>&1
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx >/dev/null 2>&1
curl -o /tmp/nginx_signing.key https://nginx.org/keys/nginx_signing.key >/dev/null 2>&1
# gpg --dry-run --quiet --import --import-options import-show /tmp/nginx_signing.key
sudo mv /tmp/nginx_signing.key /etc/apt/trusted.gpg.d/nginx_signing.asc
sudo apt update >/dev/null 2>&1
elif [[ "${release}" == "centos" ]]; then
${installType} yum-utils >/dev/null 2>&1
cat <<EOF >/etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
sudo yum-config-manager --enable nginx-mainline >/dev/null 2>&1
fi
${installType} nginx >/dev/null 2>&1
systemctl daemon-reload
systemctl enable nginx
}
# 安装工具包
installTools() {
echoContent skyBlue "\n进度 $1/${totalProgress} : 安装工具"
# 修复ubuntu个别系统问题
if [[ "${release}" == "ubuntu" ]]; then
dpkg --configure -a
fi
# 终止所有正在运行的apt进程
if pgrep -f "apt" >/dev/null 2>&1; then
pgrep -f apt | xargs kill -9
fi
echoContent green " ---> 检查、安装更新【新机器会很慢,如长时间无反应,请手动停止后重新执行】"
${upgrade} >/etc/xray-agent/install.log 2>&1
if grep -q "changed" "/etc/xray-agent/install.log"; then
${updateReleaseInfoChange} >/dev/null 2>&1
fi
if [[ "${release}" == "centos" ]]; then
rm -rf /var/run/yum.pid
${installType} epel-release >/dev/null 2>&1
fi
# 更新工具检查命令
declare -a tools=("wget" "curl" "unzip" "tar" "cron" "jq" "ld" "lsb_release" "sudo" "lsof" "dig")
for tool in "${tools[@]}"; do
if ! command -v "${tool}" >/dev/null 2>&1; then
echoContent green " ---> 安装${tool}"
# 根据工具名称选择正确的包名进行安装
if [[ "${tool}" == "cron" ]]; then
if [[ "${release}" == "ubuntu" ]] || [[ "${release}" == "debian" ]]; then
${installType} cron >/dev/null 2>&1
else
${installType} crontabs >/dev/null 2>&1
fi
elif [[ "${tool}" == "ld" ]]; then
# 'ld' 是 'binutils' 包中的命令
${installType} binutils >/dev/null 2>&1
elif [[ "${tool}" == "lsb_release" ]]; then
# 'lsb_release' 是 'lsb-release' 包中的命令
${installType} lsb-release >/dev/null 2>&1
elif [[ "${tool}" == "ping6" ]]; then
${installType} inetutils-ping >/dev/null 2>&1
elif [[ "${tool}" == "dig" ]]; then
if echo "${installType}" | grep -q -w "apt"; then
${installType} dnsutils >/dev/null 2>&1
elif echo "${installType}" | grep -q -w "yum"; then
${installType} bind-utils >/dev/null 2>&1
fi
else
${installType} "${tool}" >/dev/null 2>&1
fi
fi
done
# 检测nginx版本,并提供是否卸载的选项
if ! command -v nginx >/dev/null 2>&1; then
echoContent green " ---> 安装nginx"
installNginxTools
else
nginxVersion=$(nginx -v 2>&1)
nginxVersion=$(echo "${nginxVersion}" | awk -F "[n][g][i][n][x][/]" '{print $2}' | awk -F "[.]" '{print $2}')
if [[ ${nginxVersion} -lt 14 ]]; then
read -r -p "读取到当前的Nginx版本不支持gRPC,会导致安装失败,是否卸载Nginx后重新安装 ?[y/n]:" unInstallNginxStatus
if [[ "${unInstallNginxStatus}" == "y" ]]; then
${removeType} nginx >/dev/null 2>&1
echoContent yellow " ---> nginx卸载完成"
echoContent green " ---> 安装nginx"
installNginxTools >/dev/null 2>&1
else
exit 0
fi
fi
fi
if ! command -v semanage >/dev/null 2>&1; then
echoContent green " ---> 安装semanage"
${installType} bash-completion >/dev/null 2>&1
if [[ "${centosVersion}" == "7" ]]; then
policyCoreUtils="policycoreutils-python.x86_64"
elif [[ "${centosVersion}" == "8" ]]; then
policyCoreUtils="policycoreutils-python-utils-2.9-9.el8.noarch"
fi
if [[ -n "${policyCoreUtils}" ]]; then
${installType} "${policyCoreUtils}" >/dev/null 2>&1
fi
if command -v semanage >/dev/null 2>&1; then
semanage port -a -t http_port_t -p tcp 31300
fi
fi
if [[ ! -d "$HOME/.acme.sh" ]] || [[ -d "$HOME/.acme.sh" && -z $(find "$HOME/.acme.sh/acme.sh") ]]; then
echoContent green " ---> 安装acme.sh"
curl -s https://get.acme.sh | sh >/etc/xray-agent/tls/acme.log 2>&1
sudo "$HOME/.acme.sh/acme.sh" --upgrade --auto-upgrade
if [[ ! -d "$HOME/.acme.sh" ]] || [[ -z $(find "$HOME/.acme.sh/acme.sh") ]]; then
echoContent red " acme安装失败--->"
tail -n 100 /etc/xray-agent/tls/acme.log
echoContent yellow "错误排查:"
echoContent red " 1.获取GitHub文件失败,请等待GitHub恢复后尝试,恢复进度可查看 [https://www.githubstatus.com/]"
echoContent red " 2.acme.sh脚本出现bug,可查看[https://github.com/acmesh-official/acme.sh] issues"
echoContent red " 3.如纯IPv6机器,请设置NAT64,可执行下方命令"
echoContent skyBlue " echo -e \"nameserver 2001:67c:2b0::4\\\nnameserver 2001:67c:2b0::6\" >> /etc/resolv.conf"
exit 0
fi
fi
}
# 操作Nginx
handleNginx() {
if [[ -z $(pgrep -f "nginx") ]] && [[ "$1" == "start" ]]; then
systemctl start nginx 2>/etc/xray-agent/nginx_error.log
sleep 0.5
if [[ -z $(pgrep -f nginx) ]]; then
echoContent red " ---> Nginx启动失败"
echoContent red " ---> 请手动尝试安装nginx后,再次执行脚本"
else
echoContent green " ---> Nginx启动成功"
fi
elif [[ -n $(pgrep -f "nginx") ]] && [[ "$1" == "stop" ]]; then
systemctl stop nginx
sleep 0.5
if [[ -n $(pgrep -f "nginx") ]]; then
pgrep -f "nginx" | xargs kill -9
fi
echoContent green " ---> Nginx关闭成功"
fi
}
# 自定义端口
customPortFunction() {
if [[ "$1" == "Vision" ]]; then
port="${Port}"
elif [[ "$1" == "Reality" ]]; then
port="${RealityPort}"
fi
if [[ -n "${port}" ]]; then
echo
read -r -p "${1}读取到上次安装时的端口,是否使用上次安装时的端口 ?[y/n]:" historyCustomPortStatus
if [[ "${historyCustomPortStatus}" == "y" ]]; then
if [[ "${reuse443}" == "y" && "${port}" == "443" ]]; then
echoContent red " ---> ${1}全局设置为不允许使用端口 443"
historyCustomPortStatus="n"
else
echoContent yellow "\n ---> ${1}端口: ${port}"
fi
fi
fi
if [[ "${historyCustomPortStatus}" == "n" || -z "${port}" ]]; then
echo
echoContent yellow "${1}请输入自定义端口[例: 2083],[回车]使用443"
read -r -p "端口:" port
if [[ -n "${port}" ]]; then
if ((port >= 1 && port <= 65535)); then
if [[ "${reuse443}" == "y" && "${port}" == "443" ]]; then
echoContent red " ---> ${1}全局设置为不允许使用端口 443"
exit 0
fi
checkPort "${port}"
else
echoContent red " ---> ${1}端口输入错误"
exit 0
fi
else
if [[ "${reuse443}" == "y" ]]; then
echoContent red " ---> ${1}全局设置为不允许使用默认端口 443"
exit 0
fi
port=443
checkPort "${port}"
echoContent yellow "\n ---> ${1}端口: 443"
fi
fi
allowPort "${port}"
if [[ "$1" == "Vision" ]]; then
Port="${port}"
if [[ -f "${configPath}${frontingType}.json" ]]; then
# 捕获 jq 输出到变量
updated_json=$(jq ".inbounds[0].port = ${port}" "${configPath}${frontingType}.json")
# 将更新后的 JSON 写回文件
echo "${updated_json}" | jq . > "${configPath}${frontingType}.json"
fi
elif [[ "$1" == "Reality" ]]; then
RealityPort="${port}"
if [[ -f "${configPath}${RealityfrontingType}.json" ]]; then
# 捕获 jq 输出到变量
updated_json=$(jq ".inbounds[0].port = ${port}" "${configPath}${RealityfrontingType}.json")
# 将更新后的 JSON 写回文件
echo "${updated_json}" | jq . > "${configPath}${RealityfrontingType}.json"
fi
fi
# 删除其他自定义端口
if [[ "${historyCustomPortStatus}" == "n" ]] && [[ "$1" == "Vision" ]]; then
rm -rf "$(find ${configPath}* | grep "dokodemodoor")"
fi
}
# 检测端口是否占用
checkPort() {
port="$1"
port_progress=$(lsof -i "tcp:${port}" | grep -q LISTEN | awk '{print $1}' | head -1)
if [[ -n "${port_progress}" && "${port_progress}" != "xray" ]]; then
echoContent red "\n ---> ${port}端口被占用,请手动关闭后安装\n"
exit 0
fi
}
# 初始化Reality证书配置
initTLSRealityConfig() {
echoContent skyBlue "\n进度 $1/${totalProgress} : 初始化Reality证书配置"
while true; do
if [[ -n "${RealityDestDomain}" ]]; then
read -r -p "读取到上次安装记录,是否使用上次安装时的域名 ?[y/n]:" historyDestStatus
if [[ "${historyDestStatus}" == "y" ]]; then
echoContent green "\n ---> 使用成功"
else
echoContent skyBlue "\n ---> 生成配置回落的域名 例如: addons.mozilla.org:443\n"
read -r -p '请输入:' RealityDestDomain
fi
else
echoContent skyBlue "\n ---> 生成配置回落的域名 例如: addons.mozilla.org:443\n"
read -r -p '请输入:' RealityDestDomain
fi
# 检查域名是否为空或格式不正确
if [[ -z "${RealityDestDomain}" ]]; then
echoContent red " 域名不可为空--->"
elif [[ "${RealityDestDomain}" != *:* ]]; then
echoContent red "\n ---> 域名不合规范,请重新输入 (示例: addons.mozilla.org:443)"
else
break
fi
done
echoContent yellow "\n ${RealityDestDomain}"
echoContent skyBlue "\n >配置客户端可用的serverNames\n"
echoContent red "\n=============================================================="
if [[ "${historyDestStatus}" == "y" ]] && [[ -n "${RealityServerNames}" ]]; then
echoContent green "\n ---> 使用成功"
# 将逗号分隔的域名转换为 JSON 数组格式,并添加双引号
RealityServerNames="\"${RealityServerNames//,/\",\"}\""
else
echoContent yellow " # 注意事项\n"
tlsPingResult=$(${ctlPath} tls ping "${RealityDestDomain%%:*}")
echoContent yellow "\n ---> 可以输入的域名: ${tlsPingResult}\n"
echoContent red "\n=============================================================="
echoContent yellow "录入示例: addons.mozilla.org,services.addons.mozilla.org\n"
echoContent yellow " # 支持逗号输入多个域名,但不支持通配符\n"
read -r -p "请输入:" RealityServerNames
if [[ -z "${RealityServerNames}" ]]; then
# 如果未输入,默认使用域名部分,并添加双引号
RealityServerNames="\"${RealityDestDomain%%:*}\""
else
# 将逗号分隔的域名转换为 JSON 数组格式,并添加双引号
RealityServerNames="\"${RealityServerNames//,/\",\"}\""
fi
fi
echoContent yellow "\n ---> 客户端可用域名: ${RealityServerNames}\n"
}
# 初始化Nginx申请证书配置
initTLSNginxConfig() {
echoContent skyBlue "\n进度 $1/${totalProgress} : 初始化Nginx申请证书配置"
if [[ -n "${domain}" ]]; then
echo
read -r -p "读取到上次安装记录,是否使用上次安装时的域名 ?[y/n]:" historyDomainStatus
if [[ "${historyDomainStatus}" == "y" ]]; then
echoContent yellow "\n ---> 域名: ${domain}"
else
echo
echoContent yellow "请输入要配置的域名 例: www.xray-agent.com --->"
read -r -p "域名:" domain
fi
else
echo
echoContent yellow "请输入要配置的域名 例: www.xray-agent.com --->"
read -r -p "域名:" domain
fi
if [[ -z ${domain} ]]; then
echoContent red " 域名不可为空--->"
initTLSNginxConfig 3
fi
}
# 选择ssl安装类型
switchSSLType() {
if [[ -z "${sslType}" ]]; then
echoContent red "\n=============================================================="
echoContent yellow "1.letsencrypt[默认]"
echoContent yellow "2.zerossl"
echoContent yellow "3.HiCA"
echoContent red "=============================================================="
read -r -p "请选择[回车]使用默认:" selectSSLType
case ${selectSSLType} in
1)
sslType="letsencrypt"
;;
2)
sslType="zerossl"
;;
3)
sslType="https://acme.hi.cn/directory"
;;
*)
sslType="letsencrypt"
;;
esac
fi
}
# acme申请证书
# 初始化SSL证书配置
acmeInstallSSL() {
# 获取当前IPv6地址
currentIPv6IP=$(curl -s -6 http://www.cloudflare.com/cdn-cgi/trace | grep "ip" | cut -d "=" -f 2)
# 根据是否有IPv6地址设置参数
if [[ -z "${currentIPv6IP}" ]]; then
installSSLIPv6=""
else
installSSLIPv6="--listen-v6"
fi
# 显示SSL安装选项
echoContent red "\n=============================================================="
echoContent yellow "1. 密钥(通配证书)"
echoContent yellow "2. DNS(通配证书)"
echoContent yellow "3. 普通证书【默认】"
read -r -p "申请SSL证书的方式 [默认: 3]:" installSSLType
# 如果用户直接回车,默认选择3
installSSLType=${installSSLType:-3}
if [[ "${installSSLType}" == "1" ]]; then
# 选择DNS提供商
echoContent red "\n=============================================================="
echoContent yellow "1. Cloudflare [默认]"
echoContent yellow "2. DNSPod"
echoContent yellow "3. Aliyun"
echoContent yellow "4. 其他"
echoContent red " ---> 其他DNS运营商使用方式详见 https://github.com/acmesh-official/acme.sh/wiki/dnsapi"
echoContent red " ---> 请先根据文档自行添加密钥后,并输入n"
echoContent red "=============================================================="
read -r -p "请选择DNS服务商 [默认: 1]:" selectDNS
# 如果用户直接回车,默认选择1
selectDNS=${selectDNS:-1}
# 根据选择的DNS服务商获取相应的API密钥
if [[ "${selectDNS}" == "1" ]]; then
echoContent red " ---> 当前Token需要访问 Zone.Zone 的读取权限和 Zone.DNS 的写入权限"
echoContent red "=============================================================="
read -r -p "请输入Cloudflare API Token:" CF_Token
dnsEnvVars="CF_Token='${CF_Token}'"
dnsType="dns_cf"
elif [[ "${selectDNS}" == "2" ]]; then
echoContent red " ---> DNSPod.cn 需要先登录账号获取DNSPod API Key和ID"
echoContent red "=============================================================="
read -r -p "请输入DNSPod API Key:" DP_Key
read -r -p "请输入DNSPod API ID:" DP_Id
dnsEnvVars="DP_Key='${DP_Key}' DP_Id='${DP_Id}'"
dnsType="dns_dp"
elif [[ "${selectDNS}" == "3" ]]; then
echoContent red " ---> 请先登录您的Aliyun账户获取RAM API Key。参考: https://ram.console.aliyun.com/users"
echoContent red "=============================================================="
read -r -p "请输入Aliyun API Key:" Ali_Key
read -r -p "请输入Aliyun Secret:" Ali_Secret
dnsEnvVars="Ali_Key='${Ali_Key}' Ali_Secret='${Ali_Secret}'"
dnsType="dns_ali"
elif [[ "${selectDNS}" == "4" ]]; then
echoContent red "请确保已经通过export添加相应TOKEN、KEY、ID等"
echoContent yellow "输入类似于dns_cf; dns_dp; dns_ali "
echoContent red "=============================================================="
read -r -p "请输入DNS服务商:" dnsType
else
echoContent red "选择错误,请重新运行脚本并选择正确的选项。"
exit 1
fi
# 处理ZeroSSL选项
if [[ "${sslType}" == "2" ]]; then
echoContent red " ---> ZeroSSL需要注册账号"
read -r -p "请输入ZeroSSL后台控制面板拿到的API Key:" ZeroSSL_API
ZeroSSL_Result=$(curl -s -X POST "https://api.zerossl.com/acme/eab-credentials?access_key=${ZeroSSL_API}")
eab_kid=$(echo "$ZeroSSL_Result" | jq -r .eab_kid)
eab_hmac_key=$(echo "$ZeroSSL_Result" | jq -r .eab_hmac_key)
# 注册ZeroSSL账号
sudo "$HOME/.acme.sh/acme.sh" --register-account --server zerossl --eab-kid "${eab_kid}" --eab-hmac-key "${eab_hmac_key}"
fi
echoContent green " ---> 生成证书中"
# 申请证书
eval "${dnsEnvVars}" sudo -E "$HOME/.acme.sh/acme.sh" --issue -d "${TLSDomain}" -d "*.${TLSDomain}" --dns "${dnsType}" -k ec-256 --server "${sslType}" ${installSSLIPv6} --force 2>&1 | tee -a /etc/xray-agent/tls/acme.log >/dev/null
elif [[ "${installSSLType}" == "2" ]]; then
# DNS手动模式申请通配证书
sudo "$HOME/.acme.sh/acme.sh" --issue -d "${TLSDomain}" -d "*.${TLSDomain}" --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please -k ec-256 --server "${sslType}" ${installSSLIPv6} --force 2>&1 | tee -a /etc/xray-agent/tls/acme.log >/dev/null
# 获取TXT值
txtValue=$(tail -n 10 /etc/xray-agent/tls/acme.log | grep "TXT value" | awk -F "'" '{print $2}')
if [[ -n "${txtValue}" ]]; then
echoContent green " ---> 请手动添加DNS TXT记录"
echoContent yellow " ---> 添加方法请参考此教程,https://github.com/mack-a/v2ray-agent/blob/master/documents/dns_txt.md"
echoContent yellow " ---> 如同一个域名多台机器安装通配符证书,请添加多个TXT记录,不需要修改以前添加的TXT记录"
echoContent green " ---> name:_acme-challenge"
echoContent green " ---> value:${txtValue}"
echoContent yellow " ---> 添加完成后请等待1-2分钟"
echo
read -r -p "是否添加完成[y/n]:" addDNSTXTRecordStatus
if [[ "${addDNSTXTRecordStatus}" == "y" ]]; then
# 验证TXT记录
txtAnswer=$(dig @1.1.1.1 +nocmd "_acme-challenge.${TLSDomain}" txt +noall +answer | awk -F "[\"]" '{print $2}')
if [[ "${txtAnswer}" == "${txtValue}" ]]; then
echoContent green " ---> TXT记录验证通过"
echoContent green " ---> 生成证书中"
sudo "$HOME/.acme.sh/acme.sh" --renew -d "${TLSDomain}" -d "*.${TLSDomain}" --yes-I-know-dns-manual-mode-enough-go-ahead-please --ecc --server "${sslType}" ${installSSLIPv6} --force 2>&1 | tee -a /etc/xray-agent/tls/acme.log >/dev/null
else
echoContent red " ---> 验证失败,请等待1-2分钟后重新尝试"
exit 1
fi
else
echoContent red " ---> 放弃"
exit 0
fi
fi
elif [[ "${installSSLType}" == "3" ]]; then
# 普通证书申请
allowPort 80
allowPort 443
TLSDomain=${domain}
echoContent green " ---> 生成证书中"
sudo "$HOME/.acme.sh/acme.sh" --issue -d "${TLSDomain}" --standalone -k ec-256 --server "${sslType}" ${installSSLIPv6} --force 2>&1 | tee -a /etc/xray-agent/tls/acme.log >/dev/null
else
echoContent red "选择错误,请重新运行脚本并选择正确的选项。"
exit 1
fi
}
# 安装TLS
installTLS() {
echoContent skyBlue "\n进度 $1/${totalProgress} : 申请TLS证书\n"
# 判断证书域名与伪装域名相同, 如果未找到证书,则设置为根域名
if [[ -f "/etc/xray-agent/tls/${domain}.crt" && -f "/etc/xray-agent/tls/${domain}.key" && -s "/etc/xray-agent/tls/${domain}.crt" ]] || [[ -d "$HOME/.acme.sh/${domain}_ecc" && -f "$HOME/.acme.sh/${domain}_ecc/${domain}.key" && -f "$HOME/.acme.sh/${domain}_ecc/${domain}.cer" ]]; then
TLSDomain="${domain}"
else
# 提取根域名
TLSDomain=$(echo "${domain}" | awk -F "." '{print $(NF-1)"."$NF}')
if [[ "${TLSDomain}" == "eu.org" ]]; then
TLSDomain=$(echo "${domain}" | awk -F "." '{print $(NF-2)"."$(NF-1)"."$NF}')
fi
fi
# 安装TLS
if [[ -f "/etc/xray-agent/tls/${TLSDomain}.crt" && -f "/etc/xray-agent/tls/${TLSDomain}.key" && -s "/etc/xray-agent/tls/${TLSDomain}.crt" ]] || [[ -d "$HOME/.acme.sh/${TLSDomain}_ecc" && -f "$HOME/.acme.sh/${TLSDomain}_ecc/${TLSDomain}.key" && -f "$HOME/.acme.sh/${TLSDomain}_ecc/${TLSDomain}.cer" ]]; then
echoContent green " ---> 检测到证书"
# 尝试续期TLS证书
renewalTLS "${TLSDomain}"
# 检查续期后的证书是否存在且非空
if [[ ! -f "/etc/xray-agent/tls/${TLSDomain}.crt" || ! -f "/etc/xray-agent/tls/${TLSDomain}.key" || ! -s "/etc/xray-agent/tls/${TLSDomain}.crt" ]]; then
sudo "$HOME/.acme.sh/acme.sh" --installcert -d "${TLSDomain}" --fullchainpath "/etc/xray-agent/tls/${TLSDomain}.crt" --keypath "/etc/xray-agent/tls/${TLSDomain}.key" --ecc >/dev/null
else
echoContent yellow " ---> 如未过期或者自定义证书请选择[n]\n"
read -r -p "是否重新安装?[y/n]:" reInstallStatus
if [[ "${reInstallStatus}" == "y" ]]; then
# 移除现有证书文件
find /etc/xray-agent/tls/ -type f -name "*${TLSDomain}*" -exec rm -f {} \;
# 递归调用以重新安装证书
installTLS "$1" 0
fi
fi
elif [[ -d "$HOME/.acme.sh" ]]; then
# 停止Nginx服务
handleNginx stop
echoContent green " ---> 安装TLS证书"
# 切换SSL类型,配置邮箱,安装SSL
switchSSLType
customSSLEmail
acmeInstallSSL
# 安装证书
sudo "$HOME/.acme.sh/acme.sh" --installcert -d "${TLSDomain}" --fullchainpath "/etc/xray-agent/tls/${TLSDomain}.crt" --keypath "/etc/xray-agent/tls/${TLSDomain}.key" --ecc >/dev/null
# 启动Nginx服务
handleNginx start
# 检查证书是否成功安装
if [[ ! -f "/etc/xray-agent/tls/${TLSDomain}.crt" || ! -f "/etc/xray-agent/tls/${TLSDomain}.key" ]] || [[ ! -s "/etc/xray-agent/tls/${TLSDomain}.key" || ! -s "/etc/xray-agent/tls/${TLSDomain}.crt" ]]; then
# 显示acme日志的最后10行
tail -n 10 /etc/xray-agent/tls/acme.log
if [[ "$2" == "1" ]]; then
echoContent red " ---> TLS安装失败,请检查acme日志"
exit 0
fi
echo
echoContent yellow " ---> 重新尝试安装TLS证书"
# 检查acme日志中是否有邮箱验证错误
if grep -q "Could not validate email address as valid" /etc/xray-agent/tls/acme.log; then
echoContent red " ---> 邮箱无法通过SSL厂商验证,请重新输入"
echo
customSSLEmail "validate email"
# 递归调用以重新安装证书
installTLS "$1" 1
else
# 递归调用以重新安装证书
installTLS "$1" 1
fi
fi
echoContent green " ---> TLS生成成功"
else
echoContent yellow " ---> 未安装acme.sh"
exit 0
fi
}
# 自定义email
customSSLEmail() {
if echo "$1" | grep -q "validate email"; then
read -r -p "是否重新输入邮箱地址[y/n]:" sslEmailStatus
if [[ "${sslEmailStatus}" == "y" ]]; then
sed '/ACCOUNT_EMAIL/d' /root/.acme.sh/account.conf >/root/.acme.sh/account.conf_tmp && mv /root/.acme.sh/account.conf_tmp /root/.acme.sh/account.conf
else
exit 0
fi
fi
if [[ -d "/root/.acme.sh" && -f "/root/.acme.sh/account.conf" ]]; then
if ! grep -q "ACCOUNT_EMAIL" <"/root/.acme.sh/account.conf" && ! echo "${sslType}" | grep -q "letsencrypt"; then
read -r -p "请输入邮箱地址:" sslEmail
if echo "${sslEmail}" | grep -q "@"; then
echo "ACCOUNT_EMAIL='${sslEmail}'" >>/root/.acme.sh/account.conf
echoContent green " ---> 添加成功"
else
echoContent yellow "请重新输入正确的邮箱格式[例: username@example.com]"
customSSLEmail
fi
fi
fi
}
# 自定义/随机路径
randomPathFunction() {
echoContent skyBlue "\n进度 $1/${totalProgress} : 生成随机路径"
if [[ -n "${path}" ]]; then
echo
read -r -p "读取到上次安装记录,是否使用上次安装时的path路径 ?[y/n]:" historyPathStatus
echo
fi
if [[ "${historyPathStatus}" == "y" ]]; then
echoContent green " ---> 使用成功\n"
else
echoContent yellow "请输入自定义路径[例: alone],不需要斜杠,[回车]随机路径"
read -r -p '路径:' path
if [[ -z "${path}" ]]; then
local chars="abcdefghijklmnopqrtuxyz"
for i in {1..4}; do