From a27227ff6d7bc182835c7d1810e224dd34dd7adf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20Papp?= Date: Wed, 26 Feb 2025 17:08:53 +0100 Subject: [PATCH] Fix ip indication for lazy peers in status --- client/internal/engine.go | 2 +- client/internal/peer/conn.go | 1 - client/internal/peer/status.go | 7 ++----- client/internal/peer/status_test.go | 8 +++++--- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/client/internal/engine.go b/client/internal/engine.go index 3f17d99be..a07fcb0ec 100644 --- a/client/internal/engine.go +++ b/client/internal/engine.go @@ -1136,7 +1136,7 @@ func (e *Engine) addNewPeer(peerConfig *mgmProto.RemotePeerConfig) error { conn.AddAfterRemovePeerHook(e.afterPeerHook) } - err = e.statusRecorder.AddPeer(peerKey, peerConfig.Fqdn) + err = e.statusRecorder.AddPeer(peerKey, peerConfig.Fqdn, peerIPs[0].Addr().String()) if err != nil { log.Warnf("error adding peer %s to status recorder, got error: %v", peerKey, err) } diff --git a/client/internal/peer/conn.go b/client/internal/peer/conn.go index 513840e30..f1d81f21a 100644 --- a/client/internal/peer/conn.go +++ b/client/internal/peer/conn.go @@ -176,7 +176,6 @@ func (conn *Conn) Open() { peerState := State{ PubKey: conn.config.Key, - IP: conn.config.WgConfig.AllowedIps[0].Addr().String(), ConnStatusUpdate: time.Now(), ConnStatus: StatusDisconnected, Mux: new(sync.RWMutex), diff --git a/client/internal/peer/status.go b/client/internal/peer/status.go index e9976270c..8af30691b 100644 --- a/client/internal/peer/status.go +++ b/client/internal/peer/status.go @@ -205,7 +205,7 @@ func (d *Status) ReplaceOfflinePeers(replacement []State) { } // AddPeer adds peer to Daemon status map -func (d *Status) AddPeer(peerPubKey string, fqdn string) error { +func (d *Status) AddPeer(peerPubKey string, fqdn string, ip string) error { d.mux.Lock() defer d.mux.Unlock() @@ -215,6 +215,7 @@ func (d *Status) AddPeer(peerPubKey string, fqdn string) error { } d.peers[peerPubKey] = State{ PubKey: peerPubKey, + IP: ip, ConnStatus: StatusDisconnected, FQDN: fqdn, Mux: new(sync.RWMutex), @@ -260,10 +261,6 @@ func (d *Status) UpdatePeerState(receivedState State) error { return errors.New("peer doesn't exist") } - if receivedState.IP != "" { - peerState.IP = receivedState.IP - } - skipNotification := shouldSkipNotify(receivedState.ConnStatus, peerState) if receivedState.ConnStatus != peerState.ConnStatus { diff --git a/client/internal/peer/status_test.go b/client/internal/peer/status_test.go index 931ec9005..bdf8f087a 100644 --- a/client/internal/peer/status_test.go +++ b/client/internal/peer/status_test.go @@ -10,22 +10,24 @@ import ( func TestAddPeer(t *testing.T) { key := "abc" + ip := "100.108.254.1" status := NewRecorder("https://mgm") - err := status.AddPeer(key, "abc.netbird") + err := status.AddPeer(key, "abc.netbird", ip) assert.NoError(t, err, "shouldn't return error") _, exists := status.peers[key] assert.True(t, exists, "value was found") - err = status.AddPeer(key, "abc.netbird") + err = status.AddPeer(key, "abc.netbird", ip) assert.Error(t, err, "should return error on duplicate") } func TestGetPeer(t *testing.T) { key := "abc" + ip := "100.108.254.1" status := NewRecorder("https://mgm") - err := status.AddPeer(key, "abc.netbird") + err := status.AddPeer(key, "abc.netbird", ip) assert.NoError(t, err, "shouldn't return error") peerStatus, err := status.GetPeer(key)