Skip to content

Repository files navigation

InternetData InternetData Cloudflare Worker

release license

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.

Cloudflare already tells your site where a visitor is. This Worker adds who is behind the address, from InternetData's data: the company and the type of network it belongs to, and whether it's a VPN, proxy, Tor or hosting address, which Cloudflare keeps for its Enterprise plan. It works in front of any site behind Cloudflare, whatever runs it.

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.

{"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.

$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.

Variable Default What it does
BLOCK What the Worker acts on, from the signals below, comma-separated
BLOCK_COUNTRIES Countries whose visitors the Worker acts on, as two-letter codes, such as CU, IR, KP
ALLOW_COUNTRIES The only countries whose visitors the Worker lets through untouched; set this or BLOCK_COUNTRIES
ACTION block block refuses a visitor who matches, challenge asks them for a Turnstile check, and log lets them through marked
PROXY_MIN_HITS 1 How many times an address must have been seen in a residential, mobile or datacenter proxy pool before those signals count
ALLOW_IPS Addresses and ranges never checked, such as 203.0.113.7, 2001:db8::/32
ALLOW_PATHS Path prefixes never checked, such as /webhooks/, /robots.txt
BLOCK_MESSAGE The sentence a refused visitor reads, in place of one naming what was found
TURNSTILE_SITE_KEY Your Turnstile widget's site key, for challenge
FAIL_CLOSED false true refuses visitors while the API can't be reached
FORWARD true Whether each answer reaches your site in the X-InternetData header
TIMEOUT_MS 2500 How long a lookup may hold a request before the Worker gives up on it
CACHE_TTL 3600 How 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

Signal Matches Plan
tor Tor exit and relay nodes Every plan
relay iCloud Private Relay and company web gateways, mostly ordinary people Every plan
hosting Servers at hosting and cloud providers, where bots and scripts run Every plan, judged by the network an address belongs to on the free plan
smtp Addresses that run a mail server, which home and mobile connections can't Every plan
vpn Commercial and self-hosted VPN servers A paid plan
pubproxy Open proxies anyone can connect through A paid plan
resproxy Home connections rented out as proxies A paid plan
mobproxy Phone connections rented out as proxies, whose addresses carriers share with ordinary customers A paid plan
dcproxy Proxies running in datacenters A paid plan
spoofed A client whose User-Agent names a crawler, such as Googlebot, from an address that crawler doesn't use A 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 account also tries every signal for a while; after that, a signal the free plan doesn't answer never matches, 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. On the free plan, hosting and the country lists spare any address a known bot uses, since only a paid answer tells a verified crawler from one pretending.

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, each lookup also sends the visitor's User-Agent, and the answer then depends on it, so an address is looked up once for each agent it arrives with, which spends a plan's lookups faster.

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. 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:

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

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, and looks an address up once an hour for each agent. 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.

Other Libraries

There are official InternetData client libraries available for many languages including PHP, Python, Go, Java, and Ruby. See our GitHub at https://github.com/internetdata for more.

About InternetData

Geolocation, anonymity, ownership, network, service and domain data for any IP address, AS or domain, by API or as databases you download.

InternetData

License

This project is licensed under the MIT License.

Releases

Packages

Used by

Contributors

Languages