forked from nickw444/cf-ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip.go
43 lines (35 loc) · 734 Bytes
/
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
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
)
type IPService interface {
GetExternalIP() (net.IP, error)
}
type FakeIPService struct {
fakeIp net.IP
}
func (f *FakeIPService) GetExternalIP() (net.IP, error) {
if f.fakeIp == nil {
return nil, fmt.Errorf("FakeIPService: No IP specified")
}
return f.fakeIp, nil
}
type IpifyIPService struct {
HttpClient *http.Client
}
type IpifyAPIResponse struct {
IP string
}
func (i *IpifyIPService) GetExternalIP() (net.IP, error) {
r, err := i.HttpClient.Get("https://api.ipify.org?format=json")
if err != nil {
return nil, err
}
defer r.Body.Close()
var resp IpifyAPIResponse
json.NewDecoder(r.Body).Decode(&resp)
return net.ParseIP(resp.IP), nil
}