2021-02-02 12:05:47 +01:00
|
|
|
package geolocate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2021-02-03 12:23:15 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
2022-01-05 17:17:20 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/httpx"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ipInfoResponse struct {
|
|
|
|
IP string `json:"ip"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipInfoIPLookup(
|
|
|
|
ctx context.Context,
|
|
|
|
httpClient *http.Client,
|
2022-01-03 13:53:23 +01:00
|
|
|
logger model.Logger,
|
2021-02-02 12:05:47 +01:00
|
|
|
userAgent string,
|
|
|
|
) (string, error) {
|
|
|
|
var v ipInfoResponse
|
2022-01-05 14:15:42 +01:00
|
|
|
err := (&httpx.APIClientTemplate{
|
2021-02-02 12:05:47 +01:00
|
|
|
Accept: "application/json",
|
|
|
|
BaseURL: "https://ipinfo.io",
|
|
|
|
HTTPClient: httpClient,
|
|
|
|
Logger: logger,
|
|
|
|
UserAgent: httpheader.CLIUserAgent(), // we must be a CLI client
|
2022-01-05 16:26:51 +01:00
|
|
|
}).WithBodyLogging().Build().GetJSON(ctx, "/", &v)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return DefaultProbeIP, err
|
|
|
|
}
|
|
|
|
return v.IP, nil
|
|
|
|
}
|