Skip to content

Instantly share code, notes, and snippets.

@ZE3kr
Last active January 2, 2020 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ZE3kr/c7eb7735ee779d63996415c150591867 to your computer and use it in GitHub Desktop.
Save ZE3kr/c7eb7735ee779d63996415c150591867 to your computer and use it in GitHub Desktop.
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/ipjson' || request.url === 'http://api.tloxygen.com/ipjson') {
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