Skip to content

feat(mitm): auto-discover model domains from client configs (no hardcoding) - #125

Merged
ranxianglei merged 1 commit into
masterfrom
2026-08-13_mitm-discovery
Aug 12, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-08-13_mitm-discovery

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What

Replace the hardcoded MITM domain whitelist with auto-discovery: bili reads each coding client's config file and extracts all model-provider HTTPS hostnames at runtime. Official / third-party / future domains are covered without hardcoding anything (except the 3 endpoints truly baked into client binaries with no config).

Why

DEFAULT_MITM_DOMAINS was a hardcoded list (open.bigmodel.cn, api.anthropic.com, …). It only covered ZCode's open.bigmodel.cn provider — zcode.z.ai and api.z.ai (also official ZCode) were missing, and any third-party relay was invisible. Domains change; hardcoding them is brittle.

bili is a client-agnostic proxy: it doesn't (and shouldn't) know which app sent a given CONNECT. So discovery targets domains, not clients — it builds one global whitelist from the union of all client configs, then MITM-decrypts any CONNECT whose host is in that set and blind-tunnels the rest.

How

  • New src/client-config.ts — the existing claude/codex/pi config readers moved here out of launcher.ts (avoids a circular import: launcher.ts already imports DEFAULT_MITM_DOMAINS from mitm.ts). Plus a new ZCode reader (~/.zcode/v2/config.json → provider.*.options.baseURL). launcher.ts re-exports everything for back-compat.
  • New src/discover.ts — discoverMitmDomains() reads all client configs, extracts HTTPS hosts (unwrapping /bili/-prefixed URLs), dedupes, lowercases. Cached by config-file mtime + 2s TTL: at most one stat every 2 s, full re-scan only when a config file actually changes. Pure, side-effect-free except the cache.
  • src/mitm.ts — isMitmHost now folds in discoverMitmDomains(): the candidate set is DEFAULT + user mitm.domains + discovered. DEFAULT_MITM_DOMAINS reduced to only the 3 endpoints hardcoded in client binaries with no config to discover from: api.anthropic.com (Claude), api.openai.com + chatgpt.com (codex-login / ChatGPT). open.bigmodel.cn removed (now discovered from zcode/pi configs). The security-boundary comment is updated to reflect that the list is now minimal + that discovery is the primary source.

Verification

typecheck: clean (no circular import)
tests: 332/332 pass (323 + 9 new in tests/discover.test.ts)
build: ok
runtime: discoverMitmDomains() against real ~/.zcode + ~/.codex + ~/.pi → 6 domains
         (ai.comfly.org, api.openai.com, open.bigmodel.cn, coding.dashscope.aliyuncs.com, zcode.z.ai, api.z.ai)
         isMitmHost(open.bigmodel.cn|zcode.z.ai|api.z.ai|api.anthropic.com) → true
         isMitmHost(example.com) → false  (non-model hosts still tunneled)

Diff: +564/-154 across 7 files (client-config.ts +194, discover.ts +108, launcher.ts −154 net, mitm.ts ±20, 2 test files).

Test plan

  • bili start --debug on a machine with ZCode + codex + pi installed → log shows the discovered domains in the MITM whitelist (banner).
  • ZCode pointed at bili (proxy + cert) → all 6 providers route through bili, compression injected (was: only open.bigmodel.cn).
  • Add a new provider in ~/.codex/config.toml while bili runs → within 2 s it's auto-MITM'd (no restart).
  • A non-model HTTPS site (e.g. example.com) through bili → blind-tunneled, not decrypted.

Follow-up (separate PRs): the bili zcode launcher (Electron --proxy-server + --ignore-certificate-errors-spki-list injection) + .desktop override so the desktop-icon launch is covered.

@ranxianglei
ranxianglei merged commit c5e1c42 into master Aug 12, 2026
3 checks passed
@ranxianglei
ranxianglei deleted the 2026-08-13_mitm-discovery branch October 15, 2026 05:31
cskwork pushed a commit to cskwork/billion-context that referenced this pull request Oct 21, 2026
… stop

Per user direction (改成复用): the launcher's spawned proxy now persists after
the client exits, so subsequent 'bili <client>' invocations reuse it instead of
spawning a new one. This makes reuse robust across terminals (the detached proxy
survives terminal close — it was already detached:true, but runLaunch killed it
on client exit, defeating reuse).

- ensureProxyRunning writes a PID file (~/.local/share/billion-context/
  launcher-proxy-<port>.pid) after a successful spawn.
- runLaunch no longer kills the spawned proxy on client exit; logs
  'proxy left running at ORIGIN for reuse (run bili stop to terminate)'.
- runTestPi (bili test pi smoke) KEEPS kill-on-exit (one-off, clean up).
- New 'bili stop' command reads all launcher-proxy-*.pid files, kills the
  processes, removes the files. stopLauncherProxy() exported for cli.ts.
- HELP updated.

Discovery (ranxianglei#125, merged) ensures the standing/spawned proxy covers ALL client
domains at runtime (isMitmHost → discoverMitmDomains), so reuse always finds a
domain-matching proxy. 343/343 tests, typecheck clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant