Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Skandalik committed Oct 10, 2024
1 parent 33853af commit 3e2e065
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
17 changes: 2 additions & 15 deletions gdpr/protect_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gdpr
import (
"encoding/binary"
"net"
"net/netip"

"github.com/msales/gox/netx"
)
Expand All @@ -23,26 +22,14 @@ func ProtectIP[T IP](ip T) string {
return ""
}

// if IP is v6, we don't do anything with it.
// if IP is v6, change last 2 octets of ip.
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
cIP[15] = 0

addr, err := netip.ParseAddr(cIP.String())
// if there is an error, we don't do anything, we return empty value because something is wrong with passed ip
if err != nil {
return ""
}

return addr.StringExpanded()
return cIP.String()
}

// change last octet of IP v4 to 0 and guard
Expand Down
4 changes: 2 additions & 2 deletions gdpr/protect_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ func Test_ProtectIP_String(t *testing.T) {
{
name: "local ip v6",
ip: "::1",
want: "::1",
want: "::",
},
{
name: "raw ip v6",
ip: "2001:0000:130F:0000:0000:09C0:876A:130B",
want: "2001:0000:130f:0000:0000:09c0:0000:0000",
want: "2001:0:130f::9c0:0:0",
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 3e2e065

Please sign in to comment.