ipn/ipnlocal: update PeerByID to return SelfNode and rename it to NodeByID (#16096)

Like NodeByKey, add an if stmt for checking the NodeId is SelfNode.

Updates #16052

Signed-off-by: Jerry Yan <792602257@qq.com>
This commit is contained in:
JerryYan 2025-06-27 00:43:48 +08:00 committed by GitHub
parent 47dff33eac
commit 99aaa6e92c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 12 deletions

View File

@ -318,7 +318,7 @@ func (b *LocalBackend) driveRemotesFromPeers(nm *netmap.NetworkMap) []*drive.Rem
// - They are online
// - They are allowed to share at least one folder with us
cn := b.currentNode()
peer, ok := cn.PeerByID(peerID)
peer, ok := cn.NodeByID(peerID)
if !ok {
return false
}

View File

@ -1391,7 +1391,7 @@ func profileFromView(v tailcfg.UserProfileView) tailcfg.UserProfile {
func (b *LocalBackend) WhoIsNodeKey(k key.NodePublic) (n tailcfg.NodeView, u tailcfg.UserProfile, ok bool) {
cn := b.currentNode()
if nid, ok := cn.NodeByKey(k); ok {
if n, ok := cn.PeerByID(nid); ok {
if n, ok := cn.NodeByID(nid); ok {
up, ok := cn.NetMap().UserProfiles[n.User()]
u = profileFromView(up)
return n, u, ok
@ -1457,13 +1457,9 @@ func (b *LocalBackend) WhoIs(proto string, ipp netip.AddrPort) (n tailcfg.NodeVi
if nm == nil {
return failf("no netmap")
}
n, ok = cn.PeerByID(nid)
n, ok = cn.NodeByID(nid)
if !ok {
// Check if this the self-node, which would not appear in peers.
if !nm.SelfNode.Valid() || nid != nm.SelfNode.ID() {
return zero, u, false
}
n = nm.SelfNode
return zero, u, false
}
up, ok := cn.UserByID(n.User())
if !ok {
@ -1968,7 +1964,7 @@ func (b *LocalBackend) UpdateNetmapDelta(muts []netmap.NodeMutation) (handled bo
if !ok || mo.Online {
continue
}
n, ok := cn.PeerByID(m.NodeIDBeingMutated())
n, ok := cn.NodeByID(m.NodeIDBeingMutated())
if !ok || n.StableID() != exitNodeID {
continue
}
@ -7724,7 +7720,7 @@ func (b *LocalBackend) srcIPHasCapForFilter(srcIP netip.Addr, cap tailcfg.NodeCa
if !ok {
return false
}
n, ok := cn.PeerByID(nodeID)
n, ok := cn.NodeByID(nodeID)
if !ok {
return false
}

View File

@ -1005,7 +1005,7 @@ func TestUpdateNetmapDelta(t *testing.T) {
},
}
for _, want := range wants {
gotv, ok := b.currentNode().PeerByID(want.ID)
gotv, ok := b.currentNode().NodeByID(want.ID)
if !ok {
t.Errorf("netmap.Peer %v missing from b.profile.Peers", want.ID)
continue

View File

@ -206,9 +206,14 @@ func (nb *nodeBackend) NodeByKey(k key.NodePublic) (_ tailcfg.NodeID, ok bool) {
return 0, false
}
func (nb *nodeBackend) PeerByID(id tailcfg.NodeID) (_ tailcfg.NodeView, ok bool) {
func (nb *nodeBackend) NodeByID(id tailcfg.NodeID) (_ tailcfg.NodeView, ok bool) {
nb.mu.Lock()
defer nb.mu.Unlock()
if nb.netMap != nil {
if self := nb.netMap.SelfNode; self.Valid() && self.ID() == id {
return self, true
}
}
n, ok := nb.peers[id]
return n, ok
}