Skip to content

Route daemon WebSockets through the environment proxy - #7504

Merged
ymichael merged 1 commit into
get-bb:mainfrom
lanjak:fix-4957-daemon-ws-proxy
Nov 16, 2026
Merged

ymichael merged 1 commit into
get-bb:mainfrom
lanjak:fix-4957-daemon-ws-proxy

Conversation

@lanjak

@lanjak lanjak commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

The launcher starts the host daemon with --use-env-proxy (packages/bb-app/src/launcher.ts:2944, :3206), and it adds loopback to NO_PROXY. That flag makes Node's global http and https agents and fetch use HTTP(S)_PROXY. But ws always sets its own createConnection, and Node uses the global agent only when the request has no createConnection. The daemon opens its server connection (apps/host-daemon/src/websocket-constructor.ts:11-18) and its connect tunnel (apps/host-daemon/src/connect-tunnel/index.ts:154) with no agent. So these sockets connected directly and ignored the proxy. A daemon that can reach the server only through a proxy could not connect (#4957). The docs listed this as a known gap ("including daemon WebSockets").

What changed

  • apps/host-daemon/src/websocket-constructor.ts: new webSocketAgent(url) returns https.globalAgent for wss: and https: URLs, and http.globalAgent for other URLs. The constructor now always passes agent, with headers when they are set.
  • apps/host-daemon/src/connect-tunnel/index.ts: the default tunnel socket passes agent: webSocketAgent(url).
  • apps/host-daemon/src/ws.d.ts: adds agent?: Agent to the local ws type definitions.
  • apps/host-daemon/src/websocket-constructor.test.ts (new): starts a local proxy and a local WebSocket server. It runs the real module in a child node --use-env-proxy with HTTP(S)_PROXY and NO_PROXY=localhost,127.0.0.1,::1. A ws:// dial goes through the proxy, with and without headers. A wss:// dial sends CONNECT to the proxy. A loopback dial connects directly. The test is skipped when Node does not support --use-env-proxy, and it needs no network.
  • docs/configuration.md, packages/templates/src/templates/bb-guide-customization.md: the proxy text now includes daemon WebSockets.

No wire change and no protocol version bump. No new dependencies.

Limits:

  • Without --use-env-proxy, for example on Node 22.19-22.20 where the launcher drops the flag, the global agents do not use the proxy, so behavior is the same as before.
  • A plain ws:// server through a proxy needs a proxy that forwards upgrades. wss:// uses CONNECT.
  • The daemon WebSockets now use Node's global agents. Upgraded sockets leave the agent pool.

How you verified

  • Before the fix (new test, old websocket-constructor.ts): AssertionError: expected { proxied: 'error ENOTFOUND', ...(3) } to deeply equal { proxied: 'header=sent', ...(3) }. The proxy saw no request.
  • After the fix: the new test passes. connect-tunnel.test.ts (10 tests) and server-connection.test.ts (22 tests) pass.
  • pnpm exec turbo run lint lint:type-aware typecheck test --filter=@bb/host-daemon --filter=@bb/templates: lint and typecheck pass. @bb/templates 47 of 47. @bb/host-daemon 874 passed and 1 failed: injected-skills.test.ts timed out at 15 s under high machine load. It fails the same way on unchanged main at 88cf8f644c in the same run and passes alone.
  • A second, independent run: @bb/host-daemon typecheck passes, and the three affected test files pass, 33 of 33.
  • Tested on Node 24.18 and 24.22. Not tested on Node 22, with a real proxy such as Squid, or with a full TLS session through the tunnel.

Fixes #4957

AGENT GENERATED

The daemon starts with --use-env-proxy, which makes Node's global
http and https agents honor HTTP_PROXY, HTTPS_PROXY and NO_PROXY.
The ws library supplies its own createConnection, so a WebSocket
without an explicit agent dials the target directly. On a machine
that can reach the internet only through a CONNECT proxy, the server
connection and the bb connect tunnel never connect.

Pass Node's global agent for the URL scheme to the server WebSocket
constructor and the default connect tunnel socket. With no proxy set,
or without --use-env-proxy, the global agents connect directly, so
behavior is unchanged. Loopback stays direct through the NO_PROXY
entries the launcher already adds.

Fixes get-bb#4957
@ymichael
ymichael merged commit cf1c145 into get-bb:main Nov 16, 2026
50 checks passed
@TPOHH TPOHH mentioned this pull request Nov 24, 2026
2 tasks done
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.

Host daemon WebSocket connections ignore HTTPS_PROXY/NO_PROXY, so a daemon behind a CONNECT proxy never enrolls

2 participants