Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Demo: https://api.tloxygen.com/ip (Use Cloudflare by default, UPYUN for China) or http://api.tloxygen.net/ip (Only use Cloudflare)
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
async function fetchAndApply(request) {
if(request.url === 'https://api.tloxygen.com/ip' || request.url === 'http://api.tloxygen.com/ip') {
const ip = request.headers.get('cf-connecting-ip')
return new Response(ip)
} else if (request.url === 'https://api.tloxygen.com/country' || request.url === 'http://api.tloxygen.com/country') {
const ip = request.headers.get('cf-ipcountry')
return new Response(ip)
} else if (request.url === 'https://api.tloxygen.com/json' || request.url === 'http://api.tloxygen.com/json') {
const ip = '{"ip":"' + request.headers.get('cf-connecting-ip') + '","country":"' + request.headers.get('cf-ipcountry') + '"}'
return new Response(ip)
} else {
return new Response('Sorry, this page is not available.',
{ status: 404 })
}
}
$WHEN($EQ($_URI,'/ip'))$DEL_RSP_HEADER(Content-Type)$ADD_RSP_HEADER(Content-Type, text/plain)$ADD_RSP_HEADER(Cache-Control, no-cache)$EXIT(200,$_IP)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment