-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprovider.go
122 lines (99 loc) · 2.69 KB
/
provider.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
package rfc2136
import (
"context"
"fmt"
"github.com/libdns/libdns"
"github.com/miekg/dns"
"time"
)
type Provider struct {
KeyName string `json:"key_name,omitempty"`
KeyAlg string `json:"key_alg,omitempty"`
Key string `json:"key,omitempty"`
Server string `json:"server,omitempty"`
}
func (p *Provider) keyNameFQDN() string {
return dns.Fqdn(p.KeyName)
}
func (p *Provider) client() *dns.Client {
return &dns.Client{
TsigSecret: map[string]string{p.keyNameFQDN(): p.Key},
Net: "tcp",
}
}
func (p *Provider) setTsig(msg *dns.Msg) {
msg.SetTsig(p.keyNameFQDN(), dns.Fqdn(p.KeyAlg), 300, time.Now().Unix())
}
func (p *Provider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
zone = dns.Fqdn(zone)
conn, err := p.client().DialContext(ctx, p.Server)
if err != nil {
return nil, fmt.Errorf("dial: %w", err)
}
tn := dns.Transfer{
Conn: conn,
}
tn.TsigSecret = map[string]string{p.keyNameFQDN(): p.Key}
msg := dns.Msg{}
msg.SetAxfr(zone)
p.setTsig(&msg)
res, err := tn.In(&msg, p.Server)
if err != nil {
return nil, fmt.Errorf("start zone transfer: %w", err)
}
records := make([]libdns.Record, 0)
for e := range res {
if e.Error != nil {
return nil, fmt.Errorf("zone transfer: %w", e.Error)
}
for _, rr := range e.RR {
records = append(records, recordFromRR(rr, zone))
}
}
return records, nil
}
func (p *Provider) AppendRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
return p.SetRecords(ctx, zone, records)
}
func (p *Provider) SetRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zone = dns.Fqdn(zone)
msg := dns.Msg{}
msg.SetUpdate(zone)
for _, rec := range records {
rr, err := recordToRR(rec, zone)
if err != nil {
return nil, fmt.Errorf("invalid record %s: %w", rec.Name, err)
}
msg.Insert([]dns.RR{rr})
}
p.setTsig(&msg)
_, _, err := p.client().ExchangeContext(ctx, &msg, p.Server)
if err != nil {
return nil, err
}
return records, nil
}
func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []libdns.Record) ([]libdns.Record, error) {
zone = dns.Fqdn(zone)
msg := dns.Msg{}
msg.SetUpdate(zone)
for _, rec := range records {
rr, err := recordToRR(rec, zone)
if err != nil {
return nil, fmt.Errorf("invalid record %s: %w", rec.Name, err)
}
msg.Remove([]dns.RR{rr})
}
p.setTsig(&msg)
_, _, err := p.client().ExchangeContext(ctx, &msg, p.Server)
if err != nil {
return nil, err
}
return records, nil
}
var (
_ libdns.RecordGetter = (*Provider)(nil)
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
)