From 00feea3a5e88d68a208791eceb983fd7a11d6ce4 Mon Sep 17 00:00:00 2001 From: rkonfj Date: Thu, 6 Feb 2025 17:21:16 +0800 Subject: [PATCH] p2p: skip nodes with node.nr label when selecting relay peer --- disco/peer.go | 20 ++++++++++++++++++++ p2p/conn.go | 6 +++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/disco/peer.go b/disco/peer.go index d1c4df4..0ad14b6 100644 --- a/disco/peer.go +++ b/disco/peer.go @@ -1,5 +1,7 @@ package disco +import "strings" + type PeerID string func (id PeerID) String() string { @@ -17,3 +19,21 @@ func (id PeerID) Len() byte { func (id PeerID) Bytes() []byte { return []byte(id) } + +type Labels []string + +func (ls Labels) Get(key string) (string, bool) { + for _, l := range ls { + kv := strings.Split(l, "=") + if len(kv) == 2 { + if kv[0] == key { + return kv[1], true + } + continue + } + if kv[0] == key { + return "", true + } + } + return "", false +} diff --git a/p2p/conn.go b/p2p/conn.go index a795a6a..d821a45 100644 --- a/p2p/conn.go +++ b/p2p/conn.go @@ -270,8 +270,12 @@ func (c *PacketConn) relayPeer(peerID disco.PeerID) disco.PeerID { if meta == nil { continue } + if _, ok := disco.Labels(meta["label"]).Get("node.nr"); ok { + // can not as relay peer when `node.nr` label is present + continue + } peerNAT := disco.NATType(meta.Get("nat")) - if peerNAT == disco.Easy || peerNAT == disco.IP4 { + if peerNAT == disco.Easy || peerNAT == disco.IP4 || peerNAT == disco.IP46 { return p.PeerID } }