feat: 维护台 maintain(巡检/补号/日志)+ 配置与 .gitignore

- Flask 控制台:Cliproxy 巡检、批量补号、流水线、运行日志
- codex_stats_oauth_dir 支持统计 Cliproxy auth 目录
- 忽略 accounts/oauth、.venv、本地 DB/日志与 new.py

Made-with: Cursor
This commit is contained in:
xiaobai committed 2026-03-21 20:51:55 +08:00
1 parent ebef9acd7c
commit a089da31cd
23 files changed
+3719 -631

No files matched your search

+91
View File
@@ -37,3 +37,94 @@ protocol_bot/
├── oauth_login.py # 协议级 OAuth 登录模块
└── README.md
```
## 一条命令跑整条流水线(推荐)
维护台把 **CPA 统计 / 补号 / 子进程 run.py / 迁出 JSON / 钉钉** 收在一个进程里:
```bash
pip install -r requirements.txt
export CODEX_MAINTAIN_PASSWORD='你的密码' # 默认 admin
export CODEX_MAINTAIN_SECRET='随机长串' # 生产务必设置
python -m maintain
# 本机浏览器: http://127.0.0.1:8765
```
### 用服务器公网 IP 访问不到 8765
默认 **`CODEX_MAINTAIN_HOST=127.0.0.1`**,进程只接受本机连接,**用别的机器浏览器打 `http://IP:8765` 会连不上**。
1. 启动前监听所有网卡:
```bash
export CODEX_MAINTAIN_HOST=0.0.0.0
export CODEX_MAINTAIN_PORT=8765 # 可选
python -m maintain
# 或 .venv/bin/python -m maintain
```
2. **放行端口**:云厂商安全组 / 本机 `ufw allow 8765/tcp` 或 `iptables` 等。
3. 仍不通时用 `ss -tlnp | grep 8765`(或 `netstat`)确认进程是否在 `0.0.0.0:8765` 监听。
4. 公网暴露务必强密码 + `CODEX_MAINTAIN_SECRET`;更好是前面加 **Nginx + HTTPS** 或 **SSH 隧道** / 仅内网访问。
### 服务器上 `pip install` 报 SSL / 连不上 PyPI
若出现 `SSLEOFError`、`EOF occurred in violation of protocol`、`Could not find a version that satisfies the requirement flask`,多半是到 **pypi.org** 的 HTTPS 被墙、劫持或本机证书过旧,**不是** Flask 真的不存在。
1. **换国内镜像再装**(推荐先试清华):
```bash
.venv/bin/pip install -U pip setuptools wheel
.venv/bin/pip install -r requirements.txt \
-i https://pypi.tuna.tsinghua.edu.cn/simple \
--trusted-host pypi.tuna.tsinghua.edu.cn
```
其它镜像可把域名换成:`https://mirrors.aliyun.com/pypi/simple/`(`--trusted-host mirrors.aliyun.com`)。
2. **更新系统 CA**(Debian/Ubuntu):`sudo apt-get update && sudo apt-get install -y ca-certificates`
3. 若机房**必须走 HTTP 代理**,先 `export https_proxy=...` 再执行 pip。
4. **Python 版本**:维护台依赖 Flask 3,需 **Python ≥ 3.8**;能升到 3.10+ 更省心(`python3.10 -m venv .venv`)。
| 页签 | 能力 |
|------|------|
| **任务中心** | 巡检:管理 API 探测 + 删失效本地 JSON;仍有缺口则**自动进程内批量注册**(每批 ≤ `max_auto_register_per_tick`,与手动补号**同一把 REGISTER_LOCK**) |
| **流水线** | **仅**定时/手动的 `run.py`→迁出;**不再**由巡检触发。仍按当前统计的有效缺口决定是否执行 |
| **代理池 / 配置** | Cliproxy **管理 Base** + **Key**;若成功账号 JSON **已迁入 CPA auth 目录**,务必填 **`codex_stats_oauth_dir`** 为该目录绝对路径(与 Manager 列表一致),否则统计/巡检仍只扫项目内 `accounts/oauth`,会一直像 0 个号 |
- **巡检**一把锁(INSPECT_LOCK);**子进程流水线**一把锁(PIPELINE_LOCK);**所有批量注册**(手动 + 巡检跟进)共 **REGISTER_LOCK**。
- 巡检与批量注册、流水线若并行可能争写 `accounts/oauth`,建议错峰。
- 状态文件:`maintain/data/pipeline_state.json`;业务库:`maintain/data/maintain.db`。
- **日志**:维护台进程内任务写 `maintain/data/maintain_runtime.log`;流水线(`run.py` 输出等)写 `maintain/data/pipeline.log`(可在 SQLite `pipeline_log_file` 改路径)。Web 页签 **「运行日志」** 可拉取文件尾;亦可 SSH 执行 `tail -f`。
- 设置项 **`codex_stats_oauth_dir`**:维护台统计、巡检探测、按失败删 JSON 的目录。留空 = `accounts/oauth`;与 Cliproxy **同机**且文件最终在 **`~/.cli-proxy-api/`** 等目录时,填该绝对路径。自定义目录下 JSON 若无 `type` 字段也会计入(Cliproxy 落盘常见);默认目录仍要求 `type=codex`。
- 新注册写入的 oauth JSON 会带 `cpa_id` / `cpa_code`;无 `cpa_id` 的旧文件计入唯一账号池(id=1)。默认只预置一条「账号池」;若仍使用**旧版库里的两条 CPA**,可自行在 SQLite 里删掉多余行或合并目标。
### 验证补号链路(不真实请求 OpenAI)
在虚拟环境里执行(会 mock `run._do_one`,只测 `run_batch` 并行、成功计数、`register context` 清理):
```bash
python3 -m venv .venv && .venv/bin/pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
.venv/bin/python -m maintain.verify_batch_register
```
应输出 `verify_batch_register: OK ...`。**真实注册**仍依赖代理、邮箱服务等:在维护台用「开始批量注册」先试 `count=1`,或巡检后看是否出现 `inspect_register` 任务并观察成功/失败数。
## 可选:独立 cpa_runner 进程
若不跑维护台、只想要旧版「定时子进程 + 迁文件」,仍可用 `scripts/cpa_runner.py` + `.env.cpa-runner`(见 `.env.cpa-runner.example`)。**维护台已开启「定时流水线」时不要同时跑本脚本。**
### systemd(仅维护台)
```ini
[Service]
WorkingDirectory=/home/ubt/protocol_chatgpt
EnvironmentFile=/home/ubt/protocol_chatgpt/.env
Environment=CODEX_MAINTAIN_HOST=0.0.0.0
ExecStart=/home/ubt/protocol_chatgpt/.venv/bin/python -m maintain
Restart=on-failure
```