Skip to content
InternetData

Cloudflare

The official InternetData Worker for Cloudflare passes each visitor's IP data to your site from Cloudflare's edge, on any plan, and can block or challenge visitors by country, network and privacy signals.

View as MarkdownSee on GitHub

Getting Started

Deploy to Cloudflare

  1. Create an API key in the InternetData console. An account is free, and so is the plan it starts on.
  2. Click Deploy to Cloudflare above. Cloudflare copies this repository into your GitHub or GitLab account, asks for your API key, and deploys the Worker.
  3. Open the Worker's workers.dev address, which the deploy shows. The page there says whether your key works and what the Worker makes of your own connection.
  4. Put the Worker in front of your site. In the Cloudflare dashboard, open Workers & Pages, select internetdata, then Settings > Domains & Routes > Add > Route. Pick your zone, enter example.com/* with your own domain, and select Add route; a site on subdomains too takes a second route, *.example.com/*. The Worker sees only traffic whose DNS record is proxied through Cloudflare, the orange cloud.
  5. Set each route to Fail open, so your site still loads as if the Worker weren't there if your account ever runs past the Workers free plan's 100,000 requests a day.

From then on every request on the route reaches your site with the visitor's data in the X-InternetData header. Out of the box the Worker refuses no one: set BLOCK, BLOCK_COUNTRIES or ALLOW_COUNTRIES to act on visitors.

The data at your site

With FORWARD on, every request the Worker checks reaches your site with the answer in the X-InternetData header, as JSON. The Worker deletes any copy of the header a visitor sends, so your site can trust it.

json
{"ip":"185.65.134.1","matched":false,"signals":[],"result":{"ip":"185.65.134.1","location":{"country":"NL","city":"Amsterdam"},"asn":{"asn":39351,"name":"31173 Services AB","type":"isp"},"company":{"name":"31173 Services Netherlands","type":"business"},"is_vpn":true}}

result is the API's answer as served, with the fields your plan includes. matched says whether the visitor matched your rules, which with ACTION set to log is all that happened, and signals names what matched. challenge is passed once the visitor passed the Turnstile check, exempt is verified-bot for a crawler let through, and error says why a lookup failed. A request the Worker didn't check, such as one to an allowed path, carries no header.

php
$visitor = json_decode($_SERVER['HTTP_X_INTERNETDATA'] ?? 'null', true);
$country = $visitor['result']['location']['country'] ?? null;
$network = $visitor['result']['asn']['type'] ?? null;

Settings

Every setting has a default, so the deploy asks only for your API key. To change one, add it as a variable of type Text under Workers & Pages > internetdata > Settings > Variables and Secrets. wrangler.jsonc sets keep_vars, so later deploys from your copy keep it. You can also set it under vars in wrangler.jsonc in your copy of the repository and push, since Cloudflare deploys every push. For routes: while wrangler.jsonc names no routes, a deploy leaves the ones you added in the dashboard as they are, and once it names some, each deploy sets exactly those.

VariableDefaultWhat it does
BLOCKWhat the Worker acts on, from the signals below, comma-separated
BLOCK_COUNTRIESCountries whose visitors the Worker acts on, as two-letter codes, such as CU, IR, KP
ALLOW_COUNTRIESThe only countries whose visitors the Worker lets through untouched; set this or BLOCK_COUNTRIES
ACTIONblockblock refuses a visitor who matches, challenge asks them for a Turnstile check, and log lets them through marked
PROXY_MIN_HITS1How many times an address must have been seen in a residential, mobile or datacenter proxy pool before those signals count
ALLOW_IPSAddresses and ranges never checked, such as 203.0.113.7, 2001:db8::/32
ALLOW_PATHSPath prefixes never checked, such as /webhooks/, /robots.txt
BLOCK_MESSAGEThe sentence a refused visitor reads, in place of one naming what was found
TURNSTILE_SITE_KEYYour Turnstile widget's site key, for challenge
FAIL_CLOSEDfalsetrue refuses visitors while the API can't be reached
FORWARDtrueWhether each answer reaches your site in the X-InternetData header
TIMEOUT_MS2500How long a lookup may hold a request before the Worker gives up on it
CACHE_TTL3600How long an answer is kept, in seconds

Your API key is the secret INTERNETDATA_API_KEY. The deploy asks for it, and you can change it under Settings > Variables and Secrets in the dashboard, or with npx wrangler secret put INTERNETDATA_API_KEY.

Signals and countries

SignalMatchesPlan
torTor exit and relay nodesEvery plan
relayiCloud Private Relay and company web gateways, mostly ordinary peopleEvery plan
hostingServers at hosting and cloud providers, where bots and scripts runA paid plan
smtpAddresses that run a mail server, which home and mobile connections can'tA paid plan
vpnCommercial and self-hosted VPN serversA paid plan
pubproxyOpen proxies anyone can connect throughA paid plan
resproxyHome connections rented out as proxiesA paid plan
mobproxyPhone connections rented out as proxies, whose addresses carriers share with ordinary customersA paid plan
dcproxyProxies running in datacentersA paid plan
spoofedA client whose User-Agent names a crawler, such as Googlebot, from an address that crawler doesn't useA paid plan

A visitor from a country BLOCK_COUNTRIES names, or one ALLOW_COUNTRIES leaves out, matches country, on every plan. A visitor whose country isn't known matches neither list.

A free key's full answers cover every signal. Any other answer from the free plan lacks the signals the table lists under a paid plan, so those never match, and the Worker logs that once. A privacy relay is judged as a relay alone, even where it runs on a hosting network.

Search engines' crawlers are never refused. On a paid plan, an address whose bot operator vouches for it, such as Googlebot's, passes whatever it matched. The free plan's answer doesn't say which addresses bots use, so there the country lists can refuse a crawler too.

spoofed catches a forged crawler, a scraper that calls itself Googlebot to get past rules that let crawlers in. It's off unless BLOCK names it. While it's on, a lookup also sends the visitor's User-Agent when it names a crawler, never a browser's. An address is looked up with at most four crawler agents an hour, or while its answers are cached when CACHE_TTL is longer, and a crawler it claims past those is judged spoofed if one before it was.

Challenge instead of block

A Turnstile check lets a person in after proving they're a person, and stops scripts.

  1. In the Cloudflare dashboard, open Turnstile > Add widget, name it, add your site's hostname, and keep the Managed mode.
  2. Set TURNSTILE_SITE_KEY to the widget's site key and ACTION to challenge, as Settings says.
  3. Add the widget's secret key as the secret TURNSTILE_SECRET_KEY, under Settings > Variables and Secrets or with npx wrangler secret put TURNSTILE_SECRET_KEY.

A visitor who matches then sees the check. Passing it sets a cookie that lets that visitor's address through for 24 hours. A request that isn't a page load, such as an API call, is refused with 403 until then.

Costs and failures

The Worker looks each address up at most once an hour in each Cloudflare data center, and every copy of the Worker there shares the answer. Requests for static files (.css, .js, images and fonts) and OPTIONS requests are passed on without a lookup. Every request on the route still counts against your Workers plan. To keep a path off the Worker altogether, such as example.com/assets/*, add a route for it with no Worker: the most specific route wins.

If the API doesn't answer within TIMEOUT_MS, or answers with an error, the visitor is let through and the header says why, unless FAIL_CLOSED is true. So is a lookup past your organization's lookups with every field, which the Worker also logs as an error, once a minute at most. A setting that's wrong stops the Worker checking anyone until it's fixed, and the page at its workers.dev address names the setting. Each refusal, challenge and logged match is a line in the Worker's logs, under Workers & Pages > internetdata > Logs.

Updating

Your copy of the repository doesn't follow this one, and shares none of its history. The first time you take a newer release, mark the release your copy was made from, then merge and push:

bash
git remote add upstream https://github.com/internetdata/cloudflare-plugin.git
git fetch upstream
git merge -s ours --allow-unrelated-histories -m "Track internetdata/cloudflare-plugin" "v$(node -p "require('./package.json').version")"
git merge --no-edit upstream/main
git push

After that, git fetch upstream, git merge --no-edit upstream/main and git push take each release. Your Worker's name and anything you changed stay as they are.

Deploying without the button

bash
git clone https://github.com/internetdata/cloudflare-plugin.git internetdata-cloudflare
cd internetdata-cloudflare
npm install
npx wrangler secret put INTERNETDATA_API_KEY
npx wrangler deploy

Then add the route as in steps 4 and 5, or uncomment routes in wrangler.jsonc before you deploy. npm test runs the Worker in Cloudflare's own runtime, and npm run dev serves it locally, where it shows the same page as on workers.dev.

What it sends

The Worker calls the InternetData API at https://api.internetdata.io with your API key and the visitor's IP address, at most once an hour per address in each Cloudflare data center. With BLOCK naming spoofed, it sends the visitor's User-Agent too when it names a crawler, for up to four such agents an hour per address. With ACTION set to challenge, it sends a Turnstile token and the visitor's address to Cloudflare's Turnstile service when a visitor submits the check.