From 72b6d98298c5ec6826a13a29f49eca095ca30bdb Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 28 Apr 2021 19:05:24 -0700 Subject: [PATCH] net/interfaces: return all Tailscale addresses from Tailscale(). Signed-off-by: David Anderson --- cmd/tsshd/tsshd.go | 11 +++++++++-- net/interfaces/interfaces.go | 8 ++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cmd/tsshd/tsshd.go b/cmd/tsshd/tsshd.go index c8bc644d5..3dc4f29bc 100644 --- a/cmd/tsshd/tsshd.go +++ b/cmd/tsshd/tsshd.go @@ -59,11 +59,11 @@ func main() { warned := false for { - addr, iface, err := interfaces.Tailscale() + addrs, iface, err := interfaces.Tailscale() if err != nil { log.Fatalf("listing interfaces: %v", err) } - if addr == nil { + if len(addrs) == 0 { if !warned { log.Printf("no tailscale interface found; polling until one is available") warned = true @@ -75,6 +75,13 @@ func main() { continue } warned = false + var addr netaddr.IP + for _, a := range addrs { + if a.Is4() { + addr = a + break + } + } listen := net.JoinHostPort(addr.String(), fmt.Sprint(*port)) log.Printf("tailscale ssh server listening on %v, %v", iface.Name, listen) s := &ssh.Server{ diff --git a/net/interfaces/interfaces.go b/net/interfaces/interfaces.go index d04b75177..9b15604f1 100644 --- a/net/interfaces/interfaces.go +++ b/net/interfaces/interfaces.go @@ -26,7 +26,7 @@ var LoginEndpointForProxyDetermination = "https://login.tailscale.com/" // Tailscale returns the current machine's Tailscale interface, if any. // If none is found, all zero values are returned. // A non-nil error is only returned on a problem listing the system interfaces. -func Tailscale() (net.IP, *net.Interface, error) { +func Tailscale() ([]netaddr.IP, *net.Interface, error) { ifs, err := net.Interfaces() if err != nil { return nil, nil, err @@ -39,14 +39,18 @@ func Tailscale() (net.IP, *net.Interface, error) { if err != nil { continue } + var tsIPs []netaddr.IP for _, a := range addrs { if ipnet, ok := a.(*net.IPNet); ok { nip, ok := netaddr.FromStdIP(ipnet.IP) if ok && tsaddr.IsTailscaleIP(nip) { - return ipnet.IP, &iface, nil + tsIPs = append(tsIPs, nip) } } } + if len(tsIPs) > 0 { + return tsIPs, &iface, nil + } } return nil, nil, nil }