5a481b395a
This diff needs to be backported to the release/3.11 branch. Reference issue https://github.com/ooni/probe/issues/1865.
36 lines
801 B
Go
36 lines
801 B
Go
package geolocate
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
)
|
|
|
|
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{}
|
|
|
|
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, &net.Resolver{})
|
|
}
|