Repository navigation
Expand file tree
/
Copy pathinstall_core.sh
More file actions
370 lines (330 loc) · 10.9 KB
/
Copy pathinstall_core.sh
File metadata and controls
370 lines (330 loc) · 10.9 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
#!/usr/bin/env bash
set -e
SCRIPT_COMMIT_SHA="${SCRIPT_COMMIT_SHA:-__SCRIPT_COMMIT_SHA__}"
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
# Query the remote GitHub repository commit SHA for a branch.
# Arguments:
# $1 - Repository name (default: PasarGuard/scripts)
# $2 - Target branch (default: main)
# Outputs:
# Writes the remote commit SHA to stdout.
# Returns:
# 0 on success, 1 on lookup failure or if remote lookup is disabled.
# shellcheck disable=SC2120
resolve_remote_commit_sha() {
local repo="${1:-PasarGuard/scripts}"
local branch="${2:-main}"
local response=""
local sha=""
[ -n "${PASARGUARD_SKIP_REMOTE_COMMIT_LOOKUP:-}" ] && return 1
command -v curl >/dev/null 2>&1 || return 1
response=$(curl -fsSL --connect-timeout 3 --max-time 5 \
"https://api.github.com/repos/${repo}/commits/${branch}" 2>/dev/null) || return 1
sha=$(printf '%s' "$response" | grep -m1 -o '"sha"[[:space:]]*:[[:space:]]*"[0-9a-f]\{7,40\}"' | grep -o '[0-9a-f]\{7,40\}')
[ -n "$sha" ] || return 1
printf '%s\n' "$sha"
}
# Determine the commit SHA representing the current script version.
# Outputs:
# Writes the resolved commit SHA or fallback branch name to stdout.
# Returns:
# 0 on success.
get_script_commit_sha() {
local baked_sha="${SCRIPT_COMMIT_SHA:-}"
local commit_sha=""
if [ -n "$baked_sha" ] && [ "$baked_sha" != "__SCRIPT_COMMIT_SHA__" ]; then
printf '%s\n' "$baked_sha"
return 0
fi
if [ -n "${PASARGUARD_SCRIPT_COMMIT:-}" ] && [ "$PASARGUARD_SCRIPT_COMMIT" != "__SCRIPT_COMMIT_SHA__" ]; then
printf '%s\n' "$PASARGUARD_SCRIPT_COMMIT"
return 0
fi
if command -v git >/dev/null 2>&1; then
commit_sha=$(git -C "$SCRIPT_DIR" rev-parse HEAD 2>/dev/null || true)
if [ -n "$commit_sha" ]; then
printf '%s\n' "$commit_sha"
return 0
fi
fi
# install_core.sh is normally piped straight into bash, so there is no git
# checkout and no baked-in SHA. Ask GitHub which commit "main" currently
# points to instead of printing the literal branch name.
commit_sha=$(resolve_remote_commit_sha)
if [ -n "$commit_sha" ]; then
printf '%s\n' "$commit_sha"
return 0
fi
printf '%s\n' "main"
}
# Download Xray latest
RELEASE_TAG="latest"
TARGET_OS=""
TARGET_ARCH=""
# Display CLI usage and available command-line options.
# Outputs:
# Writes help text to stdout.
# Returns:
# 0 on success.
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 command-line arguments and set configuration flags.
# Arguments:
# $@ - Command-line arguments passed to the script.
# Returns:
# 0 on successful argument parsing; exits with code 1 on error.
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 "$@"
printf '# Executing install_core script, commit: %s\n' "$(get_script_commit_sha)"
# Verify that the script is running with root privileges.
# Returns:
# 0 if running as root; exits with code 1 otherwise.
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
}
# Detect available system package manager (apt-get, dnf, or yum).
# Returns:
# 0 if a supported package manager is found; exits with code 1 otherwise.
detect_package_manager() {
if command -v apt-get >/dev/null 2>&1; then
PACKAGE_MANAGER="apt-get"
elif command -v dnf >/dev/null 2>&1; then
PACKAGE_MANAGER="dnf"
elif command -v yum >/dev/null 2>&1; then
PACKAGE_MANAGER="yum"
else
echo "error: No supported package manager found. Install curl and unzip manually."
exit 1
fi
}
# Install a system package using the detected package manager.
# Arguments:
# $1 - Package name to install.
# Returns:
# 0 on success; exits with code 1 on unsupported package manager.
install_package() {
local package="$1"
if [[ -z "${PACKAGE_MANAGER:-}" ]]; then
detect_package_manager
fi
echo "Installing missing dependency: $package"
case "$PACKAGE_MANAGER" in
apt-get)
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y "$package"
;;
dnf | yum)
"$PACKAGE_MANAGER" install -y -q "$package"
;;
*)
echo "error: Unsupported package manager: $PACKAGE_MANAGER"
exit 1
;;
esac
}
# Ensure a command is available on PATH, installing its package if missing.
# Arguments:
# $1 - Command binary name.
# $2 - Package name providing the command.
# Returns:
# 0 if available or installed; exits with code 1 if command remains missing.
ensure_command() {
local command_name="$1"
local package_name="$2"
if command -v "$command_name" >/dev/null 2>&1; then
return
fi
install_package "$package_name"
command -v "$command_name" >/dev/null 2>&1 || {
echo "error: Required command not found after installing $package_name: $command_name"
exit 1
}
}
# Verify and install required utility dependencies (curl, unzip).
# Returns:
# 0 if dependencies are satisfied; exits with code 1 on failure.
ensure_dependencies() {
ensure_command curl curl
ensure_command unzip unzip
}
# Identify target operating system and map system architecture to Xray arch string.
# Returns:
# 0 on successful detection; exits with code 1 on unsupported OS or architecture.
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 the Xray release zip archive from GitHub into temporary storage.
# Returns:
# 0 on success; exits with code 1 on download failure.
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 the downloaded Xray zip archive and validate expected binary files.
# Returns:
# 0 on success; exits with code 1 on extraction failure or missing files.
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
}
# Install the extracted Xray binary and data files into system directories.
# Returns:
# 0 on success; exits with code 1 on installation failure.
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
ensure_dependencies
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