Files
dock/xu
2026-01-20 09:31:07 +08:00

385 lines
14 KiB
Bash
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.
#!/bin/bash
# ==========================================
# 标题X-UI 全能安装脚本 (咸V咆哮制作)
# 更新1. 集成定制化架构下载链接
# 2. 模式1与模式2分别对应特定数据库配置
# 3. 智能测速与双核下载引擎
# ==========================================
# --- 颜色配置 ---
Red="\033[31m"
Green="\033[32m"
Yellow="\033[33m"
Blue="\033[36m"
Font="\033[0m"
# --- 核心配置 (用户定制) ---
# 数据库链接
DB_URL_PUBLIC="https://git.vps3344521.xyz/attachments/aa8ba64e-3a73-48f4-9317-07e06400b1d8" # 模式1链接
DB_URL_PRIVATE="https://git.vps3344521.xyz/attachments/c457401f-76dd-4499-8ea3-a8b628bc963a" # 模式2链接
# 默认账号设置 (数据库下载后会被重置为此账号)
INSTALL_PATH="/usr/local/x-ui"
BIN_LINK="/usr/bin/x-ui"
DB_PATH="/etc/x-ui/x-ui.db"
SET_USER="3344"
SET_PASS="3344"
SET_PORT="8443"
# ==========================================
# 1. 模式选择
# ==========================================
clear
echo -e "${Blue}#################################################${Font}"
echo -e "${Blue}# X-UI 自动安装脚本 (咸V咆哮制作1.1) #${Font}"
echo -e "${Blue}#################################################${Font}"
echo -e "${Yellow}请选择安装配置模式:${Font}"
echo -e "-------------------------------------------------"
echo -e "1. ${Green}标准配置安装${Font} (使用指定的公共数据库配置)"
echo -e "2. ${Green}私人配置安装${Font} (私人数据库配置严禁个人使用)"
echo -e "-------------------------------------------------"
read -p "请输入数字 [1-2] (默认1): " INSTALL_MODE
[[ -z "$INSTALL_MODE" ]] && INSTALL_MODE="1"
# ==========================================
# 2. 网络环境智能检测
# ==========================================
check_network() {
echo -e "${Yellow}>> [0/7] 正在检测网络环境...${Font}"
HAS_IPV4=0
HAS_IPV6=0
if curl -s4m2 https://www.google.com/generate_204 >/dev/null 2>&1 || curl -s4m2 https://www.baidu.com >/dev/null 2>&1; then
HAS_IPV4=1
fi
if curl -s6m2 https://www.google.com/generate_204 >/dev/null 2>&1; then
HAS_IPV6=1
fi
if [[ $HAS_IPV4 -eq 1 ]]; then
echo -e "${Green}检测到 IPv4 网络,将优先使用 IPv4 通道${Font}"
NET_OPT="-4"
elif [[ $HAS_IPV6 -eq 1 ]]; then
echo -e "${Green}检测到纯 IPv6 网络,将自动切换至 IPv6 通道${Font}"
NET_OPT="-6"
else
echo -e "${Red}错误:未检测到任何可用网络!${Font}"
exit 1
fi
}
check_network
# ==========================================
# 3. 智能源选择 (自动测速版)
# ==========================================
auto_select_source() {
echo -e "${Yellow}>> [1/7] 正在智能测速选择最佳软件源...${Font}"
# 定义测试目标
ALIYUN_URL="https://mirrors.aliyun.com"
GOOGLE_URL="https://www.google.com"
# 测速逻辑
echo -n " 测试国际连通性... "
SPEED_GLOBAL=$(curl -o /dev/null -s -w '%{time_total}' --connect-timeout 2 "$GOOGLE_URL")
if [ $? -ne 0 ]; then SPEED_GLOBAL=999; echo "超时/无法连接"; else echo "${SPEED_GLOBAL}"; fi
echo -n " 测试国内连通性... "
SPEED_CN=$(curl -o /dev/null -s -w '%{time_total}' --connect-timeout 2 "$ALIYUN_URL")
if [ $? -ne 0 ]; then SPEED_CN=999; echo "超时/无法连接"; else echo "${SPEED_CN}"; fi
# 决策
if (( $(echo "$SPEED_GLOBAL == 999" | bc -l) )); then
echo -e "${Green}>> 判定为中国大陆环境 (无法连接Google),自动选择阿里云源${Font}"
SOURCE_CHOICE="1"
elif (( $(echo "$SPEED_CN < $SPEED_GLOBAL" | bc -l) )); then
echo -e "${Green}>> 阿里云响应更快,自动选择阿里云源${Font}"
SOURCE_CHOICE="1"
else
echo -e "${Green}>> 国际网络良好,自动选择 Cloudflare/官方源${Font}"
SOURCE_CHOICE="2"
fi
}
# 执行自动选择
if command -v bc >/dev/null 2>&1 && command -v curl >/dev/null 2>&1; then
auto_select_source
else
echo -e "${Yellow}缺失测速工具,默认选择官方源...${Font}"
SOURCE_CHOICE="2"
fi
# 执行换源操作
PM="apt"
if [[ -f /etc/redhat-release ]] || command -v yum >/dev/null 2>&1; then PM="yum"; fi
if [ "$SOURCE_CHOICE" != "3" ]; then
echo -e "${Yellow}>> 正在优化系统软件源...${Font}"
if [ "$PM" == "apt" ]; then
if [ -f /etc/os-release ]; then . /etc/os-release; CODENAME=$VERSION_CODENAME; else CODENAME="bookworm"; fi
cp /etc/apt/sources.list /etc/apt/sources.list.bak.$(date +%s)
if [ "$SOURCE_CHOICE" == "1" ]; then
DOMAIN="mirrors.aliyun.com"
else
DOMAIN="debian.cloudflare.mirrors.com"
fi
cat > /etc/apt/sources.list <<EOF
deb https://$DOMAIN/debian/ $CODENAME main non-free non-free-firmware contrib
deb-src https://$DOMAIN/debian/ $CODENAME main non-free non-free-firmware contrib
deb https://$DOMAIN/debian-security/ $CODENAME-security main
deb https://$DOMAIN/debian/ $CODENAME-updates main non-free non-free-firmware contrib
EOF
elif [ "$PM" == "yum" ]; then
mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/ 2>/dev/null
if [ "$SOURCE_CHOICE" == "1" ]; then
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
else
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.edge.kernel.org/centos/7/os/x86_64/
fi
timeout 60 yum makecache >/dev/null 2>&1
fi
fi
# ==========================================
# 4. 系统环境处理 (双核下载引擎安装)
# ==========================================
echo -e "${Yellow}>> [2/7] 安装依赖与下载引擎...${Font}"
install_soft() {
if [ "$PM" == "apt" ]; then
apt-get install -y $1 >/dev/null 2>&1
elif [ "$PM" == "yum" ]; then
yum install -y $1 >/dev/null 2>&1
fi
}
if [ "$PM" == "apt" ]; then
pgrep -x "apt" && killall apt apt-get dpkg >/dev/null 2>&1
rm -rf /var/lib/apt/lists/lock /var/lib/dpkg/lock*
dpkg --configure -a >/dev/null 2>&1
apt-get update -o Acquire::http::Timeout="20" || echo -e "${Red}源更新超时,尝试继续...${Font}"
apt-get install -y curl wget tar ca-certificates bc
install_soft sqlite3
echo -n " 安装 Axel 加速器... "
install_soft axel && echo "完成" || echo "失败(跳过)"
echo -n " 安装 Aria2 加速器... "
install_soft aria2 && echo "完成" || echo "失败(跳过)"
if ! apt-get install -y ntpdate >/dev/null 2>&1; then
apt-get install -y ntpsec-ntpdate >/dev/null 2>&1
fi
else
# CentOS
yum install -y epel-release >/dev/null 2>&1
yum install -y curl wget tar bc >/dev/null 2>&1
install_soft sqlite3
install_soft ntpdate
install_soft axel
install_soft aria2
fi
# ==========================================
# 5. 时间与架构匹配 (定制化下载链接)
# ==========================================
echo -e "${Yellow}>> [3/7] 校准时间...${Font}"
rm -f /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
if command -v ntpdate >/dev/null 2>&1; then
ntpdate pool.ntp.org >/dev/null 2>&1
else
date -s "$(curl -sI g.cn | grep Date | cut -d' ' -f3-6)Z" >/dev/null 2>&1
fi
echo -e "${Yellow}>> [4/7] 识别架构并匹配下载源...${Font}"
ARCH=$(uname -m)
DOWNLOAD_URL=""
FILE_NAME=""
# 架构匹配逻辑 - 使用您提供的具体链接
case $ARCH in
x86_64)
FILE_NAME="x-ui-linux-amd64.tar.gz"
DOWNLOAD_URL="https://git.vps3344521.xyz/attachments/40eeb013-5006-423f-ad74-a0541ab340f4"
;;
aarch64|armv8)
FILE_NAME="x-ui-linux-arm64.tar.gz"
DOWNLOAD_URL="https://git.vps3344521.xyz/attachments/4ab708d5-6bc8-42c0-8494-ef5efe03e074"
;;
i386|i686)
FILE_NAME="x-ui-linux-386.tar.gz"
DOWNLOAD_URL="https://git.vps3344521.xyz/attachments/d1604006-c6b7-4c7c-9652-b42b229ef4cb"
;;
armv5*)
FILE_NAME="x-ui-linux-armv5.tar.gz"
DOWNLOAD_URL="https://git.vps3344521.xyz/attachments/8a5c678c-4ae4-43c4-910d-7e47f7c21c22"
;;
armv6*)
FILE_NAME="x-ui-linux-armv6.tar.gz"
DOWNLOAD_URL="https://git.vps3344521.xyz/attachments/0e7c7fd7-192e-44b3-9739-785a5fb1b51f"
;;
armv7*)
FILE_NAME="x-ui-linux-armv7.tar.gz"
DOWNLOAD_URL="https://git.vps3344521.xyz/attachments/9d1bf416-afb6-4e9f-b46b-ab3a1913a998"
;;
s390x)
FILE_NAME="x-ui-linux-s390x.tar.gz"
DOWNLOAD_URL="https://git.vps3344521.xyz/attachments/18784828-0d20-4bc5-908c-15f91bcf8eb5"
;;
*)
echo -e "${Red}不支持或无法识别的架构: $ARCH${Font}"
exit 1
;;
esac
echo -e "${Green}检测到架构: $ARCH${Font}"
echo -e "${Green}匹配文件名: $FILE_NAME${Font}"
# ==========================================
# 6. 智能下载管理器 (双核+自动回退)
# ==========================================
# 参数: $1=URL, $2=OutputFilename
download_manager() {
local url=$1
local file=$2
rm -f "$file"
# 优先级 1: Axel
if command -v axel >/dev/null 2>&1; then
echo -e "${Green}>> 启用 Axel 引擎 (16线程)...${Font}"
if axel -n 16 -k -q -o "$file" "$url"; then return 0; fi
echo -e "${Red}Axel 下载失败,切换备用引擎...${Font}"
fi
# 优先级 2: Aria2
if command -v aria2c >/dev/null 2>&1; then
echo -e "${Green}>> 启用 Aria2 引擎 (16线程)...${Font}"
if aria2c -x 16 -s 16 -k 1M -o "$file" "$url" >/dev/null 2>&1; then return 0; fi
echo -e "${Red}Aria2 下载失败,切换单线程...${Font}"
fi
# 优先级 3: Wget
echo -e "${Yellow}>> 启用 Wget (单线程)...${Font}"
if wget $NET_OPT --no-check-certificate --timeout=30 --tries=3 -O "$file" "$url"; then return 0; fi
# 优先级 4: Curl
echo -e "${Yellow}>> 启用 Curl (最后保底)...${Font}"
curl $NET_OPT -L -k --connect-timeout 30 --retry 3 -o "$file" "$url"
}
echo -e "${Yellow}>> [5/7] 下载安装包 (Mode: $NET_OPT)...${Font}"
cd /usr/local/
# 调用下载管理器下载对应架构的文件
download_manager "$DOWNLOAD_URL" "$FILE_NAME"
if ! tar -tzf "$FILE_NAME" >/dev/null 2>&1; then
echo -e "${Red}严重错误:安装包下载失败或文件损坏!${Font}"
echo -e "${Red}尝试链接: $DOWNLOAD_URL${Font}"
exit 1
fi
# ==========================================
# 7. 安装与配置 (处理两种模式的数据库)
# ==========================================
echo -e "${Yellow}>> [6/7] 安装与配置...${Font}"
systemctl stop x-ui >/dev/null 2>&1
killall x-ui >/dev/null 2>&1
rm -rf x-ui
tar zxvf "$FILE_NAME" >/dev/null
cd x-ui
chmod +x x-ui x-ui.sh bin/xray-linux-*
ln -sf "$INSTALL_PATH/x-ui.sh" "$BIN_LINK"
mkdir -p /etc/x-ui/
rm -f "$DB_PATH"
# 确定目标数据库链接
if [ "$INSTALL_MODE" == "1" ]; then
echo -e "${Yellow}>> 正在下载标准/公共配置...${Font}"
TARGET_DB="$DB_URL_PUBLIC"
else
echo -e "${Yellow}>> 正在下载私人配置...${Font}"
TARGET_DB="$DB_URL_PRIVATE"
fi
# 下载配置数据库
download_manager "$TARGET_DB" "$DB_PATH"
if [ ! -s "$DB_PATH" ] || [ $(stat -c%s "$DB_PATH") -lt 1000 ]; then
echo -e "${Red}警告:数据库下载失败,尝试使用默认空数据库初始化...${Font}"
cp /usr/local/x-ui/bin/x-ui.db "$DB_PATH"
else
echo -e "${Green}数据库配置下载成功!${Font}"
fi
# --- 统一强制重置账号 ---
# 无论哪种模式,下载下来的数据库密码可能未知,强制重置为脚本头部设定的密码
echo -e "${Yellow}>> 正在强制重置账户权限...${Font}"
chmod 777 "$DB_PATH" >/dev/null 2>&1
RESET_SUCCESS=0
if command -v sqlite3 >/dev/null 2>&1; then
sqlite3 -cmd ".timeout 2000" "$DB_PATH" "UPDATE settings SET value='/' WHERE key='webBasePath';"
sqlite3 -cmd ".timeout 2000" "$DB_PATH" "UPDATE settings SET value='$SET_PORT' WHERE key='webPort';"
sqlite3 -cmd ".timeout 2000" "$DB_PATH" "UPDATE users SET username='$SET_USER', password='$SET_PASS' WHERE id=1;"
CURRENT_PASS=$(sqlite3 "$DB_PATH" "SELECT password FROM users WHERE id=1;")
if [ "$CURRENT_PASS" == "$SET_PASS" ]; then
echo -e "${Green}账户权限重置成功 (SQL模式)${Font}"
RESET_SUCCESS=1
fi
fi
if [ $RESET_SUCCESS -eq 0 ]; then
echo -e "${Yellow}使用官方接口重置账号...${Font}"
./x-ui setting -username "$SET_USER" -password "$SET_PASS" -port "$SET_PORT" >/dev/null 2>&1
fi
chmod 644 "$DB_PATH" >/dev/null 2>&1
# ==========================================
# 8. 启动与放行
# ==========================================
echo -e "${Yellow}>> [7/7] 启动服务...${Font}"
cat > /etc/systemd/system/x-ui.service <<EOF
[Unit]
Description=x-ui Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=$INSTALL_PATH
ExecStart=$INSTALL_PATH/x-ui
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable x-ui
systemctl restart x-ui
sleep 2
if command -v ufw >/dev/null 2>&1; then ufw allow $SET_PORT/tcp >/dev/null 2>&1; fi
if command -v firewall-cmd >/dev/null 2>&1; then
firewall-cmd --zone=public --add-port=$SET_PORT/tcp --permanent >/dev/null 2>&1
firewall-cmd --reload >/dev/null 2>&1
fi
iptables -I INPUT -p tcp --dport $SET_PORT -j ACCEPT 2>/dev/null
IP=$(curl -s4m5 ip.sb)
[ -z "$IP" ] && IP=$(curl -s6m5 ip.sb)
echo -e "\n${Blue}#################################################${Font}"
echo -e "${Green} X-UI 安装完成 (咸V咆哮制作) ${Font}"
echo -e "${Blue}#################################################${Font}"
echo -e "访问地址 ${Green}http://$IP:$SET_PORT${Font}"
echo -e "用户名 ${Green}$SET_USER${Font}"
echo -e "密码 ${Green}$SET_PASS${Font}"
echo -e "当前模式 ${Yellow}$([ "$INSTALL_MODE" == "1" ] && echo "标准配置" || echo "私人配置")${Font}"
echo -e "${Blue}#################################################${Font}"