-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGodyndns.go
73 lines (62 loc) · 1.38 KB
/
Godyndns.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package GOdyndns
import (
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
"github.com/nudelfabrik/GOdyndns/Digitalocean"
"github.com/nudelfabrik/GOdyndns/Gandi"
"github.com/nudelfabrik/GOdyndns/Porkbun"
"github.com/nudelfabrik/GOdyndns/settings"
)
type Client interface {
Update(string) error
}
func CreateClient(setting *settings.Settings) (Client, error) {
switch setting.API {
case "Gandi", "gandi":
return Gandi.NewGandiClient(setting)
case "DO", "do", "DigitalOcean", "digitalocean":
return Digitalocean.NewDoClient(setting)
case "Porkbun", "porkbun", "PorkBun":
return Porkbun.NewPorkbunClient(setting)
default:
return nil, fmt.Errorf("Not supported API endpoint: %s", setting.API)
}
}
func Update(c Client) error {
ip, err := getIP()
if err != nil {
go func(c Client) {
time.Sleep(time.Minute * 5)
Update(c)
}(c)
return err
}
err = c.Update(ip)
return err
}
func getIP() (string, error) {
var netClient = &http.Client{
Timeout: time.Second * 10,
}
response, err := netClient.Get("http://ipv4.icanhazip.com")
if err != nil {
return "", err
}
responseText, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return "", err
}
str := string(responseText)
str = strings.TrimSpace(str)
ip := net.ParseIP(str)
if ip == nil {
return "", errors.New("Cannot Parse IP: " + str)
}
return ip.String(), err
}