-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdns.go
51 lines (43 loc) · 1.01 KB
/
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
package main
import (
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/miekg/dns"
)
// getCNAME gets the last CNAME for a record
func getCNAME(domain, ns string, showDNSErrors bool, dnsTimeout time.Duration) (result string, took time.Duration, err error) {
domain = mkFQDN(domain)
ns = ns + ":53"
c := dns.Client{}
// Don't want for more than 2 seconds
c.Timeout = dnsTimeout
m := dns.Msg{}
m.SetQuestion(domain, dns.TypeA)
r, took, err := c.Exchange(&m, ns)
if err != nil {
if showDNSErrors {
logrus.Errorf("Error contacting DNS Server: %s", err)
}
return "", 0, err
}
if len(r.Answer) == 0 {
logrus.Warnf("No results for domain: %s, DNS server IP: %s", domain, ns)
return "", 0, err
}
var lastCNAME string
for _, ans := range r.Answer {
cname, ok := ans.(*dns.CNAME)
if ok {
lastCNAME = cname.Target
}
}
return lastCNAME, took, nil
}
func mkFQDN(domain string) (result string) {
result = domain
if !strings.HasSuffix(domain, ".") {
result = result + "."
}
return result
}