Created
October 11, 2024 23:01
-
-
Save blood72/5a689116bc2f18754962fe21b3dbd1f8 to your computer and use it in GitHub Desktop.
IP check using Cloudflare Worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default { | |
async fetch(request) { | |
const { method, url } = request | |
const { pathname } = new URL(url) | |
const createResponse = (body, status) => new Response(body, { | |
status, | |
headers: { | |
'Content-Type': 'text/plain; charset=utf-8', | |
'Cache-Control': 'no-cache, no-store, must-revalidate', | |
'X-Content-Type-Options': 'nosniff', | |
}, | |
}) | |
if (method !== 'GET') { | |
return createResponse('Method Not Allowed', 405) | |
} | |
if (pathname === '/') { | |
const clientIP = request.headers.get('CF-Connecting-IP') | |
return createResponse(clientIP, 200) | |
} | |
if (pathname === '/robots.txt') { | |
const robotsTxt = `User-agent: *\nDisallow: /` | |
return createResponse(robotsTxt, 200) | |
} | |
return createResponse('Not Found', 404) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment