forked from ak47229527/Xray-TLS-Web-setup-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Xray-TLS+Web-setup.sh
3462 lines (3372 loc) · 134 KB
/
Xray-TLS+Web-setup.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
#!/bin/bash
#系统信息
#指令集
machine=""
#什么系统
release=""
#系统版本号
systemVersion=""
debian_package_manager=""
redhat_package_manager=""
#物理内存大小
mem=""
#在运行脚本前物理内存+swap大小
mem_total=""
#在运行脚本前是否有启用swap
using_swap=""
#现在有没有通过脚本启动swap
using_swap_now=0
#安装信息
nginx_version="nginx-1.19.8"
openssl_version="openssl-openssl-3.0.0-alpha13"
nginx_prefix="/usr/local/nginx"
nginx_config="${nginx_prefix}/conf.d/xray.conf"
nginx_service="/etc/systemd/system/nginx.service"
nginx_is_installed=""
php_version="php-8.0.3"
php_prefix="/usr/local/php"
php_service="/etc/systemd/system/php-fpm.service"
php_is_installed=""
cloudreve_version="3.3.1"
cloudreve_prefix="/usr/local/cloudreve"
cloudreve_service="/etc/systemd/system/cloudreve.service"
cloudreve_is_installed=""
nextcloud_url="https://download.nextcloud.com/server/releases/nextcloud-21.0.0.zip"
xray_config="/usr/local/etc/xray/config.json"
xray_is_installed=""
temp_dir="/temp_install_update_xray_tls_web"
is_installed=""
update=""
in_install_update_xray_tls_web=0
#配置信息
#域名列表 两个列表用来区别 www.主域名
unset domain_list
unset true_domain_list
unset domain_config_list
#域名伪装列表,对应域名列表
unset pretend_list
# TCP使用的会话层协议,0代表禁用,1代表VLESS
protocol_1=""
# grpc使用的会话层协议,0代表禁用,1代表VLESS,2代表VMess
protocol_2=""
# WebSocket使用的会话层协议,0代表禁用,1代表VLESS,2代表VMess
protocol_3=""
serviceName=""
path=""
xid_1=""
xid_2=""
xid_3=""
#功能性函数:
#定义几个颜色
purple() #基佬紫
{
echo -e "\\033[35;1m${*}\\033[0m"
}
tyblue() #天依蓝
{
echo -e "\\033[36;1m${*}\\033[0m"
}
green() #原谅绿
{
echo -e "\\033[32;1m${*}\\033[0m"
}
yellow() #鸭屎黄
{
echo -e "\\033[33;1m${*}\\033[0m"
}
red() #姨妈红
{
echo -e "\\033[31;1m${*}\\033[0m"
}
blue() #蓝色
{
echo -e "\\033[34;1m${*}\\033[0m"
}
#检查基本命令
check_base_command()
{
local i
local temp_command_list=('bash' 'true' 'false' 'exit' 'echo' 'test' 'free' 'sort' 'sed' 'awk' 'grep' 'cut' 'cd' 'rm' 'cp' 'mv' 'head' 'tail' 'uname' 'tr' 'md5sum' 'tar' 'cat' 'find' 'type' 'command' 'kill' 'pkill' 'wc' 'ls' 'mktemp')
for i in ${!temp_command_list[@]}
do
if ! command -V "${temp_command_list[$i]}" > /dev/null; then
red "命令\"${temp_command_list[$i]}\"未找到"
red "不是标准的Linux系统"
exit 1
fi
done
}
check_sudo()
{
if [ "$SUDO_GID" ] && [ "$SUDO_COMMAND" ] && [ "$SUDO_USER" ] && [ "$SUDO_UID" ]; then
if [ "$SUDO_USER" = "root" ] && [ "$SUDO_UID" = "0" ]; then
#it's root using sudo, no matter it's using sudo or not, just fine
return 0
fi
if [ -n "$SUDO_COMMAND" ]; then
#it's a normal user doing "sudo su", or `sudo -i` or `sudo -s`, or `sudo su acmeuser1`
echo "$SUDO_COMMAND" | grep -- "/bin/su\$" >/dev/null 2>&1 || echo "$SUDO_COMMAND" | grep -- "/bin/su " >/dev/null 2>&1 || grep "^$SUDO_COMMAND\$" /etc/shells >/dev/null 2>&1
return $?
fi
#otherwise
return 1
fi
return 0
}
#版本比较函数
version_ge()
{
test "$(echo -e "$1\\n$2" | sort -rV | head -n 1)" == "$1"
}
#安装单个重要依赖
check_important_dependence_installed()
{
local temp_exit_code=1
if [ $release == "ubuntu" ] || [ $release == "debian" ] || [ $release == "deepin" ] || [ $release == "other-debian" ]; then
if dpkg -s "$1" > /dev/null 2>&1; then
apt-mark manual "$1" && temp_exit_code=0
elif $debian_package_manager -y --no-install-recommends install "$1"; then
temp_exit_code=0
else
$debian_package_manager update
$debian_package_manager -y -f install
$debian_package_manager -y --no-install-recommends install "$1" && temp_exit_code=0
fi
else
if rpm -q "$2" > /dev/null 2>&1; then
if [ "$redhat_package_manager" == "dnf" ]; then
dnf mark install "$2" && temp_exit_code=0
else
yumdb set reason user "$2" && temp_exit_code=0
fi
elif $redhat_package_manager -y install "$2"; then
temp_exit_code=0
fi
fi
if [ $temp_exit_code -ne 0 ]; then
if [ $release == "ubuntu" ] || [ $release == "debian" ] || [ $release == "deepin" ] || [ $release == "other-debian" ]; then
red "重要组件\"$1\"安装失败!!"
else
red "重要组件\"$2\"安装失败!!"
fi
yellow "按回车键继续或者Ctrl+c退出"
read -s
fi
}
#安装依赖
install_dependence()
{
if [ $release == "ubuntu" ] || [ $release == "debian" ] || [ $release == "deepin" ] || [ $release == "other-debian" ]; then
if ! $debian_package_manager -y --no-install-recommends install "$@"; then
$debian_package_manager update
$debian_package_manager -y -f install
if ! $debian_package_manager -y --no-install-recommends install "$@"; then
yellow "依赖安装失败!!"
green "欢迎进行Bug report(https://github.com/kirin10000/Xray-script/issues),感谢您的支持"
yellow "按回车键继续或者Ctrl+c退出"
read -s
fi
fi
else
if $redhat_package_manager --help | grep -q "\\-\\-enablerepo="; then
local temp_redhat_install="$redhat_package_manager -y --enablerepo="
else
local temp_redhat_install="$redhat_package_manager -y --enablerepo "
fi
if ! $redhat_package_manager -y install "$@"; then
if [ "$release" == "centos" ] && version_ge "$systemVersion" 8 && $temp_redhat_install"epel,PowerTools" install "$@";then
return 0
fi
if $temp_redhat_install'*' install "$@"; then
return 0
fi
yellow "依赖安装失败!!"
green "欢迎进行Bug report(https://github.com/kirin10000/Xray-script/issues),感谢您的支持"
yellow "按回车键继续或者Ctrl+c退出"
read -s
fi
fi
}
#进入工作目录
enter_temp_dir()
{
local temp_exit_code=0
cd / || temp_exit_code=1
rm -rf "$temp_dir" || temp_exit_code=1
mkdir "$temp_dir" || temp_exit_code=1
cd "$temp_dir" || temp_exit_code=1
if [ $temp_exit_code -eq 1 ]; then
yellow "进入临时目录失败"
tyblue "可能是之前运行脚本中断导致,建议先重启系统,再运行脚本"
exit 1
fi
}
#检查是否需要php
check_need_php()
{
[ $is_installed -eq 0 ] && return 1
local i
for i in ${!pretend_list[@]}
do
[ "${pretend_list[$i]}" == "2" ] && return 0
done
return 1
}
#检查是否需要cloudreve
check_need_cloudreve()
{
[ $is_installed -eq 0 ] && return 1
local i
for i in ${!pretend_list[@]}
do
[ "${pretend_list[$i]}" == "1" ] && return 0
done
return 1
}
#检查Nginx更新
check_nginx_update()
{
local nginx_version_now
local openssl_version_now
nginx_version_now="nginx-$(${nginx_prefix}/sbin/nginx -V 2>&1 | grep "^nginx version:" | cut -d / -f 2)"
openssl_version_now="openssl-openssl-$(${nginx_prefix}/sbin/nginx -V 2>&1 | grep "^built with OpenSSL" | awk '{print $4}')"
if [ "$nginx_version_now" == "$nginx_version" ] && [ "$openssl_version_now" == "$openssl_version" ]; then
return 1
else
return 0
fi
}
#检查php更新
check_php_update()
{
local php_version_now
php_version_now="php-$(${php_prefix}/bin/php -v | head -n 1 | awk '{print $2}')"
[ "$php_version_now" == "$php_version" ] && return 1
return 0
}
swap_on()
{
if [ $using_swap_now -ne 0 ]; then
red "开启swap错误发生"
green "欢迎进行Bug report(https://github.com/kirin10000/Xray-script/issues),感谢您的支持"
yellow "按回车键继续或者Ctrl+c退出"
read -s
fi
if [ $mem_total -lt $1 ]; then
tyblue "内存不足$1M,自动申请swap。。"
if dd if=/dev/zero of=${temp_dir}/swap bs=1M count=$(($1-mem)); then
chmod 0600 ${temp_dir}/swap
mkswap ${temp_dir}/swap
swapoff -a
swapon ${temp_dir}/swap
using_swap_now=1
else
rm -rf ${temp_dir}/swap
red "开启swap失败!"
yellow "可能是机器内存和硬盘空间都不足"
green "欢迎进行Bug report(https://github.com/kirin10000/Xray-script/issues),感谢您的支持"
yellow "按回车键继续或者Ctrl+c退出"
read -s
fi
fi
}
swap_off()
{
if [ $using_swap_now -eq 1 ]; then
tyblue "正在恢复swap。。。"
swapoff -a
rm -rf ${temp_dir}/swap
[ $using_swap -ne 0 ] && swapon -a
using_swap_now=0
fi
}
#启用/禁用php cloudreve
turn_on_off_php()
{
if check_need_php; then
systemctl --now enable php-fpm
else
systemctl --now disable php-fpm
fi
}
turn_on_off_cloudreve()
{
if check_need_cloudreve; then
systemctl --now enable cloudreve
else
systemctl --now disable cloudreve
fi
}
let_change_cloudreve_domain()
{
tyblue "----------- 请打开\"https://${domain_list[$1]}\"修改Cloudreve站点信息 ---------"
tyblue " 1. 登陆帐号"
tyblue " 2. 右上角头像 -> 管理面板"
tyblue " 3. 左侧的参数设置 -> 站点信息"
tyblue " 4. 站点URL改为\"https://${domain_list[$1]}\" -> 往下拉点击保存"
sleep 15s
echo -e "\\n\\n"
tyblue "按两次回车键以继续。。。"
read -s
read -s
}
init_cloudreve()
{
local temp
temp="$(timeout 5s $cloudreve_prefix/cloudreve | grep "初始管理员密码:" | awk '{print $4}')"
sleep 1s
systemctl --now enable cloudreve
tyblue "-------- 请打开\"https://${domain_list[$1]}\"进行Cloudreve初始化 -------"
tyblue " 1. 登陆帐号"
purple " 初始管理员账号:[email protected]"
purple " $temp"
tyblue " 2. 右上角头像 -> 管理面板"
tyblue " 3. 这时会弹出对话框 \"确定站点URL设置\" 选择 \"更改\""
tyblue " 4. 左侧参数设置 -> 注册与登陆 -> 不允许新用户注册 -> 往下拉点击保存"
sleep 15s
echo -e "\\n\\n"
tyblue "按两次回车键以继续。。。"
read -s
read -s
}
ask_if()
{
local choice=""
while [ "$choice" != "y" ] && [ "$choice" != "n" ]
do
tyblue "$1"
read choice
done
[ $choice == y ] && return 0
return 1
}
#卸载函数
remove_xray()
{
if ! bash <(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh) remove --purge; then
systemctl --now disable xray
rm -rf /usr/local/bin/xray
rm -rf /usr/local/etc/xray
rm -rf /etc/systemd/system/xray.service
rm -rf /etc/systemd/system/[email protected]
rm -rf /var/log/xray
systemctl daemon-reload
fi
xray_is_installed=0
is_installed=0
}
remove_nginx()
{
systemctl --now disable nginx
rm -rf $nginx_service
systemctl daemon-reload
rm -rf ${nginx_prefix}
nginx_is_installed=0
is_installed=0
}
remove_php()
{
systemctl --now disable php-fpm
rm -rf $php_service
systemctl daemon-reload
rm -rf ${php_prefix}
php_is_installed=0
}
remove_cloudreve()
{
systemctl --now disable cloudreve
rm -rf $cloudreve_service
systemctl daemon-reload
rm -rf ${cloudreve_prefix}
cloudreve_is_installed=0
}
#备份域名伪装网站
backup_domains_web()
{
local i
mkdir "${temp_dir}/domain_backup"
for i in ${!true_domain_list[@]}
do
if [ "$1" == "cp" ]; then
cp -rf ${nginx_prefix}/html/${true_domain_list[$i]} "${temp_dir}/domain_backup" 2>/dev/null
else
mv ${nginx_prefix}/html/${true_domain_list[$i]} "${temp_dir}/domain_backup" 2>/dev/null
fi
done
}
#获取配置信息
get_config_info()
{
[ $is_installed -eq 0 ] && return
local temp
if grep -q '"network"[ '$'\t]*:[ '$'\t]*"ws"' $xray_config; then
if [[ "$(grep -E '"protocol"[ '$'\t]*:[ '$'\t]*"(vmess|vless)"' $xray_config | tail -n 1)" =~ \"vmess\" ]]; then
protocol_3=2
else
protocol_3=1
fi
path="$(grep '"path"' $xray_config | tail -n 1 | cut -d : -f 2 | cut -d \" -f 2)"
xid_3="$(grep '"id"' $xray_config | tail -n 1 | cut -d : -f 2 | cut -d \" -f 2)"
else
protocol_3=0
fi
if grep -q '"network"[ '$'\t]*:[ '$'\t]*"grpc"' $xray_config; then
if [ $protocol_3 -ne 0 ]; then
temp=2
else
temp=1
fi
if [[ "$(grep -E '"protocol"[ '$'\t]*:[ '$'\t]*"(vmess|vless)"' $xray_config | tail -n $temp | head -n 1)" =~ \"vmess\" ]]; then
protocol_2=2
else
protocol_2=1
fi
serviceName="$(grep '"serviceName"' $xray_config | cut -d : -f 2 | cut -d \" -f 2)"
xid_2="$(grep '"id"' $xray_config | tail -n $temp | head -n 1 | cut -d : -f 2 | cut -d \" -f 2)"
else
protocol_2=0
fi
temp=1
[ $protocol_2 -ne 0 ] && ((temp++))
[ $protocol_3 -ne 0 ] && ((temp++))
if [ $(grep -c '"clients"' $xray_config) -eq $temp ]; then
protocol_1=1
xid_1="$(grep '"id"' $xray_config | head -n 1 | cut -d : -f 2 | cut -d \" -f 2)"
else
protocol_1=0
fi
unset domain_list
unset true_domain_list
unset domain_config_list
unset pretend_list
domain_list=($(grep "^#domain_list=" $nginx_config | cut -d = -f 2))
true_domain_list=($(grep "^#true_domain_list=" $nginx_config | cut -d = -f 2))
domain_config_list=($(grep "^#domain_config_list=" $nginx_config | cut -d = -f 2))
pretend_list=($(grep "^#pretend_list=" $nginx_config | cut -d = -f 2))
}
#删除所有域名
remove_all_domains()
{
systemctl stop xray
systemctl stop nginx
systemctl --now disable php-fpm
systemctl --now disable cloudreve
local i
for i in ${!true_domain_list[@]}
do
rm -rf ${nginx_prefix}/html/${true_domain_list[$i]}
done
rm -rf "${nginx_prefix}/certs"
mkdir "${nginx_prefix}/certs"
$HOME/.acme.sh/acme.sh --uninstall
rm -rf $HOME/.acme.sh
curl https://get.acme.sh | sh
$HOME/.acme.sh/acme.sh --upgrade --auto-upgrade
unset domain_list
unset true_domain_list
unset domain_config_list
unset pretend_list
}
check_base_command
if [[ ! -f '/etc/os-release' ]]; then
red "系统版本太老,Xray官方脚本不支持"
exit 1
fi
if [[ -f /.dockerenv ]] || grep -q 'docker\|lxc' /proc/1/cgroup && [[ "$(type -P systemctl)" ]]; then
true
elif [[ -d /run/systemd/system ]] || grep -q systemd <(ls -l /sbin/init); then
true
else
red "仅支持使用systemd的系统!"
exit 1
fi
if [[ ! -d /dev/shm ]]; then
red "/dev/shm不存在,不支持的系统"
exit 1
fi
if [[ "$(type -P apt)" ]]; then
if [[ "$(type -P dnf)" ]] || [[ "$(type -P yum)" ]]; then
red "同时存在apt和yum/dnf"
red "不支持的系统!"
exit 1
fi
release="other-debian"
debian_package_manager="apt"
redhat_package_manager="true"
elif [[ "$(type -P dnf)" ]]; then
release="other-redhat"
redhat_package_manager="dnf"
debian_package_manager="true"
elif [[ "$(type -P yum)" ]]; then
release="other-redhat"
redhat_package_manager="yum"
debian_package_manager="true"
else
red "apt yum dnf命令均不存在"
red "不支持的系统"
exit 1
fi
if [ "$EUID" != "0" ]; then
red "请用root用户运行此脚本!!"
exit 1
fi
if ! check_sudo; then
yellow "检测到正在使用sudo!"
yellow "acme.sh不支持sudo,请使用root用户运行此脚本"
tyblue "详情请见:https://github.com/acmesh-official/acme.sh/wiki/sudo"
exit 1
fi
[ -e $nginx_config ] && nginx_is_installed=1 || nginx_is_installed=0
[ -e ${php_prefix}/php-fpm.service.default ] && php_is_installed=1 || php_is_installed=0
[ -e ${cloudreve_prefix}/cloudreve.db ] && cloudreve_is_installed=1 || cloudreve_is_installed=0
[ -e /usr/local/bin/xray ] && xray_is_installed=1 || xray_is_installed=0
([ $xray_is_installed -eq 1 ] && [ $nginx_is_installed -eq 1 ]) && is_installed=1 || is_installed=0
case "$(uname -m)" in
'amd64' | 'x86_64')
machine='amd64'
;;
'armv5tel' | 'armv6l' | 'armv7' | 'armv7l')
machine='arm'
;;
'armv8' | 'aarch64')
machine='arm64'
;;
*)
machine=''
;;
esac
mem="$(free -m | sed -n 2p | awk '{print $2}')"
mem_total="$(($(free -m | sed -n 2p | awk '{print $2}')+$(free -m | tail -n 1 | awk '{print $2}')))"
[[ "$(free -b | tail -n 1 | awk '{print $2}')" -ne "0" ]] && using_swap=1 || using_swap=0
if [ $is_installed -eq 1 ] && ! grep -q "domain_list=" $nginx_config; then
red "脚本进行了一次不向下兼容的更新"
yellow "请选择 \"重新安装\"选项 来升级"
[ "$1" == "--update" ] && exit 1
sleep 3s
fi
if [ $is_installed -eq 1 ] && ! grep -q "# This file has been edited by Xray-TLS-Web setup script" /etc/systemd/system/xray.service && ! [ "$1" == "--update" ]; then
red "脚本进行了一次不向下兼容的更新"
yellow "请选择 \"更新Xray\"选项 来升级"
sleep 3s
fi
#获取系统版本信息
get_system_info()
{
local temp_release
temp_release="$(lsb_release -i -s | tr "[:upper:]" "[:lower:]")"
if [[ "$temp_release" =~ ubuntu ]]; then
release="ubuntu"
elif [[ "$temp_release" =~ debian ]]; then
release="debian"
elif [[ "$temp_release" =~ deepin ]]; then
release="deepin"
elif [[ "$temp_release" =~ centos ]]; then
release="centos"
elif [[ "$temp_release" =~ (redhatenterprise|rhel) ]]; then
release="rhel"
elif [[ "$temp_release" =~ fedora ]]; then
release="fedora"
fi
systemVersion="$(lsb_release -r -s)"
}
#检查CentOS8 epel源是否安装
check_centos8_epel()
{
if [ $release == "centos" ] && version_ge "$systemVersion" "8"; then
if $redhat_package_manager --help | grep -qw "\\-\\-all"; then
local temp_command="$redhat_package_manager --all repolist"
else
local temp_command="$redhat_package_manager repolist all"
fi
if ! $temp_command | awk '{print $1}' | grep -q epel; then
check_important_dependence_installed "" "epel-release"
fi
fi
}
#检查80端口和443端口是否被占用
check_port()
{
green "正在检查端口占用。。。"
local xray_status=0
local nginx_status=0
systemctl -q is-active xray && xray_status=1 && systemctl stop xray
systemctl -q is-active nginx && nginx_status=1 && systemctl stop nginx
([ $xray_status -eq 1 ] || [ $nginx_status -eq 1 ]) && sleep 2s
local check_list=('80' '443')
local i
for i in ${!check_list[@]}
do
if netstat -tuln | awk '{print $4}' | awk -F : '{print $NF}' | grep -E "^[0-9]+$" | grep -wq "${check_list[$i]}"; then
red "${check_list[$i]}端口被占用!"
yellow "请用 lsof -i:${check_list[$i]} 命令检查"
exit 1
fi
done
[ $xray_status -eq 1 ] && systemctl start xray
[ $nginx_status -eq 1 ] && systemctl start nginx
}
#检查Nginx是否已通过apt/dnf/yum安装
check_nginx_installed_system()
{
if [[ ! -f /usr/lib/systemd/system/nginx.service ]] && [[ ! -f /lib/systemd/system/nginx.service ]]; then
return 0
fi
red "------------检测到Nginx已安装,并且会与此脚本冲突------------"
yellow " 如果您不记得之前有安装过Nginx,那么可能是使用别的一键脚本时安装的"
yellow " 建议使用纯净的系统运行此脚本"
echo
! ask_if "是否尝试卸载?(y/n)" && exit 0
$debian_package_manager -y purge nginx
$redhat_package_manager -y remove nginx
if [[ ! -f /usr/lib/systemd/system/nginx.service ]] && [[ ! -f /lib/systemd/system/nginx.service ]]; then
return 0
fi
red "卸载失败!"
yellow "请尝试更换系统,建议使用Ubuntu最新版系统"
green "欢迎进行Bug report(https://github.com/kirin10000/Xray-script/issues),感谢您的支持"
exit 1
}
#检查SELinux
check_SELinux()
{
turn_off_selinux()
{
check_important_dependence_installed selinux-utils libselinux-utils
setenforce 0
sed -i 's/^[ \t]*SELINUX[ \t]*=[ \t]*enforcing[ \t]*$/SELINUX=disabled/g' /etc/sysconfig/selinux
$redhat_package_manager -y remove libselinux-utils
$debian_package_manager -y purge selinux-utils
}
if getenforce 2>/dev/null | grep -wqi Enforcing || grep -Eq '^[ '$'\t]*SELINUX[ '$'\t]*=[ '$'\t]*enforcing[ '$'\t]*$' /etc/sysconfig/selinux 2>/dev/null; then
yellow "检测到SELinux已开启,脚本可能无法正常运行"
if ask_if "尝试关闭SELinux?(y/n)"; then
turn_off_selinux
else
exit 0
fi
fi
}
#配置sshd
check_ssh_timeout()
{
if grep -q "#This file has been edited by Xray-TLS-Web-setup-script" /etc/ssh/sshd_config; then
return 0
fi
echo -e "\\n\\n\\n"
tyblue "------------------------------------------"
tyblue " 安装可能需要比较长的时间(5-40分钟)"
tyblue " 如果中途断开连接将会很麻烦"
tyblue " 设置ssh连接超时时间将有效降低断连可能性"
echo
! ask_if "是否设置ssh连接超时时间?(y/n)" && return 0
sed -i '/^[ \t]*ClientAliveInterval[ \t]/d' /etc/ssh/sshd_config
sed -i '/^[ \t]*ClientAliveCountMax[ \t]/d' /etc/ssh/sshd_config
echo >> /etc/ssh/sshd_config
echo "ClientAliveInterval 30" >> /etc/ssh/sshd_config
echo "ClientAliveCountMax 60" >> /etc/ssh/sshd_config
echo "#This file has been edited by Xray-TLS-Web-setup-script" >> /etc/ssh/sshd_config
systemctl restart sshd
green "----------------------配置完成----------------------"
tyblue " 请重新连接服务器以让配置生效"
if [ $in_install_update_xray_tls_web -eq 1 ]; then
yellow " 重新连接服务器后,请再次运行脚本完成 Xray-TLS+Web 剩余部分的安装/升级"
yellow " 再次运行脚本时,重复之前选过的选项即可"
yellow " 按回车键退出。。。。"
read -s
fi
exit 0
}
#删除防火墙和阿里云盾
uninstall_firewall()
{
green "正在删除防火墙。。。"
ufw disable
$debian_package_manager -y purge firewalld
$debian_package_manager -y purge ufw
systemctl stop firewalld
systemctl disable firewalld
$redhat_package_manager -y remove firewalld
green "正在删除阿里云盾和腾讯云盾 (仅对阿里云和腾讯云服务器有效)。。。"
#阿里云盾
pkill -9 assist_daemon
rm -rf /usr/local/share/assist-daemon
systemctl stop CmsGoAgent
systemctl disable CmsGoAgent
systemctl stop cloudmonitor
/etc/rc.d/init.d/cloudmonitor remove
rm -rf /usr/local/cloudmonitor
rm -rf /etc/systemd/system/CmsGoAgent.service
systemctl daemon-reload
#aliyun-assist
systemctl stop AssistDaemon
systemctl disable AssistDaemon
systemctl stop aliyun
systemctl disable aliyun
$debian_package_manager -y purge aliyun-assist
$redhat_package_manager -y remove aliyun_assist
rm -rf /usr/local/share/aliyun-assist
rm -rf /usr/sbin/aliyun_installer
rm -rf /usr/sbin/aliyun-service
rm -rf /usr/sbin/aliyun-service.backup
rm -rf /etc/systemd/system/aliyun.service
rm -rf /etc/systemd/system/AssistDaemon.service
systemctl daemon-reload
#AliYunDun aegis
pkill -9 AliYunDunUpdate
pkill -9 AliYunDun
pkill -9 AliHids
/etc/init.d/aegis uninstall
rm -rf /usr/local/aegis
rm -rf /etc/init.d/aegis
rm -rf /etc/rc2.d/S80aegis
rm -rf /etc/rc3.d/S80aegis
rm -rf /etc/rc4.d/S80aegis
rm -rf /etc/rc5.d/S80aegis
#腾讯云盾
/usr/local/qcloud/stargate/admin/uninstall.sh
/usr/local/qcloud/YunJing/uninst.sh
/usr/local/qcloud/monitor/barad/admin/uninstall.sh
systemctl daemon-reload
systemctl stop YDService
systemctl disable YDService
rm -rf /lib/systemd/system/YDService.service
systemctl daemon-reload
sed -i 's#/usr/local/qcloud#rcvtevyy4f5d#g' /etc/rc.local
sed -i '/rcvtevyy4f5d/d' /etc/rc.local
rm -rf $(find /etc/udev/rules.d -iname "*qcloud*" 2>/dev/null)
pkill -9 YDService
pkill -9 YDLive
pkill -9 sgagent
pkill -9 tat_agent
pkill -9 /usr/local/qcloud
pkill -9 barad_agent
kill -s 9 "$(ps -aux | grep '/usr/local/qcloud/nv//nv_driver_install_helper\.sh' | awk '{print $2}')"
rm -rf /usr/local/qcloud
rm -rf /usr/local/yd.socket.client
rm -rf /usr/local/yd.socket.server
mkdir /usr/local/qcloud
mkdir /usr/local/qcloud/action
mkdir /usr/local/qcloud/action/login_banner.sh
mkdir /usr/local/qcloud/action/action.sh
if [[ "$(type -P uname)" ]] && uname -a | grep solaris >/dev/null; then
crontab -l | sed "/qcloud/d" | crontab --
else
crontab -l | sed "/qcloud/d" | crontab -
fi
}
#升级系统组件
doupdate()
{
updateSystem()
{
if ! [[ "$(type -P do-release-upgrade)" ]]; then
if ! $debian_package_manager -y --no-install-recommends install ubuntu-release-upgrader-core; then
$debian_package_manager update
if ! $debian_package_manager -y --no-install-recommends install ubuntu-release-upgrader-core; then
red "脚本出错!"
yellow "按回车键继续或者Ctrl+c退出"
read -s
fi
fi
fi
echo -e "\\n\\n\\n"
tyblue "------------------请选择升级系统版本--------------------"
tyblue " 1.最新beta版(现在是21.04)(2020.11)"
tyblue " 2.最新发行版(现在是20.10)(2020.11)"
tyblue " 3.最新LTS版(现在是20.04)(2020.11)"
tyblue "-------------------------版本说明-------------------------"
tyblue " beta版:即测试版"
tyblue " 发行版:即稳定版"
tyblue " LTS版:长期支持版本,可以理解为超级稳定版"
tyblue "-------------------------注意事项-------------------------"
yellow " 1.升级过程中遇到问话/对话框,如果不明白,选择yes/y/第一个选项"
yellow " 2.升级系统可能需要15分钟或更久"
yellow " 3.有的时候不能一次性更新到所选择的版本,可能要更新多次"
yellow " 4.升级系统后以下配置可能会恢复系统默认配置:"
yellow " ssh端口 ssh超时时间 bbr加速(恢复到关闭状态)"
tyblue "----------------------------------------------------------"
green " 您现在的系统版本是:$systemVersion"
tyblue "----------------------------------------------------------"
echo
choice=""
while [ "$choice" != "1" ] && [ "$choice" != "2" ] && [ "$choice" != "3" ]
do
read -p "您的选择是:" choice
done
if ! [[ "$(grep -i '^[ '$'\t]*port[ '$'\t]' /etc/ssh/sshd_config | awk '{print $2}')" =~ ^("22"|)$ ]]; then
red "检测到ssh端口号被修改"
red "升级系统后ssh端口号可能恢复默认值(22)"
yellow "按回车键继续。。。"
read -s
fi
if [ $in_install_update_xray_tls_web -eq 1 ]; then
echo
tyblue "提示:即将开始升级系统"
yellow " 升级完系统后服务器将重启,重启后,请再次运行脚本完成 Xray-TLS+Web 剩余部分的安装/升级"
yellow " 再次运行脚本时,重复之前选过的选项即可"
echo
sleep 2s
yellow "按回车键以继续。。。"
read -s
fi
local i
for ((i=0;i<2;i++))
do
sed -i '/^[ \t]*Prompt[ \t]*=/d' /etc/update-manager/release-upgrades
echo 'Prompt=normal' >> /etc/update-manager/release-upgrades
case "$choice" in
1)
do-release-upgrade -d
do-release-upgrade -d
sed -i 's/Prompt=normal/Prompt=lts/' /etc/update-manager/release-upgrades
do-release-upgrade -d
do-release-upgrade -d
sed -i 's/Prompt=lts/Prompt=normal/' /etc/update-manager/release-upgrades
do-release-upgrade
do-release-upgrade
sed -i 's/Prompt=normal/Prompt=lts/' /etc/update-manager/release-upgrades
do-release-upgrade
do-release-upgrade
;;
2)
do-release-upgrade
do-release-upgrade
;;
3)
sed -i 's/Prompt=normal/Prompt=lts/' /etc/update-manager/release-upgrades
do-release-upgrade
do-release-upgrade
;;
esac
if ! version_ge "$systemVersion" 20.04; then
sed -i 's/Prompt=lts/Prompt=normal/' /etc/update-manager/release-upgrades
do-release-upgrade
do-release-upgrade
fi
$debian_package_manager update
$debian_package_manager -y --auto-remove --purge full-upgrade
done
}
while ((1))
do
echo -e "\\n\\n\\n"
tyblue "-----------------------是否更新系统组件?-----------------------"
green " 1. 更新已安装软件,并升级系统 (Ubuntu专享)"
green " 2. 仅更新已安装软件"
red " 3. 不更新"
if [ "$release" == "ubuntu" ] && ((mem<400)); then
red "检测到内存过小,升级系统可能导致无法开机,请谨慎选择"
fi
echo
choice=""
while [ "$choice" != "1" ] && [ "$choice" != "2" ] && [ "$choice" != "3" ]
do
read -p "您的选择是:" choice
done
if [ "$release" == "ubuntu" ] || [ $choice -ne 1 ]; then
break
fi
echo
yellow " 更新系统仅支持Ubuntu!"
sleep 3s
done
if [ $choice -eq 1 ]; then
updateSystem
$debian_package_manager -y --purge autoremove
$debian_package_manager clean
elif [ $choice -eq 2 ]; then
tyblue "-----------------------即将开始更新-----------------------"
yellow " 更新过程中遇到问话/对话框,如果不明白,选择yes/y/第一个选项"
yellow " 按回车键继续。。。"
read -s
$redhat_package_manager -y autoremove
$redhat_package_manager -y update
$debian_package_manager update
$debian_package_manager -y --auto-remove --purge full-upgrade
$debian_package_manager -y --purge autoremove
$debian_package_manager clean
$redhat_package_manager -y autoremove
$redhat_package_manager clean all
fi
}
#安装bbr
install_bbr()
{
#输出:latest_kernel_version 和 your_kernel_version
get_kernel_info()
{
green "正在获取最新版本内核版本号。。。。(60内秒未获取成功自动跳过)"
your_kernel_version="$(uname -r | cut -d - -f 1)"
while [ ${your_kernel_version##*.} -eq 0 ]
do
your_kernel_version=${your_kernel_version%.*}
done
if ! timeout 60 wget -q -O "temp_kernel_version" "https://kernel.ubuntu.com/~kernel-ppa/mainline/"; then
latest_kernel_version="error"
return 1
fi
local kernel_list=()
local kernel_list_temp
kernel_list_temp=($(awk -F'\"v' '/v[0-9]/{print $2}' "temp_kernel_version" | cut -d '"' -f1 | cut -d '/' -f1 | sort -rV))
if [ ${#kernel_list_temp[@]} -le 1 ]; then
latest_kernel_version="error"
return 1
fi
local i2=0
local i3
local kernel_rc=""
local kernel_list_temp2
while ((i2<${#kernel_list_temp[@]}))
do
if [[ "${kernel_list_temp[$i2]}" =~ -rc(0|[1-9][0-9]*)$ ]] && [ "$kernel_rc" == "" ]; then
kernel_list_temp2=("${kernel_list_temp[$i2]}")
kernel_rc="${kernel_list_temp[$i2]%-*}"
((i2++))
elif [[ "${kernel_list_temp[$i2]}" =~ -rc(0|[1-9][0-9]*)$ ]] && [ "${kernel_list_temp[$i2]%-*}" == "$kernel_rc" ]; then
kernel_list_temp2+=("${kernel_list_temp[$i2]}")
((i2++))
elif [[ "${kernel_list_temp[$i2]}" =~ -rc(0|[1-9][0-9]*)$ ]] && [ "${kernel_list_temp[$i2]%-*}" != "$kernel_rc" ]; then
for((i3=0;i3<${#kernel_list_temp2[@]};i3++))
do
kernel_list+=("${kernel_list_temp2[$i3]}")
done
kernel_rc=""
elif [ -z "$kernel_rc" ] || version_ge "${kernel_list_temp[$i2]}" "$kernel_rc"; then
kernel_list+=("${kernel_list_temp[$i2]}")
((i2++))
else
for((i3=0;i3<${#kernel_list_temp2[@]};i3++))
do
kernel_list+=("${kernel_list_temp2[$i3]}")
done
kernel_rc=""
fi
done
if [ -n "$kernel_rc" ]; then
for((i3=0;i3<${#kernel_list_temp2[@]};i3++))
do
kernel_list+=("${kernel_list_temp2[$i3]}")
done
fi
latest_kernel_version="${kernel_list[0]}"
if [ $release == "ubuntu" ] || [ $release == "debian" ] || [ $release == "deepin" ] || [ $release == "other-debian" ]; then
local rc_version
rc_version="$(uname -r | cut -d - -f 2)"
if [[ $rc_version =~ rc ]]; then
rc_version="${rc_version##*'rc'}"
your_kernel_version="${your_kernel_version}-rc${rc_version}"
fi
uname -r | grep -q xanmod && your_kernel_version="${your_kernel_version}-xanmod"
else
latest_kernel_version="${latest_kernel_version%%-*}"
fi
}
#卸载多余内核
remove_other_kernel()
{
local exit_code=1
if [ $release == "ubuntu" ] || [ $release == "debian" ] || [ $release == "deepin" ] || [ $release == "other-debian" ]; then
dpkg --list > "temp_installed_list"
local kernel_list_image
kernel_list_image=($(awk '{print $2}' "temp_installed_list" | grep '^linux-image'))
local kernel_list_modules
kernel_list_modules=($(awk '{print $2}' "temp_installed_list" | grep '^linux-modules'))
local kernel_now