forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsovergetaddrinfo.go
159 lines (134 loc) · 3.94 KB
/
dnsovergetaddrinfo.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package netxlite
//
// DNS over getaddrinfo: fake transport to allow us to observe
// lookups using getaddrinfo as a DNSTransport.
//
import (
"context"
"net"
"time"
"github.com/miekg/dns"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
// dnsOverGetaddrinfoTransport is a DNSTransport using getaddrinfo.
type dnsOverGetaddrinfoTransport struct {
// (OPTIONAL) allows to run tests with a short timeout
testableTimeout time.Duration
// (OPTIONAL) allows to mock the underlying getaddrinfo call
testableLookupANY func(ctx context.Context, domain string) ([]string, string, error)
}
// NewDNSOverGetaddrinfoTransport creates a new dns-over-getaddrinfo transport.
func NewDNSOverGetaddrinfoTransport() model.DNSTransport {
return &dnsOverGetaddrinfoTransport{}
}
var _ model.DNSTransport = &dnsOverGetaddrinfoTransport{}
func (txp *dnsOverGetaddrinfoTransport) RoundTrip(
ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
if query.Type() != dns.TypeANY {
return nil, ErrNoDNSTransport
}
addrs, cname, err := txp.lookup(ctx, query.Domain())
if err != nil {
return nil, err
}
resp := &dnsOverGetaddrinfoResponse{
addrs: addrs,
cname: cname,
query: query,
}
return resp, nil
}
type dnsOverGetaddrinfoResponse struct {
addrs []string
cname string
query model.DNSQuery
}
// Used to move addrs and cname out of the worker goroutine
type dnsOverGetaddrinfoAddrsAndCNAME struct {
// List of resolved addresses (it's a bug if this is empty)
addrs []string
// Resolved CNAME or empty string
cname string
}
func (txp *dnsOverGetaddrinfoTransport) lookup(
ctx context.Context, hostname string) ([]string, string, error) {
// This code forces adding a shorter timeout to the domain name
// resolutions when using the system resolver. We have seen cases
// in which such a timeout becomes too large. One such case is
// described in https://github.com/ooni/probe/issues/1726.
outch, errch := make(chan *dnsOverGetaddrinfoAddrsAndCNAME, 1), make(chan error, 1)
ctx, cancel := context.WithTimeout(ctx, txp.timeout())
defer cancel()
go func() {
addrs, cname, err := txp.lookupfn()(ctx, hostname)
if err != nil {
errch <- err
return
}
outch <- &dnsOverGetaddrinfoAddrsAndCNAME{
addrs: addrs,
cname: cname,
}
}()
select {
case <-ctx.Done():
return nil, "", ctx.Err()
case p := <-outch:
return p.addrs, p.cname, nil
case err := <-errch:
return nil, "", err
}
}
func (txp *dnsOverGetaddrinfoTransport) timeout() time.Duration {
if txp.testableTimeout > 0 {
return txp.testableTimeout
}
return 15 * time.Second
}
func (txp *dnsOverGetaddrinfoTransport) lookupfn() func(ctx context.Context, domain string) ([]string, string, error) {
if txp.testableLookupANY != nil {
return txp.testableLookupANY
}
return TProxy.GetaddrinfoLookupANY
}
func (txp *dnsOverGetaddrinfoTransport) RequiresPadding() bool {
return false
}
func (txp *dnsOverGetaddrinfoTransport) Network() string {
return TProxy.GetaddrinfoResolverNetwork()
}
func (txp *dnsOverGetaddrinfoTransport) Address() string {
return ""
}
func (txp *dnsOverGetaddrinfoTransport) CloseIdleConnections() {
// nothing
}
func (r *dnsOverGetaddrinfoResponse) Query() model.DNSQuery {
runtimex.PanicIfNil(r.query, "dnsOverGetaddrinfoResponse with nil query")
return r.query
}
func (r *dnsOverGetaddrinfoResponse) Bytes() []byte {
return nil
}
func (r *dnsOverGetaddrinfoResponse) Rcode() int {
return 0
}
func (r *dnsOverGetaddrinfoResponse) DecodeHTTPS() (*model.HTTPSSvc, error) {
return nil, ErrNoDNSTransport
}
func (r *dnsOverGetaddrinfoResponse) DecodeLookupHost() ([]string, error) {
if len(r.addrs) <= 0 {
return nil, ErrOODNSNoAnswer
}
return r.addrs, nil
}
func (r *dnsOverGetaddrinfoResponse) DecodeNS() ([]*net.NS, error) {
return nil, ErrNoDNSTransport
}
func (r *dnsOverGetaddrinfoResponse) DecodeCNAME() (string, error) {
if r.cname == "" {
return "", ErrOODNSNoAnswer
}
return r.cname, nil
}