Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cmd/tailcat/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/peterbourgon/ff/v4"
"github.com/peterbourgon/ff/v4/ffhelp"
"github.com/tailscale/tailcat"
"github.com/tailscale/tailcat/internal/localhostdns"
"tailscale.com/tstest"
)

Expand Down Expand Up @@ -656,3 +657,26 @@ func TestParsePortSetTargets(t *testing.T) {
}
}
}

// TestTargetDialer checks that only localhost targets are dialed
// through localhostdns.Resolver. It answers every other name with
// NXDOMAIN, so a mapping to a LAN hostname like "5555:android.lan:5555"
// could never connect through it.
func TestTargetDialer(t *testing.T) {
for _, tt := range []struct {
target string
wantLocal bool
}{
{"localhost:80", true},
{"LocalHost.:80", true},
{"app.localhost:80", true},
{"android.lan:5555", false},
{"db.example.com:5432", false},
{"notlocalhost:80", false},
} {
d := targetDialer(tt.target)
if gotLocal := d.Resolver == localhostdns.Resolver; gotLocal != tt.wantLocal {
t.Errorf("targetDialer(%q) uses localhostdns.Resolver = %v; want %v", tt.target, gotLocal, tt.wantLocal)
}
}
}
46 changes: 34 additions & 12 deletions cmd/tailcat/tailcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,27 +1433,26 @@ func server(logf logger.Logf, serveSpec string, execArgs []string) {
s.AllowClient = allow.Contains
}

// localDialer dials the local services that incoming connections
// are proxied to. Its resolver answers "localhost" itself with
// both loopback addresses; see the localhostdns package comment
// for why the OS resolver can't be trusted to (issue #108).
localDialer := &net.Dialer{Resolver: localhostdns.Resolver}

// tcpTarget returns the host:port a served TCP port is proxied
// to: its mapping's target if the serve spec gave one, else the
// same port on localhost.
tcpTarget := func(port uint16) string {
if t, ok := targets[port]; ok {
return t
}
return fmt.Sprintf("localhost:%v", port)
return net.JoinHostPort("localhost", strconv.Itoa(int(port)))
}

tcpForwardTo := func(ipPortStr string) func(net.Conn) {
// tcpForwardTo returns a handler that proxies a connection to
// hostPort. That is an IP:port for exit node traffic, but a served
// port's target (see tcpTarget) is "localhost:port" or a mapping's
// host:port, whose host can be a name such as "android.lan".
tcpForwardTo := func(hostPort string) func(net.Conn) {
d := targetDialer(hostPort)
return func(c net.Conn) {
localConn, err := localDialer.Dial("tcp", ipPortStr)
localConn, err := d.Dial("tcp", hostPort)
if err != nil {
logf("error proxying to %v: %v", ipPortStr, err)
logf("error proxying to %v: %v", hostPort, err)
c.Close()
return
}
Expand Down Expand Up @@ -1549,7 +1548,7 @@ func server(logf logger.Logf, serveSpec string, execArgs []string) {
if services.Contains("exit-node") {
// Being an exit node includes localhost without needing
// to specify all the local port ranges.
return tcpForwardTo(fmt.Sprintf("localhost:%v", port))
return tcpForwardTo(net.JoinHostPort("localhost", strconv.Itoa(int(port))))
}
if oneShotStdout {
return func(c net.Conn) {
Expand Down Expand Up @@ -1578,7 +1577,7 @@ func server(logf logger.Logf, serveSpec string, execArgs []string) {
if !portSet.Contains(port) {
return nil // RST
}
return tcpForwardTo(fmt.Sprintf("localhost:%v", port))
return tcpForwardTo(net.JoinHostPort("localhost", strconv.Itoa(int(port))))
}

if err := s.Start(); err != nil {
Expand Down Expand Up @@ -1646,6 +1645,29 @@ func server(logf logger.Logf, serveSpec string, execArgs []string) {
select {}
}

// localDialer dials the local services that incoming connections are
// proxied to. Its resolver answers "localhost" itself with both
// loopback addresses; see the localhostdns package comment for why
// the OS resolver can't be trusted to (issue #108).
var localDialer = &net.Dialer{Resolver: localhostdns.Resolver}

// systemDialer dials proxy targets that aren't localhost names, using
// the system resolver.
var systemDialer = new(net.Dialer)

// targetDialer returns the dialer for proxying to target, a host:port.
// Localhost names go through localDialer. Any other host, such as a
// port mapping's "android.lan", needs the system resolver:
// localhostdns.Resolver answers every name but localhost with NXDOMAIN.
// A target that doesn't parse as a host:port also gets systemDialer,
// whose Dial then fails with the parse error.
func targetDialer(target string) *net.Dialer {
if host, _, err := net.SplitHostPort(target); err == nil && localhostdns.IsLocalhost(host) {
return localDialer
}
return systemDialer
}

// parseFilesFlag parses the --files flag value: a directory with an
// optional :ro, :rw, :wo, or :wo+ suffix. An empty value means the current
// directory, read-only. It returns the file service and the mode's
Expand Down
11 changes: 9 additions & 2 deletions internal/localhostdns/localhostdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ func answer(query []byte) ([]byte, error) {
// isLocalhost reports whether the absolute, possibly mixed-case DNS
// name is "localhost" or a subdomain of it.
func isLocalhost(name dnsmessage.Name) bool {
n := strings.ToLower(name.String())
return n == "localhost." || strings.HasSuffix(n, ".localhost.")
return IsLocalhost(name.String())
}

// IsLocalhost reports whether host, a possibly mixed-case DNS name
// with or without a trailing period, is "localhost" or a subdomain of
// it: the names [Resolver] answers with the loopback addresses.
func IsLocalhost(host string) bool {
n := strings.ToLower(strings.TrimSuffix(host, "."))
return n == "localhost" || strings.HasSuffix(n, ".localhost")
}
Loading