Skip to content

Instantly share code, notes, and snippets.

@blood72
Created October 11, 2024 23:01
Show Gist options
  • Save blood72/5a689116bc2f18754962fe21b3dbd1f8 to your computer and use it in GitHub Desktop.
Save blood72/5a689116bc2f18754962fe21b3dbd1f8 to your computer and use it in GitHub Desktop.
IP check using Cloudflare Worker
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