Repository navigation
cmd/tailcat: resolve port mapping hostnames with the system resolver - #132
Conversation
| 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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>
dc4e8ff to
252a978
Compare
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>
Problem
tailcat servedials every proxied connection throughlocalDialer, and itslocalhostdns.Resolveranswerslocalhostitself and every other name with NXDOMAIN (#108). That's fine for plain served ports, which always go to localhost. But aport:targetmapping can name a host on the server's network, andparsePortSetaccepts one (TestParsePortSetTargetshas5555:android.lan:5555). Dialing that target always fails withno such hostunless the name is in the hosts file, so the client's connection gets reset.Repro with a name that public DNS resolves to loopback:
Fix
Only localhost names (
localhost,*.localhost) go throughlocalDialernow. Any other mapping target uses a plainnet.Dialerwith the system resolver. IP literal targets need no lookup and behave the same as before.localhostdnsexportsIsLocalhostso the check and the resolver use the same rule.Verification
TestTargetDialerincmd/tailcat/cli_test.gochecks that localhost names getlocalDialerand LAN/DNS hostnames don't. It doesn't build on main becausetargetDialerdoesn't exist yet, and it passes with this change.TestServePortMappingwith the targetlocaltest.me:<echoPort>in place of127.0.0.1:<echoPort>. On main it fails (connection reset by peerafter 30s). With this change it passes in 5s.go test ./...,go vet ./...,gofmt, and cross builds for windows/amd64 and linux withbuild-tags.txtall pass.🤖 Generated with Claude Code