57a3919d2a
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.
41 lines
866 B
Go
41 lines
866 B
Go
package geolocate
|
|
|
|
import (
|
|
"context"
|
|
"encoding/xml"
|
|
"net/http"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/httpx"
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
)
|
|
|
|
type ubuntuResponse struct {
|
|
XMLName xml.Name `xml:"Response"`
|
|
IP string `xml:"Ip"`
|
|
}
|
|
|
|
func ubuntuIPLookup(
|
|
ctx context.Context,
|
|
httpClient *http.Client,
|
|
logger model.Logger,
|
|
userAgent string,
|
|
resolver model.Resolver,
|
|
) (string, error) {
|
|
data, err := (&httpx.APIClientTemplate{
|
|
BaseURL: "https://geoip.ubuntu.com/",
|
|
HTTPClient: httpClient,
|
|
Logger: logger,
|
|
UserAgent: userAgent,
|
|
}).WithBodyLogging().Build().FetchResource(ctx, "/lookup")
|
|
if err != nil {
|
|
return model.DefaultProbeIP, err
|
|
}
|
|
logger.Debugf("ubuntu: body: %s", string(data))
|
|
var v ubuntuResponse
|
|
err = xml.Unmarshal(data, &v)
|
|
if err != nil {
|
|
return model.DefaultProbeIP, err
|
|
}
|
|
return v.IP, nil
|
|
}
|