8f7e3803eb
Acknowledge that transports MAY be used in isolation (i.e., outside of a Resolver) and add support for wrapping. Ensure that every factory that creates an unwrapped type is named accordingly to hopefully ensure there are no surprises. Implement DNSTransport wrapping and use a technique similar to the one used by Dialer to customize the DNSTransport while constructing more complex data types (e.g., a specific resolver). Ensure that the stdlib resolver's own "getaddrinfo" transport (1) is wrapped and (2) could be extended during construction. This work is part of my ongoing effort to bring to this repository websteps-illustrated changes relative to netxlite. Ref issue: https://github.com/ooni/probe/issues/2096
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
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()
|
|
}
|