-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_ip.go
37 lines (33 loc) · 926 Bytes
/
get_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
package homebase
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
)
// HTTPGetter is an interface that implements http.Client's Get method so that
// mock objects can be passed in for tests.
type HTTPGetter interface {
Get(url string) (resp *http.Response, err error)
}
// GetPublicIP returns the public IP address for the current machine.
func GetPublicIP(c HTTPGetter) (net.IP, error) {
resp, err := c.Get("http://checkip.amazonaws.com")
if err != nil {
return nil, fmt.Errorf("Error requesting IP: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Bad status requesting IP: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Error reading body: %v", err)
}
ip := net.ParseIP(strings.TrimSpace(string(body)))
if ip == nil {
return nil, fmt.Errorf("Invalid IP: %s", string(body))
}
return ip, nil
}