-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
296 lines (252 loc) · 6.73 KB
/
client.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package he
import (
"context"
"fmt"
"io"
"math"
"math/rand"
"net"
"net/http"
"net/netip"
"net/url"
"os"
"strings"
"time"
"github.com/libdns/libdns"
"github.com/pkg/errors"
"golang.org/x/time/rate"
)
const (
// User agent to use for API requests
userAgent = "libdns-he/1.0.2"
// API URL to POST updates to
updateURL = "https://dyn.dns.he.net/nic/update"
// How many times to retry after a temporary API error
maxRetries = 5
// API rate limit configuration
rateLimit = 0.125
rateBurst = 100
// API error response codes
codeGood = "good"
codeNoChg = "nochg"
codeAbuse = "abuse"
codeBadAgent = "badagent"
codeBadAuth = "badauth"
codeInterval = "interval"
codeNoHost = "nohost"
codeNotFqdn = "notfqdn"
)
var (
// Set environment variable to "TRUE" to enable debug logging
debug = (os.Getenv("LIBDNS_HE_DEBUG") == "TRUE")
)
// Query Google DNS for A/AAAA/TXT record for a given DNS name
func (p *Provider) getDomain(ctx context.Context, zone string) ([]libdns.Record, error) {
p.mutex.Lock()
defer p.mutex.Unlock()
var libRecords []libdns.Record
// The HE API only has an `/update` endpoint and no way
// to get current records. So instead, we just make
// simple DNS queries to get the A, AAAA, and TXT records.
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{Timeout: 10 * time.Second}
return d.DialContext(ctx, network, "8.8.8.8:53")
},
}
ips, err := r.LookupHost(ctx, zone)
if err != nil {
var dnsErr *net.DNSError
// Ignore missing dns record
if !(errors.As(err, &dnsErr) && dnsErr.IsNotFound) {
return libRecords, errors.Wrapf(err, "error looking up host")
}
}
for _, ip := range ips {
parsed, err := netip.ParseAddr(ip)
if err != nil {
return libRecords, errors.Wrapf(err, "error parsing ip")
}
if parsed.Is4() {
libRecords = append(libRecords, libdns.Record{
Type: "A",
Name: "@",
Value: ip,
})
} else {
libRecords = append(libRecords, libdns.Record{
Type: "AAAA",
Name: "@",
Value: ip,
})
}
}
txt, err := r.LookupTXT(ctx, zone)
if err != nil {
var dnsErr *net.DNSError
// Ignore missing dns record
if !(errors.As(err, &dnsErr) && dnsErr.IsNotFound) {
return libRecords, errors.Wrapf(err, "error looking up txt")
}
}
for _, t := range txt {
if t == "" {
continue
}
libRecords = append(libRecords, libdns.Record{
Type: "TXT",
Name: "@",
Value: t,
})
}
return libRecords, nil
}
// Set or clear the value of a DNS entry
func (p *Provider) setRecord(ctx context.Context, zone string, record libdns.Record, clear bool) error {
p.mutex.Lock()
defer p.mutex.Unlock()
// Sanitize the domain, combines the zone and record names
// the record name should typically be relative to the zone
domain := libdns.AbsoluteName(record.Name, zone)
params := map[string]string{}
switch record.Type {
case "TXT":
if clear {
params["txt"] = "\"\""
} else {
params["txt"] = record.Value
}
case "A":
if clear {
params["myip"] = "127.0.0.1"
} else {
params["myip"] = record.Value
}
case "AAAA":
if clear {
params["myip"] = "::1"
} else {
params["myip"] = record.Value
}
default:
return fmt.Errorf("unsupported record type: %s", record.Type)
}
retries := 0
for {
retries += 1
// Make the API request to HE
err := p.doRequest(ctx, domain, params)
if err != nil {
var urlErr *url.Error
if errors.As(err, &urlErr) &&
(urlErr.Temporary() || urlErr.Unwrap().Error() == "EOF") {
// Temporary error, retry with exponential backoff
if retries >= maxRetries {
return err
}
time.Sleep(backoff(retries))
continue
}
return err
}
break
}
return nil
}
// Make HTTP API request to Hurricane Electric
func (p *Provider) doRequest(ctx context.Context, domain string, params map[string]string) error {
// https://dns.he.net/docs.html
u, _ := url.Parse(updateURL)
// Set up the query with the params we always set
query := u.Query()
query.Set("hostname", strings.TrimSuffix(domain, "."))
query.Set("password", p.APIKey)
// Add the remaining query parameters for this request
for key, val := range params {
query.Set(key, val)
}
reqBody := strings.NewReader(query.Encode())
// Create the request
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), reqBody)
if err != nil {
return errors.Wrapf(err, "error creating http request")
}
// Add HTTP headers
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if p.rateLimiter == nil {
// Init rate limiter
p.rateLimiter = rate.NewLimiter(rateLimit, rateBurst)
}
// Wait for tokens from rate limiter
err = p.rateLimiter.Wait(ctx)
if err != nil {
return errors.Wrapf(err, "error waiting for rate limiter")
}
// Make HTTP request to HE API update endpoint
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrapf(err, "error making http request")
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrapf(err, "error reading body from http response")
}
respBody := string(bodyBytes)
if err := checkResponse(u, respBody); err != nil {
if debug {
return errors.Wrapf(err,
"HE api request failed, query=%s, response=%s", query, respBody,
)
}
return errors.Wrapf(err,
"HE api request failed, response=%s", respBody,
)
}
return nil
}
type rateLimitExceededError struct{}
func (e *rateLimitExceededError) Error() string { return "exceeded API rate limit" }
func (e *rateLimitExceededError) Temporary() bool { return true }
// Convert API response code to human friendly error
func checkResponse(uri *url.URL, body string) error {
code, _, _ := strings.Cut(body, " ")
switch code {
case codeGood:
return nil
case codeNoChg:
return nil
case codeAbuse:
return fmt.Errorf("blocked for abuse")
case codeBadAgent:
return fmt.Errorf("user agent not sent or HTTP method not recognized")
case codeBadAuth:
return fmt.Errorf("incorrect authentication key provided")
case codeInterval:
// This is a temporary error
return &url.Error{
Op: "Post",
URL: uri.String(),
Err: &rateLimitExceededError{},
}
case codeNoHost:
return fmt.Errorf("the record provided does not exist in this account")
case codeNotFqdn:
return fmt.Errorf("the record provided isn't an FQDN")
default:
// This is basically only server errors.
return fmt.Errorf("unknown server error")
}
}
// Calculate how many seconds to backoff for a given retry attempt
func backoff(retries int) time.Duration {
expo := int(math.Pow(2, float64(retries)))
half := int(expo / 2)
random := 0
if half >= 1 {
random = rand.Intn(half)
}
return time.Duration(expo+random) * time.Second
}