From 217e7f8a557bae76b583ac928ccc6d4fe985f4c3 Mon Sep 17 00:00:00 2001 From: milky0217 Date: Thu, 23 Jul 2026 12:25:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20deploy.sh/test.sh=20=E5=AE=A1=E8=AE=A1?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit deploy.sh: - 修复重复的 header 注释行 lib/common.sh: - view_logs/tail_logs/status: 从日志文件切换为 journalctl(上次改 日志输出到 stdout 后,日志文件已不存在) - init_env 模板: User=root → User=rust-backend(匹配最小权限原则) lib/test.sh: - test_notify_url_domain: 添加自定义 Host 头(TEST_DOMAIN),模拟 真实外部请求场景。之前测试走 127.0.0.1 直连,Host 头为 localhost 导致 notify_url 校验永远通过但实际配置可能是错误的 --- deploy.sh | 2 -- lib/common.sh | 18 +++++++----------- lib/test.sh | 26 +++++++++++++------------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/deploy.sh b/deploy.sh index 661e14b..5f31d20 100755 --- a/deploy.sh +++ b/deploy.sh @@ -2,8 +2,6 @@ # =========================================== # Rust Backend Deployment Script # =========================================== -# Rust Backend Deployment Script -# =========================================== # Usage: ./deploy.sh [development|production] [options] # # Options: diff --git a/lib/common.sh b/lib/common.sh index af60ccb..4a93517 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -187,8 +187,8 @@ Wants=network.target [Service] Type=simple -User=root -Group=root +User=rust-backend +Group=rust-backend WorkingDirectory=${REMOTE_DIR} ExecStart=${REMOTE_DIR}/rust-backend Restart=always @@ -403,19 +403,15 @@ rollback_on_failure() { view_logs() { local lines="${LOG_LINES:-50}" log_step "查看后端日志(最后 ${lines} 行)..." - [ "$DRY_RUN" = true ] && { log_dry "tail -${lines} 日志文件"; return 0; } - local lf; lf=$(remote "ls -t ${REMOTE_DIR}/logs/rust-backend.*.log 2>/dev/null | head -1" || echo "") - [ -z "$lf" ] && { log_warn "未找到日志文件"; return 1; } - echo -e "${BLUE}=== ${lf} ===${NC}"; remote "tail -${lines} '${lf}'" + [ "$DRY_RUN" = true ] && { log_dry "journalctl -u ${SERVICE_NAME} -n ${lines}"; return 0; } + remote "journalctl -u ${SERVICE_NAME} -n ${lines} --no-pager 2>/dev/null" || log_warn "无法读取 journalctl 日志" } tail_logs() { local lines="${LOG_LINES:-100}" log_step "实时跟踪日志..." [ "$DRY_RUN" = true ] && return 0 - local lf; lf=$(remote "ls -t ${REMOTE_DIR}/logs/rust-backend.*.log 2>/dev/null | head -1" || echo "") - [ -z "$lf" ] && { log_warn "未找到日志文件"; return 1; } - ssh -t "${REMOTE_USER}@${REMOTE_HOST}" "tail -f -n ${lines} '${lf}'" + ssh -t "${REMOTE_USER}@${REMOTE_HOST}" "journalctl -u ${SERVICE_NAME} -n ${lines} -f" } status() { @@ -423,8 +419,8 @@ status() { [ "$DRY_RUN" = true ] && { log_dry "systemctl status ${SERVICE_NAME}"; return 0; } echo -e "${BLUE}=== ${SERVICE_NAME} ===${NC}" remote "systemctl status ${SERVICE_NAME} --no-pager" - local lf; lf=$(remote "ls -t ${REMOTE_DIR}/logs/rust-backend.*.log 2>/dev/null | head -1" || echo "") - [ -n "$lf" ] && { echo -e "${BLUE}=== 最近日志 ===${NC}"; remote "tail -20 '${lf}'"; } + echo -e "${BLUE}=== 最近日志 ===${NC}" + remote "journalctl -u ${SERVICE_NAME} -n 20 --no-pager 2>/dev/null" || log_warn "无法读取 journalctl 日志" } # ---------- 确认 ---------- diff --git a/lib/test.sh b/lib/test.sh index a04d8b8..106eff1 100644 --- a/lib/test.sh +++ b/lib/test.sh @@ -475,10 +475,10 @@ test_nginx_proxy_temp() { test_notify_url_domain() { log_info "校验支付表单 notify_url 域名..." - local domain - domain=$(echo "$BASE_URL" | sed 's|https://||' | sed 's|http://||' | sed 's|/||g') + # 用 TEST_DOMAIN 构造 Host 头,模拟外部请求(而非 localhost 直连) + local test_domain="${TEST_DOMAIN#https://}" - # 获取访客 JWT + # 获取访客 JWT(通过本地后端) local jwt jwt=$(remote "curl -sk -X POST '${BASE_URL}/api/guest-login'" 2>/dev/null | python3 -c "import sys,json; print(json.load(sys.stdin).get('token',''))" 2>/dev/null) if [ -z "$jwt" ]; then @@ -486,26 +486,26 @@ test_notify_url_domain() { return 1 fi - # 提取支付表单的 notify_url + # 用正确的 Host 头请求支付表单(模拟外部用户访问) + # 后端此时通过 Host 头构建 notify_url,应等于 TEST_DOMAIN local notify_url - notify_url=$(remote "curl -sk '${BASE_URL}/payment/page?package=monthly&jwt=${jwt}'" 2>/dev/null | grep -oP 'notify_url" value="\K[^"]+' | head -1) + notify_url=$(remote "curl -sk '${BASE_URL}/payment/page?package=monthly&jwt=${jwt}' -H 'Host: ${test_domain}'" 2>/dev/null | grep -oP 'notify_url" value="\K[^"]+' | head -1) if [ -z "$notify_url" ]; then log_error "无法提取 notify_url(可能为 Mock 支付模式)" return 0 # Mock 模式下不校验 fi - # 检查 notify_url 的域名是否与 BASE_URL 一致 - local notify_domain - notify_domain=$(echo "$notify_url" | sed 's|https://||' | sed 's|http://||' | sed 's|/payment/notify||') - - if [ "$notify_domain" != "$domain" ]; then - log_error "notify_url 域名不匹配: 表单=${notify_domain}, 期望=${domain}" - log_error "请检查 APP_BASE_URL 配置" + # 检查 notify_url 是否以 TEST_DOMAIN 开头 + local expected_prefix="https://${test_domain}/payment/notify" + if [ "$notify_url" != "$expected_prefix" ]; then + log_error "notify_url 不匹配: 表单=${notify_url}" + log_error " 期望: ${expected_prefix}" + log_error "请检查 APP_BASE_URL 环境变量配置" return 1 fi - log_info " notify_url 域名 ${notify_domain} ✅" + log_info " notify_url 正确: ${notify_url} ✅" return 0 }