ooni-probe-cli/internal/engine/legacy/errorsx/resolver.go
Simone Basso 6d3a4f1db8
refactor: merge dnsx and errorsx into netxlite (#517)
When preparing a tutorial for netxlite, I figured it is easier
to tell people "hey, this is the package you should use for all
low-level networking stuff" rather than introducing people to
a set of packages working together where some piece of functionality
is here and some other piece is there.

Part of https://github.com/ooni/probe/issues/1591
2021-09-28 12:42:01 +02:00

57 lines
1.4 KiB
Go

package errorsx
import (
"context"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// Resolver is a DNS resolver. The *net.Resolver used by Go implements
// this interface, but other implementations are possible.
type Resolver interface {
// LookupHost resolves a hostname to a list of IP addresses.
LookupHost(ctx context.Context, hostname string) (addrs []string, err error)
}
// ErrorWrapperResolver is a Resolver that knows about wrapping errors.
type ErrorWrapperResolver struct {
Resolver
}
var _ Resolver = &ErrorWrapperResolver{}
// LookupHost implements Resolver.LookupHost
func (r *ErrorWrapperResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
addrs, err := r.Resolver.LookupHost(ctx, hostname)
err = SafeErrWrapperBuilder{
Classifier: netxlite.ClassifyResolverError,
Error: err,
Operation: netxlite.ResolveOperation,
}.MaybeBuild()
return addrs, err
}
type resolverNetworker interface {
Network() string
}
// Network implements Resolver.Network.
func (r *ErrorWrapperResolver) Network() string {
if rn, ok := r.Resolver.(resolverNetworker); ok {
return rn.Network()
}
return "errorWrapper"
}
type resolverAddresser interface {
Address() string
}
// Address implements Resolver.Address.
func (r *ErrorWrapperResolver) Address() string {
if ra, ok := r.Resolver.(resolverAddresser); ok {
return ra.Address()
}
return ""
}