-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
78 lines (69 loc) · 1.8 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
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
package libtower
import (
"context"
"errors"
"net"
"net/http/httptrace"
"time"
"github.com/miekg/dns"
)
// DNS type
type DNS struct {
ADDR string
Timeout time.Duration
Start time.Time
End time.Time
Duration time.Duration
}
// DNSLookup func
func DNSLookup(addr string) (*net.IPAddr, time.Duration, error) {
var dns time.Time
var DNS time.Duration
traceDNS := &httptrace.ClientTrace{
DNSStart: func(dsi httptrace.DNSStartInfo) {
dns = time.Now()
},
DNSDone: func(ddi httptrace.DNSDoneInfo) {
DNS = time.Since(dns)
},
}
ctx := httptrace.WithClientTrace(context.Background(), traceDNS)
ips, err := (&net.Resolver{}).LookupIPAddr(ctx, addr)
if err != nil {
return nil, 0, err
}
if len(ips) == 0 {
return nil, 0, errors.New("ips len is zero")
}
return &ips[0], DNS, nil
}
//DNSLookupFrom func
func DNSLookupFrom(addr string, server string) (*net.IPAddr, time.Duration, error) {
severIP := net.ParseIP(server)
if severIP == nil {
return new(net.IPAddr), 0, errors.New("failed to parse server ip address")
}
serverAddress := server + ":53"
msg := dns.Msg{}
msg.Id = dns.Id()
msg.RecursionDesired = true
msg.Question = []dns.Question{dns.Question{Name: dns.Fqdn(addr), Qtype: dns.TypeA, Qclass: dns.ClassINET}}
client := dns.Client{Net: "udp"}
resp, rtt, err := client.Exchange(&msg, serverAddress)
if err != nil {
return nil, 0, errors.New("dns exchange error: " + err.Error())
}
if resp == nil {
return nil, 0, errors.New("response is nil")
}
if resp != nil && resp.Rcode != dns.RcodeSuccess {
return nil, 0, errors.New(dns.RcodeToString[resp.Rcode])
}
for _, record := range resp.Answer {
if t, ok := record.(*dns.A); ok {
ipAddress := net.IPAddr{IP: t.A}
return &ipAddress, rtt, nil
}
}
return nil, 0, errors.New("record a not find in response")
}