forked from libdns/libdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
195 lines (168 loc) · 4.72 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
package dnsexit
import (
"context"
"encoding/json"
"fmt"
"net"
"net/netip"
"os"
"time"
"github.com/go-resty/resty/v2"
"github.com/libdns/libdns"
"github.com/pkg/errors"
)
const (
// API URL to POST updates to
updateURL = "https://api.dnsexit.com/dns/"
)
var (
// Set environment variable to "TRUE" to enable debug logging
debug = (os.Getenv("LIBDNS_DNSEXIT_DEBUG") == "TRUE")
client = resty.New()
)
// 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 API only supports adding/updating/deleting records 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) amendRecords(zone string, records []libdns.Record, action Action) ([]libdns.Record, error) {
var payloadRecords []dnsExitRecord
p.mutex.Lock()
defer p.mutex.Unlock()
////////////////////////////////////////////////
// BUILD PAYLOAD
////////////////////////////////////////////////
for _, record := range records {
if record.TTL/time.Second < 600 {
record.TTL = 600 * time.Second
}
ttlInSeconds := int(record.TTL / time.Second)
relativeName := libdns.RelativeName(record.Name, zone)
trimmedName := relativeName
if relativeName == "@" {
trimmedName = ""
}
currentRecord := dnsExitRecord{}
currentRecord.Type = record.Type
currentRecord.Name = trimmedName
if action != Delete {
recordValue := record.Value
currentRecord.Content = &recordValue
recordPriority := int(record.Priority)
currentRecord.Priority = &recordPriority
recordTTL := ttlInSeconds
currentRecord.TTL = &recordTTL
}
if action == Set {
truevalue := true
currentRecord.Overwrite = &truevalue
}
payloadRecords = append(payloadRecords, currentRecord)
}
payload := dnsExitPayload{}
payload.Apikey = p.APIKey
payload.Zone = zone
switch action {
case Delete:
payload.DeleteRecords = &payloadRecords
case Set:
fallthrough
case Append:
payload.AddRecords = &payloadRecords
default:
return nil, errors.New(fmt.Sprintf("Unknown action type: %d", action))
}
////////////////////////////////////////////////
//SEND PAYLOAD
////////////////////////////////////////////////
// Explore response object
reqBody, err := json.Marshal(payload)
if err != nil {
return nil, err
}
if debug {
fmt.Println("Request Info:")
fmt.Println("Body:", string(reqBody))
}
// Make the API request to DNSExit
// POST Struct, default is JSON content type. No need to set one
resp, err := client.R().
SetBody(payload).
SetResult(&dnsExitResponse{}).
SetError(&dnsExitResponse{}).
Post(updateURL)
if err != nil {
return nil, err
}
//TODO - query the response code and text to determine which updates where successful, and return both records and response text in all cases, rather than just assuming all records for a 0 code and no records for other codes.
// On any non-zero return code return the API response as the error text.
if !isResposeStatusOK(resp.Body()) {
respBody := string(resp.String())
return nil, errors.New(fmt.Sprintf("API request failed, response=%s", respBody))
}
return records, nil
}
// Convert API response code to human friendly error
func isResposeStatusOK(body []byte) bool {
var respJson dnsExitResponse
json.Unmarshal(body, &respJson)
return respJson.Code == 0
}