Skip to main content

Cloud / CDN

Cloudflare Workers Blocklist Tutorial

Block VPN and proxy IPs at the Cloudflare edge using Workers KV to store the blocklist and a Worker script to intercept requests before they reach your origin.

Supported formats: TXT (one IP per line) JSON array

Steps

  1. 1

    Create a Workers KV namespace

    In the Cloudflare dashboard (or via Wrangler CLI) create a KV namespace named BLOCKLIST to store the IP set.

  2. 2

    Upload blocklist to KV

    Use a Node.js script or the Cloudflare API to populate KV with the blocklist IPs as keys (value = "1").

  3. 3

    Write the Worker script

    The Worker reads the client IP from the CF-Connecting-IP header, checks it against KV, and returns 403 if found.

  4. 4

    Deploy the Worker

    Deploy with wrangler deploy. The Worker runs at all Cloudflare edge locations globally.

Need the blocklist files?

Download free sample data or subscribe for daily-updated lists.

Free Sample

Worker script

Edge Worker that checks the connecting IP against KV blocklist.

src/index.ts
export interface Env {
  BLOCKLIST: KVNamespace;
}

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const clientIP =
      request.headers.get('CF-Connecting-IP') ??
      request.headers.get('X-Real-IP') ??
      '0.0.0.0';

    // Check if IP is in the blocklist KV namespace
    const blocked = await env.BLOCKLIST.get(clientIP);

    if (blocked !== null) {
      return new Response('Access denied', { status: 403 });
    }

    // Pass through to origin
    return fetch(request);
  },
};

wrangler.toml

Wrangler config binding the KV namespace to the Worker.

wrangler.toml
name = "antiproxies-blocker"
main = "src/index.ts"
compatibility_date = "2025-01-01"

[[kv_namespaces]]
binding = "BLOCKLIST"
id      = "YOUR_KV_NAMESPACE_ID"

Frequently asked questions

Are Workers KV reads fast enough to not add latency?
KV reads from a Worker are served from the nearest Cloudflare edge cache. Typical latency is < 1 ms. For very latency-sensitive workloads consider Cloudflare Durable Objects for guaranteed locality.
How much does Workers KV cost for a large blocklist?
Workers KV charges per read operation. At 100k daily requests and a 150k IP blocklist the cost is minimal (< $1/month). Bulk writes are charged once on upload.
Can I also block by country at the edge?
Yes. Cloudflare Workers expose request.cf.country. Combine an IP KV lookup with a country check in the same Worker for layered protection.

Want to see what's in the database?

Download once, query as many times as you need. €99/year for all 22 databases, unlimited servers, and a full year of monthly updates. No usage limits, no per-query fees, no data leaving your servers.

30-day money-back guarantee
All databases included
Monthly updates