-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdns.go
88 lines (75 loc) · 1.57 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
79
80
81
82
83
84
85
86
87
88
package xk6_dns
import (
"context"
"fmt"
"time"
"github.com/miekg/dns"
)
type K6DNS struct {
client *dns.Client
Version string
}
func NewK6DNS(version string) *K6DNS {
return &K6DNS{
client: &dns.Client{},
Version: version,
}
}
func (k *K6DNS) SetDialTimeout(s string) error {
d, err := time.ParseDuration(s)
if err != nil {
return err
}
k.client.DialTimeout = d
return nil
}
func (k *K6DNS) SetReadTimeout(s string) error {
d, err := time.ParseDuration(s)
if err != nil {
return err
}
k.client.ReadTimeout = d
return nil
}
func (k *K6DNS) SetWriteTimeout(s string) error {
d, err := time.ParseDuration(s)
if err != nil {
return err
}
k.client.WriteTimeout = d
return nil
}
func (k *K6DNS) Resolve(ctx context.Context, addr, query, qtypeStr string) (string, error) {
qtype, ok := dns.StringToType[qtypeStr]
if !ok {
return "", fmt.Errorf("unknown query type: %s", qtypeStr)
}
msg := &dns.Msg{}
msg.Id = dns.Id()
msg.RecursionDesired = true
msg.Question = make([]dns.Question, 1)
msg.Question[0] = dns.Question{
Name: query,
Qtype: qtype,
Qclass: dns.ClassINET,
}
reportDial(ctx)
conn, err := NewK6UDPConn(addr)
if err != nil {
reportDialError(ctx)
return err.Error(), nil
}
defer func() {
conn.Close()
reportDataReceived(ctx, float64(conn.rxBytes))
reportDataSent(ctx, float64(conn.txBytes))
}()
reportRequest(ctx)
resp, rtt, err := k.client.ExchangeWithConn(msg, &dns.Conn{Conn: conn})
if err != nil {
reportRequestError(ctx)
return err.Error(), nil
}
reportResponseTime(ctx, rtt)
return resp.String(), nil
}