-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
detect.go
138 lines (122 loc) · 3.33 KB
/
detect.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package ipaddr
import (
"fmt"
"net"
)
// GetPrivateIPv4 returns the list of private network IPv4 addresses on
// all active interfaces.
func GetPrivateIPv4() ([]*net.IPAddr, error) {
addresses, err := activeInterfaceAddresses()
if err != nil {
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
}
var addrs []*net.IPAddr
for _, rawAddr := range addresses {
var ip net.IP
switch addr := rawAddr.(type) {
case *net.IPAddr:
ip = addr.IP
case *net.IPNet:
ip = addr.IP
default:
continue
}
if ip.To4() == nil {
continue
}
if !isPrivate(ip) {
continue
}
addrs = append(addrs, &net.IPAddr{IP: ip})
}
return addrs, nil
}
// GetPublicIPv6 returns the list of all public IPv6 addresses
// on all active interfaces.
func GetPublicIPv6() ([]*net.IPAddr, error) {
addresses, err := net.InterfaceAddrs()
if err != nil {
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
}
var addrs []*net.IPAddr
for _, rawAddr := range addresses {
var ip net.IP
switch addr := rawAddr.(type) {
case *net.IPAddr:
ip = addr.IP
case *net.IPNet:
ip = addr.IP
default:
continue
}
if ip.To4() != nil {
continue
}
if isPrivate(ip) {
continue
}
addrs = append(addrs, &net.IPAddr{IP: ip})
}
return addrs, nil
}
// privateBlocks contains non-forwardable address blocks which are used
// for private networks. RFC 6890 provides an overview of special
// address blocks.
var privateBlocks = []*net.IPNet{
parseCIDR("10.0.0.0/8"), // RFC 1918 IPv4 private network address
parseCIDR("100.64.0.0/10"), // RFC 6598 IPv4 shared address space
parseCIDR("127.0.0.0/8"), // RFC 1122 IPv4 loopback address
parseCIDR("169.254.0.0/16"), // RFC 3927 IPv4 link local address
parseCIDR("172.16.0.0/12"), // RFC 1918 IPv4 private network address
parseCIDR("192.0.0.0/24"), // RFC 6890 IPv4 IANA address
parseCIDR("192.0.2.0/24"), // RFC 5737 IPv4 documentation address
parseCIDR("192.168.0.0/16"), // RFC 1918 IPv4 private network address
parseCIDR("::1/128"), // RFC 1884 IPv6 loopback address
parseCIDR("fe80::/10"), // RFC 4291 IPv6 link local addresses
parseCIDR("fc00::/7"), // RFC 4193 IPv6 unique local addresses
parseCIDR("fec0::/10"), // RFC 1884 IPv6 site-local addresses
parseCIDR("2001:db8::/32"), // RFC 3849 IPv6 documentation address
}
func parseCIDR(s string) *net.IPNet {
_, block, err := net.ParseCIDR(s)
if err != nil {
panic(fmt.Sprintf("Bad CIDR %s: %s", s, err))
}
return block
}
func isPrivate(ip net.IP) bool {
for _, priv := range privateBlocks {
if priv.Contains(ip) {
return true
}
}
return false
}
// Returns addresses from interfaces that is up
func activeInterfaceAddresses() ([]net.Addr, error) {
var upAddrs []net.Addr
var loAddrs []net.Addr
interfaces, err := net.Interfaces()
if err != nil {
return nil, fmt.Errorf("Failed to get interfaces: %v", err)
}
for _, iface := range interfaces {
// Require interface to be up
if iface.Flags&net.FlagUp == 0 {
continue
}
addresses, err := iface.Addrs()
if err != nil {
return nil, fmt.Errorf("Failed to get interface addresses: %v", err)
}
if iface.Flags&net.FlagLoopback != 0 {
loAddrs = append(loAddrs, addresses...)
continue
}
upAddrs = append(upAddrs, addresses...)
}
if len(upAddrs) == 0 {
return loAddrs, nil
}
return upAddrs, nil
}