diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 21b0c7a9f4..fb05e62066 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -679,8 +679,8 @@ Full command surface (`bili --help` prints an abridged version). Precedence ever | `bili pi-test [opts --] [args]` | Like `bili pi`, but adds `--no-extensions` (clean-room test — the proxy owns compression) | | `bili codex [opts --] [args]` | Proxy + **codex** | | `bili claude [opts --] [args]` | Proxy + **claude** (Claude Code CLI) | -| `bili omp [opts --] [args]` | Proxy + **omp** (pi-based) | -| `bili opencode [opts --] [args]` | Proxy + **opencode** | +| `bili omp [opts --] [args]` | Proxy + **omp** (pi-based) — opencode zen models ride the default `opencode.ai` MITM whitelist (#1405) | +| `bili opencode [opts --] [args]` | Proxy + **opencode** — built-in zen gateway (`opencode.ai`) cert-MITM'd by default (#1405); hosts declared in `~/.config/opencode/opencode.json` / `~/.omp/omp.json` `providers[].baseURL` join the MITM whitelist automatically (#1411), and `~/.aider.conf.yml` / `~/.config/opencode/.aider.conf.yml` are watched for the same purpose | | `bili hermes [opts --] [args]` | Proxy + **hermes-agent** (`/bili/` rewrite) | | `bili dsh [opts --] [args]` | Proxy + **deepseek-harness** (non-loopback upstreams via proxy envs, loopback via `/bili/` rewrite — #535; args like `--profile web "task"` pass through) | | `bili codebuddy [opts --] [args]` | Proxy + **codebuddy** (Tencent CodeBuddy Code CLI) — `CODEBUDDY_BASE_URL` `/bili/` rewrite, OpenAI chat-completions wire; budget via `CODEBUDDY_AUTO_COMPACT_WINDOW` (#640) | @@ -797,7 +797,7 @@ Supported MITM clients: > **ZCode native mode (#1145):** ZCode is the only client in this list that also has a **native plugin mode** — `bili plugin install zcode` routes model traffic through the provider store (`~/.zcode/v2/config.json`, or `provider_config.json` on v3.14+) and needs no GUI proxy/CA setup at all. Native mode does not touch the MITM surface: if you run both, keep the GUI proxy settings (and the `"mitm://zcode.z.ai": { "passthrough": true }` route, #661) for login traffic. Full mechanics: README's *ZCode* section. -MITM is scoped to a **whitelist** of model hosts (`open.bigmodel.cn`, `api.anthropic.com`, `api.openai.com`, `chatgpt.com`). All other HTTPS hosts are blind-tunnelled — billion-context never decrypts non-model traffic. +MITM is scoped to a **whitelist** of model hosts (`open.bigmodel.cn`, `api.anthropic.com`, `api.openai.com`, `chatgpt.com`), plus per-lane stock-gateway defaults that discovery auto-seeds where a lane's config exists (e.g. `opencode.ai` — opencode's built-in zen gateway from `opencode auth login`, #1405). All other HTTPS hosts are blind-tunnelled — billion-context never decrypts non-model traffic. > **CONNECT-only clients (`http.proxy`):** many IDE-class clients (CodeBuddy, Cursor, Windsurf, …) expose no model base-URL setting — they route all traffic through an HTTP proxy via `CONNECT`. Such a client is only decrypted when its model host is whitelisted above (or discovered/auto-whitelisted by a launcher); otherwise its tunnels are **blind**: no error, but also **no compression**, because billion-context never sees the cleartext. This misconfiguration is surfaced explicitly (#897): the first blind tunnel per host logs a one-time `BLIND TUNNEL WARNING` with the fix steps; `GET /__bili/health` and `/__bili/stats` report `blindTunnels` (count + exact target hosts, loopback-only); and `acp_status` gains an `UNDECRYPTED TRAFFIC (instance-level)` section while such tunnels exist. Fix: add the client's model domain to `"mitm".domains` (or `BILI_MITM_DOMAINS`), restart, and trust the root CA per the steps below. Note proxy logs mask non-public target hosts by default (``, #255) — set `BILI_LOG_MASK_HOSTS=0` to see them verbatim in your local log. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 6eb0f05593..9f7a3915f4 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -653,8 +653,8 @@ | `bili pi-test [opts --] [args]` | 类似 `bili pi`,但追加 `--no-extensions`(干净测试 —— 压缩完全由代理负责) | | `bili codex [opts --] [args]` | 代理 + **codex** | | `bili claude [opts --] [args]` | 代理 + **claude**(Claude Code CLI) | -| `bili omp [opts --] [args]` | 代理 + **omp**(pi 内核) | -| `bili opencode [opts --] [args]` | 代理 + **opencode** | +| `bili omp [opts --] [args]` | 代理 + **omp**(pi 内核)—— opencode zen 模型走默认 `opencode.ai` MITM 白名单(#1405) | +| `bili opencode [opts --] [args]` | 代理 + **opencode** —— 内置 zen 网关(`opencode.ai`)默认证书 MITM(#1405);`~/.config/opencode/opencode.json` / `~/.omp/omp.json` 中 `providers[].baseURL` 声明的主机自动加入 MITM 白名单(#1411),`~/.aider.conf.yml` / `~/.config/opencode/.aider.conf.yml` 同受监听 | | `bili hermes [opts --] [args]` | 代理 + **hermes-agent**(`/bili/` 重写) | | `bili dsh [opts --] [args]` | 代理 + **deepseek-harness**(`/bili/` 重写;`--profile web "task"` 等参数原样透传) | | `bili codebuddy [opts --] [args]` | 代理 + **codebuddy**(Tencent CodeBuddy Code CLI)—— `CODEBUDDY_BASE_URL` `/bili/` 重写,OpenAI chat-completions wire;预算对齐走 `CODEBUDDY_AUTO_COMPACT_WINDOW`(#640) | @@ -771,7 +771,7 @@ export ANTHROPIC_BASE_URL="http://localhost:8787/bili/https://api.anthropic.com" > **ZCode 原生模式(#1145):** ZCode 是这张表里唯一同时拥有**原生插件模式**的客户端 —— `bili plugin install zcode` 经 provider store(`~/.zcode/v2/config.json`,v3.14+ 为 `provider_config.json`)路由模型流量,完全不需要 GUI 代理/CA 设置。原生模式不碰 MITM 面:若两者并用,请保留 GUI 代理设置(以及 `"mitm://zcode.z.ai": { "passthrough": true }` 路由,#661)供登录流量使用。完整机制:README「ZCode」小节。 -MITM 只对一份**白名单**中的模型域名生效(`open.bigmodel.cn`、`api.anthropic.com`、`api.openai.com`、`chatgpt.com`)。其余 HTTPS 主机全部盲转发 —— billion-context 绝不解密非模型流量。 +MITM 只对一份**白名单**中的模型域名生效(`open.bigmodel.cn`、`api.anthropic.com`、`api.openai.com`、`chatgpt.com`),外加发现机制按 lane 自动播种的自带网关默认域名(如 `opencode.ai` —— opencode `auth login` 的内置 zen 网关,#1405)。其余 HTTPS 主机全部盲转发 —— billion-context 绝不解密非模型流量。 > **只有 `http.proxy` 设置的客户端(CONNECT-only):** 许多 IDE 系客户端(CodeBuddy、Cursor、Windsurf……)没有模型 base-URL 设置 —— 它们把全部流量经 HTTP 代理以 `CONNECT` 方式发出。这类客户端只有在其模型域名被加进上面的白名单后才会被解密;否则其隧道是**盲**的:不报错,但也**不会压缩**,因为 billion-context 根本看不到明文。该误配置现在会被显式暴露(#897):每个目标域名的首个盲隧道会在日志打一条一次性 `BLIND TUNNEL WARNING` 并附修复步骤;`GET /__bili/health` 与 `/__bili/stats` 输出 `blindTunnels`(计数 + 精确目标域名,仅 loopback);存在此类隧道时 `acp_status` 会多一节 `UNDECRYPTED TRAFFIC (instance-level)`。修复:把该客户端的模型域名加进 `"mitm".domains`(或 `BILI_MITM_DOMAINS`),重启,并按下文信任根 CA。注意代理日志默认对非公开目标域名脱敏(``,#255)—— 设 `BILI_LOG_MASK_HOSTS=0` 可在本地日志看到真实域名。 diff --git a/src/client-config.ts b/src/client-config.ts index 5291622307..4e060ef4ed 100644 --- a/src/client-config.ts +++ b/src/client-config.ts @@ -89,6 +89,13 @@ export interface OpencodeConfig { providers: Record; } +/** opencode's built-in "zen" gateway (`opencode auth login`): the baseURL + * (`https://opencode.ai/zen/v1/messages`) comes from the models.dev catalog, + * not from any local config file, so discovery cannot see it — seed it like + * the other stock model gateways so `bili omp` / `bili opencode` cert-MITM + * zen traffic instead of blind-tunneling it (#1405). */ +export const OPENCODE_DEFAULT_MODEL_HOSTS = ["opencode.ai"]; + export interface HermesProvider { api?: string; } @@ -1212,6 +1219,21 @@ export function resolveOpencodeConfigFile(env: NodeJS.ProcessEnv): string { return path.join(dir, "opencode.jsonc"); } +// The complete set of files whose contents feed config.opencode (exactly what +// readOpencodeConfigRoot reads): the three global candidates + an explicit +// OPENCODE_CONFIG when set. The discovery mtime cache must watch this same set +// or edits go stale (#1411). +/** Candidate opencode config paths for the discovery mtime cache. Order is + * irrelevant (watch/mtime only); readOpencodeConfigRoot reads config.json + * first by preference — no need to keep the two orders in lockstep. */ +export function opencodeConfigFiles(env: NodeJS.ProcessEnv): string[] { + const xdg = nonEmpty(env.XDG_CONFIG_HOME) ? env.XDG_CONFIG_HOME : path.join(os.homedir(), ".config"); + const dir = path.join(xdg, "opencode"); + const files = OPENCODE_CONFIG_FILES.map((f) => path.join(dir, f)); + if (nonEmpty(env.OPENCODE_CONFIG)) files.push(env.OPENCODE_CONFIG); + return files; +} + // opencode accepts JSONC (comments, trailing commas) in every config file; a strict // JSON.parse silently yields "no config" for .jsonc users. export function parseConfigText(text: string): Record | undefined { @@ -1538,7 +1560,10 @@ export const AIDER_DEFAULT_MODEL_HOSTS = [ * default_config_files entries over earlier ones; aider lists them * cwd → git root → home): home > git root > cwd. Only top-level scalar * values are parsed; anything else is ignored. */ -export function readAiderConfUrls(cwd: string, env: NodeJS.ProcessEnv = process.env): string[] { +// The .aider.conf.yml candidates feeding config.aider.baseUrls (home > git +// root > cwd, aider's own resolution order). Exported so the discovery mtime +// cache watches the same set (#1411). +export function aiderConfFiles(cwd: string, env: NodeJS.ProcessEnv = process.env): string[] { const home = nonEmpty(env.HOME) ? env.HOME! : os.homedir(); const candidates = [path.join(home, ".aider.conf.yml")]; let dir = cwd; @@ -1552,7 +1577,11 @@ export function readAiderConfUrls(cwd: string, env: NodeJS.ProcessEnv = process. dir = parent; } candidates.push(path.join(cwd, ".aider.conf.yml")); - for (const file of candidates) { + return [...new Set(candidates)]; +} + +export function readAiderConfUrls(cwd: string, env: NodeJS.ProcessEnv = process.env): string[] { + for (const file of aiderConfFiles(cwd, env)) { let text: string; try { text = fs.readFileSync(file, "utf8"); diff --git a/src/discover.ts b/src/discover.ts index 343405d9dc..0919b9a1b7 100644 --- a/src/discover.ts +++ b/src/discover.ts @@ -3,7 +3,7 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; -import { loadClientConfig, resolvePiHome, resolveCodebuddyHome, resolveQoderHome, resolveTraeHome, resolveZcodeHome, zcodePersonalConfigFiles, nonEmpty, QODER_DEFAULT_MODEL_HOSTS, TRAE_DEFAULT_MODEL_HOSTS, AIDER_DEFAULT_MODEL_HOSTS, type ClientConfig } from "./client-config.js"; +import { loadClientConfig, resolvePiHome, resolveOmpHome, resolveCodebuddyHome, resolveQoderHome, resolveTraeHome, resolveZcodeHome, zcodePersonalConfigFiles, opencodeConfigFiles, aiderConfFiles, nonEmpty, QODER_DEFAULT_MODEL_HOSTS, TRAE_DEFAULT_MODEL_HOSTS, AIDER_DEFAULT_MODEL_HOSTS, OPENCODE_DEFAULT_MODEL_HOSTS, type ClientConfig } from "./client-config.js"; const TTL_MS = 2000; @@ -43,6 +43,14 @@ export function extractHttpsHosts(config: ClientConfig): string[] { if (config.zcode) { for (const prov of Object.values(config.zcode.providers)) push(prov.baseURL); } + // opencode/omp ride cert-MITM via HTTPS_PROXY; custom providers declared in + // their configs (options.baseURL / baseUrl) blind-tunnel without this (#1411). + for (const prov of Object.values(config.opencode?.providers ?? {})) { + if (typeof prov.baseURL === "string") push(prov.baseURL); + } + for (const prov of Object.values(config.omp?.providers ?? {})) { + if (typeof prov.baseUrl === "string") push(prov.baseUrl); + } if (config.codebuddy) { push(config.codebuddy.codebuddyBaseUrl); for (const u of config.codebuddy.modelUrls ?? []) push(u); @@ -69,6 +77,14 @@ export function extractHttpsHosts(config: ClientConfig): string[] { for (const h of AIDER_DEFAULT_MODEL_HOSTS) push(`https://${h}`); } } + if (config.opencode || config.omp) { + // #1405: opencode's built-in zen gateway has no config file to + // discover (auth-login users get their baseURL from the models.dev + // catalog). Seeded for both pi-family lanes — omp runs zen models too + // — and coexists with explicit provider base URLs (a user can mix + // custom providers with zen models in one setup). + for (const h of OPENCODE_DEFAULT_MODEL_HOSTS) push(`https://${h}`); + } return out; } @@ -88,6 +104,11 @@ function configFilePaths(env: NodeJS.ProcessEnv): string[] { path.join(process.cwd(), ".codebuddy", "models.json"), path.join(resolveQoderHome(env), "settings.json"), path.join(resolveTraeHome(env), "traecli.yaml"), + // Must mirror exactly what loadClientConfig reads for the fields + // extractHttpsHosts consumes, or edits go stale in the mtime cache (#1411). + ...opencodeConfigFiles(env), + path.join(resolveOmpHome(env), "models.yml"), + ...aiderConfFiles(process.cwd(), env), ]; } diff --git a/tests/discover.test.ts b/tests/discover.test.ts index 97d3c90b37..87da38c058 100644 --- a/tests/discover.test.ts +++ b/tests/discover.test.ts @@ -17,6 +17,7 @@ import { zcodeDataRoot, zcodeStoreFileFor, QODER_DEFAULT_MODEL_HOSTS, + OPENCODE_DEFAULT_MODEL_HOSTS, type ClientConfig, } from "../src/client-config.ts"; @@ -310,6 +311,40 @@ test("extractHttpsHosts: aider → default hosts when undeclared; declared https ); }); +test("extractHttpsHosts: opencode provider baseURL + omp provider baseUrl discovered, coexist with other lanes (#1411)", () => { + const hosts = extractHttpsHosts({ + opencode: { + providers: { + myrelay: { baseURL: "https://custom.example.com/v1" }, + plain: { baseURL: "http://insecure.example.com" }, + }, + }, + omp: { providers: { relay2: { baseUrl: "https://OMP.RELAY.example.com/v1" } } }, + claude: { anthropicBaseUrl: "https://relay.example.com" }, + }); + assert.ok(hosts.includes("custom.example.com"), `opencode custom host present: ${hosts.join(",")}`); + assert.ok(hosts.includes("omp.relay.example.com"), `omp custom host present (lowercased): ${hosts.join(",")}`); + assert.ok(hosts.includes("relay.example.com"), `coexists with other lanes: ${hosts.join(",")}`); + assert.ok(!hosts.includes("insecure.example.com"), `http dropped: ${hosts.join(",")}`); +}); + +test("extractHttpsHosts: partial opencode/omp configs are safe — seed present once #1405 lands (#1411)", () => { + assert.deepEqual(extractHttpsHosts({ opencode: {} }), OPENCODE_DEFAULT_MODEL_HOSTS); + assert.deepEqual(extractHttpsHosts({ omp: {} }), OPENCODE_DEFAULT_MODEL_HOSTS); +}); + +test("extractHttpsHosts: opencode/omp → zen gateway default host, coexists with other lanes (#1405)", () => { + assert.deepEqual(extractHttpsHosts({ opencode: {} }), OPENCODE_DEFAULT_MODEL_HOSTS); + assert.deepEqual(extractHttpsHosts({ omp: {} }), OPENCODE_DEFAULT_MODEL_HOSTS); + assert.deepEqual(extractHttpsHosts({ opencode: {}, omp: {} }), OPENCODE_DEFAULT_MODEL_HOSTS); + const mixed = extractHttpsHosts({ + opencode: { providers: { custom: { baseURL: "https://custom.example.com/v1" } } }, + claude: { anthropicBaseUrl: "https://relay.example.com" }, + }); + assert.ok(mixed.includes("opencode.ai"), `zen host present: ${mixed.join(",")}`); + assert.ok(mixed.includes("relay.example.com"), `coexists with other lanes: ${mixed.join(",")}`); +}); + async function withTempHome(fn: (home: string, env: NodeJS.ProcessEnv) => Promise): Promise { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-disc-")); const savedHome = process.env.HOME; @@ -322,7 +357,9 @@ async function withTempHome(fn: (home: string, env: NodeJS.ProcessEnv) => Pro ZCODE_DATA_BASE_DIR: path.join(tmp, ".zcode"), PI_CODING_AGENT_DIR: path.join(tmp, ".pi", "agent"), CODEBUDDY_CONFIG_DIR: path.join(tmp, ".codebuddy"), + XDG_CONFIG_HOME: path.join(tmp, ".config"), }; + delete env.OPENCODE_CONFIG; return await fn(tmp, env); } finally { process.env.HOME = savedHome; @@ -355,6 +392,15 @@ test("discoverMitmDomains: returns union of https hosts from client configs", as }); }); +test("discoverMitmDomains: seeds opencode zen gateway host even with no client configs (#1405)", async () => { + await withTempHome((_home, env) => { + _resetDiscoveryCacheForTest(); + const domains = discoverMitmDomains(env); + assert.ok(domains.includes("opencode.ai"), `zen gateway host present: ${domains.join(",")}`); + return Promise.resolve(); + }); +}); + test("discoverMitmDomains: codebuddy settings.json + models.json hosts discovered", async () => { await withTempHome((home, env) => { const cbDir = path.join(home, ".codebuddy"); @@ -437,4 +483,95 @@ test("discoverMitmDomains: discovers hosts from provider_config.json (new person const after = discoverMitmDomains(env); assert.ok(after.includes("newp.example.com"), `present after provider_config.json appears: ${after.join(",")}`); }); -}); \ No newline at end of file +}); + +test("discoverMitmDomains: discovers opencode custom provider host from opencode.json, coexists with other lanes (#1411)", async () => { + await withTempHome((home, env) => { + const ocDir = path.join(home, ".config", "opencode"); + fs.mkdirSync(ocDir, { recursive: true }); + fs.writeFileSync( + path.join(ocDir, "opencode.json"), + JSON.stringify({ + provider: { myrelay: { npm: "@ai-sdk/openai-compatible", options: { baseURL: "https://custom.example.com/v1" } } }, + }), + ); + writeZcodeConfig(home, ["https://zlane.example.com/anthropic"]); + _resetDiscoveryCacheForTest(); + const domains = discoverMitmDomains(env); + assert.ok(domains.includes("custom.example.com"), `opencode custom provider host present: ${domains.join(",")}`); + assert.ok(domains.includes("zlane.example.com"), `coexists with other lanes: ${domains.join(",")}`); + return Promise.resolve(); + }); +}); + +test("discoverMitmDomains: discovers omp provider baseUrl from models.yml (#1411)", async () => { + await withTempHome((home, env) => { + fs.mkdirSync(path.join(home, ".pi", "agent"), { recursive: true }); + fs.writeFileSync( + path.join(home, ".pi", "agent", "models.yml"), + "providers:\n myrelay:\n baseUrl: https://omp.example.com/v1\n", + ); + _resetDiscoveryCacheForTest(); + const domains = discoverMitmDomains(env); + assert.ok(domains.includes("omp.example.com"), `omp provider host present: ${domains.join(",")}`); + return Promise.resolve(); + }); +}); + +test("discoverMitmDomains: OPENCODE_CONFIG file's provider host discovered (#1411)", async () => { + await withTempHome((home, env) => { + const custom = path.join(home, "my-opencode-config.jsonc"); + fs.writeFileSync(custom, '{ "provider": { "relay": { "options": { "baseURL": "https://explicit.example.com/v1" } } } }'); + env.OPENCODE_CONFIG = custom; + _resetDiscoveryCacheForTest(); + const domains = discoverMitmDomains(env); + assert.ok(domains.includes("explicit.example.com"), `OPENCODE_CONFIG host present: ${domains.join(",")}`); + return Promise.resolve(); + }); +}); + +test("discoverMitmDomains: opencode.json edit invalidates cache after TTL (#1411)", async () => { + await withTempHome(async (home, env) => { + const ocDir = path.join(home, ".config", "opencode"); + fs.mkdirSync(ocDir, { recursive: true }); + const cfgPath = path.join(ocDir, "opencode.json"); + const write = (url: string): void => { + fs.writeFileSync(cfgPath, JSON.stringify({ provider: { r: { options: { baseURL: url } } } })); + }; + write("https://v1.example.com"); + const baseMtime = Math.floor(fs.statSync(cfgPath).mtimeMs / 1000); + _resetDiscoveryCacheForTest(); + const first = discoverMitmDomains(env); + assert.ok(first.includes("v1.example.com")); + + write("https://v2.example.com"); + fs.utimesSync(cfgPath, baseMtime + 60, baseMtime + 60); + const withinTtl = discoverMitmDomains(env); + assert.strictEqual(withinTtl, first, "within TTL: still cached"); + + await new Promise((r) => setTimeout(r, 2100)); + + const after = discoverMitmDomains(env); + assert.ok(after.includes("v2.example.com"), `v2 present after rescan: ${after.join(",")}`); + assert.ok(!after.includes("v1.example.com"), `v1 gone: ${after.join(",")}`); + }); +}); + +test("discoverMitmDomains: .aider.conf.yml edit invalidates cache after TTL (#1411)", async () => { + await withTempHome(async (home, env) => { + const confPath = path.join(home, ".aider.conf.yml"); + fs.writeFileSync(confPath, "openai-api-base: https://av1.example.com/v1\n"); + const baseMtime = Math.floor(fs.statSync(confPath).mtimeMs / 1000); + _resetDiscoveryCacheForTest(); + const first = discoverMitmDomains(env); + assert.ok(first.includes("av1.example.com")); + + fs.writeFileSync(confPath, "openai-api-base: https://av2.example.com/v1\n"); + fs.utimesSync(confPath, baseMtime + 60, baseMtime + 60); + await new Promise((r) => setTimeout(r, 2100)); + + const after = discoverMitmDomains(env); + assert.ok(after.includes("av2.example.com"), `av2 present after rescan: ${after.join(",")}`); + assert.ok(!after.includes("av1.example.com"), `av1 gone: ${after.join(",")}`); + }); +});