#!/bin/bash # ========================================== # 标题:X-UI 全能安装脚本 (精简服务器版) # 更新:移除第三方多线程下载器,保留原生 wget/curl # ========================================== # --- 颜色配置 --- Red="\033[31m" Green="\033[32m" Yellow="\033[33m" Blue="\033[36m" Font="\033[0m" # --- 核心配置 (用户定制) --- DB_URL_PUBLIC="https://git.77582585.xyz/3344/dock/releases/download/3xui/x-ui%20%E6%88%B702.db" DB_URL_PRIVATE="https://git.77582585.xyz/attachments/c457401f-76dd-4499-8ea3-a8b628bc963a" 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 自动安装脚本 (轻量精简版) #${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}>> [1/6] 正在检测网络环境...${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. 系统环境与依赖处理 # ========================================== echo -e "${Yellow}>> [2/6] 安装基础依赖...${Font}" PM="apt" if [[ -f /etc/redhat-release ]] || command -v yum >/dev/null 2>&1; then PM="yum"; fi 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 sqlite3 if ! apt-get install -y ntpdate >/dev/null 2>&1; then apt-get install -y ntpsec-ntpdate >/dev/null 2>&1 fi else yum install -y epel-release >/dev/null 2>&1 yum install -y curl wget tar bc sqlite3 ntpdate >/dev/null 2>&1 fi # ========================================== # 4. 时间校准与架构匹配 # ========================================== echo -e "${Yellow}>> [3/6] 校准时间与识别架构...${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 ARCH=$(uname -m) DOWNLOAD_URL="" FILE_NAME="" case $ARCH in x86_64) FILE_NAME="x-ui-linux-amd64.tar.gz" DOWNLOAD_URL="https://git.77582585.xyz/attachments/40eeb013-5006-423f-ad74-a0541ab340f4" ;; aarch64|armv8) FILE_NAME="x-ui-linux-arm64.tar.gz" DOWNLOAD_URL="https://git.77582585.xyz/attachments/4ab708d5-6bc8-42c0-8494-ef5efe03e074" ;; i386|i686) FILE_NAME="x-ui-linux-386.tar.gz" DOWNLOAD_URL="https://git.77582585.xyz/attachments/d1604006-c6b7-4c7c-9652-b42b229ef4cb" ;; armv5*) FILE_NAME="x-ui-linux-armv5.tar.gz" DOWNLOAD_URL="https://git.77582585.xyz/attachments/8a5c678c-4ae4-43c4-910d-7e47f7c21c22" ;; armv6*) FILE_NAME="x-ui-linux-armv6.tar.gz" DOWNLOAD_URL="https://git.77582585.xyz/attachments/0e7c7fd7-192e-44b3-9739-785a5fb1b51f" ;; armv7*) FILE_NAME="x-ui-linux-armv7.tar.gz" DOWNLOAD_URL="https://git.77582585.xyz/attachments/9d1bf416-afb6-4e9f-b46b-ab3a1913a998" ;; s390x) FILE_NAME="x-ui-linux-s390x.tar.gz" DOWNLOAD_URL="https://git.77582585.xyz/attachments/18784828-0d20-4bc5-908c-15f91bcf8eb5" ;; *) echo -e "${Red}不支持或无法识别的架构: $ARCH${Font}" exit 1 ;; esac echo -e "${Green}检测到架构: $ARCH,匹配文件名: $FILE_NAME${Font}" # ========================================== # 5. 本地基础下载逻辑 # ========================================== download_manager() { local url=$1 local file=$2 rm -f "$file" echo -e "${Yellow}>> 尝试使用 Wget 下载...${Font}" if wget $NET_OPT --no-check-certificate --timeout=30 --tries=3 -O "$file" "$url"; then return 0; fi echo -e "${Yellow}>> Wget 失败,尝试使用 Curl 兜底...${Font}" curl $NET_OPT -L -k --connect-timeout 30 --retry 3 -o "$file" "$url" } echo -e "${Yellow}>> [4/6] 下载安装包 (Mode: $NET_OPT)...${Font}" mkdir -p /usr/local/ cd /usr/local/ download_manager "$DOWNLOAD_URL" "$FILE_NAME" if ! tar -tzf "$FILE_NAME" >/dev/null 2>&1; then echo -e "${Red}严重错误:安装包下载失败或文件损坏!${Font}" exit 1 fi # ========================================== # 6. 安装与配置 # ========================================== echo -e "${Yellow}>> [5/6] 解压与配置...${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 TARGET_DB="$DB_URL_PUBLIC" else TARGET_DB="$DB_URL_PRIVATE" fi echo -e "${Yellow}>> 正在下载数据库配置...${Font}" 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" 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 RESET_SUCCESS=1 fi fi if [ $RESET_SUCCESS -eq 0 ]; then ./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 # ========================================== # 7. 启动与放行 # ========================================== echo -e "${Yellow}>> [6/6] 启动服务与防火墙放行...${Font}" cat > /etc/systemd/system/x-ui.service </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 安装完成 (精简服务器版) ${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 "${Blue}#################################################${Font}"