publicIP returns an IP that is supposed to be Public.
()
| 78 | |
| 79 | // publicIP returns an IP that is supposed to be Public. |
| 80 | func publicIP() (net.IP, error) { |
| 81 | resp, err := http.Get(publicEcho) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | defer resp.Body.Close() |
| 86 | |
| 87 | // The ip address is 16 chars long, we read more |
| 88 | // to account for excessive whitespace. |
| 89 | p, err := ioutil.ReadAll(io.LimitReader(resp.Body, 24)) |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | |
| 94 | n := net.ParseIP(string(bytes.TrimSpace(p))) |
| 95 | if n == nil { |
| 96 | return nil, fmt.Errorf("cannot parse ip %s", p) |
| 97 | } |
| 98 | |
| 99 | return n, nil |
| 100 | } |
no test coverage detected