/
ipclassifiers.go
59 lines (50 loc) · 1.32 KB
/
ipclassifiers.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
47
48
49
50
51
52
53
54
55
56
57
58
59
package classify
import (
"github.com/google/gopacket"
log "github.com/sirupsen/logrus"
"net"
)
var privateIPBlocks []*net.IPNet
func init() {
for _, cidr := range []string{
"10.0.0.0/8",
"172.16.0.0/12",
"192.168.0.0/16",
} {
_, block, _ := net.ParseCIDR(cidr)
privateIPBlocks = append(privateIPBlocks, block)
}
}
func isPrivateIP(ip net.IP) bool {
for _, block := range privateIPBlocks {
if block.Contains(ip) {
return true
}
}
return !net.IP.IsGlobalUnicast(ip)
}
// Deployment specific logic to determine if traffic is from a user.
func User(endpoint gopacket.Endpoint, userSubnet string, ignoredUserIPs []string) bool {
ip := net.ParseIP(endpoint.String())
if ip == nil {
log.WithField("Endpoint", endpoint).Error("Endpoint is not IP parseable")
return false
}
// Exclude specific IPs assigned to our network hardware in the user subnet.
for _, address := range ignoredUserIPs {
if ip.Equal(net.ParseIP(address)) {
return false
}
}
_, userBlock, _ := net.ParseCIDR(userSubnet)
return userBlock.Contains(ip)
}
// Deployment specific logic to determine if traffic is local only
func Local(endpoint gopacket.Endpoint) bool {
ip := net.ParseIP(endpoint.String())
if ip == nil {
log.WithField("Endpoint", endpoint).Error("Endpoint is not IP parseable")
return false
}
return isPrivateIP(ip)
}