ooni-probe-cli/internal/engine/geolocate/cloudflare.go
Simone Basso 2d721baa91
cleanup: merge httpheader and httpfailure into model (#758)
These two small packages could easily be merged into the model
package, since they're clearly model-like packages.

Part of https://github.com/ooni/probe/issues/2115
2022-05-25 09:54:50 +02:00

33 lines
744 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 DefaultProbeIP, err
}
r := regexp.MustCompile("(?:ip)=(.*)")
ip := strings.Trim(string(r.Find(data)), "ip=")
logger.Debugf("cloudflare: body: %s", ip)
return ip, nil
}