ooni-probe-cli/internal/engine/geolocate/ubuntu.go
Simone Basso dba861d262
feat(httpx): implement optional body logging also on http error (#651)
1. we want optionally to log the body (we don't want to log the body
when we're fetching psiphon secrets or tor targets)

2. we want body logging to _also_ happen on error since this is quite
useful to debug possible errors when accessing the API

This diff adds the above functionality, which were previously
described in https://github.com/ooni/probe/issues/1951.

This diff also adds comprehensive testing.
2022-01-05 16:26:51 +01:00

40 lines
835 B
Go

package geolocate
import (
"context"
"encoding/xml"
"net/http"
"github.com/ooni/probe-cli/v3/internal/engine/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 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
}