From 418adae379348fb71067c1d9726279b29041a08b Mon Sep 17 00:00:00 2001 From: David Anderson Date: Fri, 29 Oct 2021 16:19:27 -0700 Subject: [PATCH] various: use NodePublic.AsNodeKey() instead of tailcfg.NodeKeyFromNodePublic() Updates #3206 Signed-off-by: David Anderson --- control/controlclient/direct.go | 6 +++--- control/controlclient/map.go | 2 +- ipn/ipnlocal/local.go | 4 ++-- ipn/ipnlocal/loglines_test.go | 4 ++-- tstest/integration/testcontrol/testcontrol.go | 2 +- wgengine/bench/wg.go | 4 ++-- wgengine/magicsock/magicsock.go | 8 ++++---- wgengine/magicsock/magicsock_test.go | 8 ++++---- wgengine/userspace.go | 14 +++++++------- wgengine/userspace_test.go | 4 ++-- 10 files changed, 28 insertions(+), 28 deletions(-) diff --git a/control/controlclient/direct.go b/control/controlclient/direct.go index e74c01c50..029d2e45c 100644 --- a/control/controlclient/direct.go +++ b/control/controlclient/direct.go @@ -357,8 +357,8 @@ func (c *Direct) doLogin(ctx context.Context, opt loginOpt) (mustRegen bool, new now := time.Now().Round(time.Second) request := tailcfg.RegisterRequest{ Version: 1, - OldNodeKey: tailcfg.NodeKeyFromNodePublic(oldNodeKey), - NodeKey: tailcfg.NodeKeyFromNodePublic(tryingNewKey.Public()), + OldNodeKey: oldNodeKey.AsNodeKey(), + NodeKey: tryingNewKey.Public().AsNodeKey(), Hostinfo: hostinfo, Followup: opt.URL, Timestamp: &now, @@ -595,7 +595,7 @@ func (c *Direct) sendMapRequest(ctx context.Context, maxPolls int, cb func(*netm request := &tailcfg.MapRequest{ Version: tailcfg.CurrentMapRequestVersion, KeepAlive: c.keepAlive, - NodeKey: tailcfg.NodeKeyFromNodePublic(persist.PrivateNodeKey.Public()), + NodeKey: persist.PrivateNodeKey.Public().AsNodeKey(), DiscoKey: c.discoPubKey, Endpoints: epStrs, EndpointTypes: epTypes, diff --git a/control/controlclient/map.go b/control/controlclient/map.go index 502770387..9e8c2cacb 100644 --- a/control/controlclient/map.go +++ b/control/controlclient/map.go @@ -110,7 +110,7 @@ func (ms *mapSession) netmapForResponse(resp *tailcfg.MapResponse) *netmap.Netwo } nm := &netmap.NetworkMap{ - NodeKey: tailcfg.NodeKeyFromNodePublic(ms.privateNodeKey.Public()), + NodeKey: ms.privateNodeKey.Public().AsNodeKey(), PrivateKey: ms.privateNodeKey, MachineKey: ms.machinePubKey, Peers: resp.Peers, diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index c2f6b4e81..a1c6342c0 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -2692,7 +2692,7 @@ func (b *LocalBackend) TestOnlyPublicKeys() (machineKey key.MachinePublic, nodeK mk := machinePrivKey.Public() nk := prefs.Persist.PrivateNodeKey.Public() - return mk, tailcfg.NodeKeyFromNodePublic(nk) + return mk, nk.AsNodeKey() } func (b *LocalBackend) WaitingFiles() ([]apitype.WaitingFile, error) { @@ -2782,7 +2782,7 @@ func (b *LocalBackend) SetDNS(ctx context.Context, name, value string) error { b.mu.Lock() cc := b.cc if prefs := b.prefs; prefs != nil { - req.NodeKey = tailcfg.NodeKeyFromNodePublic(prefs.Persist.PrivateNodeKey.Public()) + req.NodeKey = prefs.Persist.PrivateNodeKey.Public().AsNodeKey() } b.mu.Unlock() if cc == nil { diff --git a/ipn/ipnlocal/loglines_test.go b/ipn/ipnlocal/loglines_test.go index a7e8ccd7e..3943a308b 100644 --- a/ipn/ipnlocal/loglines_test.go +++ b/ipn/ipnlocal/loglines_test.go @@ -90,7 +90,7 @@ func TestLocalLogLines(t *testing.T) { TxBytes: 10, RxBytes: 10, LastHandshake: time.Now(), - NodeKey: tailcfg.NodeKeyFromNodePublic(key.NewNode().Public()), + NodeKey: key.NewNode().Public().AsNodeKey(), }}, }) lb.mu.Unlock() @@ -105,7 +105,7 @@ func TestLocalLogLines(t *testing.T) { TxBytes: 11, RxBytes: 12, LastHandshake: time.Now(), - NodeKey: tailcfg.NodeKeyFromNodePublic(key.NewNode().Public()), + NodeKey: key.NewNode().Public().AsNodeKey(), }}, }) lb.mu.Unlock() diff --git a/tstest/integration/testcontrol/testcontrol.go b/tstest/integration/testcontrol/testcontrol.go index 4fea34680..1c83bfe67 100644 --- a/tstest/integration/testcontrol/testcontrol.go +++ b/tstest/integration/testcontrol/testcontrol.go @@ -274,7 +274,7 @@ func (s *Server) AddFakeNode() { if s.nodes == nil { s.nodes = make(map[tailcfg.NodeKey]*tailcfg.Node) } - nk := tailcfg.NodeKeyFromNodePublic(key.NewNode().Public()) + nk := key.NewNode().Public().AsNodeKey() mk := key.NewMachine().Public() dk := tailcfg.DiscoKeyFromDiscoPublic(key.NewDisco().Public()) id := int64(binary.LittleEndian.Uint64(nk[:])) diff --git a/wgengine/bench/wg.go b/wgengine/bench/wg.go index 7321b10d0..cf79c5d8e 100644 --- a/wgengine/bench/wg.go +++ b/wgengine/bench/wg.go @@ -105,7 +105,7 @@ func setupWGTest(b *testing.B, logf logger.Logf, traf *TrafficGen, a1, a2 netadd Endpoints: eps, } e2.SetNetworkMap(&netmap.NetworkMap{ - NodeKey: tailcfg.NodeKeyFromNodePublic(k2.Public()), + NodeKey: k2.Public().AsNodeKey(), PrivateKey: k2, Peers: []*tailcfg.Node{&n}, }) @@ -142,7 +142,7 @@ func setupWGTest(b *testing.B, logf logger.Logf, traf *TrafficGen, a1, a2 netadd Endpoints: eps, } e1.SetNetworkMap(&netmap.NetworkMap{ - NodeKey: tailcfg.NodeKeyFromNodePublic(k1.Public()), + NodeKey: k1.Public().AsNodeKey(), PrivateKey: k1, Peers: []*tailcfg.Node{&n}, }) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 192c340e9..cf7eb122e 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -1710,13 +1710,13 @@ func (c *Conn) processDERPReadResult(dm derpReadResult, b []byte) (n int, ep *en } ipp := netaddr.IPPortFrom(derpMagicIPAddr, uint16(regionID)) - if c.handleDiscoMessage(b[:n], ipp, tailcfg.NodeKeyFromNodePublic(dm.src)) { + if c.handleDiscoMessage(b[:n], ipp, dm.src.AsNodeKey()) { return 0, nil } var ok bool c.mu.Lock() - ep, ok = c.peerMap.endpointForNodeKey(tailcfg.NodeKeyFromNodePublic(dm.src)) + ep, ok = c.peerMap.endpointForNodeKey(dm.src.AsNodeKey()) c.mu.Unlock() if !ok { // We don't know anything about this node key, nothing to @@ -2132,7 +2132,7 @@ func (c *Conn) SetPrivateKey(privateKey key.NodePrivate) error { if newKey.IsZero() { c.publicKeyAtomic.Store(tailcfg.NodeKey{}) } else { - c.publicKeyAtomic.Store(tailcfg.NodeKeyFromNodePublic(newKey.Public())) + c.publicKeyAtomic.Store(newKey.Public().AsNodeKey()) } if oldKey.IsZero() { @@ -2818,7 +2818,7 @@ func (c *Conn) ParseEndpoint(nodeKeyStr string) (conn.Endpoint, error) { if err != nil { return nil, fmt.Errorf("magicsock: ParseEndpoint: parse failed on %q: %w", nodeKeyStr, err) } - pk := tailcfg.NodeKeyFromNodePublic(k) + pk := k.AsNodeKey() c.mu.Lock() defer c.mu.Unlock() diff --git a/wgengine/magicsock/magicsock_test.go b/wgengine/magicsock/magicsock_test.go index e2df694b4..9cb69c83d 100644 --- a/wgengine/magicsock/magicsock_test.go +++ b/wgengine/magicsock/magicsock_test.go @@ -247,7 +247,7 @@ func meshStacks(logf logger.Logf, mutateNetmap func(idx int, nm *netmap.NetworkM me := ms[myIdx] nm := &netmap.NetworkMap{ PrivateKey: me.privateKey, - NodeKey: tailcfg.NodeKeyFromNodePublic(me.privateKey.Public()), + NodeKey: me.privateKey.Public().AsNodeKey(), Addresses: []netaddr.IPPrefix{netaddr.IPPrefixFrom(netaddr.IPv4(1, 0, 0, byte(myIdx+1)), 32)}, } for i, peer := range ms { @@ -258,7 +258,7 @@ func meshStacks(logf logger.Logf, mutateNetmap func(idx int, nm *netmap.NetworkM peer := &tailcfg.Node{ ID: tailcfg.NodeID(i + 1), Name: fmt.Sprintf("node%d", i+1), - Key: tailcfg.NodeKeyFromNodePublic(peer.privateKey.Public()), + Key: peer.privateKey.Public().AsNodeKey(), DiscoKey: tailcfg.DiscoKeyFromDiscoPublic(peer.conn.DiscoPublicKey()), Addresses: addrs, AllowedIPs: addrs, @@ -1136,7 +1136,7 @@ func TestDiscoMessage(t *testing.T) { peer1Pub := c.DiscoPublicKey() peer1Priv := c.discoPrivate n := &tailcfg.Node{ - Key: tailcfg.NodeKeyFromNodePublic(key.NewNode().Public()), + Key: key.NewNode().Public().AsNodeKey(), DiscoKey: tailcfg.DiscoKeyFromDiscoPublic(peer1Pub), } c.peerMap.upsertEndpoint(&endpoint{ @@ -1229,7 +1229,7 @@ func addTestEndpoint(tb testing.TB, conn *Conn, sendConn net.PacketConn) (tailcf // codepath. discoKey := key.DiscoPublicFromRaw32(mem.B([]byte{31: 1})) nodeKey := key.NodePublicFromRaw32(mem.B([]byte{0: 'N', 1: 'K', 31: 0})) - tnk := tailcfg.NodeKeyFromNodePublic(nodeKey) + tnk := nodeKey.AsNodeKey() conn.SetNetworkMap(&netmap.NetworkMap{ Peers: []*tailcfg.Node{ { diff --git a/wgengine/userspace.go b/wgengine/userspace.go index 1112b87ef..a778f6555 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -648,7 +648,7 @@ func (e *userspaceEngine) maybeReconfigWireguardLocked(discoChanged map[tailcfg. for i := range full.Peers { p := &full.Peers[i] nk := p.PublicKey - tnk := tailcfg.NodeKeyFromNodePublic(nk) + tnk := nk.AsNodeKey() if !isTrimmablePeer(p, len(full.Peers)) { min.Peers = append(min.Peers, *p) if discoChanged[tnk] { @@ -687,7 +687,7 @@ func (e *userspaceEngine) maybeReconfigWireguardLocked(discoChanged map[tailcfg. minner.Peers = nil numRemove := 0 for _, p := range min.Peers { - if discoChanged[tailcfg.NodeKeyFromNodePublic(p.PublicKey)] { + if discoChanged[p.PublicKey.AsNodeKey()] { numRemove++ continue } @@ -807,7 +807,7 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config, e.mu.Lock() e.peerSequence = e.peerSequence[:0] for _, p := range cfg.Peers { - e.peerSequence = append(e.peerSequence, tailcfg.NodeKeyFromNodePublic(p.PublicKey)) + e.peerSequence = append(e.peerSequence, p.PublicKey.AsNodeKey()) peerSet[p.PublicKey] = struct{}{} } e.mu.Unlock() @@ -845,7 +845,7 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config, prevEP := make(map[tailcfg.NodeKey]key.DiscoPublic) for i := range e.lastCfgFull.Peers { if p := &e.lastCfgFull.Peers[i]; !p.DiscoKey.IsZero() { - prevEP[tailcfg.NodeKeyFromNodePublic(p.PublicKey)] = p.DiscoKey + prevEP[p.PublicKey.AsNodeKey()] = p.DiscoKey } } for i := range cfg.Peers { @@ -853,7 +853,7 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, routerCfg *router.Config, if p.DiscoKey.IsZero() { continue } - pub := tailcfg.NodeKeyFromNodePublic(p.PublicKey) + pub := p.PublicKey.AsNodeKey() if old, ok := prevEP[pub]; ok && old != p.DiscoKey { discoChanged[pub] = true e.logf("wgengine: Reconfig: %s changed from %q to %q", pub.ShortString(), old, p.DiscoKey) @@ -1014,7 +1014,7 @@ func (e *userspaceEngine) getStatus() (*Status, error) { if !p.NodeKey.IsZero() { pp[p.NodeKey] = p } - p = ipnstate.PeerStatusLite{NodeKey: tailcfg.NodeKeyFromNodePublic(pk)} + p = ipnstate.PeerStatusLite{NodeKey: pk.AsNodeKey()} case "rx_bytes": n, err = mem.ParseInt(v, 10, 64) p.RxBytes = n @@ -1464,7 +1464,7 @@ func (e *userspaceEngine) peerForIP(ip netaddr.IP) (n *tailcfg.Node, isSelf bool } if best.IsZero() || cidr.Bits() > best.Bits() { best = cidr - bestKey = tailcfg.NodeKeyFromNodePublic(p.PublicKey) + bestKey = p.PublicKey.AsNodeKey() } } } diff --git a/wgengine/userspace_test.go b/wgengine/userspace_test.go index 18c1ba5e6..be5afed41 100644 --- a/wgengine/userspace_test.go +++ b/wgengine/userspace_test.go @@ -45,7 +45,7 @@ func TestNoteReceiveActivity(t *testing.T) { } ra := e.recvActivityAt - nk := tailcfg.NodeKeyFromNodePublic(key.NewNode().Public()) + nk := key.NewNode().Public().AsNodeKey() // Activity on an untracked key should do nothing. e.noteRecvActivity(nk) @@ -217,7 +217,7 @@ func nkFromHex(hex string) tailcfg.NodeKey { if err != nil { panic(fmt.Sprintf("%q is not hex: %v", hex, err)) } - return tailcfg.NodeKeyFromNodePublic(k) + return k.AsNodeKey() } // an experiment to see if genLocalAddrFunc was worth it. As of Go