net/interfaces: return all Tailscale addresses from Tailscale().

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson 2021-04-28 19:05:24 -07:00
parent b7a497a30b
commit 72b6d98298
2 changed files with 15 additions and 4 deletions

View File

@ -59,11 +59,11 @@ func main() {
warned := false warned := false
for { for {
addr, iface, err := interfaces.Tailscale() addrs, iface, err := interfaces.Tailscale()
if err != nil { if err != nil {
log.Fatalf("listing interfaces: %v", err) log.Fatalf("listing interfaces: %v", err)
} }
if addr == nil { if len(addrs) == 0 {
if !warned { if !warned {
log.Printf("no tailscale interface found; polling until one is available") log.Printf("no tailscale interface found; polling until one is available")
warned = true warned = true
@ -75,6 +75,13 @@ func main() {
continue continue
} }
warned = false warned = false
var addr netaddr.IP
for _, a := range addrs {
if a.Is4() {
addr = a
break
}
}
listen := net.JoinHostPort(addr.String(), fmt.Sprint(*port)) listen := net.JoinHostPort(addr.String(), fmt.Sprint(*port))
log.Printf("tailscale ssh server listening on %v, %v", iface.Name, listen) log.Printf("tailscale ssh server listening on %v, %v", iface.Name, listen)
s := &ssh.Server{ s := &ssh.Server{

View File

@ -26,7 +26,7 @@
// Tailscale returns the current machine's Tailscale interface, if any. // Tailscale returns the current machine's Tailscale interface, if any.
// If none is found, all zero values are returned. // If none is found, all zero values are returned.
// A non-nil error is only returned on a problem listing the system interfaces. // 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() ifs, err := net.Interfaces()
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -39,14 +39,18 @@ func Tailscale() (net.IP, *net.Interface, error) {
if err != nil { if err != nil {
continue continue
} }
var tsIPs []netaddr.IP
for _, a := range addrs { for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok { if ipnet, ok := a.(*net.IPNet); ok {
nip, ok := netaddr.FromStdIP(ipnet.IP) nip, ok := netaddr.FromStdIP(ipnet.IP)
if ok && tsaddr.IsTailscaleIP(nip) { 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 return nil, nil, nil
} }