GetFirstAddressOf returns the first IPv4 address of the supplied interface names, omitting any 169.254.x.x automatic private IPs if possible.
(names []string)
| 12 | |
| 13 | // GetFirstAddressOf returns the first IPv4 address of the supplied interface names, omitting any 169.254.x.x automatic private IPs if possible. |
| 14 | func GetFirstAddressOf(names []string) (string, error) { |
| 15 | var ipAddr string |
| 16 | for _, name := range names { |
| 17 | inf, err := net.InterfaceByName(name) |
| 18 | if err != nil { |
| 19 | level.Warn(util_log.Logger).Log("msg", "error getting interface", "inf", name, "err", err) |
| 20 | continue |
| 21 | } |
| 22 | addrs, err := inf.Addrs() |
| 23 | if err != nil { |
| 24 | level.Warn(util_log.Logger).Log("msg", "error getting addresses for interface", "inf", name, "err", err) |
| 25 | continue |
| 26 | } |
| 27 | if len(addrs) <= 0 { |
| 28 | level.Warn(util_log.Logger).Log("msg", "no addresses found for interface", "inf", name, "err", err) |
| 29 | continue |
| 30 | } |
| 31 | if ip := filterIPs(addrs); ip != "" { |
| 32 | ipAddr = ip |
| 33 | } |
| 34 | if strings.HasPrefix(ipAddr, `169.254.`) || ipAddr == "" { |
| 35 | continue |
| 36 | } |
| 37 | return ipAddr, nil |
| 38 | } |
| 39 | if ipAddr == "" { |
| 40 | return "", fmt.Errorf("no address found for %s", names) |
| 41 | } |
| 42 | if strings.HasPrefix(ipAddr, `169.254.`) { |
| 43 | level.Warn(util_log.Logger).Log("msg", "using automatic private ip", "address", ipAddr) |
| 44 | } |
| 45 | return ipAddr, nil |
| 46 | } |
| 47 | |
| 48 | // filterIPs attempts to return the first non automatic private IP (APIPA / 169.254.x.x) if possible, only returning APIPA if available and no other valid IP is found. |
| 49 | func filterIPs(addrs []net.Addr) string { |
no test coverage detected