-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinstall_core.sh
More file actions
202 lines (181 loc) · 5.44 KB
/
install_core.sh
File metadata and controls
202 lines (181 loc) · 5.44 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
#!/usr/bin/env bash
set -e
# Download Xray latest
RELEASE_TAG="latest"
TARGET_OS=""
TARGET_ARCH=""
usage() {
cat <<'EOF'
Usage: install_core.sh [--tag <release-tag>] [--os <linux>] [--arch <arch>]
Options:
--tag Xray release tag (default: latest)
--os Target OS (default: autodetect; supported: linux)
--arch Target arch (default: autodetect; supported: 32,64,arm32-v5,arm32-v6,arm32-v7a,arm64-v8a,mips32,mips32le,mips64,mips64le,ppc64,ppc64le,riscv64,s390x)
-h, --help Show this help
Examples:
install_core.sh --arch arm64-v8a
install_core.sh --tag v1.8.10 --arch mips32le
EOF
}
parse_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
--tag)
RELEASE_TAG="$2"; shift 2;;
--os)
TARGET_OS="$2"; shift 2;;
--arch)
TARGET_ARCH="$2"; shift 2;;
-h|--help)
usage; exit 0;;
*)
echo "error: unknown option $1"
usage
exit 1
;;
esac
done
}
parse_args "$@"
check_if_running_as_root() {
# If you want to run as another user, please modify $EUID to be owned by this user
if [[ "$EUID" -ne '0' ]]; then
echo "error: You must run this script as root!"
exit 1
fi
}
identify_the_operating_system_and_architecture() {
if [[ -n "$TARGET_OS" && "$TARGET_OS" != "linux" ]]; then
echo "error: This operating system is not supported (supported: linux)."
exit 1
fi
# If arch explicitly provided, trust it after minimal validation
if [[ -n "$TARGET_ARCH" ]]; then
ARCH="$TARGET_ARCH"
return
fi
if [[ "$(uname)" == 'Linux' ]]; then
case "$(uname -m)" in
'i386' | 'i686')
ARCH='32'
;;
'amd64' | 'x86_64')
ARCH='64'
;;
'armv5tel')
ARCH='arm32-v5'
;;
'armv6l')
ARCH='arm32-v6'
grep Features /proc/cpuinfo | grep -qw 'vfp' || ARCH='arm32-v5'
;;
'armv7' | 'armv7l')
ARCH='arm32-v7a'
grep Features /proc/cpuinfo | grep -qw 'vfp' || ARCH='arm32-v5'
;;
'armv8' | 'aarch64')
ARCH='arm64-v8a'
;;
'mips')
ARCH='mips32'
;;
'mipsle')
ARCH='mips32le'
;;
'mips64')
ARCH='mips64'
lscpu | grep -q "Little Endian" && ARCH='mips64le'
;;
'mips64le')
ARCH='mips64le'
;;
'ppc64')
ARCH='ppc64'
;;
'ppc64le')
ARCH='ppc64le'
;;
'riscv64')
ARCH='riscv64'
;;
's390x')
ARCH='s390x'
;;
*)
echo "error: The architecture is not supported."
exit 1
;;
esac
else
echo "error: This operating system is not supported."
exit 1
fi
}
download_xray() {
TARGET_OS_VALUE="${TARGET_OS:-linux}"
if [[ "$RELEASE_TAG" == "latest" ]]; then
DOWNLOAD_LINK="https://github.com/XTLS/Xray-core/releases/latest/download/Xray-${TARGET_OS_VALUE}-$ARCH.zip"
else
DOWNLOAD_LINK="https://github.com/XTLS/Xray-core/releases/download/$RELEASE_TAG/Xray-${TARGET_OS_VALUE}-$ARCH.zip"
fi
echo "Downloading Xray archive: $DOWNLOAD_LINK"
if ! curl -RL -H 'Cache-Control: no-cache' -o "$ZIP_FILE" "$DOWNLOAD_LINK"; then
echo 'error: Download failed! Please check your network or try again.'
rm -rf "$TMP_DIRECTORY"
exit 1
fi
}
extract_xray() {
if ! unzip -q "$ZIP_FILE" -d "$TMP_DIRECTORY"; then
echo 'error: Xray decompression failed.'
rm -rf "$TMP_DIRECTORY"
echo "removed: $TMP_DIRECTORY"
exit 1
fi
echo "Extracted Xray archive to $TMP_DIRECTORY"
# Validate required files exist
if [[ ! -f "${TMP_DIRECTORY}/xray" ]]; then
echo 'error: xray binary not found in archive.'
rm -rf "$TMP_DIRECTORY"
exit 1
fi
if [[ ! -f "${TMP_DIRECTORY}/geoip.dat" ]]; then
echo 'error: geoip.dat not found in archive.'
rm -rf "$TMP_DIRECTORY"
exit 1
fi
if [[ ! -f "${TMP_DIRECTORY}/geosite.dat" ]]; then
echo 'error: geosite.dat not found in archive.'
rm -rf "$TMP_DIRECTORY"
exit 1
fi
}
place_xray() {
if ! install -m 755 "${TMP_DIRECTORY}/xray" "/usr/local/bin/xray"; then
echo 'error: Failed to install xray binary.'
rm -rf "$TMP_DIRECTORY"
exit 1
fi
install -d "/usr/local/share/xray/"
if ! install -m 644 "${TMP_DIRECTORY}/geoip.dat" "/usr/local/share/xray/geoip.dat"; then
echo 'error: Failed to install geoip.dat.'
rm -rf "$TMP_DIRECTORY"
exit 1
fi
if ! install -m 644 "${TMP_DIRECTORY}/geosite.dat" "/usr/local/share/xray/geosite.dat"; then
echo 'error: Failed to install geosite.dat.'
rm -rf "$TMP_DIRECTORY"
exit 1
fi
echo "Xray files installed"
}
check_if_running_as_root
identify_the_operating_system_and_architecture
TMP_DIRECTORY="$(mktemp -d)"
ZIP_FILE="${TMP_DIRECTORY}/Xray-linux-$ARCH.zip"
download_xray
extract_xray
place_xray
rm -rf "$TMP_DIRECTORY"
echo "Installation complete!"
exit 0