Files
dock/xu
2025-12-26 19:37:18 +08:00

372 lines
13 KiB
Bash
Raw Permalink 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
# ==========================================
# 标题咸v咆哮制作 - X-UI 终极全能版 (V5.6 智能极速版)
# 更新1. 新增源自动测速选择 (Ping检测)
# 2. 新增 Aria2 + Axel 双核多线程下载引擎
# 基底V5.5 (全兼容架构)
# ==========================================
# --- 颜色配置 ---
Red="\033[31m"
Green="\033[32m"
Yellow="\033[33m"
Blue="\033[36m"
Font="\033[0m"
# --- 核心配置 ---
BASE_URL="https://freeyx.vps3344.dpdns.org/xui"
DB_URL_1="https://g1.vps7k7k.xyz/xui/x-ui%E6%A0%87%E5%87%86.db"
DB_URL_2="https://pub-b69a7194f4ea42fba6aa990c49bded91.r2.dev/xui/x-ui%E6%A0%87%E5%87%86.db"
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 自动安装脚本 (V5.6 智能极速版) #${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"
# 测速逻辑:获取 HTTP 状态码的时间 (超时设为 2秒)
# 1. 测国外 (Google)
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
# 2. 测国内 (Aliyun)
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
# 3. 决策
# 逻辑:如果 Google 连不上(999) 或者 阿里云速度明显快于 Google则选国内源
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}>> 阿里云响应更快 (CN:$SPEED_CN vs Global:$SPEED_GLOBAL),自动选择阿里云源${Font}"
SOURCE_CHOICE="1"
else
echo -e "${Green}>> 国际网络良好 (Global:$SPEED_GLOBAL),自动选择 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
# 如果没有 bc 工具默认给个提示让用户盲选或者默认选2
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)
FILE_NAME=""
case $ARCH in
x86_64) FILE_NAME="x-ui-linux-amd64.tar.gz" ;;
aarch64) FILE_NAME="x-ui-linux-arm64.tar.gz" ;;
i386|i686) FILE_NAME="x-ui-linux-386.tar.gz" ;;
armv5*) FILE_NAME="x-ui-linux-armv5.tar.gz" ;;
*) echo -e "${Red}不支持的架构: $ARCH${Font}"; exit 1 ;;
esac
# ==========================================
# 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}"
# -x16:16连接, -s16:16服务器, -k1M:分块
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_URL="${BASE_URL}/${FILE_NAME}"
# 调用下载管理器
download_manager "$DOWNLOAD_URL" "$FILE_NAME"
if ! tar -tzf "$FILE_NAME" >/dev/null 2>&1; then
echo -e "${Red}严重错误:安装包下载失败或文件损坏!${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/
if [ "$INSTALL_MODE" == "2" ]; then
echo -e "${Yellow}>> 正在执行恢复模式,下载配置文件...${Font}"
rm -f "$DB_PATH"
# 尝试下载主备份 (使用智能下载器)
download_manager "$DB_URL_1" "$DB_PATH"
# 检查是否成功,失败则尝试备用
if [ ! -s "$DB_PATH" ] || [ $(stat -c%s "$DB_PATH") -lt 1000 ]; then
echo -e "${Red}主链接失败,尝试备用链接...${Font}"
download_manager "$DB_URL_2" "$DB_PATH"
fi
if [ -s "$DB_PATH" ]; then
echo -e "${Green}配置文件恢复成功!${Font}"
else
echo -e "${Red}配置文件下载失败,将降级为标准安装...${Font}"
cp /usr/local/x-ui/bin/x-ui.db "$DB_PATH"
fi
else
echo -e "${Yellow}>> 执行标准安装...${Font}"
./x-ui setting -port $SET_PORT -username $SET_USER -password $SET_PASS >/dev/null 2>&1
[ ! -f "$DB_PATH" ] && cp /usr/local/x-ui/bin/x-ui.db "$DB_PATH"
fi
# --- 统一强制重置账号 ---
echo -e "${Yellow}>> 正在强制重置账户权限...${Font}"
systemctl stop x-ui >/dev/null 2>&1
killall x-ui >/dev/null 2>&1
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} 咸v咆哮制作 - 安装完成 (V5.6 智能极速版) ${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}"
if [ "$INSTALL_MODE" == "2" ]; then
echo -e "提示 ${Yellow}已成功恢复节点配置,账号密码已重置为 3344${Font}"
fi
echo -e "${Blue}#################################################${Font}"