Skip to content

Add IPv6 support to macOS port forwarding and nginx - #52

Merged
peterjaap merged 1 commit into
qoliber:mainfrom
Aquive:fix/ipv6-port-forwarding
Mar 31, 2026
Merged

peterjaap merged 1 commit into
qoliber:mainfrom
Aquive:fix/ipv6-port-forwarding

Conversation

@Aquive

@Aquive Aquive commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes connection refused errors on macOS when accessing .test domains via HTTPS (or HTTP). This is the macOS counterpart to #20, which solved the same root cause on Linux.

Problem

macOS resolves .test domains (via /etc/resolver/test) to both ::1 (IPv6) and 127.0.0.1 (IPv4). Browsers and curl prefer IPv6 and connect to [::1]:443 first. However:

  1. The pf redirect rules only handled IPv4 (inet), so [::1]:443 was never forwarded to nginx on port 8443
  2. Nginx on macOS had EnableIPv6 = false, so it wasn't listening on [::]:8080 / [::]:8443

The result: immediate connection refused, with no fallback to IPv4.

#20 addressed this on Linux by adding [::]:port listen directives to nginx, noting "macOS is unaffected as it uses port forwarding." Turns out macOS is affected too — just at the pf layer rather than the nginx layer.

Changes

  • internal/portforward/pf.go — Add inet6 redirect rules for loopback, bump LaunchDaemon version to 4
  • internal/nginx/vhost.go — Enable EnableIPv6 = true on macOS so nginx listens on [::]:8080 and [::]:8443

Design decisions

Why always rewrite pf rules on bootstrap?

The original Setup() only wrote pf rules on fresh install or when the LaunchDaemon version changed. This created a problem: the pf rules file and the LaunchDaemon version could get out of sync (e.g., if the daemon was upgraded but the rules write failed, or if a user ran bootstrap multiple times). Since the rules file is small and writing it is idempotent, we now always write and reload it during bootstrap. The LaunchDaemon still uses version-gated upgrades since it requires unload/load cycles.

Why enable IPv6 in nginx on macOS?

Even with IPv6 pf rules redirecting [::1]:443 → [::1]:8443, nginx needs to accept connections on the IPv6 address. Enabling EnableIPv6 adds listen [::]:8080 and listen [::]:8443 directives to the vhost templates, which is what #20 already did for Linux.

Test plan

  • Fresh install: remove pf config (sudo rm /Library/LaunchDaemons/com.magebox.portforward.plist /etc/pf.anchors/com.magebox), run magebox bootstrap, verify .test domains work
  • Upgrade from v3: set plist to MageBox-Version-3, run magebox bootstrap, verify pf rules contain inet6 lines and .test domains work
  • Verify curl -v https://<project>.test connects via [::1]:443
  • Verify IPv4 still works: curl --resolve "<project>.test:443:127.0.0.1" https://<project>.test
  • Linux: no behavioral change (already uses EnableIPv6 = true and doesn't use pf)

🤖 Generated with Claude Code

macOS resolves .test domains to both ::1 (IPv6) and 127.0.0.1 (IPv4),
and clients try IPv6 first. The pf redirect rules only handled IPv4,
causing connection refused errors. This adds inet6 pf rules and enables
IPv6 listen directives in nginx on macOS.

Also fixes the upgrade path to always rewrite pf rules on bootstrap,
preventing stale rules when the anchor file changes between versions.

Related: qoliber#20

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@Aquive
Aquive force-pushed the fix/ipv6-port-forwarding branch from 33953c0 to 7b67925 Compare March 30, 2026 12:40
@peterjaap
peterjaap merged commit d355ba9 into qoliber:main Mar 31, 2026
9 checks passed
@peterjaap

Copy link
Copy Markdown
Collaborator

Blind merge since I don't run on Macs :-)

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