-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ip.go
53 lines (48 loc) · 1.03 KB
/
ip.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
package extconfig
import (
"errors"
"net"
"go.uber.org/zap"
)
func mustParseCIDR(cidr string) *net.IPNet {
_, net, err := net.ParseCIDR(cidr)
if err != nil {
panic(err)
}
return net
}
var ulaPrefix = mustParseCIDR("fc00::/7")
func (e *ExtConfig) GetIP() (net.IP, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, i := range ifaces {
if i.Flags&net.FlagLoopback != 0 {
continue
}
if i.Flags&net.FlagUp == 0 {
continue
}
addrs, err := i.Addrs()
if err != nil {
e.Logger().Debug("failed to get IPs from interface", zap.Error(err), zap.String("if", i.Name))
continue
}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
continue
}
if ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
continue
}
if ulaPrefix.Contains(ip) {
continue
}
e.Logger().Debug("Detected IP of instance", zap.String("ip", ip.String()))
return ip, nil
}
}
return nil, errors.New("failed to find IP, set `INSTANCE_IP`")
}