diff --git a/pkg/helpers/helpers.go b/pkg/helpers/helpers.go index 3eb948a0f..c40d7ce5a 100644 --- a/pkg/helpers/helpers.go +++ b/pkg/helpers/helpers.go @@ -4,40 +4,27 @@ package helpers import ( "fmt" - "math/big" - "net" + "net/netip" ) // IsIPInRange checks if the target IP falls within the start and end IP range (inclusive). func IsIPInRange(startIP, endIP, targetIP string) (bool, error) { - // Parse the IPs - start := net.ParseIP(startIP) - end := net.ParseIP(endIP) - target := net.ParseIP(targetIP) - - // Ensure all IPs are valid - if start == nil { - return false, fmt.Errorf("invalid start IP: %q", startIP) + start, err := netip.ParseAddr(startIP) + if err != nil { + return false, fmt.Errorf("invalid start IP: %w", err) } - if end == nil { - return false, fmt.Errorf("invalid end IP: %q", endIP) + end, err := netip.ParseAddr(endIP) + if err != nil { + return false, fmt.Errorf("invalid end IP: %w", err) } - if target == nil { - return false, fmt.Errorf("invalid target IP: %q", targetIP) + target, err := netip.ParseAddr(targetIP) + if err != nil { + return false, fmt.Errorf("invalid target IP: %w", err) } - // Convert IPs to big integers - startInt := ipToBigInt(start) - endInt := ipToBigInt(end) - targetInt := ipToBigInt(target) - - // Check if target IP is within the range - return targetInt.Cmp(startInt) >= 0 && targetInt.Cmp(endInt) <= 0, nil -} + if start.Compare(target) <= 0 && end.Compare(target) >= 0 { + return true, nil + } -// ipToBigInt converts a net.IP to a big.Int for comparison. -func ipToBigInt(ip net.IP) *big.Int { - // Normalize to 16-byte representation for both IPv4 and IPv6 - ip = ip.To16() - return big.NewInt(0).SetBytes(ip) + return false, nil } diff --git a/pkg/helpers/helpers_test.go b/pkg/helpers/helpers_test.go index 9095e89c2..360cd1dee 100644 --- a/pkg/helpers/helpers_test.go +++ b/pkg/helpers/helpers_test.go @@ -57,7 +57,10 @@ func TestIsIPInRange(t *testing.T) { endIP: "192.168.1.10", targetIP: "192.168.1.5", expectedInRange: false, - expectedErr: fmt.Errorf("invalid start IP: %q", "invalid-ip"), + expectedErr: fmt.Errorf( + "invalid start IP: ParseAddr(%q): unable to parse IP", + "invalid-ip", + ), }, { name: "Invalid end IP", @@ -65,7 +68,10 @@ func TestIsIPInRange(t *testing.T) { endIP: "invalid-ip", targetIP: "192.168.1.5", expectedInRange: false, - expectedErr: fmt.Errorf("invalid end IP: %q", "invalid-ip"), + expectedErr: fmt.Errorf( + "invalid end IP: ParseAddr(%q): unable to parse IP", + "invalid-ip", + ), }, { name: "Invalid target IP", @@ -73,7 +79,10 @@ func TestIsIPInRange(t *testing.T) { endIP: "192.168.1.10", targetIP: "invalid-ip", expectedInRange: false, - expectedErr: fmt.Errorf("invalid target IP: %q", "invalid-ip"), + expectedErr: fmt.Errorf( + "invalid target IP: ParseAddr(%q): unable to parse IP", + "invalid-ip", + ), }, { name: "IPv6 range - target within range",