2021-02-02 12:05:47 +01:00
|
|
|
package geolocate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-10-12 18:07:42 +02:00
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-10-12 18:07:42 +02:00
|
|
|
type resolverLookupClient struct {
|
|
|
|
Resolver model.Resolver
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
func (rlc resolverLookupClient) do(ctx context.Context, r dnsResolver) (string, error) {
|
|
|
|
var ips []string
|
2021-11-12 14:53:15 +01:00
|
|
|
ips, err := r.LookupHost(ctx, "whoami.v4.powerdns.org")
|
2021-02-02 12:05:47 +01:00
|
|
|
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) {
|
2022-10-12 18:07:42 +02:00
|
|
|
return rlc.do(ctx, rlc.Resolver)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|