-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatcher.go
46 lines (39 loc) · 1.02 KB
/
matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package realip
import "net"
// Matcher provides a way to match the IP address.
type Matcher interface {
Match(address string) (bool, error)
}
// NetMatcher contains logic to perform matching of the IP addresses
// agains a list of networks.
type NetMatcher []net.IPNet
// NetMatcherFromCIDR constructs a NetMatcher from a list of networks
// in CIDR notation.
// See net.ParseCIDR.
func NetMatcherFromCIDR(strings []string) (*NetMatcher, error) {
nets := make([]net.IPNet, len(strings))
for i, string := range strings {
_, Net, err := net.ParseCIDR(string)
if err != nil {
return nil, err
}
nets[i] = *Net
}
return (*NetMatcher)(&nets), nil
}
var _ Matcher = (*NetMatcher)(nil)
// Match determines whether any of the networks contain the specified
// address.
func (m *NetMatcher) Match(address string) (bool, error) {
ip := net.ParseIP(address)
if ip == nil {
return false, ErrInvalidIP
}
list := ([]net.IPNet)(*m)
for i := range list {
if list[i].Contains(ip) {
return true, nil
}
}
return false, nil
}