ooni-probe-cli/internal/engine/geolocate/resolverlookup.go
Simone Basso 57a3919d2a
fix(geolocate): always use netxlite functionality (#976)
This change ensures that, in turn, we're able to "remote" all the traffic generated by the `geolocate` package, rather than missing some bits of it that were still using the standard library and caused _some_ geolocations to geolocate as the local host rather than as the remote host.

Extracted from https://github.com/ooni/probe-cli/pull/969, where we tested this functionality.

Closes https://github.com/ooni/probe/issues/1383 (which was long overdue).

Part of https://github.com/ooni/probe/issues/2340, because it allows us to make progress with that.
2022-10-12 18:07:42 +02:00

39 lines
866 B
Go

package geolocate
import (
"context"
"errors"
"github.com/ooni/probe-cli/v3/internal/model"
)
var (
// ErrNoIPAddressReturned indicates that no IP address was
// returned by a specific DNS resolver.
ErrNoIPAddressReturned = errors.New("geolocate: no IP address returned")
)
type dnsResolver interface {
LookupHost(ctx context.Context, host string) (addrs []string, err error)
}
type resolverLookupClient struct {
Resolver model.Resolver
}
func (rlc resolverLookupClient) do(ctx context.Context, r dnsResolver) (string, error) {
var ips []string
ips, err := r.LookupHost(ctx, "whoami.v4.powerdns.org")
if err != nil {
return "", err
}
if len(ips) < 1 {
return "", ErrNoIPAddressReturned
}
return ips[0], nil
}
func (rlc resolverLookupClient) LookupResolverIP(ctx context.Context) (ip string, err error) {
return rlc.do(ctx, rlc.Resolver)
}