-
-
Notifications
You must be signed in to change notification settings - Fork 286
/
resolver-plain.go
108 lines (90 loc) Β· 2.56 KB
/
resolver-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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package resolver
import (
"context"
"errors"
"net"
"time"
"github.com/miekg/dns"
"github.com/safing/portbase/log"
"github.com/safing/portmaster/service/netenv"
)
var (
defaultClientTTL = 5 * time.Minute
defaultRequestTimeout = 3 * time.Second // dns query
defaultConnectTimeout = 5 * time.Second // tcp/tls
maxRequestTimeout = 5 * time.Second
)
// PlainResolver is a resolver using plain DNS.
type PlainResolver struct {
BasicResolverConn
}
// NewPlainResolver returns a new TPCResolver.
func NewPlainResolver(resolver *Resolver) *PlainResolver {
newResolver := &PlainResolver{
BasicResolverConn: BasicResolverConn{
resolver: resolver,
},
}
newResolver.BasicResolverConn.init()
return newResolver
}
// Query executes the given query against the resolver.
func (pr *PlainResolver) Query(ctx context.Context, q *Query) (*RRCache, error) {
queryStarted := time.Now()
// create query
dnsQuery := new(dns.Msg)
dnsQuery.SetQuestion(q.FQDN, uint16(q.QType))
// get timeout from context and config
var timeout time.Duration
if deadline, ok := ctx.Deadline(); !ok {
timeout = 0
} else {
timeout = time.Until(deadline)
}
if timeout > defaultRequestTimeout {
timeout = defaultRequestTimeout
}
// create client
dnsClient := &dns.Client{
UDPSize: 1024,
Timeout: timeout,
Dialer: &net.Dialer{
Timeout: timeout,
LocalAddr: getLocalAddr("udp"),
},
}
// query server
reply, ttl, err := dnsClient.Exchange(dnsQuery, pr.resolver.ServerAddress)
log.Tracer(ctx).Tracef("resolver: query took %s", ttl)
// error handling
if err != nil {
// Hint network environment at failed connection if err is not a timeout.
var nErr net.Error
if errors.As(err, &nErr) && !nErr.Timeout() {
netenv.ReportFailedConnection()
}
return nil, err
}
// check if blocked
if pr.resolver.IsBlockedUpstream(reply) {
return nil, &BlockedUpstreamError{pr.resolver.Info.DescriptiveName()}
}
// Hint network environment at successful connection.
netenv.ReportSuccessfulConnection()
// Report request duration for metrics.
reportRequestDuration(queryStarted, pr.resolver)
newRecord := &RRCache{
Domain: q.FQDN,
Question: q.QType,
RCode: reply.Rcode,
Answer: reply.Answer,
Ns: reply.Ns,
Extra: reply.Extra,
Resolver: pr.resolver.Info.Copy(),
}
// TODO: check if reply.Answer is valid
return newRecord, nil
}
// ForceReconnect forces the resolver to re-establish the connection to the server.
// Does nothing for PlainResolver, as every request uses its own connection.
func (pr *PlainResolver) ForceReconnect(_ context.Context) {}