-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip_addresses.go
70 lines (55 loc) · 1.29 KB
/
ip_addresses.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
60
61
62
63
64
65
66
67
68
69
70
package utils
import (
"net"
)
// Private IPV4 Address Blocks (CIDR) as per https://en.wikipedia.org/wiki/Reserved_IP_addresses
var privateIPV4Blocks = []string{
"10.0.0.0/8",
"100.64.0.0/10",
"172.16.0.0/12",
"192.0.0.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
}
// Private IPV6 Address Blocks (CIDR) as per https://en.wikipedia.org/wiki/Reserved_IP_addresses
var privateIPV6Blocks = []string{
"fc00::/7",
}
// inRange checks to see if a given ip address is within a IP.net
func inRange(net *net.IPNet, ipAddress net.IP) bool {
return net.Contains(ipAddress)
}
// isPrivateSubnet function check to see if this ip is in a private subnet
func isPrivateSubnet(ipAddress net.IP) bool {
// my use case is only concerned with ipv4 atm
privateBlocks := []string{}
if isIPV4(ipAddress) {
privateBlocks = privateIPV4Blocks
} else if isIPV6(ipAddress) {
privateBlocks = privateIPV6Blocks
} else {
return false
}
for _, block := range privateBlocks {
_, net, err := net.ParseCIDR(block)
if err != nil {
return false
}
if inRange(net, ipAddress) {
return true
}
}
return false
}
func isIPV4(ipAddress net.IP) bool {
if ipAddress.To4() != nil {
return true
}
return false
}
func isIPV6(ipAddress net.IP) bool {
if ipAddress.To16() != nil {
return true
}
return false
}