fix(huddle): rewrite localhost LiveKit URLs using relay host#319
Open
tlongwell-block wants to merge 3 commits intomainfrom
Open
fix(huddle): rewrite localhost LiveKit URLs using relay host#319tlongwell-block wants to merge 3 commits intomainfrom
tlongwell-block wants to merge 3 commits intomainfrom
Conversation
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).
e742ec7 to
9846a38
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In multi-machine dev setups (e.g. two humans testing huddles), the relay hands back its configured
LIVEKIT_URLverbatim in the token response — typicallyws://localhost:7880. The second machine tries to connect to LiveKit onlocalhost, 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
localhostor127.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:
ws://localhost:3000ws://localhost:7880ws://10.0.0.201:3000ws://localhost:7880ws://10.0.0.201:7880wss://relay.example.comwss://livekit.example.comNo 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
.envchanges on the relay side.