Skip to content

fix(huddle): rewrite localhost LiveKit URLs using relay host#319

Open
tlongwell-block wants to merge 3 commits intomainfrom
fix/livekit-localhost-rewrite
Open

fix(huddle): rewrite localhost LiveKit URLs using relay host#319
tlongwell-block wants to merge 3 commits intomainfrom
fix/livekit-localhost-rewrite

Conversation

@tlongwell-block
Copy link
Copy Markdown
Collaborator

Problem

In multi-machine dev setups (e.g. two humans testing huddles), the relay hands back its configured LIVEKIT_URL verbatim in the token response — typically ws://localhost:7880. The second machine tries to connect to LiveKit on localhost, which is its own machine where no LiveKit server is running. Result: WebSocket connection failed: Could not connect to the server.

Fix

Before connecting to LiveKit, the desktop client checks if the LiveKit URL points to localhost or 127.0.0.1. If so, and the relay URL (SPROUT_RELAY_URL) points to a real host, it swaps the hostname. The relay URL is the one thing every client has that is guaranteed reachable — they are already talking to it.

Behavior matrix:

Relay URL LiveKit URL (from relay) Result
ws://localhost:3000 ws://localhost:7880 No rewrite (both local)
ws://10.0.0.201:3000 ws://localhost:7880 Rewritten to ws://10.0.0.201:7880
wss://relay.example.com wss://livekit.example.com No rewrite (neither localhost)

No server-side changes. No config changes needed. Just works.

Testing

Tested with two machines on the same LAN — second machine can now join huddles without any .env changes on the relay side.

In multi-machine dev setups, the relay hands back its configured
LIVEKIT_URL verbatim (typically ws://localhost:7880). A second
machine joining the huddle can't reach localhost — it's a different
host.

The desktop client already knows a reachable relay host (from
SPROUT_RELAY_URL). Before connecting to LiveKit, if the LiveKit URL
points to localhost/127.0.0.1 and the relay URL doesn't, swap the
hostname. No-op when both are localhost (single-machine dev) or when
neither is localhost (production).
@tlongwell-block tlongwell-block force-pushed the fix/livekit-localhost-rewrite branch from e742ec7 to 9846a38 Compare April 14, 2026 18:46
WKWebView's networking bypasses Cloudflare WARP, so the browser can't
reach external WebSocket servers behind corporate VPN. The Rust process
IS covered by WARP.

Adds a local WS proxy (ws_proxy.rs, 156 lines) that:
- Binds TcpListener on 127.0.0.1:0 (random port, localhost only)
- Accepts WebSocket from webview via accept_hdr_async
- Captures path+query from the upgrade request (/rtc/v1?access_token=...)
- Connects upstream via tokio-tungstenite (goes through WARP)
- Bidirectional relay via futures-util forward()

Safety:
- wss:// scheme validation (no arbitrary protocol SSRF)
- Mutex-protected init (no TOCTOU race)
- Rejects upstream URL changes after first init (no cross-session routing)
- Semaphore-capped at 16 concurrent connections
- No production unwrap()s (poisoned mutex recovery)

Frontend: getProxiedLivekitUrl() starts the proxy for remote relays,
rewrites LiveKit URL to ws://127.0.0.1:{port}. Local dev (relay on
localhost) connects directly — no proxy needed.

Zero new crate dependencies (tokio-tungstenite + futures-util already
in Cargo.lock via tauri-plugin-websocket).

Codex review: 6/10 round 2 (up from 4/10). Remaining concern is that
wss:// validation doesn't fully close SSRF — acceptable because the
upstream URL comes from the relay token response (server-controlled),
not user input.
WebRTC signaling works through our WS proxy, but the browser can't
reach LiveKit's ICE candidates (pod internal IPs) for media. The
browser's WebRTC stack bypasses WARP.

When connecting to a remote relay (not localhost), set
iceTransportPolicy=relay in the RTCConfiguration. This forces the
browser to use ONLY TURN relay candidates — no direct ICE connections.

Critical: do NOT override iceServers. LiveKit's join response includes
the TURN server URL with time-limited HMAC credentials. Overriding
iceServers would strip those credentials and break TURN allocation.
We only set the transport policy and let LiveKit handle the rest.

Local dev (relay on localhost) skips both the proxy and relay mode —
direct connections work fine on the same machine.
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