Last active
November 16, 2021 00:16
-
-
Save maxmilton/2f2ebbe8223bfcf76f177290a6f26cd8 to your computer and use it in GitHub Desktop.
Get client IP etc. from cloudflare trace (client-side; in the browser)
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
// 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