Files
Nexus/docs/release/20260708/Nexus-宝塔Session自动修复正式发布-20260707.md
T
r 9b583ccc91
Nexus CI/CD / test (push) Waiting to run
Nexus CI/CD / e2e (push) Blocked by required conditions
Nexus CI/CD / deploy (push) Blocked by required conditions
Nexus Pre-commit Checks / quick-check (push) Waiting to run
docs(release): 主与 release 分支合并功能清单 + 导入 release 文档目录
便于 Gitea 上 main ↔ release/20260708-axs 合并评审与冲突对照。
2026-07-09 01:14:32 +08:00

7.1 KiB
Raw Blame History

Nexus 宝塔 Session 自动修复正式发布记录

日期:2026-07-07
正式域名:https://api.synaglobal.vip
正式主机:20.24.218.235
正式容器:nexus-prod-nexus-1
测试提交:5ac3f56 fix: auto repair btpanel session cleanup ttl

发布目标

把“新增宝塔自动检测/修复 hardcoded_3600”与“一键登录前兜底检测/修复”发布到正式环境,解决宝塔 tmp_login session 被 /www/server/panel/task.py 固定 3600 秒清理导致一键登录后 1~2 小时刷新掉线的问题。

本次正式发布范围

发布方式:docker cp 热更新正式容器内文件,然后重启容器。
说明:正式机 /opt/nexus 工作树较脏,本次没有执行 git reset / git pull,避免覆盖线上已有改动。

覆盖正式容器内文件:

  • /app/server/infrastructure/btpanel/ssh_bootstrap.py
  • /app/server/infrastructure/btpanel/bootstrap_state.py
  • /app/server/application/services/btpanel_service.py

来源文件本地留存:

  • C:\Users\uzuma\Documents\Codex\2026-07-06\yuu\work\prod-release-20260707-bt-session\ssh_bootstrap.py
  • C:\Users\uzuma\Documents\Codex\2026-07-06\yuu\work\prod-release-20260707-bt-session\bootstrap_state.py
  • C:\Users\uzuma\Documents\Codex\2026-07-06\yuu\work\prod-release-20260707-bt-session\btpanel_service.py

正式机 staging 目录:

  • /tmp/nexus_bt_session_release_20260707/

正式机备份目录:

  • /tmp/nexus_bt_session_backup_20260707_194326/

已发布能力

1. 新增宝塔 / bootstrap 阶段自动修复

在远程宝塔服务器上检查:

  • /www/server/panel/task.py

如果发现旧版精确模式:

if f_time > 3600:

则自动执行:

  1. 备份原文件为 /www/server/panel/task.py.nexus-session-ttl.bak
  2. 改为读取宝塔自身 session timeout 配置:
session_cleanup_timeout = int(public.get_session_timeout() or 86400)
if f_time > session_cleanup_timeout:
  1. 保留原文件权限;
  2. 写入前执行 compile(...)py_compile.compile(..., doraise=True)
  3. 通过临时文件 + os.replace(...) 原子替换;
  4. 变更后执行 /etc/init.d/bt reload

如果 task.py 结构不是已知安全模式,则记录 unknown_task_pattern,不做模糊替换,避免误改宝塔代码。

2. 一键登录前兜底检测/修复

BtPanelService.create_login_url() 生成一键登录 URL 前会检查已配置宝塔 API 的服务器:

  • 成功修复/确认后:24 小时内不重复检查;
  • 失败后:1 小时后允许重试;
  • 兜底检查失败不会阻断本次一键登录,只会记录 diagnostics 和状态字段。

3. 状态字段

状态写入:servers.extra_attrs.bt_panel

新增/更新字段:

  • session_cleanup_ttl_ok
  • session_cleanup_ttl_patched
  • session_cleanup_task_state
  • session_cleanup_ttl_checked_at
  • session_cleanup_ttl_last_error
  • panel_ssl

session_cleanup_task_state 常见值:

  • patched_timeout:已修好或本次修好;
  • unknown_task_pattern:存在 task.py,但结构不是可安全自动 patch 的已知模式;
  • no_panel:未找到宝塔面板 task.py
  • error:检测/修复异常。

一键登录响应 diagnostics 会返回非敏感诊断字段,例如:

{
  "session_cleanup_ttl_ok": true,
  "session_cleanup_ttl_patched": true,
  "session_cleanup_task_state": "patched_timeout"
}

不会输出宝塔临时登录 token。

验证记录

测试部署机验证

测试部署机:192.168.124.219
项目目录:/opt/nexus-dev-current
分支:audit/security-review

执行测试:

.venv/bin/python -m pytest \
  tests/test_btpanel_ssh_bootstrap.py \
  tests/test_btpanel_login_url.py \
  tests/test_btpanel_bootstrap_loop.py \
  tests/test_btpanel_login_url_route.py \
  tests/test_btpanel_ssh_login.py \
  tests/test_btpanel_temp_login_ttl.py -q

结果:

39 passed in 0.61s

正式容器发布前检查

正式容器临时目录内 3 个 Python 文件编译通过:

python -m py_compile \
  /tmp/nexus_bt_session_release_20260707/ssh_bootstrap.py \
  /tmp/nexus_bt_session_release_20260707/bootstrap_state.py \
  /tmp/nexus_bt_session_release_20260707/btpanel_service.py

正式容器覆盖后检查

覆盖到 /app/server/... 后再次编译通过:

python -m py_compile \
  /app/server/infrastructure/btpanel/ssh_bootstrap.py \
  /app/server/infrastructure/btpanel/bootstrap_state.py \
  /app/server/application/services/btpanel_service.py

代码关键字确认已生效:

  • unknown_task_pattern
  • py_compile.compile(tmp, doraise=True)
  • session_cleanup_task_state

正式健康检查

正式容器重启后:

nexus-prod-nexus-1 Up / healthy
http://127.0.0.1:8600/health => ok
https://api.synaglobal.vip/health => ok

安全边界

本次没有做以下高风险动作:

  • 不显示、不保存、不输出宝塔 tmp_token
  • 不输出 API Key、JWT、Cookie、密码、私钥;
  • 不自动关闭宝塔 check_client_hash()
  • 不自动修改宝塔面板 SSL 开关;
  • 不对未知 task.py 结构做模糊替换;
  • 不对正式机代码仓库做 git reset / git pull
  • 不批量重启用户业务服务器,只在需要 patch 宝塔 task.py 时 reload 宝塔面板。

回滚方式

如需回滚本次正式热更新,可在正式主机执行以下思路:

# 1. 从备份目录恢复 3 个文件到容器
sudo docker cp /tmp/nexus_bt_session_backup_20260707_194326/ssh_bootstrap.py \
  nexus-prod-nexus-1:/app/server/infrastructure/btpanel/ssh_bootstrap.py

sudo docker cp /tmp/nexus_bt_session_backup_20260707_194326/bootstrap_state.py \
  nexus-prod-nexus-1:/app/server/infrastructure/btpanel/bootstrap_state.py

sudo docker cp /tmp/nexus_bt_session_backup_20260707_194326/btpanel_service.py \
  nexus-prod-nexus-1:/app/server/application/services/btpanel_service.py

# 2. 编译检查
sudo docker exec nexus-prod-nexus-1 python -m py_compile \
  /app/server/infrastructure/btpanel/ssh_bootstrap.py \
  /app/server/infrastructure/btpanel/bootstrap_state.py \
  /app/server/application/services/btpanel_service.py

# 3. 重启并检查健康
sudo docker restart nexus-prod-nexus-1
curl -sf http://127.0.0.1:8600/health

后续建议

  1. 后续新增宝塔:bootstrap 阶段会自动检测/修复 hardcoded_3600。
  2. 老的已绑定宝塔:用户点击一键登录前会兜底检测/修复。
  3. 如果某台机器 diagnostics 显示 unknown_task_pattern,需要单独人工看那台宝塔的 /www/server/panel/task.py,不要自动硬替换。
  4. 如果用户仍反馈“刷新后掉线”,优先检查该服务器的 diagnostics
    • session_cleanup_task_state
    • session_cleanup_ttl_ok
    • session_cleanup_ttl_last_error
    • 面板 SSL / 浏览器混合内容 / Cookie SameSite 等。

结论

正式环境 https://api.synaglobal.vip 已发布“新增宝塔自动检测/修复 hardcoded_3600”和“一键登录前兜底检测/修复”。当前正式健康检查通过,测试机相关用例全部通过。后续新增宝塔和老宝塔的一键登录入口都会自动进入这套检测/修复流程。