-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdnsrecords.go
73 lines (67 loc) · 1.98 KB
/
dnsrecords.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
package resolution
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/unstoppabledomains/resolution-go/v3/dnsrecords"
)
func dnsTypesToCryptoRecordKeys(types []dnsrecords.Type) ([]string, error) {
recordKeys := []string{"dns.ttl"}
var key strings.Builder
var ttlKey strings.Builder
for _, dnsType := range types {
key.Reset()
ttlKey.Reset()
dnsTypeUppercase := strings.ToUpper(string(dnsType))
_, err := fmt.Fprintf(&key, "dns.%s", dnsTypeUppercase)
if err != nil {
return nil, err
}
_, err = fmt.Fprintf(&ttlKey, "dns.%s.ttl", dnsTypeUppercase)
if err != nil {
return nil, err
}
recordKeys = append(recordKeys, key.String(), ttlKey.String())
}
return recordKeys, nil
}
func cryptoRecordsToDNS(cryptoRecords map[string]string) ([]dnsrecords.Record, error) {
var globalTTL = dnsrecords.DefaultTTL
if cryptoRecords["dns.ttl"] != "" {
parsedTTL, err := strconv.ParseUint(cryptoRecords["dns.ttl"], 10, 32)
if err == nil {
globalTTL = uint32(parsedTTL)
}
}
var ttlKey strings.Builder
var parsedDNSRecords []dnsrecords.Record
for cryptoKey, cryptoValue := range cryptoRecords {
if strings.Index(cryptoKey, "dns.") == 0 {
keyParts := strings.Split(cryptoKey, ".")
if len(keyParts) == 2 {
recordTTL := globalTTL
recordType := dnsrecords.Type(keyParts[1])
ttlKey.Reset()
_, err := fmt.Fprintf(&ttlKey, "dns.%v.ttl", recordType)
if err != nil {
return nil, err
}
if cryptoRecords[ttlKey.String()] != "" {
parsedTTL, err := strconv.ParseUint(cryptoRecords[ttlKey.String()], 10, 32)
if err == nil {
recordTTL = uint32(parsedTTL)
}
}
var parsedDNSRecordValues []string
err = json.Unmarshal([]byte(cryptoValue), &parsedDNSRecordValues)
if err == nil {
for _, dnsRecordValue := range parsedDNSRecordValues {
parsedDNSRecords = append(parsedDNSRecords, dnsrecords.Record{Type: recordType, TTL: recordTTL, Value: dnsRecordValue})
}
}
}
}
}
return parsedDNSRecords, nil
}