diff options
Diffstat (limited to 'dns/cilent.go')
-rw-r--r-- | dns/cilent.go | 78 |
1 files changed, 44 insertions, 34 deletions
diff --git a/dns/cilent.go b/dns/cilent.go index 63e3734..989bb4d 100644 --- a/dns/cilent.go +++ b/dns/cilent.go | |||
@@ -1,17 +1,44 @@ | |||
1 | package dns | 1 | package dns |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "code.crute.me/mcrute/go_ddns_manager/bind" | ||
5 | "github.com/miekg/dns" | 4 | "github.com/miekg/dns" |
5 | |||
6 | "code.crute.me/mcrute/go_ddns_manager/bind" | ||
6 | ) | 7 | ) |
7 | 8 | ||
8 | type DNSClient struct { | 9 | type DNSClient struct { |
9 | Server string | 10 | Server string |
10 | } | 11 | } |
11 | 12 | ||
13 | type DNSTransaction struct { | ||
14 | zone *bind.Zone | ||
15 | key *bind.Key | ||
16 | msg *dns.Msg | ||
17 | } | ||
18 | |||
19 | func (t *DNSTransaction) Upsert(rrs ...RR) *DNSTransaction { | ||
20 | t.RemoveAll(rrs...).Insert(rrs...) | ||
21 | return t | ||
22 | } | ||
23 | |||
24 | func (t *DNSTransaction) Insert(rrs ...RR) *DNSTransaction { | ||
25 | t.msg.Insert(toRRSet(t.zone, rrs...)) | ||
26 | return t | ||
27 | } | ||
28 | |||
29 | func (t *DNSTransaction) Remove(rrs ...RR) *DNSTransaction { | ||
30 | t.msg.Remove(toRRSet(t.zone, rrs...)) | ||
31 | return t | ||
32 | } | ||
33 | |||
34 | func (t *DNSTransaction) RemoveAll(rrs ...RR) *DNSTransaction { | ||
35 | t.msg.RemoveRRset(toRRSet(t.zone, rrs...)) | ||
36 | return t | ||
37 | } | ||
38 | |||
12 | func (c *DNSClient) AXFR(zone *bind.Zone) (chan *dns.Envelope, error) { | 39 | func (c *DNSClient) AXFR(zone *bind.Zone) (chan *dns.Envelope, error) { |
13 | k := zone.Keys()[0] | 40 | k := zone.Keys()[0] |
14 | t := &dns.Transfer{TsigSecret: k.AsMap()} | 41 | t := &dns.Transfer{TsigSecret: k.AsMap()} // Always uses tcp |
15 | 42 | ||
16 | m := &dns.Msg{} | 43 | m := &dns.Msg{} |
17 | m.SetAxfr(zone.Name) | 44 | m.SetAxfr(zone.Name) |
@@ -20,48 +47,31 @@ func (c *DNSClient) AXFR(zone *bind.Zone) (chan *dns.Envelope, error) { | |||
20 | return t.In(m, c.Server) | 47 | return t.In(m, c.Server) |
21 | } | 48 | } |
22 | 49 | ||
23 | func (c *DNSClient) Insert(zone *bind.Zone, rrs ...RR) error { | 50 | func (c *DNSClient) StartUpdate(zone *bind.Zone) *DNSTransaction { |
24 | k := zone.Keys()[0] | ||
25 | dc := &dns.Client{TsigSecret: k.AsMap()} | ||
26 | |||
27 | m := &dns.Msg{} | 51 | m := &dns.Msg{} |
28 | m.SetUpdate(zone.Name) | 52 | m.SetUpdate(zone.Name) |
29 | m.Insert(toRRSet(zone, rrs...)) | ||
30 | k.Sign(m) | ||
31 | 53 | ||
32 | if _, _, err := dc.Exchange(m, c.Server); err != nil { | 54 | return &DNSTransaction{ |
33 | return err | 55 | zone: zone, |
56 | key: zone.Keys()[0], | ||
57 | msg: m, | ||
34 | } | 58 | } |
35 | |||
36 | return nil | ||
37 | } | 59 | } |
38 | 60 | ||
39 | func (c *DNSClient) Remove(zone *bind.Zone, rrs ...RR) error { | 61 | func (c *DNSClient) SendUpdate(t *DNSTransaction) error { |
40 | k := zone.Keys()[0] | 62 | udp := &dns.Client{Net: "udp", TsigSecret: t.key.AsMap()} |
41 | dc := &dns.Client{TsigSecret: k.AsMap()} | 63 | tcp := &dns.Client{Net: "tcp", TsigSecret: t.key.AsMap()} |
42 | 64 | ||
43 | m := &dns.Msg{} | 65 | t.msg.SetEdns0(4096, false) |
44 | m.SetUpdate(zone.Name) | 66 | t.key.Sign(t.msg) |
45 | m.Remove(toRRSet(zone, rrs...)) | ||
46 | k.Sign(m) | ||
47 | 67 | ||
48 | if _, _, err := dc.Exchange(m, c.Server); err != nil { | 68 | in, _, err := udp.Exchange(t.msg, c.Server) |
49 | return err | 69 | if in != nil && in.Truncated { |
70 | // If the TCP request succeeds, the err will reset to nil | ||
71 | in, _, err = tcp.Exchange(t.msg, c.Server) | ||
50 | } | 72 | } |
51 | 73 | ||
52 | return nil | 74 | if err != nil { |
53 | } | ||
54 | |||
55 | func (c *DNSClient) RemoveAll(zone *bind.Zone, rrs ...RR) error { | ||
56 | k := zone.Keys()[0] | ||
57 | dc := &dns.Client{TsigSecret: k.AsMap()} | ||
58 | |||
59 | m := &dns.Msg{} | ||
60 | m.SetUpdate(zone.Name) | ||
61 | m.RemoveRRset(toRRSet(zone, rrs...)) | ||
62 | k.Sign(m) | ||
63 | |||
64 | if _, _, err := dc.Exchange(m, c.Server); err != nil { | ||
65 | return err | 75 | return err |
66 | } | 76 | } |
67 | 77 | ||