2022-02-09 11:54:19 +01:00
|
|
|
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,
|
2022-10-12 18:07:42 +02:00
|
|
|
resolver model.Resolver,
|
2022-02-09 11:54:19 +01:00
|
|
|
) (string, error) {
|
|
|
|
data, err := (&httpx.APIClientTemplate{
|
|
|
|
BaseURL: "https://www.cloudflare.com",
|
|
|
|
HTTPClient: httpClient,
|
|
|
|
Logger: logger,
|
2022-05-25 09:54:50 +02:00
|
|
|
UserAgent: model.HTTPHeaderUserAgent,
|
2022-02-09 11:54:19 +01:00
|
|
|
}).WithBodyLogging().Build().FetchResource(ctx, "/cdn-cgi/trace")
|
|
|
|
if err != nil {
|
2022-08-28 20:00:25 +02:00
|
|
|
return model.DefaultProbeIP, err
|
2022-02-09 11:54:19 +01:00
|
|
|
}
|
|
|
|
r := regexp.MustCompile("(?:ip)=(.*)")
|
|
|
|
ip := strings.Trim(string(r.Find(data)), "ip=")
|
|
|
|
logger.Debugf("cloudflare: body: %s", ip)
|
|
|
|
return ip, nil
|
|
|
|
}
|