From 2dd26628cbc6b694ade00b1a9dc4b2ffa9c25486 Mon Sep 17 00:00:00 2001 From: Tomek Date: Thu, 10 Oct 2024 10:18:24 +0200 Subject: [PATCH] Test fixes --- gdpr/protect_ip.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/gdpr/protect_ip.go b/gdpr/protect_ip.go index 6676aed..629b7c4 100644 --- a/gdpr/protect_ip.go +++ b/gdpr/protect_ip.go @@ -18,8 +18,19 @@ func ProtectIP[T IP](ip T) string { switch cIP := any(ip).(type) { // Every other type should end up in this case. case net.IP: + // if ip is somehow empty or nil, return empty string + if cIP == nil || len(cIP) == 0 { + return "" + } + // if IP is v6, we don't do anything with it. if cIP.To4() == nil { + + // if IPv6 is local we don't do anything + if cIP.String() == "::1" { + return cIP.String() + } + cIP[12] = 0 cIP[13] = 0 cIP[14] = 0 @@ -34,11 +45,6 @@ func ProtectIP[T IP](ip T) string { return addr.StringExpanded() } - // if ip is somehow empty, return empty string - if len(cIP) == 0 { - return "" - } - // change last octet of IP v4 to 0 and guard cIP[15] = 0 return cIP.String()