#!/bin/bash # x-ui 多架构自动安装脚本 # 支持: amd64, 386, arm64, armv5, armv6, armv7, s390x # 默认配置: 端口 8443, 账号 3344, 密码 3344 # 咸鱼咆哮制作 - 一键搞定所有架构! set -e # 遇到错误立即退出 # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' # No Color # 自定义配置 PANEL_PORT="8443" PANEL_USERNAME="3344" PANEL_PASSWORD="3344" # 架构映射表 declare -A ARCH_MAP=( ["x86_64"]="amd64" ["i386"]="386" ["i486"]="386" ["i586"]="386" ["i686"]="386" ["aarch64"]="arm64" ["armv5l"]="armv5" ["armv6l"]="armv6" ["armv7l"]="armv7" ["s390x"]="s390x" ) # 下载URL基础 BASE_URL="https://github.vps7k7k.xyz/https://github.com/MHSanaei/3x-ui/releases/download/v2.8.5" # 打印彩色信息 info() { echo -e "${GREEN}[INFO]${NC} $1"; } warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } error() { echo -e "${RED}[ERROR]${NC} $1"; } step() { echo -e "${BLUE}[STEP]${NC} $1"; } debug() { echo -e "${PURPLE}[DEBUG]${NC} $1"; } cyan() { echo -e "${CYAN}$1${NC}"; } # 显示大字标题 show_banner() { echo echo -e "${CYAN}" echo " ███████╗██╗ ██╗ ██╗ ██╗██╗" echo " ╚══███╔╝╚██╗██╔╝ ██║ ██║██║" echo " ███╔╝ ╚███╔╝ ██║ ██║██║" echo " ███╔╝ ██╔██╗ ██║ ██║██║" echo " ███████╗██╔╝ ██╗ ╚██████╔╝██║" echo " ╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝" echo echo -e "${RED}╔════════════════════════════════════════╗" echo -e "║ ║" echo -e "║ ${YELLOW}🚀 超强多架构 X-UI 一键安装脚本 ${RED} ║" echo -e "║ ║" echo -e "║ ${GREEN}📦 支持所有主流CPU架构 ${RED} ║" echo -e "║ ${BLUE}⚡ 自动检测 + 自动配置 ${RED} ║" echo -e "║ ${PURPLE}🎯 咸鱼咆哮制作 ${RED} ║" echo -e "║ ║" echo -e "${RED}╚════════════════════════════════════════╝" echo -e "${NC}" } # 显示制作信息 show_creator() { echo echo -e "${RED}╔══════════════════════════════════════════════════╗" echo -e "║ ║" echo -e "║ ${YELLOW}🔥 咸鱼咆哮宣言:${RED} ║" echo -e "║ ║" echo -e "║ ${GREEN}「多架构支持,智能适配!」${RED} ║" echo -e "║ ${CYAN}「一键配置,省心省力!」${RED} ║" echo -e "║ ${PURPLE}「有问题就反馈,持续改进!」${RED} ║" echo -e "║ ║" echo -e "${RED}╚══════════════════════════════════════════════════╝" echo -e "${NC}" } # 检测系统架构 detect_architecture() { local arch=$(uname -m) step "🔍 正在检测系统架构..." cyan "👉 当前架构: $arch" # 映射到对应的下载架构 if [[ -n "${ARCH_MAP[$arch]}" ]]; then DETECTED_ARCH="${ARCH_MAP[$arch]}" info "🎯 架构识别: $arch → $DETECTED_ARCH" else error "❌ 不支持的架构: $arch" echo show_arch_info exit 1 fi } # 构建下载URL build_download_url() { local filename="x-ui-linux-${DETECTED_ARCH}.tar.gz" DOWNLOAD_URL="${BASE_URL}/${filename}" DOWNLOAD_FILENAME="$filename" info "📥 下载文件: $DOWNLOAD_FILENAME" debug "🔗 下载URL: $DOWNLOAD_URL" } # 检查 root 权限 check_root() { if [ "$EUID" -ne 0 ]; then error "❌ 请使用 root 权限运行此脚本!" info "💡 尝试使用: ${CYAN}sudo bash $0${NC}" exit 1 fi info "✅ Root权限检查通过" } # 安装依赖 install_dependencies() { info "📦 检查并安装必要的依赖..." if command -v apt-get >/dev/null 2>&1; then # Debian/Ubuntu apt-get update apt-get install -y wget tar curl sqlite3 elif command -v yum >/dev/null 2>&1; then # CentOS/RHEL yum install -y wget tar curl sqlite elif command -v dnf >/dev/null 2>&1; then # Fedora dnf install -y wget tar curl sqlite elif command -v apk >/dev/null 2>&1; then # Alpine apk add wget tar curl sqlite else warn "⚠ 无法确定包管理器,跳过依赖安装" fi info "✅ 依赖安装完成" } # 下载 x-ui download_xui() { info "📥 开始下载 x-ui..." # 检查是否已存在文件 if [ -f "$DOWNLOAD_FILENAME" ]; then warn "📁 发现已存在的文件: $DOWNLOAD_FILENAME" read -p "🔄 是否重新下载? (y/N): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then rm -f "$DOWNLOAD_FILENAME" else info "✅ 使用已存在的文件" return 0 fi fi # 下载文件 info "⏬ 正在下载,请稍候..." cyan "💾 文件: $DOWNLOAD_FILENAME" if wget --progress=bar:force "$DOWNLOAD_URL" -O "$DOWNLOAD_FILENAME"; then info "✅ 下载完成!" else error "❌ 下载失败!" error "🔗 请检查URL: $DOWNLOAD_URL" exit 1 fi } # 解压并手动安装 extract_and_install() { info "📂 解压文件..." if [ ! -f "$DOWNLOAD_FILENAME" ]; then error "❌ 文件 $DOWNLOAD_FILENAME 不存在" exit 1 fi # 清理旧文件 rm -rf x-ui-temp mkdir -p x-ui-temp # 解压文件 cyan "🔓 正在解压 $DOWNLOAD_FILENAME..." if ! tar zxvf "$DOWNLOAD_FILENAME" -C x-ui-temp; then error "❌ 解压失败,文件可能已损坏" exit 1 fi # 进入解压目录 cd x-ui-temp # 查找 x-ui 目录 if [ -d "x-ui" ]; then cd x-ui fi info "🔧 开始手动安装 x-ui..." # 检查必要的文件 if [ ! -f "x-ui" ]; then error "❌ 未找到 x-ui 可执行文件" ls -la exit 1 fi # 设置执行权限 chmod +x x-ui if [ -f "x-ui.sh" ]; then chmod +x x-ui.sh fi # 手动安装 info "⚡ 执行手动安装..." # 停止可能存在的旧服务 systemctl stop x-ui 2>/dev/null || true # 复制文件到系统目录 mkdir -p /usr/local/x-ui/ cp -f x-ui /usr/local/x-ui/ if [ -f "x-ui.sh" ]; then cp -f x-ui.sh /usr/local/x-ui/ fi if [ -f "x-ui.service" ]; then cp -f x-ui.service /etc/systemd/system/ fi # 复制 bin 目录 if [ -d "bin" ]; then cp -rf bin /usr/local/x-ui/ fi # 设置权限 chmod +x /usr/local/x-ui/x-ui if [ -f "/usr/local/x-ui/x-ui.sh" ]; then chmod +x /usr/local/x-ui/x-ui.sh fi chmod +x /usr/local/x-ui/bin/xray-linux-* 2>/dev/null || true # 创建符号链接 if [ -f "/usr/local/x-ui/x-ui.sh" ]; then ln -sf /usr/local/x-ui/x-ui.sh /usr/bin/x-ui else ln -sf /usr/local/x-ui/x-ui /usr/bin/x-ui fi # 返回原目录 cd - > /dev/null info "✅ 文件安装完成" } # 配置系统服务 setup_service() { info "🔌 配置系统服务..." # 如果服务文件不存在,创建它 if [ ! -f "/etc/systemd/system/x-ui.service" ]; then cat > /etc/systemd/system/x-ui.service << 'EOF' [Unit] Description=x-ui Service Documentation=https://github.com/MHSanaei/3x-ui After=network.target [Service] Type=simple User=root WorkingDirectory=/usr/local/x-ui/ ExecStart=/usr/local/x-ui/x-ui Restart=on-failure RestartSec=5s [Install] WantedBy=multi-user.target EOF info "📄 创建了 systemd 服务文件" fi systemctl daemon-reload systemctl enable x-ui info "✅ 系统服务配置完成" } # 使用x-ui命令修改配置(修复版) modify_panel_config() { step "🎛️ 开始修改面板配置..." # 等待服务启动 info "⏳ 等待服务启动..." systemctl start x-ui 2>/dev/null || true sleep 8 # 检查服务状态 if ! systemctl is-active --quiet x-ui; then warn "⚠ 服务未运行,尝试启动..." systemctl start x-ui sleep 5 fi # 修改端口 info "🔧 修改面板端口为: $PANEL_PORT" if command -v x-ui >/dev/null 2>&1; then # 使用expect自动交互修改端口 if command -v expect >/dev/null 2>&1; then expect << EOF spawn x-ui expect "Please enter your selection" send "9\r" expect "Enter port number" send "$PANEL_PORT\r" expect "Restart the panel" send "y\r" expect "Press enter to return" send "\r" expect eof EOF info "✅ 端口修改完成" else warn "⚠ 未安装expect,无法自动修改端口" info "💡 请手动运行 'x-ui' 选择 9 修改端口为 $PANEL_PORT" fi sleep 3 # 修改用户名和密码 info "👤 修改用户名和密码为: $PANEL_USERNAME/$PANEL_PASSWORD" if command -v expect >/dev/null 2>&1; then expect << EOF spawn x-ui expect "Please enter your selection" send "6\r" expect "Are you sure to reset" send "y\r" expect "Please set the login username" send "$PANEL_USERNAME\r" expect "Please set the login password" send "$PANEL_PASSWORD\r" expect "Do you want to disable currently configured two-factor authentication" send "y\r" expect "Restart the panel" send "y\r" expect "Press enter to return" send "\r" expect eof EOF info "✅ 账号密码修改完成" else warn "⚠ 未安装expect,无法自动修改账号密码" info "💡 请手动运行 'x-ui' 选择 6 修改账号密码" fi else error "❌ x-ui 命令未找到,无法自动配置" info "💡 请手动运行配置命令" fi } # 直接修改数据库配置(备用方案) direct_db_config() { step "🗃️ 尝试直接修改数据库配置..." # 停止服务 systemctl stop x-ui 2>/dev/null || true sleep 3 # 检查数据库文件 if [ -f "/etc/x-ui/x-ui.db" ] && command -v sqlite3 >/dev/null 2>&1; then info "🔧 直接修改数据库配置..." # 修改端口 if sqlite3 /etc/x-ui/x-ui.db "UPDATE setting SET value = '$PANEL_PORT' WHERE key = 'port';"; then info "✅ 端口修改为: $PANEL_PORT" else warn "⚠ 端口修改失败" fi # 修改用户名密码 if sqlite3 /etc/x-ui/x-ui.db "UPDATE users SET username = '$PANEL_USERNAME', password = '$PANEL_PASSWORD' WHERE id = 1;"; then info "✅ 账号修改为: $PANEL_USERNAME" info "✅ 密码修改为: $PANEL_PASSWORD" else warn "⚠ 账号密码修改失败" fi # 禁用默认凭据警告 sqlite3 /etc/x-ui/x-ui.db "UPDATE setting SET value = 'false' WHERE key = 'hasDefaultCredential';" 2>/dev/null || true else warn "⚠ 无法直接修改数据库,将使用交互式配置" fi # 重启服务 systemctl start x-ui sleep 5 } # 配置防火墙 setup_firewall() { info "🔥 配置防火墙..." # 检查防火墙状态 if command -v ufw >/dev/null 2>&1 && ufw status | grep -q "active"; then # ufw ufw allow ${PANEL_PORT}/tcp comment "x-ui Panel" ufw allow 10000-20000/tcp comment "x-ui Proxy Ports" info "✅ UFW 防火墙已配置" elif command -v firewall-cmd >/dev/null 2>&1 && firewall-cmd --state >/dev/null 2>&1; then # firewalld firewall-cmd --permanent --add-port=${PANEL_PORT}/tcp firewall-cmd --permanent --add-port=10000-20000/tcp firewall-cmd --reload info "✅ Firewalld 防火墙已配置" elif command -v iptables >/dev/null 2>&1; then # iptables iptables -I INPUT -p tcp --dport ${PANEL_PORT} -j ACCEPT iptables -I INPUT -p tcp --dport 10000:20000 -j ACCEPT info "✅ iptables 规则已添加" # 尝试保存 iptables 规则 if command -v iptables-save >/dev/null 2>&1; then iptables-save > /etc/iptables.rules 2>/dev/null || true fi else warn "⚠ 未检测到防火墙,跳过配置" fi } # 启动服务 start_service() { info "🚀 启动 x-ui 服务..." systemctl daemon-reload systemctl enable x-ui # 重启服务确保配置生效 if systemctl restart x-ui; then info "✅ x-ui 服务重启成功" else error "❌ x-ui 服务重启失败" return 1 fi # 等待服务启动 sleep 8 # 检查服务状态 if systemctl is-active --quiet x-ui; then info "✅ x-ui 服务运行正常" else warn "⚠ x-ui 服务未运行,检查日志中..." systemctl status x-ui --no-pager -l fi } # 验证安装 verify_installation() { step "🔍 验证安装..." # 检查服务状态 if systemctl is-active --quiet x-ui; then info "✅ 服务运行正常" else warn "⚠ 服务未运行" return 0 fi # 检查端口监听 if command -v netstat >/dev/null 2>&1; then if netstat -tunlp 2>/dev/null | grep -q ":${PANEL_PORT} "; then info "✅ 端口 ${PANEL_PORT} 监听正常" else warn "⚠ 端口 ${PANEL_PORT} 未监听" fi elif command -v ss >/dev/null 2>&1; then if ss -tunlp 2>/dev/null | grep -q ":${PANEL_PORT} "; then info "✅ 端口 ${PANEL_PORT} 监听正常" else warn "⚠ 端口 ${PANEL_PORT} 未监听" fi fi # 显示最终配置 info "📋 最终配置验证:" if command -v x-ui >/dev/null 2>&1; then echo "n" | x-ui | grep -A 10 "View Current Settings" || true fi info "✅ 验证完成" } # 显示安装信息 show_info() { # 获取服务器IP local server_ip=$(curl -s --connect-timeout 5 ipv4.icanhazip.com || hostname -I | awk '{print $1}' || echo "你的服务器IP") echo echo -e "${GREEN}╔══════════════════════════════════════════════════╗" echo -e "║ ║" echo -e "║ 🎉 X-UI 多架构安装完成!咸v咆哮制作! ║" echo -e "║ ║" echo -e "╚══════════════════════════════════════════════════╝${NC}" echo info "🖥️ 系统架构: $(uname -m) → $DETECTED_ARCH" info "🌐 管理面板: http://${server_ip}:${PANEL_PORT}" info "👤 用户名: $PANEL_USERNAME" info "🔑 密码: $PANEL_PASSWORD" echo cyan "💡 配置状态: 已自动设置端口和账号密码" echo warn "⚠️ 重要提醒:" warn "1. 请立即访问面板验证登录" warn "2. 建议定期修改密码" warn "3. 确保防火墙已正确配置" echo info "🔧 常用命令:" info " 启动服务: systemctl start x-ui" info " 停止服务: systemctl stop x-ui" info " 重启服务: systemctl restart x-ui" info " 查看状态: systemctl status x-ui" info " 查看日志: journalctl -u x-ui -f" info " 管理菜单: x-ui" echo # 显示访问URL echo -e "${CYAN}" echo "╔══════════════════════════════════════════════════╗" echo "║ 🚀 立即访问 🚀 ║" echo "║ ║" echo "║ http://${server_ip}:${PANEL_PORT} ║" echo "║ ║" echo "║ 👤 账号: $PANEL_USERNAME ║" echo "║ 🔑 密码: $PANEL_PASSWORD ║" echo "║ ║" echo "╚══════════════════════════════════════════════════╝" echo -e "${NC}" } # 显示架构信息 show_arch_info() { echo info "🖥️ 支持的架构列表:" echo -e " ${CYAN}x86_64${NC} - 64位 Intel/AMD 处理器 ${GREEN}→ amd64${NC}" echo -e " ${CYAN}i386${NC} - 32位 Intel/AMD 处理器 ${GREEN}→ 386${NC}" echo -e " ${CYAN}aarch64${NC} - 64位 ARM 处理器 ${GREEN}→ arm64${NC}" echo -e " ${CYAN}armv7l${NC} - 32位 ARM v7 ${GREEN}→ armv7${NC}" echo -e " ${CYAN}armv6l${NC} - 32位 ARM v6 ${GREEN}→ armv6${NC}" echo -e " ${CYAN}armv5l${NC} - 32位 ARM v5 ${GREEN}→ armv5${NC}" echo -e " ${CYAN}s390x${NC} - IBM 大型机 ${GREEN}→ s390x${NC}" echo } # 安装expect工具 install_expect() { if ! command -v expect >/dev/null 2>&1; then info "📦 安装 expect 工具用于自动配置..." if command -v apt-get >/dev/null 2>&1; then apt-get install -y expect elif command -v yum >/dev/null 2>&1; then yum install -y expect elif command -v dnf >/dev/null 2>&1; then dnf install -y expect elif command -v apk >/dev/null 2>&1; then apk add expect else warn "⚠ 无法安装expect,将使用备用配置方案" fi fi } # 主函数 main() { show_banner show_creator info "🚀 开始 x-ui 多架构自动安装..." show_arch_info # 执行安装步骤 check_root detect_architecture build_download_url install_dependencies install_expect download_xui extract_and_install setup_service direct_db_config # 先尝试直接修改数据库 modify_panel_config # 再使用交互式配置确保生效 setup_firewall start_service verify_installation show_info echo echo -e "${RED}╔════════════════════════════════════════╗" echo -e "║ ║" echo -e "║ ${YELLOW}🎊 安装完成!咸v咆哮感谢使用!${RED} ║" echo -e "║ ${GREEN}🐟 不想上班不想上班!${RED} ║" echo -e "║ ║" echo -e "${RED}╚════════════════════════════════════════╝${NC}" echo } # 显示欢迎信息 show_banner echo cyan "🔧 默认配置: 端口 ${PANEL_PORT} | 账号 ${PANEL_USERNAME} | 密码 ${PANEL_PASSWORD}" echo read -p "🔥 是否开始安装? (y/N): " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then info "👋 安装已取消" exit 0 fi # 执行主函数 main