Skip to content

fix: bind to 127.0.0.1 when config.host is 'localhost'#27

Merged
jesse23 merged 3 commits intomainfrom
fix/localhost-ipv6-binding
Apr 9, 2026
Merged

fix: bind to 127.0.0.1 when config.host is 'localhost'#27
jesse23 merged 3 commits intomainfrom
fix/localhost-ipv6-binding

Conversation

@jesse23
Copy link
Copy Markdown
Owner

@jesse23 jesse23 commented Apr 9, 2026

Summary

  • On modern macOS/Node.js (v17+), 'localhost' resolves to ::1 (IPv6 loopback), causing httpServer.listen to bind on IPv6 only — making the server unreachable for browsers that block 127.0.0.1 URLs but expect localhost
  • Server now binds to 127.0.0.1 when config.host === 'localhost' while keeping localhost in the displayed and browser-opened URL
  • Internal CLI↔server API calls (BASE_URL) always use 127.0.0.1; browser URL is built from loadConfig().host at call time in cmdGo

Test plan

  • Set "host": "localhost" in ~/.config/webtty/config.json
  • Run webtty — server should start and print listening on http://localhost:<port>
  • Browser opens to http://localhost:<port>/s/main (not 127.0.0.1)
  • Verify webtty stop and other API commands still work (they use 127.0.0.1 internally)

🤖 Generated with Claude Code

On modern macOS/Node, 'localhost' resolves to ::1 (IPv6), causing the
server to fail or be unreachable when browsers block 127.0.0.1 URLs.
Internal CLI API calls always use 127.0.0.1; the browser URL uses the
configured host as-is so 'localhost' is preserved in opened URLs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts how webtty binds and advertises its loopback host to avoid IPv6-only binds when users configure host: "localhost" (notably on macOS/Node 17+), while keeping the user-facing URL as localhost.

Changes:

  • Bind the HTTP server to 127.0.0.1 when config.host === "localhost" but log localhost as the display host.
  • Clarify CLI internal API base URL documentation (always 127.0.0.1).
  • Build the browser-opened URL in cmdGo from loadConfig().host rather than BASE_URL.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/server/index.ts Separates bind host vs display host; binds to IPv4 when config host is localhost.
src/cli/http.ts Updates comment clarifying why CLI uses 127.0.0.1 for internal calls.
src/cli/commands.ts Uses config host for the browser URL in cmdGo and imports PORT for URL construction.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/cli/commands.ts Outdated
Comment thread src/cli/commands.ts Outdated
Extract toBrowserHost() to map bind-all addresses to 'localhost' and
bracket bare IPv6 addresses, so the browser URL is always valid and
routable regardless of config.host value. Add unit tests for all cases.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jesse23 jesse23 merged commit 08a6bf0 into main Apr 9, 2026
3 checks passed
@jesse23 jesse23 deleted the fix/localhost-ipv6-binding branch April 9, 2026 17:05
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.

2 participants