Files
xray/install-xray.sh
2026-04-14 02:08:32 +08:00

180 lines
4.3 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# 固定下载源一键安装Xray脚本
# 下载地址https://www.ecoo.top/update/soft_init/xray/
# 固定安装路径(保持不变)
DAT_PATH=${DAT_PATH:-/usr/local/share/xray}
JSON_PATH=${JSON_PATH:-/usr/local/etc/xray}
# 基础配置
red=$(tput setaf 1)
green=$(tput setaf 2)
reset=$(tput sgr0)
# 自定义curl重试机制
curl() {
$(type -P curl) -L -q --retry 5 --retry-delay 10 --retry-max-time 60 "$@"
}
# 必须ROOT运行
check_if_running_as_root() {
if [[ "$(id -u)" -ne 0 ]]; then
echo "error: 必须使用root运行此脚本!"
exit 1
fi
}
# 识别系统架构(确定下载文件名)
identify_architecture() {
case "$(uname -m)" in
'amd64' | 'x86_64')
XRAY_FILE="xray-amd64"
;;
'aarch64' | 'arm64')
XRAY_FILE="xray-arm64"
;;
'armv7' | 'armv7l')
XRAY_FILE="xray-armhf"
;;
*)
echo "error: 不支持的架构: $(uname -m)"
exit 1
;;
esac
echo "info: 检测到架构: $(uname -m) -> 下载文件: ${XRAY_FILE}"
}
# 安装依赖
install_dependencies() {
echo "info: 检查并安装依赖..."
if [[ "$(type -P apt)" ]]; then
apt -y update >/dev/null 2>&1
apt -y install curl unzip >/dev/null 2>&1
elif [[ "$(type -P dnf)" || "$(type -P yum)" ]]; then
${PACKAGE_MANAGEMENT_INSTALL} curl unzip -y >/dev/null 2>&1
elif [[ "$(type -P pacman)" ]]; then
pacman -S --noconfirm curl unzip >/dev/null 2>&1
fi
}
# 下载Xray核心和规则文件
download_files() {
BASE_URL="https://www.ecoo.top/update/soft_init/xray"
TMP_DIR=$(mktemp -d)
echo "info: 开始下载固定版本文件..."
echo "下载地址: ${BASE_URL}"
# 下载Xray核心
if ! curl -f -o "${TMP_DIR}/xray" "${BASE_URL}/${XRAY_FILE}"; then
echo "error: 下载Xray失败"
rm -rf ${TMP_DIR}
exit 1
fi
# 下载规则文件
curl -f -o "${TMP_DIR}/geoip.dat" "${BASE_URL}/geoip.dat"
curl -f -o "${TMP_DIR}/geosite.dat" "${BASE_URL}/geosite.dat"
# 赋权
chmod +x "${TMP_DIR}/xray"
}
# 安装文件
install_xray_files() {
echo "info: 安装文件到系统..."
# 安装主程序
install -m 755 "${TMP_DIR}/xray" "/usr/local/bin/xray"
# 安装规则文件
install -d "${DAT_PATH}"
install -m 644 "${TMP_DIR}/geoip.dat" "${DAT_PATH}/"
install -m 644 "${TMP_DIR}/geosite.dat" "${DAT_PATH}/"
# 安装默认配置文件(不存在则创建)
install -d "${JSON_PATH}"
if [[ ! -f "${JSON_PATH}/config.json" ]]; then
echo "{}" > "${JSON_PATH}/config.json"
fi
# 日志目录
install -d -m 755 /var/log/xray/
install -m 600 /dev/null /var/log/xray/access.log
install -m 600 /dev/null /var/log/xray/error.log
}
# 安装systemd服务保持原版逻辑不变
install_systemd_service() {
echo "info: 安装systemd服务..."
cat >/etc/systemd/system/xray.service <<EOF
[Unit]
Description=Xray Service
Documentation=https://github.com/xtls
After=network.target nss-lookup.target
[Service]
User=nobody
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
NoNewPrivileges=true
ExecStart=/usr/local/bin/xray run -config ${JSON_PATH}/config.json
Restart=on-failure
RestartPreventExitStatus=23
LimitNPROC=10000
LimitNOFILE=1000000
RuntimeDirectory=xray
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
chmod 644 /etc/systemd/system/xray.service
systemctl daemon-reload
}
# 启动并开机自启
start_and_enable() {
echo "info: 启动Xray并设置开机自启..."
systemctl stop xray >/dev/null 2>&1
systemctl enable xray
systemctl start xray
sleep 1
if systemctl is-active --quiet xray; then
echo "${green}info: Xray安装并启动成功${reset}"
else
echo "${red}error: Xray启动失败${reset}"
exit 1
fi
}
# 清理临时文件
clean_up() {
rm -rf "${TMP_DIR}"
echo "info: 清理临时文件完成"
}
# 主流程
main() {
check_if_running_as_root
identify_architecture
install_dependencies
download_files
install_xray_files
install_systemd_service
start_and_enable
clean_up
echo -e "\n${green}===== 安装完成 =====${reset}"
echo "Xray路径: /usr/local/bin/xray"
echo "配置路径: ${JSON_PATH}/config.json"
echo "规则路径: ${DAT_PATH}/"
echo "日志路径: /var/log/xray/"
echo "服务状态: systemctl status xray"
}
main