#!/bin/bash # ═══════════════════════════════════════════════ # 🔺 Sovereign: TCS-0002∞ | Root: SYS-GLW-0001 # 📜 Copyright: 国作登字-2026-A-00037559 # ═══════════════════════════════════════════════ # server/proxy/deploy-proxy.sh # 🚀 铸渊专线 · 一键部署脚本 # # 在SG服务器上执行,完成代理服务的完整部署: # 1. 安装Xray-core + BBR # 2. 生成密钥 # 3. 配置Xray (从环境变量或密钥文件读取) # 4. 配置Nginx反代 # 5. 启动PM2服务 # 6. 健康检查 # # 用法: # bash deploy-proxy.sh install — 首次安装 # bash deploy-proxy.sh update — 更新配置 # bash deploy-proxy.sh status — 检查状态 # bash deploy-proxy.sh restart — 重启所有服务 # ═══════════════════════════════════════════════ set -uo pipefail # 注意: 不使用 set -e,关键步骤手动检查错误 PROXY_DIR="/opt/zhuyuan/proxy" REPO_PROXY_DIR="$(dirname "$0")" ACTION="${1:-status}" echo "════════════════════════════════════════" echo "🌐 铸渊专线 · 部署 · action=$ACTION" echo "════════════════════════════════════════" # ── 共用: 确保Xray以root运行 (修复User=nobody问题) ── ensure_xray_root_user() { if [ ! -f /etc/systemd/system/xray.service.d/override.conf ]; then mkdir -p /etc/systemd/system/xray.service.d cat > /etc/systemd/system/xray.service.d/override.conf < "$CONFIG_OUTPUT" # 验证配置 if xray run -test -c "$CONFIG_OUTPUT" 2>/dev/null; then echo "✅ Xray配置验证通过" else echo "⚠️ Xray配置验证失败,查看详情:" xray run -test -c "$CONFIG_OUTPUT" 2>&1 || true echo " 配置文件: $CONFIG_OUTPUT" return 1 fi } # ── 部署服务代码 ────────────────────────────── deploy_services() { mkdir -p "$PROXY_DIR"/{service,data,logs,dashboard} # 复制服务文件 cp "$REPO_PROXY_DIR"/service/*.js "$PROXY_DIR/service/" cp "$REPO_PROXY_DIR"/dashboard/*.js "$PROXY_DIR/dashboard/" cp "$REPO_PROXY_DIR"/ecosystem.proxy.config.js "$PROXY_DIR/" echo "✅ 服务代码已部署到 $PROXY_DIR" } # ── 配置Nginx ───────────────────────────────── configure_nginx() { # 检查Nginx配置片段是否已添加 NGINX_CONF="/etc/nginx/sites-enabled/default" SNIPPET="$REPO_PROXY_DIR/config/nginx-proxy-snippet.conf" if ! grep -q "proxy-sub" "$NGINX_CONF" 2>/dev/null; then echo " 添加Nginx代理订阅反向代理..." # 在最后一个 } 之前插入 # 注意: 实际应该由铸渊智能合并到主nginx配置 echo " ⚠️ 请手动将以下配置添加到Nginx:" echo " $SNIPPET" else echo " Nginx代理配置已存在" fi if nginx -t 2>/dev/null; then nginx -s reload || true fi } # ── 启动PM2服务 ─────────────────────────────── start_pm2_services() { cd "$PROXY_DIR" || { echo "❌ 无法进入 $PROXY_DIR"; return 1; } # 加载密钥作为环境变量 if [ -f "$PROXY_DIR/.env.keys" ]; then set -a # shellcheck source=/dev/null source "$PROXY_DIR/.env.keys" set +a fi pm2 start ecosystem.proxy.config.js pm2 save echo "✅ PM2代理服务已启动" pm2 list } # ── 健康检查 ────────────────────────────────── health_check() { echo "检查服务状态..." # Xray if systemctl is-active --quiet xray; then echo " ✅ Xray: 运行中" else echo " ❌ Xray: 未运行" fi # 443端口 if ss -tlnp | grep -q ":443 "; then echo " ✅ 端口443: 监听中" else echo " ❌ 端口443: 未监听" fi # 订阅服务 if curl -sf http://127.0.0.1:3802/health >/dev/null 2>&1; then echo " ✅ 订阅服务: 正常" else echo " ⏳ 订阅服务: 启动中..." fi # PM2 pm2 list 2>/dev/null || echo " ⚠️ PM2: 未配置" } # ── update: 更新配置 ────────────────────────── update() { echo "更新代理服务..." deploy_services configure_xray ensure_xray_root_user ensure_log_permissions systemctl restart xray pm2 restart zy-proxy-sub zy-proxy-monitor zy-proxy-guardian 2>/dev/null || true health_check echo "✅ 更新完成" } # ── status: 检查状态 ────────────────────────── status() { health_check } # ── restart: 重启所有 ───────────────────────── restart() { echo "重启所有代理服务..." systemctl restart xray pm2 restart zy-proxy-sub zy-proxy-monitor zy-proxy-guardian 2>/dev/null || true sleep 3 health_check } # ── 执行 ────────────────────────────────────── case "$ACTION" in install) install ;; update) update ;; status) status ;; restart) restart ;; *) echo "用法: bash deploy-proxy.sh {install|update|status|restart}" exit 1 ;; esac