/
client.go
364 lines (343 loc) · 8.59 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
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
package loopia
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/kolo/xmlrpc"
"github.com/libdns/libdns"
)
const (
apiurl = "https://api.loopia.se/RPCSERV"
)
type client struct {
rpc *xmlrpc.Client
mutex sync.Mutex
}
type loopiaRecord struct {
ID int64 `xmlrpc:"record_id"`
TTL int `xmlrpc:"ttl"`
Type string `xmlrpc:"type"`
Value string `xmlrpc:"rdata"`
Priority int `xmlrpc:"priority"`
}
func cleanZone(zone string) string {
if strings.HasSuffix(zone, ".") {
zone = zone[:len(zone)-1]
}
return zone
}
func validZone(zone string) bool {
if zone == "" || len(zone) < 4 {
return false
}
return true
}
func validRecord(r libdns.Record) bool {
if r.Name == "" {
return false
}
if r.Type == "" {
return false
}
if r.Value == "" {
return false
}
if r.TTL < 0 || r.TTL > (time.Hour*8*24) {
return false
}
if r.ID != "" {
_, err := strconv.ParseInt(r.ID, 10, 64)
if err != nil {
return false
}
}
return true
}
func toLoopiaRecord(r libdns.Record) loopiaRecord {
out := loopiaRecord{Type: r.Type, TTL: int(r.TTL / time.Second), Value: r.Value, ID: idToInt(r.ID)}
return out
}
func idToInt(id string) int64 {
idInt, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return 0
}
return idInt
}
func (p *Provider) getRpc() *xmlrpc.Client {
if p.rpc == nil {
rpc, err := xmlrpc.NewClient(apiurl, nil)
if err != nil {
Log().Errorw("error", err)
os.Exit(1)
}
p.rpc = rpc
}
return p.rpc
}
func (p *Provider) call(serviceMethod string, args []interface{}, reply interface{}) error {
params := []interface{}{
p.Username,
p.Password,
}
if p.Customer != "" {
params = append(params, p.Customer)
}
params = append(params, args...)
return p.getRpc().Call(
serviceMethod,
params,
reply,
)
}
func (p *Provider) getRecords(ctx context.Context, zone, name string) ([]libdns.Record, error) {
if !validZone(zone) {
return nil, fmt.Errorf("invalid zone '%s'", zone)
}
if name == "" {
return nil, fmt.Errorf("invalide name '%s'", name)
}
records := []loopiaRecord{}
Log().Debugw("getRecords", "zone", zone, "name", name)
err := p.call("getZoneRecords", params(zone, name), &records)
if err != nil {
return nil, fmt.Errorf("unexpected error getting zone records: %w", err)
}
result := []libdns.Record{}
for _, r := range records {
result = append(result, libdns.Record{
ID: strconv.FormatInt(r.ID, 10),
Type: r.Type,
Name: name,
Value: r.Value,
TTL: time.Duration(r.TTL * int(time.Second)),
})
}
Log().Debugw("end-getRecords", "zone", zone, "name", name, "count", len(result), "err", err)
return result, nil
}
func (p *Provider) addRecord(ctx context.Context, zone string, record libdns.Record, withSubdomain bool) (*libdns.Record, error) {
Log().Debugw("addRecord",
"zone", zone,
"record", record,
"withSubdomain", withSubdomain,
)
if withSubdomain {
var response string
err := p.call("addSubdomain", params(zone, record.Name), &response)
if err != nil {
return nil, fmt.Errorf("unexpected error adding subdomain: %w", err)
}
}
new := &loopiaRecord{Type: record.Type, TTL: int(record.TTL / time.Second), Value: record.Value}
var result string
if err := p.call("addZoneRecord", params(zone, record.Name, new), &result); err != nil || result != "OK" {
return nil, fmt.Errorf("unexpected error adding zone record: %w", err)
}
Log().Debugw("getting records to fetch ID", "zone", zone, "name", record.Name)
records, err := p.getRecords(ctx, zone, record.Name)
if err != nil {
return nil, err
}
for _, r := range records {
id := r.ID
r.ID = record.ID
Log().Debugw("comparing", "a", r, "b", record)
if r == record {
// match
r.ID = id
return &r, nil
}
}
return nil, fmt.Errorf("unable to retreive new record to get it's ID")
}
func params(args ...interface{}) []interface{} {
return args
}
func (p *Provider) getZoneRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
if !validZone(zone) {
return nil, fmt.Errorf("invalide zone '%s'", zone)
}
zone = cleanZone(zone)
names := []string{}
err := p.call("getSubdomains", params(zone), &names)
if err != nil {
return nil, fmt.Errorf("unexpected error getting subdomains: %w", err)
}
result := []libdns.Record{}
myloop:
for _, name := range names {
select {
case <-ctx.Done():
break myloop
default:
records, err := p.getRecords(ctx, zone, name)
if err != nil {
return nil, fmt.Errorf("error getting zone records for %s: %w", name, err)
}
result = append(result, records...)
}
}
return result, err
}
func (p *Provider) addDNSEntries(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
Log().Debugw("addDNSEntries",
"zone", zone,
"records", records,
)
if !validZone(zone) {
return nil, fmt.Errorf("invalide zone '%s'", zone)
}
if len(records) == 0 {
return nil, fmt.Errorf("records is nil or empty")
}
for i, r := range records {
if !validRecord(r) {
return nil, fmt.Errorf("record %d is invalid", i)
}
}
zone = cleanZone(zone)
result := []libdns.Record{}
cache := make(map[string][]libdns.Record)
subsCreated := make(map[string]bool)
OUTER:
for _, new := range records {
select {
case <-ctx.Done():
break OUTER
default:
if cache[new.Name] == nil {
existingRecords, err := p.getRecords(ctx, zone, new.Name)
if err != nil {
return result, err
}
cache[new.Name] = existingRecords
}
withSubdomain := false
if len(cache[new.Name]) == 0 && !subsCreated[new.Name] {
withSubdomain = true
}
for _, existing := range cache[new.Name] {
id := existing.ID
existing.ID = ""
if existing == new {
Log().Debugw("identical record exists, skipping",
"record", new,
"id", id)
existing.ID = id
result = append(result, existing)
continue OUTER
}
existing.ID = id
}
if withSubdomain {
subsCreated[new.Name] = true
}
cn, err := p.addRecord(ctx, zone, new, withSubdomain)
if err != nil {
return result, err
}
Log().Debugw("added record returned", "record", cn)
result = append(result, *cn)
}
}
Log().Debug("done with addDNSEntries")
return result, nil
}
func (p *Provider) setDNSEntries(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
if !validZone(zone) {
return nil, fmt.Errorf("invalide zone '%s'", zone)
}
if len(records) == 0 {
return nil, fmt.Errorf("records is nil or empty")
}
for i, r := range records {
if !validRecord(r) {
return nil, fmt.Errorf("record %d is invalid", i)
}
if idToInt(r.ID) < 1 {
return nil, fmt.Errorf("record %d has invalid ID", i)
}
}
zone = cleanZone(zone)
result := []libdns.Record{}
myloop:
for _, r := range records {
select {
case <-ctx.Done():
break myloop
default:
updated := toLoopiaRecord(r)
var response string
err := p.call("updateZoneRecord", params(zone, r.Name, updated), &response)
if err != nil {
return result, fmt.Errorf("unexpected error updating zone record: %w", err)
}
result = append(result, r)
}
}
return result, nil
}
func (p *Provider) removeDNSEntries(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
if !validZone(zone) {
return nil, fmt.Errorf("invalide zone '%s'", zone)
}
if len(records) == 0 {
return nil, fmt.Errorf("records is nil or empty")
}
for i, r := range records {
if idToInt(r.ID) < 1 {
return nil, fmt.Errorf("record %d has invalid ID", i)
}
if r.Name == "" {
return nil, fmt.Errorf("record %d has invalid name", i)
}
}
zone = cleanZone(zone)
result := []libdns.Record{}
firstloop:
for _, r := range records {
select {
case <-ctx.Done():
break firstloop
default:
// logger.Debug().Object("record", myRecord{&r}).Msg("Removing")
var response string
err := p.call("removeZoneRecord", params(zone, r.Name, idToInt(r.ID)), &response)
if err != nil {
return result, fmt.Errorf("unexpected error removing zone record: %w", err)
}
result = append(result, r)
}
}
names := make(map[string]bool)
secondloop:
for _, r := range result {
select {
case <-ctx.Done():
break secondloop
default:
if !names[r.Name] {
names[r.Name] = true
res, err := p.getRecords(ctx, zone, r.Name)
if err != nil {
Log().Warnw("unexpected error getting zone records", "err", err)
continue
}
if len(res) == 0 {
var response string
err := p.call("removeSubdomain", params(zone, r.Name), &response)
if err != nil {
Log().Warnw("unexpected error deleting subdomain", "err", err, "response", response)
}
}
}
}
}
p.getZoneRecords(ctx, zone)
return result, nil
}