110a11828b
A bunch of packages (including oohelperd) just need the ability to use MaxMind-like databases. They don't need the additional functionality implemented by the geolocate package. Such a package, in fact, is mostly (if not only) needed by the engine package. Therefore, move code to query MaxMind-like databases to a separate package, and avoid depending on geolocate in all the packages for which it's sufficient to use geoipx. Part of https://github.com/ooni/probe/issues/2240
40 lines
840 B
Go
40 lines
840 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,
|
|
) (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
|
|
}
|