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
33 lines
750 B
Go
33 lines
750 B
Go
package geolocate
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/httpx"
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
)
|
|
|
|
func cloudflareIPLookup(
|
|
ctx context.Context,
|
|
httpClient *http.Client,
|
|
logger model.Logger,
|
|
userAgent string,
|
|
) (string, error) {
|
|
data, err := (&httpx.APIClientTemplate{
|
|
BaseURL: "https://www.cloudflare.com",
|
|
HTTPClient: httpClient,
|
|
Logger: logger,
|
|
UserAgent: model.HTTPHeaderUserAgent,
|
|
}).WithBodyLogging().Build().FetchResource(ctx, "/cdn-cgi/trace")
|
|
if err != nil {
|
|
return model.DefaultProbeIP, err
|
|
}
|
|
r := regexp.MustCompile("(?:ip)=(.*)")
|
|
ip := strings.Trim(string(r.Find(data)), "ip=")
|
|
logger.Debugf("cloudflare: body: %s", ip)
|
|
return ip, nil
|
|
}
|