Repository navigation
Expand file tree
/
Copy pathinstall-release.sh
More file actions
executable file
·1082 lines (1001 loc) · 34 KB
/
Copy pathinstall-release.sh
File metadata and controls
executable file
·1082 lines (1001 loc) · 34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
# The files installed by the script conform to the Filesystem Hierarchy Standard:
# https://wiki.linuxfoundation.org/lsb/fhs
# The URL of the script project is:
# https://github.com/XTLS/Xray-install
# The URL of the script is:
# https://github.com/XTLS/Xray-install/raw/main/install-release.sh
# If the script executes incorrectly, go to:
# https://github.com/XTLS/Xray-install/issues
# You can set this variable whatever you want in shell session right before running this script by issuing:
# export DAT_PATH='/usr/local/share/xray'
DAT_PATH=${DAT_PATH:-/usr/local/share/xray}
# You can set this variable whatever you want in shell session right before running this script by issuing:
# export JSON_PATH='/usr/local/etc/xray'
JSON_PATH=${JSON_PATH:-/usr/local/etc/xray}
# Set this variable only if you are starting Xray with multiple configuration files:
# export JSONS_PATH='/usr/local/etc/xray'
JSONS_PATH=${JSONS_PATH:-}
# Set this variable only if you want this script to check all the systemd unit file:
# export check_all_service_files='yes'
check_all_service_files=${check_all_service_files:-}
red=$'\033[31m'
green=$'\033[32m'
aoi=$'\033[36m'
reset=$'\033[0m'
# Gobal verbals
if [[ -f '/etc/systemd/system/xray.service' ]] && [[ -f '/usr/local/bin/xray' ]]; then
XRAY_IS_INSTALLED_BEFORE_RUNNING_SCRIPT=1
else
XRAY_IS_INSTALLED_BEFORE_RUNNING_SCRIPT=0
fi
# Xray current version
CURRENT_VERSION=''
# Xray latest release version
RELEASE_LATEST=''
# Xray latest prerelease/release version
PRE_RELEASE_LATEST=''
# Xray version will be installed
INSTALL_VERSION=''
# install
INSTALL='0'
# update-geodata
UPDATE_GEODATA='0'
# remove
REMOVE='0'
# help
HELP='0'
# check
CHECK='0'
# --force
FORCE='0'
# --beta
BETA='0'
# --without-geodata
NO_GEODATA='0'
# --without-logfiles
NO_LOGFILES='0'
# --logrotate
LOGROTATE='0'
# --no-update-service
N_UP_SERVICE='0'
# --reinstall
REINSTALL='0'
# --version ?
SPECIFIED_VERSION=''
# --local ?
LOCAL_FILE=''
# --geoip / --geosite ?
LOCAL_GEOIP=''
LOCAL_GEOSITE=''
# --proxy ?
PROXY=''
# --purge
PURGE='0'
# --manually-restart-later
NO_RESTART='0'
# Installation status
GEODATA='0'
CONFIG_NEW='0'
CONFDIR='0'
LOG='0'
LOGROTATE_FIN='0'
LOGROTATE_DIR='0'
SYSTEMD='0'
XRAY_RUNNING='0'
TMP_PATHS=()
cleanup() {
if [[ "${#TMP_PATHS[@]}" -gt '0' ]]; then
"rm" -rf -- "${TMP_PATHS[@]}" || :
fi
}
trap cleanup EXIT
curl() {
if [[ -n "$PROXY" ]]; then
set -- --proxy "$PROXY" "$@"
fi
"$(type -P curl)" -q -L --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
}
systemd_cat_config() {
if systemd-analyze --help | grep -w 'cat-config' >/dev/null; then
systemd-analyze --no-pager cat-config "$@"
echo
else
echo "${aoi}~~~~~~~~~~~~~~~~"
cat "$@" "$1".d/*
echo "${aoi}~~~~~~~~~~~~~~~~"
echo "${red}warning: ${green}The systemd version on the current operating system is too low."
echo "${red}warning: ${green}Please consider to upgrade the systemd or the operating system.${reset}"
echo
fi
}
check_if_running_as_root() {
if [[ "$(id -u)" -eq 0 ]]; then
return 0
else
echo "error: You must run this script as root!"
return 1
fi
}
identify_the_operating_system_and_architecture() {
if [[ "$(uname)" != 'Linux' ]]; then
echo "error: This operating system is not supported."
return 1
fi
case "$(uname -m)" in
'i386' | 'i686')
MACHINE='32'
;;
'amd64' | 'x86_64')
MACHINE='64'
;;
'armv5tel' | 'armv5tejl')
MACHINE='arm32-v5'
;;
'armv6l')
MACHINE='arm32-v6'
grep Features /proc/cpuinfo | grep -w 'vfp' >/dev/null || MACHINE='arm32-v5'
;;
'armv7' | 'armv7l' | 'armv8l')
MACHINE='arm32-v7a'
grep Features /proc/cpuinfo | grep -w 'vfp' >/dev/null || MACHINE='arm32-v5'
;;
'armv8' | 'aarch64')
MACHINE='arm64-v8a'
;;
'loongarch64')
MACHINE='loong64'
;;
'mips')
MACHINE='mips32'
;;
'mipsle')
MACHINE='mips32le'
;;
'mips64')
MACHINE='mips64'
lscpu | grep "Little Endian" >/dev/null && MACHINE='mips64le'
;;
'mips64le')
MACHINE='mips64le'
;;
'ppc64')
MACHINE='ppc64'
;;
'ppc64le')
MACHINE='ppc64le'
;;
'riscv64')
MACHINE='riscv64'
;;
's390x')
MACHINE='s390x'
;;
*)
echo "error: The architecture is not supported."
return 1
;;
esac
if [[ ! -f '/etc/os-release' ]]; then
echo "error: Don't use outdated Linux distributions."
return 1
fi
# Do not combine this judgment condition with the following judgment condition.
## Be aware of Linux distribution like Gentoo, which kernel supports switch between Systemd and OpenRC.
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
echo "error: Only Linux distributions using systemd are supported."
return 1
fi
if [[ "$(type -P apt)" ]]; then
PACKAGE_MANAGEMENT_INSTALL='apt -y --no-install-recommends install'
PACKAGE_MANAGEMENT_REMOVE='apt purge'
elif [[ "$(type -P dnf)" ]]; then
PACKAGE_MANAGEMENT_INSTALL='dnf -y install'
PACKAGE_MANAGEMENT_REMOVE='dnf remove'
elif [[ "$(type -P yum)" ]]; then
PACKAGE_MANAGEMENT_INSTALL='yum -y install'
PACKAGE_MANAGEMENT_REMOVE='yum remove'
elif [[ "$(type -P zypper)" ]]; then
PACKAGE_MANAGEMENT_INSTALL='zypper install -y --no-recommends'
PACKAGE_MANAGEMENT_REMOVE='zypper remove'
elif [[ "$(type -P pacman)" ]]; then
PACKAGE_MANAGEMENT_INSTALL='pacman -Syy --noconfirm'
PACKAGE_MANAGEMENT_REMOVE='pacman -Rsn'
elif [[ "$(type -P emerge)" ]]; then
PACKAGE_MANAGEMENT_INSTALL='emerge -qv'
PACKAGE_MANAGEMENT_REMOVE='emerge -Cv'
else
echo "error: The script does not support the package manager in this operating system."
return 1
fi
}
## Demo function for processing parameters
judgment_parameters() {
local local_install='0'
local temp_version='0'
while [[ "$#" -gt '0' ]]; do
case "$1" in
'install')
INSTALL='1'
;;
'update-geodata')
UPDATE_GEODATA='1'
;;
'remove')
REMOVE='1'
;;
'help')
HELP='1'
;;
'check')
CHECK='1'
;;
'--without-geodata')
NO_GEODATA='1'
;;
'--without-logfiles')
NO_LOGFILES='1'
;;
'--purge')
PURGE='1'
;;
'--version')
if [[ -z "${2:-}" || "$2" == -* ]]; then
echo "error: Please specify the correct version."
return 1
fi
temp_version='1'
SPECIFIED_VERSION="$2"
shift
;;
'-f' | '--force')
FORCE='1'
;;
'--beta')
BETA='1'
;;
'-l' | '--local')
local_install='1'
if [[ -z "${2:-}" || "$2" == -* ]]; then
echo "error: Please specify a local Xray zip file after --local."
return 1
fi
LOCAL_FILE="$2"
shift
;;
'--geoip' | '--geosite')
if [[ -z "${2:-}" || "$2" == -* ]]; then
echo "error: Please specify a local geodata file after $1."
return 1
fi
if [[ "$1" == '--geoip' ]]; then
LOCAL_GEOIP="$2"
else
LOCAL_GEOSITE="$2"
fi
shift
;;
'-p' | '--proxy')
if [[ -z "${2:-}" || "$2" == -* ]]; then
echo "error: Please specify the proxy server address."
return 1
fi
PROXY="$2"
shift
;;
'--reinstall')
REINSTALL='1'
;;
'--no-update-service')
N_UP_SERVICE='1'
;;
'--logrotate')
if [[ ! "${2:-}" =~ ^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$ ]]; then
echo "error: Wrong format of time, it should be in the format of 12:34:56, under 10:00:00 should be start with 0, e.g. 01:23:45."
return 1
fi
LOGROTATE='1'
LOGROTATE_TIME="$2"
shift
;;
'--manually-restart-later')
NO_RESTART='1'
;;
*)
echo "$0: unknown option -- -"
return 1
;;
esac
shift
done
if ((INSTALL + UPDATE_GEODATA + HELP + CHECK + REMOVE == 0)); then
INSTALL='1'
elif ((INSTALL + UPDATE_GEODATA + HELP + CHECK + REMOVE > 1)); then
echo 'You can only choose one action.'
return 1
fi
if [[ "$local_install" -eq '1' && "$INSTALL" -ne '1' ]]; then
echo 'error: --local can only be used with install.'
return 1
fi
if [[ "$INSTALL" -eq '1' ]] && ((temp_version + local_install + REINSTALL + BETA > 1)); then
echo "--version,--reinstall,--beta and --local can't be used together."
return 1
fi
if [[ -n "$LOCAL_GEOIP" || -n "$LOCAL_GEOSITE" ]]; then
if [[ -z "$LOCAL_FILE" ]]; then
echo 'error: --geoip and --geosite can only be used with --local.'
return 1
fi
if [[ "$NO_GEODATA" -eq '1' ]]; then
echo 'error: --geoip and --geosite cannot be used with --without-geodata.'
return 1
fi
fi
if [[ "$INSTALL" -eq '1' ]] && ((FORCE + NO_RESTART > 1)); then
echo "-f/--force and --manually-restart-later can't be used together."
return 1
fi
if [[ "$INSTALL" -eq '1' ]] && ((REINSTALL + NO_RESTART > 1)); then
echo "--reinstall and --manually-restart-later can't be used together."
return 1
fi
}
check_local_xray_version() {
if ! chmod u+x "$TMP_DIRECTORY/xray" || ! "$TMP_DIRECTORY/xray" -version >/dev/null 2>&1; then
echo 'error: Local Xray binary cannot run. Please check its architecture and format.'
return 1
fi
}
prepare_local_geodata() {
if [[ "$NO_GEODATA" -eq '1' ]]; then
return 0
fi
local file source
for file in geoip.dat geosite.dat; do
case "$file" in
geoip.dat) source="${LOCAL_GEOIP:-${TMP_DIRECTORY}/$file}" ;;
geosite.dat) source="${LOCAL_GEOSITE:-${TMP_DIRECTORY}/$file}" ;;
esac
if [[ ! -f "$source" || ! -r "$source" || ! -s "$source" ]]; then
echo "error: Missing, empty or unreadable geodata: $source"
return 1
fi
if [[ "$source" != "${TMP_DIRECTORY}/$file" ]]; then
cp -- "$source" "${TMP_DIRECTORY}/$file"
fi
done
}
install_software() {
package_name="$1"
file_to_detect="$2"
type -P "$file_to_detect" >/dev/null 2>&1 && return
if ${PACKAGE_MANAGEMENT_INSTALL} "$package_name" >/dev/null 2>&1; then
echo "info: $package_name is installed."
else
echo "error: Installation of $package_name failed, please check your network."
exit 1
fi
}
get_current_version() {
# Get the CURRENT_VERSION
if [[ -f '/usr/local/bin/xray' ]]; then
CURRENT_VERSION="$(/usr/local/bin/xray -version | awk 'NR==1 {print $2}')"
CURRENT_VERSION="v${CURRENT_VERSION#v}"
else
CURRENT_VERSION=""
fi
}
get_latest_version() {
# Get Xray latest release version number
local tmp_file
tmp_file="$(mktemp)"
TMP_PATHS+=("$tmp_file")
local url='https://api.github.com/repos/XTLS/Xray-core/releases/latest'
if curl -fsS -o "$tmp_file" -H "Accept: application/vnd.github.v3+json" "$url"; then
echo "get release list success"
else
echo 'error: Failed to get release list, please check your network.'
exit 1
fi
RELEASE_LATEST="$(sed 'y/,/\n/' "$tmp_file" | grep 'tag_name' | awk -F '"' '{print $4}')" || RELEASE_LATEST=''
if [[ -z "$RELEASE_LATEST" ]]; then
if grep -q "API rate limit exceeded" "$tmp_file"; then
echo "error: github API rate limit exceeded"
else
echo "error: Failed to get the latest release version."
echo "Welcome bug report:https://github.com/XTLS/Xray-install/issues"
fi
exit 1
fi
RELEASE_LATEST="v${RELEASE_LATEST#v}"
url='https://api.github.com/repos/XTLS/Xray-core/releases'
if curl -fsS -o "$tmp_file" -H "Accept: application/vnd.github.v3+json" "$url"; then
echo "get release list success"
else
echo 'error: Failed to get release list, please check your network.'
exit 1
fi
local releases_list
readarray -t releases_list < <(sed 'y/,/\n/' "$tmp_file" | grep 'tag_name' | awk -F '"' '{print $4}')
if [[ "${#releases_list[@]}" -eq 0 ]]; then
if grep -q "API rate limit exceeded" "$tmp_file"; then
echo "error: github API rate limit exceeded"
else
echo "error: Failed to get the latest release version."
echo "Welcome bug report:https://github.com/XTLS/Xray-install/issues"
fi
exit 1
fi
local i url_zip
for i in "${!releases_list[@]}"; do
releases_list["$i"]="v${releases_list[$i]#v}"
url_zip="https://github.com/XTLS/Xray-core/releases/download/${releases_list[$i]}/Xray-linux-$MACHINE.zip"
if grep -q "$url_zip" "$tmp_file"; then
PRE_RELEASE_LATEST="${releases_list[$i]}"
break
fi
done
}
version_gt() {
test "$(echo -e "$1\\n$2" | sort -V | head -n 1)" != "$1"
}
download_xray() {
local DOWNLOAD_LINK="https://github.com/XTLS/Xray-core/releases/download/${INSTALL_VERSION}/Xray-linux-${MACHINE}.zip"
echo "Downloading Xray archive: $DOWNLOAD_LINK"
if curl -f -H 'Cache-Control: no-cache' -o "$ZIP_FILE" "$DOWNLOAD_LINK"; then
echo "ok."
else
echo 'error: Download failed! Please check your network or try again.'
return 1
fi
echo "Downloading verification file for Xray archive: ${DOWNLOAD_LINK}.dgst"
if curl -fsS -H 'Cache-Control: no-cache' -o "${ZIP_FILE}.dgst" "${DOWNLOAD_LINK}.dgst"; then
echo "ok."
else
echo 'error: Download failed! Please check your network or try again.'
return 1
fi
if grep 'Not Found' "${ZIP_FILE}.dgst"; then
echo 'error: This version does not support verification. Please replace with another version.'
return 1
fi
# Verification of Xray archive
CHECKSUM=$(awk -F '= ' '/256=/ {print $2}' "${ZIP_FILE}.dgst")
LOCALSUM=$(sha256sum "$ZIP_FILE" | awk '{printf $1}')
if [[ "$CHECKSUM" != "$LOCALSUM" ]]; then
echo 'error: SHA256 check failed! Please check your network or try again.'
return 1
fi
}
decompression() {
if ! unzip -q "$1" -d "$TMP_DIRECTORY"; then
echo 'error: Xray decompression failed.'
exit 1
fi
echo "info: Extract the Xray package to $TMP_DIRECTORY and prepare it for installation."
}
create_xray_user() {
if [[ "$XRAY_IS_INSTALLED_BEFORE_RUNNING_SCRIPT" -eq '0' ]] && id -u xray >/dev/null 2>&1; then
printf '\033[1;31m%s\033[0m\n' \
"WARNING: The 'xray' user already exists and will be reused to run Xray." \
'WARNING: Ensure this user is dedicated to Xray; it will own Xray logs and geodata.' >&2
fi
systemd-sysusers - <<'EOF'
u xray - "Xray" /var/empty
EOF
if [[ "$(id -u xray)" -eq '0' || "$(id -g xray)" -eq '0' ]]; then
echo 'error: The xray user and its primary group must be non-root.'
exit 1
fi
}
install_file() {
NAME="$1"
if [[ "$NAME" == 'xray' ]]; then
install -m 755 -o 0 -g 0 "${TMP_DIRECTORY}/$NAME" "/usr/local/bin/$NAME"
elif [[ "$NAME" == 'geoip.dat' ]] || [[ "$NAME" == 'geosite.dat' ]]; then
install -m 644 -o xray -g xray "${TMP_DIRECTORY}/$NAME" "${DAT_PATH}/$NAME"
fi
}
install_geodata_dir() {
if [[ -d "$DAT_PATH" ]]; then
find "${DAT_PATH%/}" -type d -exec chown xray:xray {} + -exec chmod 755 {} +
find "${DAT_PATH%/}" -type f -exec chown xray:xray {} + -exec chmod 644 {} +
else
install -d -m 755 -o xray -g xray "$DAT_PATH"
fi
}
install_xray() {
# Install Xray binary to /usr/local/bin/ and $DAT_PATH
install_file xray
if [[ "$NO_GEODATA" -eq '0' ]]; then
install_geodata_dir
install_file geoip.dat
install_file geosite.dat
GEODATA='1'
fi
# Install Xray configuration files and maintain existing permissions.
local config_path="${JSONS_PATH:-$JSON_PATH}"
if [[ -d "$config_path" ]]; then
find "${config_path%/}" -type d -exec chown 0:xray {} + -exec chmod 750 {} +
find "${config_path%/}" -type f -name '*.json' -exec chown 0:xray {} + -exec chmod 640 {} +
else
install -d -m 750 -o 0 -g xray "$config_path"
if [[ -z "$JSONS_PATH" ]]; then
install -m 640 -o 0 -g xray /dev/null "${config_path}/config.json"
echo '{}' >"${config_path}/config.json"
CONFIG_NEW='1'
else
for BASE in 00_log 01_api 02_dns 03_routing 04_policy 05_inbounds 06_outbounds 07_stats 08_fakedns 09_metrics 10_observatory 11_geodata 12_env 99_version; do
install -m 640 -o 0 -g xray /dev/null "${config_path}/${BASE}.json"
echo '{}' >"${config_path}/${BASE}.json"
done
CONFDIR='1'
fi
fi
# Used to store Xray log files
if [[ "$NO_LOGFILES" -eq '0' ]]; then
if [[ ! -d '/var/log/xray/' ]]; then
install -d -m 700 -o xray -g xray /var/log/xray/
LOG='1'
else
chown -hR xray:xray /var/log/xray/
chmod 700 /var/log/xray/
fi
fi
}
install_startup_service_file() {
mkdir -p '/etc/systemd/system/xray.service.d'
mkdir -p '/etc/systemd/system/xray@.service.d/'
cat >/etc/systemd/system/xray.service <<EOF
[Unit]
Description=Xray Service
Documentation=https://xtls.github.io/
After=network.target nss-lookup.target
[Service]
User=xray
Group=xray
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
NoNewPrivileges=yes
UMask=0077
ExecStart=/usr/local/bin/xray run -config /usr/local/etc/xray/config.json
Restart=on-failure
RestartPreventExitStatus=23
LimitNPROC=10000
LimitNOFILE=1000000
RuntimeDirectory=xray
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
cat >/etc/systemd/system/xray@.service <<EOF
[Unit]
Description=Xray Service
Documentation=https://xtls.github.io/
After=network.target nss-lookup.target
[Service]
User=xray
Group=xray
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
NoNewPrivileges=yes
UMask=0077
ExecStart=/usr/local/bin/xray run -config /usr/local/etc/xray/%i.json
Restart=on-failure
RestartPreventExitStatus=23
LimitNPROC=10000
LimitNOFILE=1000000
RuntimeDirectory=xray-%i
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
chmod 644 /etc/systemd/system/xray.service /etc/systemd/system/xray@.service
if [[ -n "$JSONS_PATH" ]]; then
"rm" -f '/etc/systemd/system/xray.service.d/10-donot_touch_single_conf.conf' \
'/etc/systemd/system/xray@.service.d/10-donot_touch_single_conf.conf'
echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there.
# Or all changes you made will be lost! # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
[Service]
ExecStart=
ExecStart=/usr/local/bin/xray run -confdir $JSONS_PATH" > \
'/etc/systemd/system/xray.service.d/10-donot_touch_multi_conf.conf'
echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there.
# Or all changes you made will be lost! # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
[Service]
ExecStart=
ExecStart=/usr/local/bin/xray run -confdir ${JSONS_PATH%/}/%i/" > \
'/etc/systemd/system/xray@.service.d/10-donot_touch_multi_conf.conf'
else
"rm" -f '/etc/systemd/system/xray.service.d/10-donot_touch_multi_conf.conf' \
'/etc/systemd/system/xray@.service.d/10-donot_touch_multi_conf.conf'
echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there.
# Or all changes you made will be lost! # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
[Service]
ExecStart=
ExecStart=/usr/local/bin/xray run -config ${JSON_PATH}/config.json" > \
'/etc/systemd/system/xray.service.d/10-donot_touch_single_conf.conf'
echo "# In case you have a good reason to do so, duplicate this file in the same directory and make your customizes there.
# Or all changes you made will be lost! # Refer: https://www.freedesktop.org/software/systemd/man/systemd.unit.html
[Service]
ExecStart=
ExecStart=/usr/local/bin/xray run -config ${JSON_PATH}/%i.json" > \
'/etc/systemd/system/xray@.service.d/10-donot_touch_single_conf.conf'
fi
echo "info: Systemd service files have been installed successfully!"
echo "${red}warning: ${green}The following are the actual parameters for the Xray service startup."
echo "${red}warning: ${green}Please make sure the configuration file path is correctly set.${reset}"
systemd_cat_config /etc/systemd/system/xray.service
# shellcheck disable=SC2154
if [[ "${check_all_service_files:0:1}" = 'y' ]]; then
echo
echo
systemd_cat_config /etc/systemd/system/xray@.service
fi
systemctl daemon-reload
SYSTEMD='1'
}
start_xray() {
if [[ -f '/etc/systemd/system/xray.service' ]]; then
systemctl start "${XRAY_CUSTOMIZE[@]:-xray}"
sleep 1s
local xray_daemon
for xray_daemon in "${XRAY_CUSTOMIZE[@]:-xray}"; do
if ! systemctl -q is-active "$xray_daemon"; then
echo 'error: Failed to start Xray service.'
exit 1
fi
done
echo 'info: Start the Xray service.'
fi
}
stop_xray() {
local xray_units
xray_units="$(systemctl list-units | awk '$1 == "xray.service" || $1 ~ /^xray@.*\.service$/ {print $1}')"
XRAY_CUSTOMIZE=()
if [[ -n "$xray_units" ]]; then
readarray -t XRAY_CUSTOMIZE <<<"$xray_units"
fi
if ! systemctl stop "${XRAY_CUSTOMIZE[@]:-xray.service}"; then
echo 'error: Stopping the Xray service failed.'
exit 1
fi
echo 'info: Stop the Xray service.'
}
install_with_logrotate() {
install_software 'logrotate' 'logrotate'
if [[ -z "${LOGROTATE_TIME:-}" ]]; then
LOGROTATE_TIME="00:00:00"
fi
cat <<EOF >/etc/systemd/system/logrotate@.service
[Unit]
Description=Rotate log files
Documentation=man:logrotate(8)
[Service]
Type=oneshot
ExecStart=/usr/sbin/logrotate /etc/logrotate.d/%i
EOF
cat <<EOF >/etc/systemd/system/logrotate@.timer
[Unit]
Description=Run logrotate for %i logs
[Timer]
OnCalendar=*-*-* $LOGROTATE_TIME
Persistent=true
[Install]
WantedBy=timers.target
EOF
if [[ ! -d '/etc/logrotate.d/' ]]; then
LOGROTATE_DIR='1'
fi
install -d -m 755 -o 0 -g 0 /etc/logrotate.d/
cat <<EOF >/etc/logrotate.d/xray
/var/log/xray/*.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
copytruncate
}
EOF
LOGROTATE_FIN='1'
}
check_geodata_requirements() {
if ! id -u xray >/dev/null 2>&1 || ! grep -q '^xray:' /etc/group; then
echo 'error: Updating geodata requires the xray user and group to exist.'
return 1
fi
}
update_geodata() {
check_geodata_requirements
install_software 'curl' 'curl'
install_software 'gzip' 'gzip'
stream_update_geodata() {
if ! curl -f -H 'Cache-Control: no-cache' -o "${dir_tmp}/${2}.sha256sum" "${1}.sha256sum"; then
echo 'error: Download failed! Please check your network or try again.'
exit 1
fi
if ! curl -f -H 'Cache-Control: no-cache' "${1}.gz" | gzip -d > "${dir_tmp}/${2}"; then
echo 'error: Download failed! Please check your network or try again.'
exit 1
fi
cd "${dir_tmp}" || exit
if ! sha256sum -c "${dir_tmp}/${2}.sha256sum"; then
echo 'error: Check failed! Please check your network or try again.'
exit 1
fi
cd - >/dev/null || exit 1
install -m 644 -o xray -g xray "${dir_tmp}/${2}" "${DAT_PATH}/${3}"
rm -f "${dir_tmp}/${2}" "${dir_tmp}/${2}.sha256sum"
}
local file_ip='geoip-full.dat'
local file_site='geosite-expand.dat'
local download_link_geoip="https://github.com/XTLS/Xray-rules-dat/releases/latest/download/${file_ip}"
local download_link_geosite="https://github.com/XTLS/Xray-rules-dat/releases/latest/download/${file_site}"
local install_ip='geoip.dat'
local install_site='geosite.dat'
local dir_tmp
dir_tmp="$(mktemp -d)"
TMP_PATHS+=("$dir_tmp")
install_geodata_dir
stream_update_geodata "${download_link_geoip}" "${file_ip}" "${install_ip}"
stream_update_geodata "${download_link_geosite}" "${file_site}" "${install_site}"
echo 'info: Geodata is updated. You may need to restart Xray for the changes to take effect.'
exit 0
}
check_update() {
if [[ "$XRAY_IS_INSTALLED_BEFORE_RUNNING_SCRIPT" -eq '1' ]]; then
get_current_version
echo "info: The current version of Xray is ${CURRENT_VERSION}."
else
echo 'warning: Xray is not installed.'
fi
get_latest_version
echo "info: The latest release version of Xray is ${RELEASE_LATEST}."
echo "info: The latest pre-release/release version of Xray is ${PRE_RELEASE_LATEST}."
exit 0
}
remove_xray() {
if systemctl list-unit-files | grep -w 'xray' >/dev/null; then
if [[ -n "$(pidof xray)" ]]; then
stop_xray
fi
local delete_files=('/usr/local/bin/xray' '/etc/systemd/system/xray.service' '/etc/systemd/system/xray@.service' '/etc/systemd/system/xray.service.d' '/etc/systemd/system/xray@.service.d')
[[ -d "$DAT_PATH" ]] && delete_files+=("$DAT_PATH")
[[ -f '/etc/logrotate.d/xray' ]] && delete_files+=('/etc/logrotate.d/xray')
if [[ "$PURGE" -eq '1' ]]; then
if [[ -z "$JSONS_PATH" ]]; then
delete_files+=("$JSON_PATH")
else
delete_files+=("$JSONS_PATH")
fi
[[ -d '/var/log/xray' ]] && delete_files+=('/var/log/xray')
[[ -f '/etc/systemd/system/logrotate@.service' ]] && delete_files+=('/etc/systemd/system/logrotate@.service')
[[ -f '/etc/systemd/system/logrotate@.timer' ]] && delete_files+=('/etc/systemd/system/logrotate@.timer')
fi
systemctl disable xray
if [[ -f '/etc/systemd/system/logrotate@.timer' ]]; then
if ! systemctl stop logrotate@xray.timer || ! systemctl disable logrotate@xray.timer; then
echo 'error: Stopping and disabling the logrotate service failed.'
exit 1
fi
echo 'info: Stop and disable the logrotate service.'
fi
if ! ("rm" -r "${delete_files[@]}"); then
echo 'error: Failed to remove Xray.'
exit 1
else
for i in "${!delete_files[@]}"; do
echo "removed: ${delete_files[$i]}"
done
systemctl daemon-reload
echo "You may need to execute a command to remove dependent software: $PACKAGE_MANAGEMENT_REMOVE curl unzip"
echo 'info: Xray has been removed.'
if [[ "$PURGE" -eq '0' ]]; then
echo 'info: If necessary, manually delete the configuration and log files.'
if [[ -n "$JSONS_PATH" ]]; then
echo "info: e.g., $JSONS_PATH and /var/log/xray/ ..."
else
echo "info: e.g., $JSON_PATH and /var/log/xray/ ..."
fi
fi
exit 0
fi
else
echo 'error: Xray is not installed.'
exit 1
fi
}
# Explanation of parameters in the script
show_help() {
echo "usage: $0 ACTION [OPTION]..."
echo
echo 'ACTION:'
echo ' install Install/Update Xray'
echo ' update-geodata Update GeoIP full and GeoSite expanded once'
echo ' Xray bundles GeoIP lite and GeoSite standard; see https://github.com/XTLS/Xray-rules-dat'
echo ' remove Remove Xray'
echo ' help Show help'
echo ' check Check if Xray can be updated'
echo 'If no action is specified, then install will be selected'
echo
echo 'OPTION:'
echo ' install:'
echo ' --version Install the specified version of Xray, e.g., --version v1.0.0'
echo ' -f, --force Force install even though the versions are same'
echo ' --beta Install the pre-release version if it is exist'
echo ' -l, --local <zip> Install a local Xray zip without accessing GitHub'
echo ' Relative paths use the current working directory; quote paths containing spaces'
echo ' Missing tools may be installed from system package repositories'
echo ' Uses bundled geodata unless --geoip or --geosite is specified'
echo ' --geoip <file> Use a local GeoIP file with --local'
echo ' --geosite <file> Use a local GeoSite file with --local'
echo ' -p, --proxy Download through a proxy server, e.g., -p http://127.0.0.1:8118 or -p socks5://127.0.0.1:1080'
echo ' --reinstall Reinstall current Xray version'
echo " --no-update-service Don't change service files if they are exist"
echo " --without-geodata Don't install/update geoip.dat and geosite.dat"
echo " --without-logfiles Don't install /var/log/xray"
echo " --logrotate [time] Install with logrotate."
echo " [time] need be in the format of 12:34:56, under 10:00:00 should be start with 0, e.g. 01:23:45."
echo ' --manually-restart-later Update Xray without stopping or restarting it; restart manually afterward'
echo ' update-geodata:'
echo ' -p, --proxy Download through a proxy server'
echo ' remove:'
echo ' --purge Remove all the Xray files, include logs, configs, etc'
echo ' check:'
echo ' -p, --proxy Check new version through a proxy server'
exit 0
}
main() {
judgment_parameters "$@"
[[ "$HELP" -eq '1' ]] && show_help
check_if_running_as_root
identify_the_operating_system_and_architecture
# Parameter information
[[ "$CHECK" -eq '1' ]] && check_update
[[ "$REMOVE" -eq '1' ]] && remove_xray
[[ "$UPDATE_GEODATA" -eq '1' ]] && update_geodata
# Two very important variables
TMP_DIRECTORY="$(mktemp -d)"
TMP_PATHS+=("$TMP_DIRECTORY")
ZIP_FILE="${TMP_DIRECTORY}/Xray-linux-$MACHINE.zip"
# Local archives never require GitHub; missing tools may use system repositories.
if [[ -n "$LOCAL_FILE" ]]; then
install_software 'unzip' 'unzip'
decompression "$LOCAL_FILE"
check_local_xray_version
prepare_local_geodata
else
get_current_version
if [[ "$REINSTALL" -eq '1' ]]; then
if [[ -z "$CURRENT_VERSION" ]]; then
echo "error: Xray is not installed"
exit 1
fi
INSTALL_VERSION="$CURRENT_VERSION"
echo "info: Reinstalling Xray $CURRENT_VERSION"
elif [[ -n "$SPECIFIED_VERSION" ]]; then
SPECIFIED_VERSION="v${SPECIFIED_VERSION#v}"
if [[ "$CURRENT_VERSION" == "$SPECIFIED_VERSION" ]] && [[ "$FORCE" -eq '0' ]]; then
echo "info: The current version is same as the specified version. The version is ${CURRENT_VERSION}."
exit 0
fi
INSTALL_VERSION="$SPECIFIED_VERSION"
echo "info: Installing specified Xray version $INSTALL_VERSION for $(uname -m)"
else
install_software 'curl' 'curl'
get_latest_version
if [[ "$BETA" -eq '0' ]]; then
INSTALL_VERSION="$RELEASE_LATEST"
else
INSTALL_VERSION="$PRE_RELEASE_LATEST"
fi
if ! version_gt "$INSTALL_VERSION" "$CURRENT_VERSION" && [[ "$FORCE" -eq '0' ]]; then
echo "info: No new version. The current version of Xray is ${CURRENT_VERSION}."
exit 0
fi
echo "info: Installing Xray $INSTALL_VERSION for $(uname -m)"
fi
install_software 'curl' 'curl'
install_software 'unzip' 'unzip'
download_xray || exit 1
decompression "$ZIP_FILE"
fi
[[ "$LOGROTATE" -eq '1' ]] && install_with_logrotate
# Determine if Xray is running
if systemctl list-unit-files | grep -w 'xray' >/dev/null; then
if [[ -n "$(pidof xray)" ]]; then
if [[ "$NO_RESTART" -eq '0' ]]; then
stop_xray
fi
XRAY_RUNNING='1'
fi
fi