# Codex HTTP Gateway Go 网关提供 `POST /v1/chat/completions`。通过自签发的 Bearer key 验证后,网关经 Unix socket 把文本请求交给运行 Codex CLI 的 worker 容器。两个容器分别挂载网关 key 的哈希文件和 Codex 登录目录。 ## HTTP 接口 - 客户端的 OpenAI SDK `base_url` 为 `https://<入口>/v1`,`api_key` 填网关签发的 key,模型名填 `codex`。实际模型默认由 Codex CLI 选择,部署者也可以设置 `CODEX_MODEL`。 - 请求支持 `model`、`messages` 和可选的 `stream: false`。`messages` 支持字符串 `content` 以及 `system`、`developer`、`user`、`assistant` 四种角色,最后一条消息须来自 `user`。CLI 通过文本对话记录接收这些角色。每次请求都是独立的,多轮对话由客户端随请求重发。 - 响应包含 `choices[0].message.content`,CLI 返回用量时也包含 `usage`。第一版支持非流式文本,流式输出、文件和工具调用参数会返回 `400`。 - 缺少 key 或 key 无效时返回 `401`。单 worker 同时执行一条请求,忙时返回 `429`,最长执行 90 秒。 ```sh curl "$GATEWAY_URL/v1/chat/completions" \ -H "Authorization: Bearer $GATEWAY_KEY" \ -H 'Content-Type: application/json' \ -d '{"model":"codex","messages":[{"role":"user","content":"Reply with hello."}]}' ``` ## 构建与部署 Gitea Actions 在 `ces-control` 上跑 Go 测试、检查 Compose 配置并构建两个镜像。以下部署命令在确认的远程环境中执行。在检出的项目目录运行 `docker compose build`,然后建立状态目录并登录。默认宿主机端口是 `127.0.0.1:18080`,可以通过 `GATEWAY_PORT` 修改。 ### 建立状态目录并签发网关 key 在部署机的管理员 shell 执行。这里的 `GATEWAY_KEY` 是客户端凭据,与 OpenAI 或 ChatGPT 的登录凭据独立。用受控的密码管理器保存该值。服务器的网关只读取 SHA-256 哈希。 ```sh install -d -m 0755 /opt/codex-http-gateway install -d -m 0700 -o 10001 -g 10001 \ /opt/codex-http-gateway/run /opt/codex-http-gateway/codex-home umask 077 GATEWAY_KEY="cgw_$(openssl rand -hex 32)" printf '%s' "$GATEWAY_KEY" | openssl dgst -sha256 -r | cut -d ' ' -f 1 \ >> /opt/codex-http-gateway/keys.sha256 chown 10001:10001 /opt/codex-http-gateway/keys.sha256 chmod 0400 /opt/codex-http-gateway/keys.sha256 ``` 文件每行对应一个已签发的 key。追加新哈希或移除被撤销的哈希后,运行 `docker compose up -d --no-build --force-recreate gateway` 重新加载文件。用完当前 shell 中的明文后运行 `unset GATEWAY_KEY`。 ### 给 worker 登录 构建完镜像后,可以用设备码将 worker 绑定到你自己的 ChatGPT/Codex 账号。登录前在 ChatGPT 安全设置里启用设备码。在部署机运行以下命令,然后由你在自己的浏览器里完成授权。 ```sh docker compose run --rm --no-deps \ --entrypoint /opt/codex/node_modules/.bin/codex worker \ -c 'cli_auth_credentials_store="file"' login --device-auth ``` 文件凭据保存在宿主机 `/opt/codex-http-gateway/codex-home/auth.json`,worker 在使用过程中可以更新文件。不要把凭据放进仓库、镜像或聊天。个人账号模式仅供账号持有人使用。若需要向其他人签发网关 key,请给 worker 使用专用的 OpenAI 项目 API key,并从部署机受保护文件经 stdin 登录。 ```sh docker compose run --rm --no-deps -T \ --entrypoint /opt/codex/node_modules/.bin/codex worker \ -c 'cli_auth_credentials_store="file"' login --with-api-key \ < /path/to/protected-openai-api-key docker compose up -d --no-build ``` 个人账号使用设备码登录后,同样运行 `docker compose up -d --no-build` 启动服务。配置参考 [Codex CLI 登录方式](https://learn.chatgpt.com/docs/auth)和 [OpenAI 使用条款](https://openai.com/policies/terms-of-use/)。 ### 公网 HTTPS 入口 将宿主机现有反向代理的 `POST /v1/chat/completions` 转发到 `http://127.0.0.1:18080`,保留 `Authorization` 请求头,并将上游读取超时设为至少 105 秒。公网入口请用 HTTPS,以保护网关 key 和请求内容。 没有域名时,可以为固定公网 IPv4 申请短期受信证书。Certbot 5.4 或更新版本支持用 webroot 验证 IP。用现有反向代理在公网 TCP/80 提供 `/.well-known/acme-challenge/`,申请证书后在公网 TCP/443 配置 HTTPS。证书有效期约六天,启用定时续期和续签后重载代理。具体命令见 [Let's Encrypt 的 IP 地址证书指南](https://letsencrypt.org/2026/03/11/shorter-certs-certbot)。 公网入口上线前可用 `http://127.0.0.1:18080` 验证。无 Authorization 应返回 `401`,带有效 key 和已登录 worker 应返回 `200` 及 `chat.completion`。