-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgodyndns.go
174 lines (160 loc) · 5.63 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package godyndns
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/jpillora/go-tld"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
)
type domain struct {
Data string `json:"data"`
Name string `json:"name"`
TTL int `json:"ttl"`
Type string `json:"type"`
}
type domainUpdate struct {
Data string `json:"data"`
TTL int `json:"ttl"`
}
type domainUpdates []domainUpdate
const domainsPath = "https://api.godaddy.com/v1/domains"
// GetPublicIP Gets the public ip of the current host, assuming that it can reach the internet
// it accepts an *http.Client as a param mainly for testing purposes
func GetPublicIP(client *http.Client) (net.IP, error) {
var ipResolvers = [3]string{"http://ipinfo.io/ip", "https://api.ipify.org?format=text", "https://checkip.amazonaws.com/api"}
for _, url := range ipResolvers {
ip, err := getPublicIPFrom(client, url)
if err == nil {
log.Printf("My public IP is:%s\n", ip)
return ip, nil
}
}
return nil, fmt.Errorf("couldn't get my public IP. Tried %v", ipResolvers)
}
// UpdateGoDaddyARecord updates the A record of a given GoDaddy subdomain if the public IP that it points to
// is different compared to the publicIP parameter. The domainName param needs to look like : subdomain.domain.com.
// The function will then make a REST api call on the domain.com and will update the subdomain with the publicIP
func UpdateGoDaddyARecord(client *http.Client, domainName string, publicIP net.IP, apiKey, secretKey string) error {
if publicIP == nil {
log.Println("Given publicIP is nll")
return errors.New("given publicIP is nll")
}
domainURL, err := constructURL(domainName)
if err != nil {
log.Printf("Failed to update the A record as I couldn't extract the domain from %s\n", domainName)
return err
}
url := fmt.Sprintf("%s/%s.%s/records/A/%s", domainsPath, domainURL.Domain, domainURL.TLD, domainURL.Subdomain)
record, _ := json.Marshal(domainUpdates{domainUpdate{publicIP.String(), 600}})
req, _ := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(record))
addHeaders(req, apiKey, secretKey)
_, err = doRequest(client, req)
return err
}
// GetGodaddyARecordIP gets the A record associated with the domainName. The domainName param needs to look like :
// subdomain.domain.com . Upon successful retrieval, it returns the IP address associated with that subdomain
func GetGodaddyARecordIP(client *http.Client, domainName string, apiKey, secretKey string) (net.IP, error) {
domainURL, err := constructURL(domainName)
if err != nil {
log.Printf("Failed to get A record as I couldn't extract the domain from %s\n", domainName)
return nil, err
}
targetURL := fmt.Sprintf("%s/%s.%s/records/A/%s", domainsPath, domainURL.Domain, domainURL.TLD, domainURL.Subdomain)
req, err := http.NewRequest(http.MethodGet, targetURL, nil)
if err != nil {
log.Printf("Failed to get the record details for domain %s : %s", domainName, err)
return nil, err
}
addHeaders(req, apiKey, secretKey)
resp, err := client.Do(req)
if err != nil {
log.Printf("Failed to execute request %s : %s", req.URL, err)
return nil, err
}
if resp.StatusCode != 200 {
log.Printf("%s to %s returned %s.\n", req.Method, req.URL, resp.Status)
return nil, errors.New(resp.Status)
}
defer resp.Body.Close()
var record []domain
err = json.NewDecoder(resp.Body).Decode(&record)
if err != nil {
log.Printf("Failed to decode the response body. %s", err)
return nil, err
}
if len(record) == 0 {
log.Printf("Couldn't get info on the domain : %s. Do we own that domain?", domainName)
return nil, errors.New("invalid domain")
}
ip := net.ParseIP(record[0].Data)
if ip == nil {
return ip, fmt.Errorf("couldn't parse %s to an IP address", record[0].Data)
}
return net.ParseIP(record[0].Data), nil
}
func constructURL(subdomain string) (*tld.URL, error) {
u, err := tld.Parse(subdomain)
if err != nil {
log.Printf("Couldn't construct domain from %s : %s", subdomain, err)
return nil, err
}
if !u.ICANN {
u, err = tld.Parse("https://" + subdomain)
if err != nil {
log.Printf("Couldn't construct domain from %s : %s", subdomain, err)
return nil, err
}
}
if len(u.Domain) == 0 || len(u.TLD) == 0 {
return nil, errors.New("Couldn't extract domain from " + subdomain)
}
if len(u.Subdomain) == 0 {
return nil, errors.New("Couldn't extract subdomain from " + subdomain)
}
return u, nil
}
func addHeaders(r *http.Request, apiKey, secretKey string) *http.Request {
r.Header.Set("Authorization", fmt.Sprintf("sso-key %s:%s", apiKey, secretKey))
r.Header.Set("accept", "application/json")
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Content-Encoding", "application/json")
return r
}
func doRequest(client *http.Client, r *http.Request) (string, error) {
resp, err := client.Do(r)
if err != nil {
log.Fatalf("Failed to execute request %s : %s", r.URL, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Failed to parse request response : %s", err)
return "", err
}
if resp.StatusCode != 200 {
log.Printf("%s to %s returned %s.", r.Method, r.URL, resp.Status)
return "", errors.New(resp.Status)
}
return string(body), nil
}
func getPublicIPFrom(client *http.Client, url string) (net.IP, error) {
log.Printf("Getting my public IP address from %s ...\n", url)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Fatalf("Failed to reach %s to get my public IP address: %s", url, err)
}
ret, err := doRequest(client, req)
if err != nil {
return nil, err
}
ip := net.ParseIP(strings.TrimSuffix(ret, "\n"))
if ip == nil {
return nil, fmt.Errorf("couldn't parse %s to an IP address", strings.TrimSuffix(ret, "\n"))
}
return ip, nil
}