forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnstransport.go
60 lines (49 loc) · 1.55 KB
/
dnstransport.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
package netxlite
//
// Generic DNS transport code.
//
import (
"context"
"github.com/ooni/probe-cli/v3/internal/model"
)
// WrapDNSTransport wraps a DNSTransport to provide error wrapping. This function will
// apply all the provided wrappers around the default transport wrapping. If any of the
// wrappers is nil, we just silently and gracefully ignore it.
func WrapDNSTransport(txp model.DNSTransport,
wrappers ...model.DNSTransportWrapper) (out model.DNSTransport) {
out = &dnsTransportErrWrapper{
DNSTransport: txp,
}
for _, wrapper := range wrappers {
if wrapper == nil {
continue // skip as documented
}
out = wrapper.WrapDNSTransport(out) // compose with user-provided wrappers
}
return
}
// dnsTransportErrWrapper wraps DNSTransport to provide error wrapping.
type dnsTransportErrWrapper struct {
DNSTransport model.DNSTransport
}
var _ model.DNSTransport = &dnsTransportErrWrapper{}
func (t *dnsTransportErrWrapper) RoundTrip(
ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
resp, err := t.DNSTransport.RoundTrip(ctx, query)
if err != nil {
return nil, NewErrWrapper(ClassifyResolverError, DNSRoundTripOperation, err)
}
return resp, nil
}
func (t *dnsTransportErrWrapper) RequiresPadding() bool {
return t.DNSTransport.RequiresPadding()
}
func (t *dnsTransportErrWrapper) Network() string {
return t.DNSTransport.Network()
}
func (t *dnsTransportErrWrapper) Address() string {
return t.DNSTransport.Address()
}
func (t *dnsTransportErrWrapper) CloseIdleConnections() {
t.DNSTransport.CloseIdleConnections()
}