Spaces:
Paused
Paused
KJ20051223 commited on
Commit ·
26cbbd0
1
Parent(s): 0c39688
docs: explain how to adapt other auth file formats
Browse files- README.md +96 -0
- README_EN.md +81 -0
README.md
CHANGED
|
@@ -268,6 +268,102 @@ systemctl restart CLIProxyAPI-cleaner.service
|
|
| 268 |
- `app.py` 是否在跑
|
| 269 |
- 反代后有没有多余的路径重写
|
| 270 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
## 致谢
|
| 272 |
|
| 273 |
感谢 **LinuxDo 社区** 提供交流环境,也感谢 LinuxDo 佬友 [@jingtai123](https://linux.do/t/topic/1810923),本项目基于其相关脚本思路继续二开整理。
|
|
|
|
| 268 |
- `app.py` 是否在跑
|
| 269 |
- 反代后有没有多余的路径重写
|
| 270 |
|
| 271 |
+
## 如何适配别的认证文件 / provider
|
| 272 |
+
|
| 273 |
+
当前仓库里默认实现,主要是围绕 **codex / openai / chatgpt** 这类认证文件来写的,尤其是“额度号后续 refresh + 复活探测”这一段,默认依赖以下前提:
|
| 274 |
+
|
| 275 |
+
- 本地 auth-file 是 JSON 对象
|
| 276 |
+
- 文件里能拿到 `refresh_token`
|
| 277 |
+
- refresh 逻辑兼容 OpenAI family 的 token 接口
|
| 278 |
+
- refresh 成功后能拿到新的 `access_token`(以及可能的新 `refresh_token` / `id_token`)
|
| 279 |
+
- 后续可以通过配置的 `api_call_url` 再 probe 一次
|
| 280 |
+
|
| 281 |
+
如果你要接入**别的认证文件格式**,思路也不复杂,主要改这几层:
|
| 282 |
+
|
| 283 |
+
### 1. 先改分类逻辑
|
| 284 |
+
|
| 285 |
+
看 `classify()` 和 `classify_api_call_response()`:
|
| 286 |
+
|
| 287 |
+
- 你自己的 provider 会返回什么错误
|
| 288 |
+
- 哪些错误应该判成 401 / token 失效
|
| 289 |
+
- 哪些错误应该判成额度耗尽 / rate limit / billing 限制
|
| 290 |
+
|
| 291 |
+
先把这层规则调整对,后面的处理策略才会跟着对。
|
| 292 |
+
|
| 293 |
+
### 2. 改 account_id / 请求头提取逻辑
|
| 294 |
+
|
| 295 |
+
看 `choose_account_id()` 和 `direct_probe_auth()`:
|
| 296 |
+
|
| 297 |
+
- 你的认证文件里账号 ID 在哪
|
| 298 |
+
- 你的上游接口需要什么 header
|
| 299 |
+
- 是否还需要别的字段而不是 `Chatgpt-Account-Id`
|
| 300 |
+
|
| 301 |
+
如果不是 OpenAI / Codex 体系,这里通常都要按你自己的接口规范改。
|
| 302 |
+
|
| 303 |
+
### 3. 改 auth-file 读取 / 写回格式
|
| 304 |
+
|
| 305 |
+
看 `load_auth_payload_from_path()` 和 `write_auth_payload()`:
|
| 306 |
+
|
| 307 |
+
如果你的认证文件不是当前这种 JSON 结构,而是别的字段命名,甚至不是 JSON,那这里要先适配。
|
| 308 |
+
|
| 309 |
+
### 4. 改 refresh 逻辑
|
| 310 |
+
|
| 311 |
+
看 `refresh_openai_family_tokens()`:
|
| 312 |
+
|
| 313 |
+
这一段现在是按 OpenAI family 的 refresh 流程写的。
|
| 314 |
+
如果你接的是别的 provider,最常见的改法是:
|
| 315 |
+
|
| 316 |
+
- 替换 token endpoint
|
| 317 |
+
- 替换请求参数
|
| 318 |
+
- 替换返回字段解析
|
| 319 |
+
- 把新的 token 写回成你自己的 auth-file 格式
|
| 320 |
+
|
| 321 |
+
也就是说,**这一段不是通用标准层,而是当前 provider 适配层**。
|
| 322 |
+
|
| 323 |
+
### 5. 改 revival 支持范围
|
| 324 |
+
|
| 325 |
+
看 `run_revival_cycle()`:
|
| 326 |
+
|
| 327 |
+
现在里面默认只对 `codex`、`openai`、`chatgpt` 做 revival。
|
| 328 |
+
如果你要支持别的 provider,需要:
|
| 329 |
+
|
| 330 |
+
- 把 provider 名加入支持列表
|
| 331 |
+
- 保证它有可读本地文件路径
|
| 332 |
+
- 保证你前面的 refresh / probe 逻辑已经适配好
|
| 333 |
+
|
| 334 |
+
### 6. 如果只是“能检测不能 refresh”也可以先接
|
| 335 |
+
|
| 336 |
+
有些 provider 可能你只能做到:
|
| 337 |
+
|
| 338 |
+
- 检测可用 / 不可用
|
| 339 |
+
- 检测 401
|
| 340 |
+
- 检测额度问题
|
| 341 |
+
|
| 342 |
+
但没法做 refresh。
|
| 343 |
+
|
| 344 |
+
这种情况也完全可以先接入,只是 revival 部分要降级处理,比如:
|
| 345 |
+
|
| 346 |
+
- 只禁用
|
| 347 |
+
- 到期后只做 probe
|
| 348 |
+
- 不做 token refresh
|
| 349 |
+
- 或者直接跳过复活机制
|
| 350 |
+
|
| 351 |
+
### 7. 最实用的适配顺序
|
| 352 |
+
|
| 353 |
+
如果你要接一个新 provider,我建议按这个顺序来:
|
| 354 |
+
|
| 355 |
+
1. 先让 `classify()` 正确识别状态
|
| 356 |
+
2. 再让 `/api-call` 主动探测可用
|
| 357 |
+
3. 再适配 auth-file 读取
|
| 358 |
+
4. 最后再补 refresh + revival
|
| 359 |
+
|
| 360 |
+
这样比较稳,不容易一上来把整套东西改乱。
|
| 361 |
+
|
| 362 |
+
简单说:
|
| 363 |
+
|
| 364 |
+
> 这个仓库不是只能支持 codex,但当前“最完整的一套实现”确实是以 codex / openai / chatgpt 这类 auth-file 为例。
|
| 365 |
+
> 如果换别的认证文件,重点是改 **分类、请求头、文件结构、refresh、revival** 这几层。
|
| 366 |
+
|
| 367 |
## 致谢
|
| 368 |
|
| 369 |
感谢 **LinuxDo 社区** 提供交流环境,也感谢 LinuxDo 佬友 [@jingtai123](https://linux.do/t/topic/1810923),本项目基于其相关脚本思路继续二开整理。
|
README_EN.md
CHANGED
|
@@ -168,6 +168,87 @@ systemctl daemon-reload
|
|
| 168 |
- Keep `allowed_hosts` strict
|
| 169 |
- Prefer binding to `127.0.0.1` and exposing only via Nginx
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
## Acknowledgements
|
| 172 |
|
| 173 |
Thanks to the **LinuxDo community** for the discussion space, and special thanks to LinuxDo contributor [@jingtai123](https://linux.do/t/topic/1810923). This project is a further derivative / secondary development based on that script direction.
|
|
|
|
| 168 |
- Keep `allowed_hosts` strict
|
| 169 |
- Prefer binding to `127.0.0.1` and exposing only via Nginx
|
| 170 |
|
| 171 |
+
## How to adapt other auth file formats / providers
|
| 172 |
+
|
| 173 |
+
The current repository provides its most complete implementation for **codex / openai / chatgpt** style auth files, especially for the “quota exhausted -> refresh -> revival probe” flow. That flow currently assumes:
|
| 174 |
+
|
| 175 |
+
- the local auth file is a JSON object
|
| 176 |
+
- a `refresh_token` is available
|
| 177 |
+
- refresh logic is compatible with the OpenAI-family token endpoint
|
| 178 |
+
- a new `access_token` can be written back after refresh
|
| 179 |
+
- a follow-up probe can be executed through the configured `api_call_url`
|
| 180 |
+
|
| 181 |
+
If you want to support **other auth file formats** or providers, the main adaptation points are:
|
| 182 |
+
|
| 183 |
+
### 1. Classification rules
|
| 184 |
+
|
| 185 |
+
Check `classify()` and `classify_api_call_response()`.
|
| 186 |
+
You need to decide:
|
| 187 |
+
|
| 188 |
+
- which errors mean 401 / invalid auth
|
| 189 |
+
- which errors mean quota exhaustion / rate limit / billing issue
|
| 190 |
+
- which states should stay recoverable instead of being deleted
|
| 191 |
+
|
| 192 |
+
### 2. Account ID and request header extraction
|
| 193 |
+
|
| 194 |
+
Check `choose_account_id()` and `direct_probe_auth()`.
|
| 195 |
+
If your provider does not use `Chatgpt-Account-Id`, you should adapt the required headers and account identity extraction here.
|
| 196 |
+
|
| 197 |
+
### 3. Auth file read / write format
|
| 198 |
+
|
| 199 |
+
Check `load_auth_payload_from_path()` and `write_auth_payload()`.
|
| 200 |
+
If your auth file is not the current JSON structure, this layer must be adapted first.
|
| 201 |
+
|
| 202 |
+
### 4. Refresh logic
|
| 203 |
+
|
| 204 |
+
Check `refresh_openai_family_tokens()`.
|
| 205 |
+
This is not a universal refresh layer; it is the current provider-specific implementation.
|
| 206 |
+
For another provider, you will usually need to replace:
|
| 207 |
+
|
| 208 |
+
- token endpoint
|
| 209 |
+
- request parameters
|
| 210 |
+
- response parsing
|
| 211 |
+
- token write-back format
|
| 212 |
+
|
| 213 |
+
### 5. Revival support scope
|
| 214 |
+
|
| 215 |
+
Check `run_revival_cycle()`.
|
| 216 |
+
Right now revival is only enabled for `codex`, `openai`, and `chatgpt`.
|
| 217 |
+
To support another provider, you need to:
|
| 218 |
+
|
| 219 |
+
- add that provider to the supported list
|
| 220 |
+
- ensure a valid local auth file path exists
|
| 221 |
+
- make sure refresh / probe logic is already adapted
|
| 222 |
+
|
| 223 |
+
### 6. Partial support is also fine
|
| 224 |
+
|
| 225 |
+
For some providers, you may only be able to support:
|
| 226 |
+
|
| 227 |
+
- availability detection
|
| 228 |
+
- 401 detection
|
| 229 |
+
- quota detection
|
| 230 |
+
|
| 231 |
+
and not token refresh. That is still fine. In that case, you can keep revival in a reduced mode, such as:
|
| 232 |
+
|
| 233 |
+
- disable first
|
| 234 |
+
- probe later
|
| 235 |
+
- skip refresh entirely
|
| 236 |
+
- or disable revival for that provider
|
| 237 |
+
|
| 238 |
+
### 7. Practical adaptation order
|
| 239 |
+
|
| 240 |
+
A stable way to adapt a new provider is:
|
| 241 |
+
|
| 242 |
+
1. make `classify()` correct
|
| 243 |
+
2. make `/api-call` probing work
|
| 244 |
+
3. adapt auth-file reading
|
| 245 |
+
4. then add refresh + revival
|
| 246 |
+
|
| 247 |
+
In short:
|
| 248 |
+
|
| 249 |
+
> this repository is not limited to codex only, but the most complete built-in implementation is currently centered on codex / openai / chatgpt style auth files.
|
| 250 |
+
> For other auth file formats, the main adaptation layers are **classification, headers, file structure, refresh, and revival**.
|
| 251 |
+
|
| 252 |
## Acknowledgements
|
| 253 |
|
| 254 |
Thanks to the **LinuxDo community** for the discussion space, and special thanks to LinuxDo contributor [@jingtai123](https://linux.do/t/topic/1810923). This project is a further derivative / secondary development based on that script direction.
|