-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert.go
More file actions
225 lines (200 loc) · 5.28 KB
/
convert.go
File metadata and controls
225 lines (200 loc) · 5.28 KB
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
package wedos
import (
"fmt"
"net/netip"
"strconv"
"strings"
"time"
"github.com/libdns/libdns"
)
const (
ARecord = "A"
AAAARecord = "AAAA"
AliasRecord = "ALIAS"
CNAMERecord = "CNAME"
MXRecord = "MX"
TXTRecord = "TXT"
SRVRecord = "SRV"
NSRecord = "NS"
SORARRecord = "SOA"
DNAMERecord = "DNAME"
NAPROOTRecord = "NAPTR"
CAARecord = "CAA"
HTTPSRecord = "HTTPS"
SSHFPRecord = "SSHFP"
TLSARecord = "TLSA"
)
// normalizeNameFromAPI converts provider `name` into libdns-style Name (relative).
// zone must be canonicalized (no trailing dot, lower case).
func normalizeNameFromAPI(apiName string) (string, error) {
apiName = strings.TrimSpace(apiName)
if apiName == "" {
return "@", nil
}
return apiName, nil
}
func normalizeNameToAPI(apiName string) (string, error) {
apiName = strings.TrimSpace(apiName)
if apiName == "@" {
return "", nil
}
return apiName, nil
}
// toLibDNSRecord converts a rowItem (response from WEDOS API) into a libdns.Record
func toLibDNSRecord(row rowItem) (libdns.Record, error) {
var record libdns.Record
recordType := strings.ToUpper(strings.TrimSpace(row.Rdtype))
ttlSeconds, err := strconv.Atoi(row.TTL)
if err != nil {
return nil, fmt.Errorf("toLibDNSRecord: failed to convert row.TTL (%s) to type int: %v", row.Rdata, err)
}
name, err := normalizeNameFromAPI(row.Name)
if err != nil {
return nil, err
}
switch recordType {
case ARecord, AAAARecord:
ip, err := netip.ParseAddr(strings.TrimSpace(row.Rdata))
if err != nil {
return nil, fmt.Errorf("invalid %s record value: %s: %w", recordType, row.Rdata, err)
}
record = libdns.Address{
Name: name,
IP: ip,
TTL: time.Duration(ttlSeconds) * time.Second,
}
case CNAMERecord:
record = libdns.CNAME{
Name: name,
Target: strings.TrimSpace(row.Rdata),
TTL: time.Duration(ttlSeconds) * time.Second,
}
case TXTRecord:
record = libdns.TXT{
Name: name,
Text: row.Rdata,
TTL: time.Duration(ttlSeconds) * time.Second,
}
case SRVRecord:
service := strings.Split(row.Name, ".")
serviceName := service[0][1:]
serviceProtocol := service[1][1:]
serviceSubdomain := service[2]
parts := strings.Split(row.Rdata, " ")
if len(parts) != 4 {
return nil, fmt.Errorf("toLibDNSRecord: invalid SRV record value: %s", row.Rdata)
}
priority, err := strconv.Atoi(parts[0])
if err != nil {
return nil, fmt.Errorf("toLibDNSRecord: invalid SRV record value: %s: %w", row.Rdata, err)
}
weight, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("toLibDNSRecord: invalid SRV record value: %s: %w", row.Rdata, err)
}
port, err := strconv.Atoi(parts[2])
if err != nil {
return nil, fmt.Errorf("toLibDNSRecord: invalid SRV record value: %s: %w", row.Rdata, err)
}
hostname := parts[3]
record = libdns.SRV{
Service: serviceName,
Transport: serviceProtocol,
Name: serviceSubdomain, // todo "_minecraft._tcp.mc" or "mc"?
TTL: time.Duration(ttlSeconds) * time.Second,
Priority: uint16(priority),
Weight: uint16(weight),
Port: uint16(port),
Target: hostname,
}
default:
// Generic RR fallback
record = libdns.RR{
Name: name,
TTL: time.Duration(ttlSeconds) * time.Second,
Type: row.Rdtype,
Data: row.Rdata,
}
}
return record, nil
}
// toWedosDNSRecord converts a libdns.Record to a JSON format represented by a map for the WEDOS API
func toWedosDNSRecord(record libdns.Record, zone string) (map[string]string, error) {
zone = strings.TrimSuffix(zone, ".")
name, err := normalizeNameToAPI(record.RR().Name)
if err != nil {
return nil, fmt.Errorf("toWedosDNSRecord: failed to normalize name: %v", err)
}
switch r := record.(type) {
case libdns.Address:
recordType := ARecord
if r.IP.Is6() {
recordType = AAAARecord
}
return map[string]string{
"domain": zone,
"name": name,
"ttl": strconv.Itoa(int(r.TTL)),
"type": recordType,
"rdata": r.IP.String(),
}, nil
case libdns.CNAME:
return map[string]string{
"domain": zone,
"name": name,
"ttl": strconv.Itoa(int(r.TTL)),
"type": CNAMERecord,
"rdata": r.Target,
}, nil
case libdns.TXT:
p := map[string]string{
"domain": zone,
"name": name,
"ttl": strconv.Itoa(int(r.TTL)),
"type": TXTRecord,
"rdata": r.Text,
}
return p, nil
case libdns.MX:
return map[string]string{
"domain": zone,
"name": name,
"ttl": strconv.Itoa(int(r.TTL)),
"type": MXRecord,
"rdata": fmt.Sprintf("%d %s", r.Preference, r.Target),
}, nil
case libdns.NS:
return map[string]string{
"domain": zone,
"name": name,
"ttl": strconv.Itoa(int(r.TTL)),
"type": NSRecord,
"rdata": r.Target,
}, nil
case libdns.SRV:
return map[string]string{
"domain": zone,
"name": name,
"ttl": strconv.Itoa(int(r.TTL)),
"type": SRVRecord,
"rdata": fmt.Sprintf("%d %d %d %s", r.Priority, r.Weight, r.Port, r.Target),
}, nil
case libdns.RR:
return map[string]string{
"domain": zone,
"name": name,
"ttl": strconv.Itoa(int(r.TTL)),
"type": r.Type,
"rdata": r.Data,
}, nil
default:
rr := record.RR()
return map[string]string{
"domain": zone,
"name": name,
"ttl": strconv.Itoa(int(rr.TTL)),
"type": rr.Type,
"rdata": rr.Data,
}, nil
}
}