ooni-probe-cli/internal/engine/geolocate/ubuntu.go
Simone Basso 4eeadd06a5
refactor: move more commands to internal/cmd (#207)
* refactor: move more commands to internal/cmd

Part of https://github.com/ooni/probe/issues/1335.

We would like all commands to be at the same level of engine
rather than inside engine (now that we can do it).

* fix: update .gitignore

* refactor: also move jafar outside engine

* We should be good now?
2021-02-03 12:23:15 +01:00

39 lines
744 B
Go

package geolocate
import (
"context"
"encoding/xml"
"net/http"
"github.com/ooni/probe-cli/v3/internal/engine/httpx"
)
type ubuntuResponse struct {
XMLName xml.Name `xml:"Response"`
IP string `xml:"Ip"`
}
func ubuntuIPLookup(
ctx context.Context,
httpClient *http.Client,
logger Logger,
userAgent string,
) (string, error) {
data, err := (httpx.Client{
BaseURL: "https://geoip.ubuntu.com/",
HTTPClient: httpClient,
Logger: logger,
UserAgent: userAgent,
}).FetchResource(ctx, "/lookup")
if err != nil {
return DefaultProbeIP, err
}
logger.Debugf("ubuntu: body: %s", string(data))
var v ubuntuResponse
err = xml.Unmarshal(data, &v)
if err != nil {
return DefaultProbeIP, err
}
return v.IP, nil
}