GetIPAddress returns a valid public IP Address from a collection of IP Addresses and a range of private subnets. Reports whether a valid IP was found.
(ipAddresses []string, privateRanges []IPRange)
| 42 | // |
| 43 | // Reports whether a valid IP was found. |
| 44 | func GetIPAddress(ipAddresses []string, privateRanges []IPRange) (string, bool) { |
| 45 | // march from right to left until we get a public address |
| 46 | // that will be the address right before our proxy. |
| 47 | for i := len(ipAddresses) - 1; i >= 0; i-- { |
| 48 | ip := strings.TrimSpace(ipAddresses[i]) |
| 49 | realIP := net.ParseIP(ip) |
| 50 | if !realIP.IsGlobalUnicast() || IPIsPrivateSubnet(realIP, privateRanges) { |
| 51 | // bad address, go to next |
| 52 | continue |
| 53 | } |
| 54 | return ip, true |
| 55 | |
| 56 | } |
| 57 | |
| 58 | return "", false |
| 59 | } |
searching dependent graphs…