-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
405 lines (310 loc) · 11.3 KB
/
dns.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package mailinabox
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
)
// Record Represents a DNS record.
type Record struct {
Name string `json:"qname,omitempty"`
Type string `json:"rtype,omitempty"`
Value string `json:"value,omitempty"`
Explanation string `json:"explanation,omitempty"`
}
// Zone Represents a DNS zone.
type Zone struct {
Zone string
Records []Record
}
// Zones a slice of Zones.
// Use a custom unmarshalling method.
type Zones []Zone
// UnmarshalJSON customs unmarshalling.
func (z *Zones) UnmarshalJSON(data []byte) error {
if string(data) == "null" || string(data) == `""` {
return nil
}
var a []json.RawMessage
if err := json.Unmarshal(data, &a); err != nil {
return err
}
var all []Zone
for _, message := range a {
var b []json.RawMessage
if err := json.Unmarshal(message, &b); err != nil {
return err
}
zone := Zone{}
if err := json.Unmarshal(b[0], &zone.Zone); err != nil {
return err
}
if len(b) <= 1 {
all = append(all, zone)
continue
}
if err := json.Unmarshal(b[1], &zone.Records); err != nil {
return err
}
all = append(all, zone)
}
*z = all
return nil
}
// Nameserver Represents DNS nameservers.
type Nameserver struct {
Hostnames []string `json:"hostnames"`
}
// DNSService DNS API.
// https://mailinabox.email/api-docs.html#tag/DNS
type DNSService service
// GetSecondaryNameserver Returns a list of nameserver hostnames.
// https://mailinabox.email/api-docs.html#operation/getDnsSecondaryNameserver
func (s *DNSService) GetSecondaryNameserver(ctx context.Context) ([]string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "dns", "secondary-nameserver")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var results Nameserver
err = s.client.doJSON(req, &results)
if err != nil {
return nil, err
}
return results.Hostnames, nil
}
// AddSecondaryNameserver Adds one or more secondary nameservers.
// https://mailinabox.email/api-docs.html#operation/addDnsSecondaryNameserver
func (s *DNSService) AddSecondaryNameserver(ctx context.Context, hostnames []string) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "dns", "secondary-nameserver")
data := url.Values{}
data.Set("hostnames", strings.Join(hostnames, ","))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetZones Returns an array of all managed top-level domains.
// https://mailinabox.email/api-docs.html#operation/getDnsZones
func (s *DNSService) GetZones(ctx context.Context) ([]string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "dns", "zones")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var results []string
err = s.client.doJSON(req, &results)
if err != nil {
return nil, err
}
return results, nil
}
// GetZoneFile Returns a DNS zone file for a hostname.
// https://mailinabox.email/api-docs.html#operation/getDnsZonefile
func (s *DNSService) GetZoneFile(ctx context.Context, zone string) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "dns", "zonefile", zone)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
var results string
err = s.client.doJSON(req, &results)
if err != nil {
return "", err
}
return results, nil
}
// UpdateDNS Updates the DNS. Involves creating zone files and restarting `nsd`.
// https://mailinabox.email/api-docs.html#operation/updateDns
func (s *DNSService) UpdateDNS(ctx context.Context, force bool) (string, error) {
endpoint := s.client.baseURL.JoinPath("admin", "dns", "update")
data := url.Values{}
data.Set("force", boolToIntStr(force))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(data.Encode()))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetAllRecords Returns all custom DNS records.
// https://mailinabox.email/api-docs.html#operation/getDnsCustomRecords
func (s *DNSService) GetAllRecords(ctx context.Context) ([]Record, error) {
endpoint := s.client.baseURL.JoinPath("admin", "dns", "custom")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var results []Record
err = s.client.doJSON(req, &results)
if err != nil {
return nil, err
}
return results, nil
}
// GetRecords Returns all custom records for the specified query name and type.
// https://mailinabox.email/api-docs.html#operation/getDnsCustomRecordsForQNameAndType
func (s *DNSService) GetRecords(ctx context.Context, name, rType string) ([]Record, error) {
if name == "" || rType == "" {
return nil, errors.New("qname and rtype are required")
}
endpoint := s.client.baseURL.JoinPath("admin", "dns", "custom", name, rType)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var results []Record
err = s.client.doJSON(req, &results)
if err != nil {
return nil, err
}
return results, nil
}
// AddRecord Adds a custom DNS record for the specified query name and type.
// https://mailinabox.email/api-docs.html#operation/addDnsCustomRecord
func (s *DNSService) AddRecord(ctx context.Context, record Record) (string, error) {
if record.Name == "" || record.Type == "" {
return "", errors.New("qname and rtype are required")
}
endpoint := s.client.baseURL.JoinPath("admin", "dns", "custom", record.Name, record.Type)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(record.Value))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// UpdateRecord Updates an existing DNS custom record value for the specified qname and type.
// https://mailinabox.email/api-docs.html#operation/updateDnsCustomRecord
func (s *DNSService) UpdateRecord(ctx context.Context, record Record, value string) (string, error) {
if record.Name == "" || record.Type == "" {
return "", errors.New("qname and rtype are required")
}
endpoint := s.client.baseURL.JoinPath("admin", "dns", "custom", record.Name, record.Type)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, endpoint.String(), strings.NewReader(value))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// RemoveRecord Removes a DNS custom record for the specified domain, type & value.
// https://mailinabox.email/api-docs.html#operation/removeDnsCustomRecord
func (s *DNSService) RemoveRecord(ctx context.Context, record Record) (string, error) {
if record.Name == "" || record.Type == "" {
return "", errors.New("qname and rtype are required")
}
endpoint := s.client.baseURL.JoinPath("admin", "dns", "custom", record.Name, record.Type)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, endpoint.String(), strings.NewReader(record.Value))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetARecords Returns all custom A records for the specified query name.
// https://mailinabox.email/api-docs.html#operation/getDnsCustomARecordsForQName
func (s *DNSService) GetARecords(ctx context.Context, name string) ([]Record, error) {
if name == "" {
return nil, errors.New("qname is required")
}
endpoint := s.client.baseURL.JoinPath("admin", "dns", "custom", name)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var results []Record
err = s.client.doJSON(req, &results)
if err != nil {
return nil, err
}
return results, nil
}
// AddARecord Adds a custom DNS A record for the specified query name.
// https://mailinabox.email/api-docs.html#operation/addDnsCustomARecord
func (s *DNSService) AddARecord(ctx context.Context, name, value string) (string, error) {
if name == "" {
return "", errors.New("qname is required")
}
endpoint := s.client.baseURL.JoinPath("admin", "dns", "custom", name)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint.String(), strings.NewReader(value))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// UpdateARecord Updates an existing DNS custom A record value for the specified qname.
// https://mailinabox.email/api-docs.html#operation/updateDnsCustomARecord
func (s *DNSService) UpdateARecord(ctx context.Context, name, value string) (string, error) {
if name == "" {
return "", errors.New("qname is required")
}
endpoint := s.client.baseURL.JoinPath("admin", "dns", "custom", name)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, endpoint.String(), strings.NewReader(value))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// RemoveARecord Removes a DNS custom A record for the specified domain & value.
// https://mailinabox.email/api-docs.html#operation/removeDnsCustomARecord
func (s *DNSService) RemoveARecord(ctx context.Context, name, value string) (string, error) {
if name == "" {
return "", errors.New("qname is required")
}
endpoint := s.client.baseURL.JoinPath("admin", "dns", "custom", name)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, endpoint.String(), strings.NewReader(value))
if err != nil {
return "", fmt.Errorf("unable to create request: %w", err)
}
resp, err := s.client.doPlain(req)
if err != nil {
return "", err
}
return strings.TrimSpace(string(resp)), nil
}
// GetDump Returns all DNS records.
// https://mailinabox.email/api-docs.html#operation/getDnsDump
func (s *DNSService) GetDump(ctx context.Context) ([]Zone, error) {
endpoint := s.client.baseURL.JoinPath("admin", "dns", "dump")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
var results Zones
err = s.client.doJSON(req, &results)
if err != nil {
return nil, err
}
return results, nil
}