/
Xray-TLS+Web-setup.sh
3100 lines (3012 loc) · 125 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
#system message
#Instruction Set
machine=""
#What system
release=""
#System version number
systemVersion=""
redhat_version=""
debian_package_manager=""
redhat_package_manager=""
#Physical memory size
mem=""
#Physical memory + swap size before running the script
mem_total=""
#Whether swap is enabled before running the script
using_swap=""
#Is there any script to start swap now?
using_swap_now=0
#Installation Information
nginx_version="nginx-1.19.6"
openssl_version="openssl-openssl-3.0.0-alpha11"
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.1"
php_prefix="/usr/local/php"
php_service="/etc/systemd/system/php-fpm.service"
php_is_installed=""
cloudreve_version="3.2.1"
cloudreve_prefix="/usr/local/cloudreve"
cloudreve_service="/etc/systemd/system/cloudreve.service"
cloudreve_is_installed=""
nextcloud_url="https://download.nextcloud.com/server/prereleases/nextcloud-21.0.0beta8.zip"
xray_config="/usr/local/etc/xray/config.json"
xray_is_installed=""
temp_dir="/temp_install_update_xray_tls_web"
is_installed=""
update=""
#Configuration Information
#Domain name list Two lists are used to distinguish www.main domain name
unset domain_list
unset true_domain_list
unset domain_config_list
#Domain name disguise list, corresponding domain name list
unset pretend_list
#Xray-TCP-TLS protocol used, 0 means disabled, 1 means VLESS
protocol_1=""
#Xray-WS-TLS protocol used, 0 means disabled, 1 means VLESS, 2 means VMess
protocol_2=""
path=""
xid_1=""
xid_2=""
#Functional function:
#Define a few colors
purple() #Keel 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() #Duck feces yellow
{
echo -e "\\033[33;1m${*}\\033[0m"
}
red() #Aunt Red
{
echo -e "\\033[31;1m${*}\\033[0m"
}
#Check basic commands
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 "command\"${temp_command_list[$i]}\"not found"
red "Not a standard Linux system"
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 comparison function
version_ge()
{
test "$(echo -e "$1\\n$2" | sort -rV | head -n 1)" == "$1"
}
#Install a single important dependency
check_important_dependence_installed()
{
if [$release == "ubuntu"] || [$release == "other-debian" ]; then
if dpkg -s "$1"> /dev/null 2>&1; then
apt-mark manual "$1"
elif! $debian_package_manager -y --no-install-recommends install "$1"; then
$debian_package_manager update
if! $debian_package_manager -y --no-install-recommends install "$1"; then
red "Important component\"$1\"installation failed!!!"
yellow "Press Enter to continue or Ctrl+c to exit"
read -s
fi
fi
else
if rpm -q "$2"> /dev/null 2>&1; then
if ["$redhat_package_manager" == "dnf" ]; then
dnf mark install "$2"
else
yumdb set reason user "$2"
fi
elif! $redhat_package_manager -y install "$2"; then
red "Important component\"$2\"installation failed!!!"
yellow "Press Enter to continue or Ctrl+c to exit"
read -s
fi
fi
}
#Installation dependencies
install_dependence()
{
if [$release == "ubuntu"] || [$release == "other-debian" ]; then
if! $debian_package_manager -y --no-install-recommends install "$@"; then
$debian_package_manager update
if! $debian_package_manager -y --no-install-recommends install "$@"; then
yellow "Dependency installation failed!!!"
green "Welcome to a bug report (https://github.com/kirin10000/Xray-script/issues), thank you for your support"
yellow "Press Enter to continue or Ctrl+c to exit"
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 "Dependency installation failed!!!"
green "Welcome to a bug report (https://github.com/kirin10000/Xray-script/issues), thank you for your support"
yellow "Press Enter to continue or Ctrl+c to exit"
read -s
fi
fi
}
#Enter working directory
enter_temp_dir()
{
rm -rf "$temp_dir"
mkdir "$temp_dir"
cd "$temp_dir"
}
#Check if php is required
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
}
#Check if cloudreve is needed
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
}
#Check for Nginx updates
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
}
#Check php update
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 "An error occurred when opening swap"
green "Welcome to a bug report (https://github.com/kirin10000/Xray-script/issues), thank you for your support"
yellow "Press Enter to continue or Ctrl+c to exit"
read -s
fi
if [$mem_total -lt $1 ]; then
tyblue "Insufficient memory of $1M, automatically apply for 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 "Failed to open swap!"
yellow "Maybe the machine's memory and hard disk space are insufficient"
green "Welcome to a bug report (https://github.com/kirin10000/Xray-script/issues), thank you for your support"
yellow "Press Enter to continue or Ctrl+c to exit"
read -s
fi
fi
}
swap_off()
{
if [ $using_swap_now -eq 1 ]; then
tyblue "正在恢复swap。。。"
swapoff -a
[ $using_swap -ne 0 ] && swapon -a
using_swap_now=0
fi
}
#Enable/disable 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 "----------- Please open \"https://${domain_list[$1]}\"Modify Cloudreve site information ---------"
tyblue "1. Login account"
tyblue "2. Picture in the upper right corner -> Management Panel"
tyblue "3. Parameter settings on the left -> site information"
tyblue "4. Change the site URL to \"https://${domain_list[$1]}\" -> pull down and click save"
sleep 15s
echo -e "\\n\\n"
tyblue "Press Enter twice to continue..."
read -s
read -s
}
init_cloudreve()
{
local temp
temp="$(timeout 5s $cloudreve_prefix/cloudreve | grep "Initial administrator password:" | awk'{print $4}')"
sleep 1s
systemctl --now enable cloudreve
tyblue "-------- Please open \"https://${domain_list[$1]}\" to initialize Cloudreve -------"
tyblue "1. Login account"
purple "Initial administrator account: admin@cloudreve.org"
purple "$temp"
tyblue "2. Picture in the upper right corner -> Management Panel"
tyblue "3. At this time a dialog box will pop up \"Confirm Site URL Settings\" Select \"Change\""
tyblue "4. Parameter settings on the left -> Registration and login -> New user registration is not allowed -> drop down and click save"
sleep 15s
echo -e "\\n\\n"
tyblue "Press Enter twice to continue..."
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
}
#Uninstall function
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/xray@.service
rm -rf /var/log/xray
systemctl daemon-reload
fi
}
remove_nginx()
{
systemctl --now disable nginx
rm -rf $nginx_service
systemctl daemon-reload
rm -rf ${nginx_prefix}
nginx_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 domain name disguise website
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 configuration information
get_config_info()
{
[ $is_installed -eq 0 ] && return
if [ $(grep -c '"clients"' $xray_config) -eq 2 ] || [ $(grep -Ec '"(vmess|vless)"' $xray_config) -eq 1 ]; then
protocol_1=1
xid_1=$(grep '"id"' $xray_config | head -n 1 | cut -d : -f 2)
xid_1=${xid_1#*'"'}
xid_1=${xid_1%'"'*}
else
protocol_1=0
xid_1=""
fi
if [ $(grep -Ec '"(vmess|vless)"' $xray_config) -eq 2 ]; then
grep -q '"vmess"' $xray_config && protocol_2=2 || protocol_2=1
path=$(grep '"path"' $xray_config | head -n 1 | cut -d : -f 2)
path=${path#*'"'}
path=${path%'"'*}
xid_2=$(grep '"id"' $xray_config | tail -n 1 | cut -d : -f 2)
xid_2=${xid_2#*'"'}
xid_2=${xid_2%'"'*}
else
protocol_2=0
path=""
xid_2=""
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))
}
#Delete all domains
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 [ "$EUID" != "0" ]; then
red "Please run this script as the root user!!!"
exit 1
fi
if ! check_sudo; then
red "--------------------------- sudo is detected in use ---------------- -----------"
yellow "acme.sh does not support sudo, please use root user to run this script"
tyblue "For details, please see: https://github.com/acmesh-official/acme.sh/wiki/sudo"
exit 1
fi
if [[! -f'/etc/os-release' ]]; then
red "The system version is too old, Xray official script does not support"
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 "Only systems that use systemd are supported!"
exit 1
fi
if [[! -d /dev/shm ]]; then
red "/dev/shm does not exist, unsupported system"
exit 1
fi
if [[ "$(type -P apt)" ]]; then
if [[ "$(type -P dnf)" ]] || [[ "$(type -P yum)" ]]; then
red "Apt and yum/dnf exist at the same time"
red "Unsupported system!"
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 "No apt yum dnf commands exist"
red "Unsupported system"
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 "The script has been updated for an incompatible downward compatibility"
yellow "Please select \"Reinstall\" to upgrade"
sleep 3s
fi
#Get system version information
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" =~ centos ]]; then
release="centos"
elif [[ "$temp_release" =~ fedora ]]; then
release="fedora"
fi
systemVersion="$(lsb_release -r -s)"
if [ $release == "fedora" ]; then
if version_ge "$systemVersion" 30; then
redhat_version=8
elif version_ge "$systemVersion" 19; then
redhat_version=7
elif version_ge "$systemVersion" 12; then
redhat_version=6
else
redhat_version=5
fi
else
redhat_version="$systemVersion"
fi
}
#Check if port 80 and port 443 are occupied
check_port()
{
green "Checking port occupancy..."
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]} port is occupied!"
yellow "Please use lsof -i:${check_list[$i]} command to check"
exit 1
fi
done
[$xray_status -eq 1] && systemctl start xray
[$nginx_status -eq 1] && systemctl start nginx
}
#Check whether Nginx has been installed via 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 "------------ It is detected that Nginx is installed and will conflict with this script------------"
yellow "If you don't remember that Nginx was installed before, it may have been installed using another one-click script"
yellow "It is recommended to use a clean system to run this script"
echo
! ask_if "Try uninstalling? (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 "Uninstallation failed!"
yellow "Please try to change the system, it is recommended to use the latest version of Ubuntu system"
green "Welcome to a bug report (https://github.com/kirin10000/Xray-script/issues), thank you for your support"
exit 1
}
#Check 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 has been detected, the script may not run properly"
if ask_if "Try to close 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 "Installation may take a long time (5-40 minutes)"
tyblue "It will be troublesome if you disconnect halfway"
tyblue "Setting the ssh connection timeout will effectively reduce the possibility of disconnection"
echo
! ask_if "Whether to set the ssh connection timeout time? (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 "----------------------Configuration complete----------------------"
tyblue "Please re-connect to ssh (ie re-login to the server) and run this script again"
yellow "Press Enter to exit..."
read -s
exit 0
}
#Delete firewall and Alibaba Cloud Shield
uninstall_firewall()
{
green "The firewall is being deleted..."
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 "Alibaba Cloud Shield and Tencent Cloud Shield are being deleted (only valid for Alibaba Cloud and Tencent Cloud servers)..."
#Alibaba Cloud
if [ $release == "ubuntu" ] || [ $release == "other-debian" ]; then
systemctl stop CmsGoAgent
systemctl disable CmsGoAgent
rm -rf /usr/local/cloudmonitor
rm -rf /etc/systemd/system/CmsGoAgent.service
systemctl daemon-reload
else
systemctl stop cloudmonitor
/etc/rc.d/init.d/cloudmonitor remove
rm -rf /usr/local/cloudmonitor
systemctl daemon-reload
fi
systemctl stop aliyun
systemctl disable aliyun
rm -rf /etc/systemd/system/aliyun.service
systemctl daemon-reload
$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
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
# Tencent Cloud Shield
/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
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
}
#Upgrade system components
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 "Script error!"
yellow "Press Enter to continue or Ctrl+c to exit"
read -s
fi
fi
fi
echo -e "\\n\\n\\n"
tyblue "------------------Please choose to upgrade the system version--------------------"
tyblue "1. The latest beta version (now 21.04) (2020.11)"
tyblue "2. The latest release (now 20.10) (2020.11)"
tyblue "3. The latest LTS version (now 20.04) (2020.11)"
tyblue "-------------------------Release note--------------------- ----"
tyblue "beta version: the beta version"
tyblue "release version: stable version"
tyblue "LTS version: long-term support version, can be understood as super stable version"
tyblue "-------------------------Cautions--------------------- ----"
yellow "1. Encountered a question/dialog box during the upgrade process, if you don’t understand, select yes/y/the first option"
yellow "2. After the upgrade system is completed, it will restart. After restarting, please run this script again to complete the remaining installation"
yellow "3. It may take 15 minutes or more to upgrade the system"
yellow "4. Sometimes it is not possible to update to the selected version at one time, it may have to be updated multiple times"
yellow "5. The following configuration may restore the system default configuration after upgrading the system:"
yellow "ssh port ssh timeout time bbr acceleration (return to closed state)"
tyblue "------------------------------------------------ ----------"
green "Your current system version is "$systemVersion""
tyblue "------------------------------------------------ ----------"
echo
choice=""
while ["$choice" != "1"] && ["$choice" != "2"] && ["$choice" != "3"]
do
read -p "Your choice is:" choice
done
if! [[ "$(grep -i'^['$'\t]*port '/etc/ssh/sshd_config | awk'{print $2}')" =~ ^("22"|)$ ]] ; then
red "Detected that the ssh port number has been modified"
red "After upgrading the system, the ssh port number may be restored to the default value (22)"
yellow "Press Enter to continue..."
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 "-----------------------Do you want to update the system components?-------------------- ---"
green "1. Update the installed software and upgrade the system (for Ubuntu only)"
green "2. Only update installed software"
red "3. Do not update"
if ["$release" == "ubuntu"] && ((mem<400)); then
red "Detected that the memory is too small, upgrading the system may result in failure to boot, please choose carefully"
fi
echo
choice=""
while ["$choice" != "1"] && ["$choice" != "2"] && ["$choice" != "3"]
do
read -p "Your choice is:" choice
done
if ["$release" == "ubuntu"] || [$choice -ne 1 ]; then
break
fi
echo
yellow "The update system only supports 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 "-----------------------The update will start soon---------------------- -"
yellow "If you encounter a question/dialog box during the update process, if you don’t understand, select yes/y/the first option"
yellow "Press Enter to continue..."
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
}
#Install bbr
install_bbr()
{
#Output: latest_kernel_version and your_kernel_version
get_kernel_info()
{
green "Getting the latest version of the kernel version number... (Skip automatically if it is not obtained within 60 seconds)"
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 -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 == "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
}
#Unload redundant kernel
remove_other_kernel()
{
if [$release == "ubuntu"] || [$release == "other-debian" ]; then
local kernel_list_image
kernel_list_image=($(dpkg --list | awk'{print $2}' | grep'^linux-image'))
local kernel_list_modules
kernel_list_modules=($(dpkg --list | awk'{print $2}' | grep'^linux-modules'))
local kernel_now
kernel_now="$(uname -r)"
local ok_install=0
for ((i=${#kernel_list_image[@]}-1;i>=0;i--))
do
if [[ "${kernel_list_image[$i]}" =~ "$kernel_now" ]]; then
unset'kernel_list_image[$i]'
((ok_install++))
fi
done
if [$ok_install -lt 1 ]; then
red "The kernel in use was not found, it may have been uninstalled, please restart first"
yellow "Press Enter to continue..."
read -s
return 1
fi
for ((i=${#kernel_list_modules[@]}-1;i>=0;i--))
do
if [[ "${kernel_list_modules[$i]}" =~ "$kernel_now" ]]; then
unset'kernel_list_modules[$i]'
fi
done
if [${#kernel_list_modules[@]} -eq 0] && [${#kernel_list_image[@]} -eq 0 ]; then
yellow "No kernel to uninstall"
return 0
fi
$debian_package_manager -y purge "${kernel_list_image[@]}" "${kernel_list_modules[@]}"
apt-mark manual "^grub"
else
local kernel_list
kernel_list=($(rpm -qa |grep '^kernel-[0-9]\|^kernel-ml-[0-9]'))
local kernel_list_devel
kernel_list_devel=($(rpm -qa | grep '^kernel-devel\|^kernel-ml-devel'))
if version_ge "$redhat_version" 8; then
local kernel_list_modules
kernel_list_modules=($(rpm -qa |grep '^kernel-modules\|^kernel-ml-modules'))
local kernel_list_core
kernel_list_core=($(rpm -qa | grep '^kernel-core\|^kernel-ml-core'))
fi
local kernel_now
kernel_now="$(uname -r)"
local ok_install=0
for ((i=${#kernel_list[@]}-1;i>=0;i--))
do
if [[ "${kernel_list[$i]}" =~ "$kernel_now" ]]; then
unset 'kernel_list[$i]'
((ok_install++))
fi
done
if [ $ok_install -lt 1 ]; then
red "The kernel in use was not found, it may have been uninstalled, please restart first"
yellow "Press Enter to continue..."
read -s
return 1
fi
for ((i=${#kernel_list_devel[@]}-1;i>=0;i--))
do
if [[ "${kernel_list_devel[$i]}" =~ "$kernel_now" ]]; then
unset'kernel_list_devel[$i]'
fi
done
if version_ge "$redhat_version" 8; then
ok_install=0
for ((i=${#kernel_list_modules[@]}-1;i>=0;i--))
do
if [[ "${kernel_list_modules[$i]}" =~ "$kernel_now" ]]; then
unset'kernel_list_modules[$i]'
((ok_install++))
fi
done
if [$ok_install -lt 1 ]; then
red "The kernel in use was not found, it may have been uninstalled, please restart first"
yellow "Press Enter to continue..."
read -s
return 1
fi