Skip to content

Commit

Permalink
Fix private network check
Browse files Browse the repository at this point in the history
  • Loading branch information
anjmao committed Aug 12, 2024
1 parent f536343 commit 4b2feea
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (c *Collector) collect() error {
defer c.mu.Unlock()

for _, conn := range conns {
if c.cfg.GroupPublicIPs && !conn.Dst.IP().IsPrivate() {
if c.cfg.GroupPublicIPs && !isPrivateNetwork(conn.Dst.IP()) {
conn.Dst = netaddr.IPPortFrom(netaddr.IPv4(0, 0, 0, 0), 0)
}
connKey := conntrackEntryKey(conn)
Expand Down Expand Up @@ -383,3 +383,12 @@ func conntrackEntryKey(conn *conntrack.Entry) uint64 {
conntrackEntryHash.Reset()
return res
}

func isPrivateNetwork(ip netaddr.IP) bool {
return ip.IsPrivate() ||
ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsLinkLocalUnicast() ||
ip.IsLinkLocalMulticast() ||
ip.IsInterfaceLocalMulticast()
}

0 comments on commit 4b2feea

Please sign in to comment.