diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 265366a01..6f24c1f5c 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -1327,7 +1327,7 @@ func (b *LocalBackend) getPeerAPIPortForTSMPPing(ip netaddr.IP) (port uint16, ok defer b.mu.Unlock() for _, pln := range b.peerAPIListeners { if pln.ip.BitLen() == ip.BitLen() { - return uint16(pln.Port()), true + return uint16(pln.port), true } } return 0, false @@ -1341,7 +1341,7 @@ func (b *LocalBackend) peerAPIServicesLocked() (ret []tailcfg.Service) { } ret = append(ret, tailcfg.Service{ Proto: proto, - Port: uint16(pln.Port()), + Port: uint16(pln.port), }) } return ret @@ -1556,13 +1556,12 @@ func (b *LocalBackend) initPeerAPIListener() { ln: ln, // nil for 2nd+ on netstack lb: b, } - var port int if skipListen { - port = b.peerAPIListeners[0].Port() + pln.port = b.peerAPIListeners[0].port } else { - port = pln.Port() + pln.port = ln.Addr().(*net.TCPAddr).Port } - pln.urlStr = "http://" + net.JoinHostPort(a.IP.String(), strconv.Itoa(port)) + pln.urlStr = "http://" + net.JoinHostPort(a.IP.String(), strconv.Itoa(pln.port)) b.logf("peerapi: serving on %s", pln.urlStr) go pln.serve() b.peerAPIListeners = append(b.peerAPIListeners, pln) diff --git a/ipn/ipnlocal/peerapi.go b/ipn/ipnlocal/peerapi.go index 568c6a811..bfbf327a4 100644 --- a/ipn/ipnlocal/peerapi.go +++ b/ipn/ipnlocal/peerapi.go @@ -218,11 +218,19 @@ func (s *peerAPIServer) listen(ip netaddr.IP, ifState *interfaces.State) (ln net } type peerAPIListener struct { - ps *peerAPIServer - ip netaddr.IP - ln net.Listener // or nil for 2nd+ address family in netstack mdoe - lb *LocalBackend + ps *peerAPIServer + ip netaddr.IP + lb *LocalBackend + + // ln is the Listener. It can be nil in netstack mode if there are more than + // 1 local addresses (e.g. both an IPv4 and IPv6). When it's nil, port + // and urlStr are still populated. + ln net.Listener + + // urlStr is the base URL to access the peer API (http://ip:port/). urlStr string + // port is just the port of urlStr. + port int } func (pln *peerAPIListener) Close() error { @@ -232,17 +240,6 @@ func (pln *peerAPIListener) Close() error { return nil } -func (pln *peerAPIListener) Port() int { - if pln.ln == nil { - return 0 - } - ta, ok := pln.ln.Addr().(*net.TCPAddr) - if !ok { - return 0 - } - return ta.Port -} - func (pln *peerAPIListener) serve() { if pln.ln == nil { return