Skip to content

Instantly share code, notes, and snippets.

@maxmilton
Last active November 16, 2021 00:16
Show Gist options
  • Save maxmilton/2f2ebbe8223bfcf76f177290a6f26cd8 to your computer and use it in GitHub Desktop.
Save maxmilton/2f2ebbe8223bfcf76f177290a6f26cd8 to your computer and use it in GitHub Desktop.
Get client IP etc. from cloudflare trace (client-side; in the browser)
// XXX: Use '/cdn-cgi/trace' if the site is on cloudflare to avoid CORS issues
// otherwise can use 'https://www.cloudflare.com/cdn-cgi/trace'
const trace = await fetch('https://www.cloudflare.com/cdn-cgi/trace')
.then(x=>x.text())
.then(x=>new URLSearchParams(x.replace(/\n/g, '&')));
console.log(trace.get('ip'))
// ------------------------------------------------------------
const ip = await fetch('https://www.cloudflare.com/cdn-cgi/trace')
.then(x=>x.text())
.then(x=>x.match(/ip=(.*)/)[1]);
console.log(ip)
// ------------------------------------------------------------
// More simple but probably worse availability than Cloudflare
const ip = await fetch('https://ifconfig.me/ip').then(x=>x.text())
console.log(ip)
// ------------------------------------------------------------
fetch('https://www.cloudflare.com/cdn-cgi/trace')
.then(x=>x.text())
.then(x=>new URLSearchParams(x.replace(/\n/g, '&')))
.then(x=>console.log(Object.fromEntries(x)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment