Steven Prins

Steven Prins

Cloudflare Worker that returns the IP of client

18-10-2022 | 63 words | 1 minute reading time | Tags:

Below you can find a Cloudflare Worker that returns the IP of the client. This is useful if you want to know the IP of the client, for example if you want to block a specific IP.

1addEventListener('fetch', event => {
2  event.respondWith(handleRequest(event.request))
3})
4
5async function handleRequest(request) {
6  return new Response(request.headers.get('cf-connecting-ip'), {
7    headers: { 'content-type': 'text/plain' },
8  })
9}