Skip to content

fix(remote-control): honor HTTP proxies for WebSocket connections - #5004

Open
sailist wants to merge 2 commits into
release/2026-11-16from
bug-387-11-16-websocket-proxy
Open

sailist wants to merge 2 commits into
release/2026-11-16from
bug-387-11-16-websocket-proxy

Conversation

@sailist

@sailist sailist commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

在只能通过 HTTP 代理访问公网的 CLI / desktop 内嵌服务器环境中,Remote Control 的 relay WebSocket 仍尝试直连,导致连接无法建立。此 PR 让 management、HTTP tunnel 和 stream WebSocket 遵循现有代理环境变量,使这些环境中的 Remote Control 可以通过代理连接 relay。

Bug Reproduction Steps

  1. 在无法直接访问 relay 的环境中设置有效的 HTTP_PROXY / HTTPS_PROXY,然后启用 Remote Control。
  2. 修复前,relay WebSocket 绕过代理并直连,初始 management / HTTP tunnel 建连失败。
  3. 修复后,这些连接通过代理的 HTTP CONNECT 隧道建立;NO_PROXY 命中的目标和本地 WebSocket 仍直连。

Root Cause

ws 不会读取 CLI 为 fetch / undici 安装的全局 dispatcher。Remote Control 在 connectWebSocketAttempt 中直接构造 WebSocket,但没有传 Node HTTP agent,因此代理环境变量对这条传输路径无效。

这是根本修复:在 relay 建连边界补齐 ws 原生支持的代理 agent,而不是增加重试或绕过现有传输。

Code Changes

复用现有代理地址和 NO_PROXY 解析,为每个 Remote Control client 的 relay origin 创建一个 HttpsProxyAgent。该 agent 只注入 relay 连接,不安装为全局 agent,因此本地 loopback WebSocket 不会受到影响。

 RemoteControlClient
+  relayAgent = createRelayProxyAgent(relayOrigin, env)
   connectRelay
     ├─ management WebSocket
     ├─ HTTP tunnel WebSocket
     └─ stream WebSocket
       connectWebSocketAttempt
+        agent: relayAgent(两种鉴权尝试共用)
   openStream
     └─ local WebSocket(agent 仍为空)
  • ws 使用 HTTP_PROXY / http_proxy,wss 使用 HTTPS_PROXY / https_proxy,保留 HTTP ALL_PROXY 回退和 NO_PROXY 语义。
  • 未配置代理或命中 NO_PROXY 时维持原直连路径;非法代理 URL 沿用现有容错方式,警告后直连。
  • 将 https-proxy-agent@7.0.6 补为 remote-control 的直接依赖,并更新锁文件。
  • 在既有建连用例中接入真实本地 CONNECT 代理,使用无法直接解析的中性 relay 域名验证两条初始 relay 连接都经过代理,同时保留 Authorization 断言;测试总数不变。

Behavior Changes and Affected Users

行为 修复前 修复后 依赖旧行为的用户 退路
配置代理后的 relay WebSocket 忽略代理并直连 通过代理的 HTTP CONNECT 建连 配置了代理但当前依赖 relay 直连的 CLI / desktop 用户 将 relay 主机加入 NO_PROXY
未配置代理,或 relay 命中 NO_PROXY 直连 直连,无行为变化 现有直连用户及本地 relay 开发场景 无需
Remote Control 的本地 loopback WebSocket 直连本地 kap-server 仍直连,不注入 relay agent 所有使用 Remote Control 的本地转发连接 无需
非法 HTTP 代理 URL ws 不读取配置,静默直连 每个 client 启动时警告一次,然后直连 代理 URL 非法但当前仍可直连的用户 修正或移除代理变量

受影响模块与覆盖:

  • packages/remote-control:本地和 m0-dev 均为 51/51;typecheck、build 通过。真实回归覆盖代理 CONNECT、management / HTTP tunnel 和既有 Authorization 行为;stream 与鉴权回退复用同一已接线的 connectRelay 路径。
  • packages/agent-core-v2:仅导出既有代理解析函数,typecheck 通过;既有 NO_PROXY 工具测试继续覆盖回环地址和匹配语义。
  • 发布产物:remote-control、kap-server、CLI build 通过,CLI bundle 已确认包含代理实现。
  • 全仓 lint:0 error。无 UI 变化,无文档更新需求;现有文档已经描述这些代理变量的预期行为。
完整验证记录与未改动区域失败
  • m0-dev 全量:17488 passed / 6 failed / 30 skipped;853 个测试文件通过、2 个失败、5 个跳过。
  • 其中 packages/agent-core-v2/test/remote/process.test.ts 的 2 个全量失败在隔离重跑中随全文件 23/23 通过。
  • apps/kimi-code/test/tui/kimi-tui-startup.test.ts 的 4 个 OAuth 启动用例在全量和隔离重跑中均为 5 秒超时。该文件及其行为不在本 PR diff 或代理建连路径中,因此这里不宣称全量测试通过。
  • lint:imports 仍有 4 处位于未改文件的报告;sherif 仍有 3 个缺失 package.json 的 workspace warning,均未由本 PR 引入或扩大。

Checklist

  • I have read the CONTRIBUTING document.
  • Related issue: N/A(内部缺陷报告,复现步骤已在上文给出)。
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 97d0324

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T02:03:31.743329Z 97d0324 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 17, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@97d0324
npx https://pkg.pr.new/@moonshot-ai/kimi-code@97d0324

commit: 97d0324

@sailist

sailist commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

Re-submitted against main in #5037 (same two commits rebased onto main@origin).

This branch has not been deployed

No deployments
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.

2 participants