Repository navigation
Bug: Web terminal fails when accessing via network IP instead of localhost #5844
Description
Description
When running opencode web --hostname 0.0.0.0 and accessing the web interface via a public/network IP address (not localhost), the terminal fails to connect. The PTY WebSocket URL is incorrectly constructed as wss://pty/pty_xxx/connect where "pty" is treated as the hostname instead of the actual server address.
Root Cause:
The issue is in packages/desktop/src/app.tsx:
const url =
new URLSearchParams(document.location.search).get("url") ||
(location.hostname.includes("opencode.ai") || location.hostname.includes("localhost")
? `http://${host}:${port}`
: "/")When accessing via a public IP (e.g., 192.168.1.100):
location.hostname="192.168.1.100"- It does NOT include
"opencode.ai"or"localhost" - So
sdk.urldefaults to"/"(Probably assuming the desktop app environment)
The Terminal component constructs the WebSocket URL as:
new WebSocket(sdk.url + `/pty/${local.pty.id}/connect?directory=...`)When sdk.url = "/":
"/" + "/pty/pty_xxx/connect" = "//pty/pty_xxx/connect"
The browser interprets //pty/... as a protocol-relative URL where pty becomes the hostname.
Suggested Fix:
Replace the fallback "/" with window.location.origin:
const url =
new URLSearchParams(document.location.search).get("url") ||
(location.hostname.includes("opencode.ai") || location.hostname.includes("localhost")
? `http://${host}:${port}`
: window.location.origin)Safety Analysis: This fix will NOT affect the desktop app because:
- Tauri production uses
tauri://localhostprotocol →location.hostname= "localhost" ✓ - Tauri dev uses
http://localhost:1420→location.hostname= "localhost" ✓ - Both cases match the existing condition and never reach the fallback branch
OpenCode version
v1.0.164 (reproducible on latest)
Steps to reproduce
- Start OpenCode web server bound to all interfaces:
opencode web --hostname 0.0.0.0 --port 4096
- Access the web interface via network IP (not localhost):
http://192.168.1.100:4096 - Open a terminal session from the web interface
- Observe the WebSocket connection error in browser console:
wss://pty/pty_b3c0b4b830013ynf0aTbDhq6e7/connect
Expected: WebSocket connects to wss://192.168.1.100:4096/pty/pty_xxx/connect
Actual: WebSocket attempts to connect to wss://pty/pty_xxx/connect (invalid hostname)
Workaround: Pass URL explicitly via query parameter:
http://YOUR_PUBLIC_IP:4096?url=http://YOUR_PUBLIC_IP:4096
Screenshot and/or share link
Browser console showing the malformed WebSocket URL:
WebSocket connection to 'wss://pty/pty_b3c0b4b830013ynf0aTbDhq6e7/connect?directory=...' failed
Operating System
Ubuntu 24.04, also reproducible on other Linux distros (The server crashes if we try to do this on Ubuntu 22, we can ignore this as we are focusing on LTS version)
Terminal
Chrome/Firefox (any browser) - this is a web interface issue
Activity
fix(app): use window.location.origin for API URL fallback
FCCMac commentedon May 7, 2026
for anyone else running into this while the PR situation gets sorted out, I was able to create a port proxy in the windows firewall to forward the desired port on the windows machine down to the WSL network:
Once the forwarding rule is in place, I'm able to start opencode serve normally and reach it from anywhere on the LAN at
http://<windows_ip>:<port>Hope this helps anyone else who lands here!