mirror of
https://github.com/hay-kot/homebox.git
synced 2025-03-12 03:56:20 +00:00
38 lines
672 B
Go
38 lines
672 B
Go
|
// Package ipcheck provides helper functions to validate IP addresses against criteria
|
||
|
package ipcheck
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func ValidateAgainstList(ip string, comaSeparatedList string) bool {
|
||
|
if comaSeparatedList == "" || ip == "" {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if net.ParseIP(ip) == nil {
|
||
|
ip, _, _ = net.SplitHostPort(ip)
|
||
|
}
|
||
|
|
||
|
if ip == "" {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
cidrs := strings.Split(comaSeparatedList, ",")
|
||
|
testedIP, _, err := net.ParseCIDR(fmt.Sprintf("%s/32", ip))
|
||
|
if err != nil {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
for _, cidr := range cidrs {
|
||
|
_, ipnet, err := net.ParseCIDR(cidr)
|
||
|
if err == nil && ipnet.Contains(testedIP) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false
|
||
|
}
|