Added more test cases for netutil ipmatch

This commit is contained in:
Toby Chui
2025-09-19 21:14:20 +08:00
parent 52f652fbaf
commit 0805da9d13
2 changed files with 65 additions and 2 deletions

View File

@@ -108,14 +108,24 @@ func MatchIpCIDR(ip string, cidr string) bool {
// Check if a ip is private IP range
func IsPrivateIP(ipStr string) bool {
if ipStr == "127.0.0.1" || ipStr == "::1" {
//local loopback
// local loopback
return true
}
ip := net.ParseIP(ipStr)
if ip == nil {
return false
}
return ip.IsPrivate()
if ip.IsPrivate() {
return true
}
// Check for IPv6 link-local addresses (fe80::/10)
if ip.To16() != nil && ip.To4() == nil {
// IPv6 only
if ip[0] == 0xfe && (ip[1]&0xc0) == 0x80 {
return true
}
}
return false
}
// Check if an Ip string is ipv6