From 9659ab81e0e74f3f650a1cb78eb74f0df3914ef2 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 29 Mar 2021 12:49:13 -0700 Subject: [PATCH] ipn/ipnlocal: send peerapi port(s) in Hostinfo.Services Signed-off-by: Brad Fitzpatrick --- ipn/ipnlocal/local.go | 41 ++++++++++++++++++++++++++++++++--------- ipn/ipnlocal/peerapi.go | 1 + 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index 9a8c59bce..f225ecfe4 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -1330,25 +1330,47 @@ func (b *LocalBackend) SetPrefs(newp *ipn.Prefs) { b.send(ipn.Notify{Prefs: newp}) } +func (b *LocalBackend) peerAPIServicesLocked() (ret []tailcfg.Service) { + for _, pln := range b.peerAPIListeners { + proto := tailcfg.ServiceProto("peerapi4") + if pln.ip.Is6() { + proto = "peerapi6" + } + ret = append(ret, tailcfg.Service{ + Proto: proto, + Port: uint16(pln.Port()), + }) + } + return ret +} + // doSetHostinfoFilterServices calls SetHostinfo on the controlclient, // possibly after mangling the given hostinfo. // // TODO(danderson): we shouldn't be mangling hostinfo here after // painstakingly constructing it in twelvety other places. func (b *LocalBackend) doSetHostinfoFilterServices(hi *tailcfg.Hostinfo) { - hi2 := *hi - if !b.shouldUploadServices() { - hi2.Services = []tailcfg.Service{} - } - b.mu.Lock() - cli := b.c + cc := b.c + if cc == nil { + // Control client isn't up yet. + b.mu.Unlock() + return + } + peerAPIServices := b.peerAPIServicesLocked() b.mu.Unlock() - // b.c might not be started yet - if cli != nil { - cli.SetHostinfo(&hi2) + // Make a shallow copy of hostinfo so we can mutate + // at the Service field. + hi2 := *hi // shallow copy + if !b.shouldUploadServices() { + hi2.Services = []tailcfg.Service{} } + // Don't mutate hi.Service's underlying array. Append to + // the slice with no free capacity. + c := len(hi2.Services) + hi2.Services = append(hi2.Services[:c:c], peerAPIServices...) + cc.SetHostinfo(&hi2) } // NetMap returns the latest cached network map received from @@ -1490,6 +1512,7 @@ func (b *LocalBackend) initPeerAPIListener() { } pln := &peerAPIListener{ ps: ps, + ip: a.IP, ln: ln, lb: b, } diff --git a/ipn/ipnlocal/peerapi.go b/ipn/ipnlocal/peerapi.go index 43d6ceb78..857639610 100644 --- a/ipn/ipnlocal/peerapi.go +++ b/ipn/ipnlocal/peerapi.go @@ -80,6 +80,7 @@ func (s *peerAPIServer) listen(ip netaddr.IP, ifState *interfaces.State) (ln net type peerAPIListener struct { ps *peerAPIServer + ip netaddr.IP ln net.Listener lb *LocalBackend urlStr string