-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
147 lines (132 loc) · 3.44 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"net/http"
"log"
"encoding/json"
"fmt"
"errors"
"bytes"
"time"
"flag"
"os"
)
var IP_PROVIDER = "https://v4.ident.me/"
func getOwnIPv4() (string, error) {
resp, err := http.Get(IP_PROVIDER)
if err != nil {
return "", err
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return buf.String(), nil
}
func getDomainIPv4() (string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.godaddy.com/v1/domains/%s/records/A/%s", DOMAIN, SUBDOMAIN), nil)
if err != nil {
return "", err
}
req.Header.Set("Authorization", fmt.Sprintf("sso-key %s:%s", GODADDY_KEY, GODADDY_SECRET))
c := new(http.Client)
resp, err := c.Do(req)
if err != nil {
return "", err
}
in := make([]struct {
Data string `json:"data"`
}, 1)
json.NewDecoder(resp.Body).Decode(&in)
return in[0].Data, nil
}
func putNewIP(ip string) error {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(&struct {
Name string `json:"name"`
Data string `json:"data"`
TTL int64 `json:"ttl"`
} {
SUBDOMAIN,
ip,
600,
})
if err != nil {
return err
}
req, err := http.NewRequest("PUT",
fmt.Sprintf("https://api.godaddy.com/v1/domains/%s/records/A", DOMAIN),
&buf)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("sso-key %s:%s", GODADDY_KEY, GODADDY_SECRET))
c := new(http.Client)
resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode == 200 {
return nil
} else {
return errors.New(fmt.Sprintf("Failed with HTTP status code %d\n", resp.StatusCode))
}
}
func run() {
ownIP, err := getOwnIPv4()
if err != nil {
log.Fatal(err)
}
domainIP, err := getDomainIPv4()
if err != nil {
log.Fatal(err)
}
if domainIP != ownIP {
if err := putNewIP(ownIP); err != nil {
log.Fatal(err)
}
}
}
// globals
var GODADDY_KEY = ""
var GODADDY_SECRET = ""
var DOMAIN = ""
var SUBDOMAIN = ""
func main() {
// log file flag
logFile := flag.String("log", "", "Path for log file (will be created if it doesn't exist)")
// required flags
keyPtr := flag.String("key", "", "Godaddy API key")
secretPtr := flag.String("secret", "", "Godaddy API secret")
domainPtr := flag.String("domain", "", "Your top level domain (e.g., example.com) registered with Godaddy and on the same account as your API key")
// optional flags
subdomainPtr := flag.String("subdomain", "@", "The data value (aka host) for the A record. It can be a 'subdomain' (e.g., 'subdomain' where 'subdomain.example.com' is the qualified domain name). Note that such an A record must be set up first in your Godaddy account beforehand. Defaults to @. (Optional)")
POLLING := flag.Int64("interval", 360, "Polling interval in seconds. Lookup Godaddy's current rate limits before setting too low. Defaults to 360. (Optional)")
flag.Parse()
SUBDOMAIN = *subdomainPtr
DOMAIN = *domainPtr
GODADDY_SECRET = *secretPtr
GODADDY_KEY = *keyPtr
if *logFile == "" {
log.SetOutput(os.Stdout)
} else {
f, err := os.OpenFile(*logFile, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Couldn't open log file: %s", err)
}
defer f.Close()
log.SetOutput(f)
}
if DOMAIN == "" {
log.Fatalf("You need to provide your domain")
}
if GODADDY_SECRET == "" {
log.Fatalf("You need to provide your API secret")
}
if GODADDY_KEY == "" {
log.Fatalf("You need to provide your API key")
}
// run
for {
run()
time.Sleep(time.Second * time.Duration(*POLLING))
}
}