Skip to content

cmd/tailcat: resolve port mapping hostnames with the system resolver - #132

Merged
bradfitz merged 3 commits into
tailscale:mainfrom
breken-ai:fix-mapping-hostname-dns
Nov 19, 2026
Merged

bradfitz merged 3 commits into
tailscale:mainfrom
breken-ai:fix-mapping-hostname-dns

Conversation

@breken-ai

Copy link
Copy Markdown
Contributor

Problem

tailcat serve dials every proxied connection through localDialer, and its localhostdns.Resolver answers localhost itself and every other name with NXDOMAIN (#108). That's fine for plain served ports, which always go to localhost. But a port:target mapping can name a host on the server's network, and parsePortSet accepts one (TestParsePortSetTargets has 5555:android.lan:5555). Dialing that target always fails with no such host unless the name is in the hosts file, so the client's connection gets reset.

Repro with a name that public DNS resolves to loopback:

tailcat serve 18080:localtest.me:8080
# client: read tcp [fd7a:...]:...: connection reset by peer
# server (-v): error proxying to localtest.me:8080: dial tcp: lookup localtest.me on ...: no such host

Fix

Only localhost names (localhost, *.localhost) go through localDialer now. Any other mapping target uses a plain net.Dialer with the system resolver. IP literal targets need no lookup and behave the same as before. localhostdns exports IsLocalhost so the check and the resolver use the same rule.

Verification

  • New TestTargetDialer in cmd/tailcat/cli_test.go checks that localhost names get localDialer and LAN/DNS hostnames don't. It doesn't build on main because targetDialer doesn't exist yet, and it passes with this change.
  • I also ran an end-to-end check, not committed because it needs public DNS: a copy of TestServePortMapping with the target localtest.me:<echoPort> in place of 127.0.0.1:<echoPort>. On main it fails (connection reset by peer after 30s). With this change it passes in 5s.
  • go test ./..., go vet ./..., gofmt, and cross builds for windows/amd64 and linux with build-tags.txt all pass.

🤖 Generated with Claude Code

Comment thread cmd/tailcat/tailcat.go Outdated
tcpForwardTo := func(ipPortStr string) func(net.Conn) {
return func(c net.Conn) {
localConn, err := localDialer.Dial("tcp", ipPortStr)
localConn, err := targetDialer(ipPortStr).Dial("tcp", ipPortStr)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This "ipPortStr" is was misleading before but now it's really misleading. Time to clean this up and fix the callers?

Wait, tcpForwardTo is only called with a netip.AddrPort.String()? So how is this ever used? I'm not convinced this change does anything.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It isn't only called with a netip.AddrPort.String(). That's true for the exit-node OnTCPForward caller (dst.String()). But OnTCP calls tcpForwardTo(tcpTarget(port)) for every port in the serve spec, and tcpTarget returns targets[port]. That value comes from parsePortTarget, which returns net.JoinHostPort(host, targetPort) for any non-empty host. So tailcat serve 5555:android.lan:5555 puts android.lan:5555 into tcpForwardTo, and on main that goes to localDialer, whose localhostdns.Resolver returns NXDOMAIN for every name that isn't localhost.

Here's the end-to-end repro. It's TestServePortMapping with the mapping changed from %d:127.0.0.1:%d to %d:localtest.me:%d (public DNS resolves localtest.me to 127.0.0.1, and it isn't in /etc/hosts):

  • main @ 52fbad3: client to mapped port: exit status 1 ... read tcp [fd7a:...]:50414: connection reset by peer
  • this branch: --- PASS: TestServePortMappingHostname

I didn't commit that test because it needs public DNS. TestTargetDialer covers the dialer choice offline.

On the cleanup: I renamed the parameter to hostPort, added a comment saying which callers pass what, and built the two localhost:%v call sites with net.JoinHostPort (252a978). I also rebased onto main. go test ./..., go vet ./... and gofmt -l . are clean.

bradfitz

This comment was marked as resolved.

breken-ai and others added 2 commits October 7, 2026 17:21
Every connection a server proxies is dialed through localDialer, whose
localhostdns.Resolver answers "localhost" itself and every other name
with NXDOMAIN (see tailscale#108). That's right for the plain served ports,
which always go to localhost, but a "port:target" mapping may name a
host on the server's network, and parsePortSet accepts one
("5555:android.lan:5555"). The dial for such a target always failed
with "no such host" unless the name was in the hosts file, so the
client's connection was reset.

Dial only localhost names through localDialer, and every other target
with a plain net.Dialer and the system resolver. IP literal targets
need no lookup and are unaffected.

Signed-off-by: breken-ai <312387581+breken-ai@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
tcpForwardTo is called with an IP:port only for exit node traffic. Served
ports pass tcpTarget(port), which is "localhost:port" or a mapping's
host:port, so the parameter was never only an IP. Say so, and build the
localhost targets with net.JoinHostPort like parsePortTarget does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: breken-ai <312387581+breken-ai@users.noreply.github.com>
@breken-ai
breken-ai force-pushed the fix-mapping-hostname-dns branch from dc4e8ff to 252a978 Compare October 8, 2026 00:24
The target passed to tcpForwardTo is fixed when the handler is made,
so classify it once instead of on every proxied connection. And use
one shared net.Dialer for non-localhost targets instead of allocating
one per dial; a net.Dialer is safe for concurrent use.

Also document what targetDialer does with a target that isn't a valid
host:port.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
@bradfitz
bradfitz merged commit 1dd28b9 into tailscale:main Nov 19, 2026
10 checks passed
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