Skip to content

Bug: Web terminal fails when accessing via network IP instead of localhost #5844

Description

@ashutoshpw

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.url defaults 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://localhost protocol → 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

  1. Start OpenCode web server bound to all interfaces:
    opencode web --hostname 0.0.0.0 --port 4096
  2. Access the web interface via network IP (not localhost):
    http://192.168.1.100:4096
    
  3. Open a terminal session from the web interface
  4. 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

  1. added a commit that references this issue on Dec 25, 2025
    8ca9a6c
  2. FCCMac commented on May 7, 2026

    @FCCMac

    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:

    # get the WSL IP
    bash.exe -c "ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'"
    
    # create proxy to forward windows port to WSL (admin required)
    netsh interface portproxy add v4tov4 listenport=<port> listenaddress=0.0.0.0 connectport=<port> connectaddress=<wsl_ip_from above command>

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

help-wantedwebRelates to opencode on web / desktop

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions