2020-02-19 05:15:09 +07:00
|
|
|
package authorization
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// isIPMatching check whether user's IP is in one of the network ranges.
|
|
|
|
func isIPMatching(ip net.IP, networks []string) bool {
|
|
|
|
// If no network is provided in the rule, we match any network
|
|
|
|
if len(networks) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, network := range networks {
|
|
|
|
if !strings.Contains(network, "/") {
|
|
|
|
if ip.String() == network {
|
|
|
|
return true
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-02-19 05:15:09 +07:00
|
|
|
continue
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-02-19 05:15:09 +07:00
|
|
|
_, ipNet, err := net.ParseCIDR(network)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-02-19 05:15:09 +07:00
|
|
|
if err != nil {
|
|
|
|
// TODO(c.michaud): make sure the rule is valid at startup to
|
|
|
|
// to such a case here.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ipNet.Contains(ip) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-02-19 05:15:09 +07:00
|
|
|
return false
|
|
|
|
}
|