@@ -4,40 +4,27 @@ package helpers
4
4
5
5
import (
6
6
"fmt"
7
- "math/big"
8
- "net"
7
+ "net/netip"
9
8
)
10
9
11
10
// IsIPInRange checks if the target IP falls within the start and end IP range (inclusive).
12
11
func IsIPInRange (startIP , endIP , targetIP string ) (bool , error ) {
13
- // Parse the IPs
14
- start := net .ParseIP (startIP )
15
- end := net .ParseIP (endIP )
16
- target := net .ParseIP (targetIP )
17
-
18
- // Ensure all IPs are valid
19
- if start == nil {
20
- return false , fmt .Errorf ("invalid start IP: %q" , startIP )
12
+ start , err := netip .ParseAddr (startIP )
13
+ if err != nil {
14
+ return false , fmt .Errorf ("invalid start IP: %w" , err )
21
15
}
22
- if end == nil {
23
- return false , fmt .Errorf ("invalid end IP: %q" , endIP )
16
+ end , err := netip .ParseAddr (endIP )
17
+ if err != nil {
18
+ return false , fmt .Errorf ("invalid end IP: %w" , err )
24
19
}
25
- if target == nil {
26
- return false , fmt .Errorf ("invalid target IP: %q" , targetIP )
20
+ target , err := netip .ParseAddr (targetIP )
21
+ if err != nil {
22
+ return false , fmt .Errorf ("invalid target IP: %w" , err )
27
23
}
28
24
29
- // Convert IPs to big integers
30
- startInt := ipToBigInt (start )
31
- endInt := ipToBigInt (end )
32
- targetInt := ipToBigInt (target )
33
-
34
- // Check if target IP is within the range
35
- return targetInt .Cmp (startInt ) >= 0 && targetInt .Cmp (endInt ) <= 0 , nil
36
- }
25
+ if start .Compare (target ) <= 0 && end .Compare (target ) >= 0 {
26
+ return true , nil
27
+ }
37
28
38
- // ipToBigInt converts a net.IP to a big.Int for comparison.
39
- func ipToBigInt (ip net.IP ) * big.Int {
40
- // Normalize to 16-byte representation for both IPv4 and IPv6
41
- ip = ip .To16 ()
42
- return big .NewInt (0 ).SetBytes (ip )
29
+ return false , nil
43
30
}
0 commit comments