...
1 package probers
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/miekg/dns"
8 )
9
10
11
12 type DNSProbe struct {
13 proto string
14 server string
15 recurse bool
16 qname string
17 qtype uint16
18 }
19
20
21 func (p DNSProbe) Name() string {
22 recursion := func() string {
23 if p.recurse {
24 return "recurse"
25 }
26 return "no-recurse"
27 }()
28 return fmt.Sprintf(
29 "%s-%s-%s-%s-%s", p.server, p.proto, recursion, dns.TypeToString[p.qtype], p.qname)
30 }
31
32
33 func (p DNSProbe) Kind() string {
34 return "DNS"
35 }
36
37
38 func (p DNSProbe) Probe(timeout time.Duration) (bool, time.Duration) {
39 m := new(dns.Msg)
40 m.SetQuestion(dns.Fqdn(p.qname), p.qtype)
41 m.RecursionDesired = p.recurse
42 c := dns.Client{Timeout: timeout, Net: p.proto}
43 start := time.Now()
44 r, _, err := c.Exchange(m, p.server)
45 if err != nil {
46 return false, time.Since(start)
47 }
48 if r == nil {
49 return false, time.Since(start)
50 }
51 if r.Rcode != dns.RcodeSuccess {
52 return false, time.Since(start)
53 }
54 return true, time.Since(start)
55 }
56
View as plain text