Skip to content

Commit c9e6f7d

Browse files
Fix dual-stack statistics update (#100)
Related issue: DefGuard/client#617
1 parent 42d478b commit c9e6f7d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/host.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,24 @@ impl Host {
355355
WgDeviceAttrs::Peers(nlas) => {
356356
for nla in nlas {
357357
let peer = Peer::from_nlas(nla);
358-
self.peers.insert(peer.public_key.clone(), peer);
358+
// On some systems, a dual-stack (IPv4 + IPv6) configuration creates separate peer entries
359+
// for each address family. These entries share the same public key, so inserting a new
360+
// peer would overwrite the existing one. To avoid this, we first check if the peer
361+
// already exists and, if it does, update its statistics instead of replacing it.
362+
// https://github.com/DefGuard/client/issues/617
363+
if let Some(existing_peer) = self.peers.get_mut(&peer.public_key) {
364+
existing_peer.rx_bytes += peer.rx_bytes;
365+
existing_peer.tx_bytes += peer.tx_bytes;
366+
existing_peer.last_handshake =
367+
match (existing_peer.last_handshake, peer.last_handshake) {
368+
(Some(x), Some(y)) => Some(x.max(y)),
369+
(Some(x), None) => Some(x),
370+
(None, Some(y)) => Some(y),
371+
(None, None) => None,
372+
};
373+
} else {
374+
self.peers.insert(peer.public_key.clone(), peer);
375+
}
359376
}
360377
}
361378
_ => (),

0 commit comments

Comments
 (0)