All checks were successful
Cadoles/bouncer/pipeline/head This commit looks good
43 lines
762 B
Go
43 lines
762 B
Go
package cidr
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func MatchAny(hostPort string, CIDRs ...string) (bool, error) {
|
|
var remoteHost string
|
|
if strings.Contains(hostPort, ":") {
|
|
var err error
|
|
remoteHost, _, err = net.SplitHostPort(hostPort)
|
|
if err != nil {
|
|
return false, errors.WithStack(err)
|
|
}
|
|
} else {
|
|
remoteHost = hostPort
|
|
}
|
|
|
|
remoteAddr := net.ParseIP(remoteHost)
|
|
if remoteAddr == nil {
|
|
return false, errors.Errorf("remote host '%s' is not a valid ip address", remoteHost)
|
|
}
|
|
|
|
for _, rawCIDR := range CIDRs {
|
|
_, net, err := net.ParseCIDR(rawCIDR)
|
|
if err != nil {
|
|
return false, errors.WithStack(err)
|
|
}
|
|
|
|
match := net.Contains(remoteAddr)
|
|
if !match {
|
|
continue
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
return false, nil
|
|
}
|