2026-07-13 codeo.top 生产加固

ssh.codeo.top 当前使用的生产脚本已增加 GitHub API 官方 SHA-256 校验、独立端口 canary、旧二进制备份、启动版本确认、API 健康探测和失败自动回滚,并将 timer 调整为每天北京时间 03:00–03:15 一次。详见 VanLab/VPS/codeo.top/codeo.top-CLIProxyAPI-升级至-v7.2.72-与每日自动更新-2026-07-13 和 cli-proxy-api-upgrade.sh。下文保留为最初方案的历史实施基线。

适用场景

CLIProxyAPI 部署在内网服务器(192.168.114.9),需要定时检查上游 GitHub releases 是否有新版本,自动下载并热更新。以下命令均在目标服务器上执行。

1. 方案设计

1.1 核心流程

  1. systemd timer 每日 07:00 触发 oneshot service。
  2. service 执行升级脚本,通过 GitHub redirect 解析最新版本号。
  3. 与本地 .current-version 比对,版本一致则跳过。
  4. 版本不同则下载 linux_amd64.tar.gz,校验 ELF,原子替换二进制。
  5. systemctl restart cli-proxy-api 重启服务并验证。

1.2 文件清单

文件用途
/opt/cli-proxy-api/bin/cli-proxy-api-upgrade.sh升级脚本(本文档附录 A)
/opt/cli-proxy-api/bin/.current-version记录当前已安装版本号
/etc/systemd/system/cli-proxy-api-upgrade.serviceoneshot 服务 unit
/etc/systemd/system/cli-proxy-api-upgrade.timer定时器 unit(每日 07:00)

2. 从零部署命令

2.1 部署升级脚本

install -m 755 cli-proxy-api-upgrade.sh /opt/cli-proxy-api/bin/cli-proxy-api-upgrade.sh

2.2 初始化版本记录

printf "6.9.6" > /opt/cli-proxy-api/bin/.current-version

2.3 创建 systemd service unit

# /etc/systemd/system/cli-proxy-api-upgrade.service
[Unit]
Description=CLIProxyAPI auto-upgrade from GitHub releases
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
Environment="ALL_PROXY=http://192.168.114.9:7890"
Environment="HTTP_PROXY=http://192.168.114.9:7890"
Environment="HTTPS_PROXY=http://192.168.114.9:7890"
ExecStart=/opt/cli-proxy-api/bin/cli-proxy-api-upgrade.sh
TimeoutStartSec=300

关于代理

服务器需要通过 HTTP 代理(7890)访问 GitHub,因此 service 中通过 Environment 注入代理环境变量,与主服务 cli-proxy-api.service 保持一致。

2.4 创建 systemd timer unit

# /etc/systemd/system/cli-proxy-api-upgrade.timer
[Unit]
Description=Check and upgrade CLIProxyAPI daily at 07:00

[Timer]
OnCalendar=*-*-* 07:00:00
Persistent=true
RandomizedDelaySec=300
Unit=cli-proxy-api-upgrade.service

[Install]
WantedBy=timers.target

Timer 参数说明

  • Persistent=true:如果服务器在 07:00 处于关机状态,开机后会补执行。
  • RandomizedDelaySec=300:在 07:00 ~ 07:05 之间随机触发,避免精确时间点的网络拥塞。

2.5 生效并启用

systemctl daemon-reload
systemctl enable --now cli-proxy-api-upgrade.timer

3. 验证

systemctl list-timers cli-proxy-api-upgrade.timer --no-pager
systemctl start cli-proxy-api-upgrade.service
journalctl -u cli-proxy-api-upgrade.service -n 20 --no-pager
cat /opt/cli-proxy-api/bin/.current-version

3.1 成功输出示例

[cli-proxy-api-upgrade] current=6.9.6 latest=6.9.6
[cli-proxy-api-upgrade] already up to date, skipping

当有新版本时:

[cli-proxy-api-upgrade] current=6.9.6 latest=6.9.7
[cli-proxy-api-upgrade] downloading https://github.com/.../CLIProxyAPI_6.9.7_linux_amd64.tar.gz
[cli-proxy-api-upgrade] binary upgraded to 6.9.7, restarting service
[cli-proxy-api-upgrade] service restarted successfully
[cli-proxy-api-upgrade] upgrade to 6.9.7 completed

4. 回滚方案

systemctl disable --now cli-proxy-api-upgrade.timer
rm -f /etc/systemd/system/cli-proxy-api-upgrade.timer
rm -f /etc/systemd/system/cli-proxy-api-upgrade.service
systemctl daemon-reload

如需回退到旧版本二进制,手动替换即可:

cd /opt/cli-proxy-api/bin
tar xzf CLIProxyAPI_6.9.6_linux_amd64.tar.gz cli-proxy-api --overwrite
chmod +x cli-proxy-api
systemctl restart cli-proxy-api
printf "6.9.6" > .current-version

5. 注意事项

  1. 脚本通过 curl -L 跟随 GitHub redirect 解析版本号,需要代理能访问 github.com。
  2. 版本比对是字符串精确匹配(6.9.6 vs 6.9.7),不做语义化版本解析。
  3. 下载的 tarball 不会保留,脚本结束后 trap 自动清理临时目录。
  4. 二进制替换是原子操作(先写 .new 再 mv),避免写入中途崩溃导致文件损坏。
  5. 升级前不会自动备份旧二进制,如有需要可手动保留 tarball 或在脚本中添加备份逻辑。
  6. .current-version 文件需要手动初始化为当前安装版本,否则首次运行会将"无版本记录"视为需要升级。

6. 相关文档


附录 A:升级脚本完整源码

脚本也保存在 vault 中:cli-proxy-api-upgrade.sh