-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddrm-dns.go
108 lines (88 loc) · 2.66 KB
/
ddrm-dns.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
//go:build client
// +build client
package main
import (
"os"
"github.com/miekg/dns"
)
// Simple record type to help make the JSON parsing easier
type DdrmRecordType string
const (
//lint:ignore U1000 Ignore unused types: they are used during JSON parsing but go-staticcheck gets upset because it can't know that
ddrmRecordTypeA DdrmRecordType = "A"
ddrmRecordTypeAAAA DdrmRecordType = "AAAA"
ddrmRecordTypeTXT DdrmRecordType = "TXT"
ddrmRecordTypeMX DdrmRecordType = "MX"
ddrmRecordTypeCAA DdrmRecordType = "CAA"
ddrmRecordTypeCNAME DdrmRecordType = "CNAME"
ddrmRecordTypeNS DdrmRecordType = "NS"
ddrmRecordTypePTR DdrmRecordType = "PTR"
ddrmRecordTypeSOA DdrmRecordType = "SOA"
ddrmRecordTypeSRV DdrmRecordType = "SRV"
)
// try and ask a question to get a record's records
func getRecordData(fqdn string, recordtype DdrmRecordType) (answer []string) {
dnsclient := new(dns.Client)
dnsclient.Net = "udp"
if stateTCP {
dnsclient.Net = "tcp"
}
if stateIPV4 {
dnsclient.Net += "4"
}
if stateIPV6 {
dnsclient.Net += "6"
}
dnsclient.Timeout = stateDNSTimeout
dnsType := dns.StringToType[string(recordtype)]
msg := new(dns.Msg)
msg.SetQuestion(dns.Fqdn(fqdn), dnsType)
in, _, err := dnsclient.Exchange(msg, ddrmAppConfig.DnsServer1)
if err != nil {
dbgf(ddrmReportDNSClientErr, ddrmAppConfig.DnsServer1, fqdn, recordtype, err)
// try the second configured DNS server if it's configured
if len(ddrmAppConfig.DnsServer2) > 0 {
in, _, err = dnsclient.Exchange(msg, ddrmAppConfig.DnsServer2)
}
if err != nil {
return nil
}
}
answer = []string{}
for _, r := range in.Answer {
switch t := r.(type) {
case *dns.A:
answer = append(answer, stringProcessor(t.A.String()))
case *dns.AAAA:
answer = append(answer, stringProcessor(t.AAAA.String()))
case *dns.CNAME:
answer = append(answer, stringProcessor(t.Target))
case *dns.CAA:
answer = append(answer, stringProcessor(t.Value))
case *dns.MX:
answer = append(answer, stringProcessor(t.Mx))
case *dns.TXT:
for _, txt := range t.Txt {
answer = append(answer, stringProcessor(txt))
}
case *dns.NS:
answer = append(answer, stringProcessor(t.Ns))
case *dns.PTR:
answer = append(answer, stringProcessor(t.Ptr))
case *dns.SRV:
answer = append(answer, stringProcessor(t.Target))
case *dns.SOA:
answer = append(answer, stringProcessor(t.String()))
}
}
return answer
}
func testDnsClient() {
if stateDNSClientTest {
dbgp(getRecordData("sommefeldt.com", "MX"))
dbgp(getRecordData("sommefeldt.com", "A"))
dbgp(getRecordData("sommefeldt.com", "SOA"))
dbgp(getRecordData("sommefeldt.com", "TXT"))
os.Exit(ddrmExitAfterDNSClientTest)
}
}