-
Notifications
You must be signed in to change notification settings - Fork 248
/
upstream_plain.go
54 lines (44 loc) · 1.2 KB
/
upstream_plain.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
package upstream
import (
"time"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)
//
// plain DNS
//
type plainDNS struct {
address string
timeout time.Duration
preferTCP bool
}
// type check
var _ Upstream = &plainDNS{}
// Address returns the original address that we've put in initially, not resolved one
func (p *plainDNS) Address() string {
if p.preferTCP {
return "tcp://" + p.address
}
return p.address
}
func (p *plainDNS) Exchange(m *dns.Msg) (*dns.Msg, error) {
if p.preferTCP {
tcpClient := dns.Client{Net: "tcp", Timeout: p.timeout}
logBegin(p.Address(), m)
reply, _, tcpErr := tcpClient.Exchange(m, p.address)
logFinish(p.Address(), tcpErr)
return reply, tcpErr
}
client := dns.Client{Timeout: p.timeout, UDPSize: dns.MaxMsgSize}
logBegin(p.Address(), m)
reply, _, err := client.Exchange(m, p.address)
logFinish(p.Address(), err)
if reply != nil && reply.Truncated {
log.Tracef("Truncated message was received, retrying over TCP, question: %s", m.Question[0].String())
tcpClient := dns.Client{Net: "tcp", Timeout: p.timeout}
logBegin(p.Address(), m)
reply, _, err = tcpClient.Exchange(m, p.address)
logFinish(p.Address(), err)
}
return reply, err
}