-
Notifications
You must be signed in to change notification settings - Fork 244
/
upstream_plain.go
38 lines (31 loc) · 957 Bytes
/
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
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
}
// Address returns the original address that we've put in initially, not resolved one
func (p *plainDNS) Address() string { return p.address }
func (p *plainDNS) Exchange(m *dns.Msg) (*dns.Msg, error) {
if p.preferTCP {
tcpClient := dns.Client{Net: "tcp", Timeout: p.timeout}
reply, _, tcpErr := tcpClient.Exchange(m, p.address)
return reply, tcpErr
}
client := dns.Client{Timeout: p.timeout, UDPSize: dns.MaxMsgSize}
reply, _, err := client.Exchange(m, p.address)
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}
reply, _, err = tcpClient.Exchange(m, p.address)
}
return reply, err
}