Last active
December 31, 2023 12:29
-
-
Save ikawka/6efa4551ca765d7d14e00e9a5ea539f8 to your computer and use it in GitHub Desktop.
Trace current user's location via javascript with CloudFlare provided the /cdn-cgi/trace is enabled.
This file contains 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
(function($){ | |
$(function(){ | |
$.ajax({ | |
contentType: 'application/text; charset=utf-8', | |
crossBrowser: true, | |
type: 'GET', | |
url: '/cdn-cgi/trace', | |
}).done(function(d){ | |
var data = d.replace(/[\r\n]+/g, '","').replace(/\=+/g, '":"'); | |
data = '{"' + data.slice(0, data.lastIndexOf('","')) + '"}'; | |
var jsondata = $.parseJSON(data); | |
console.log(jsondata.loc); | |
}); | |
}); | |
})(jQuery); |
Updated (September 2022):
async function getCloudflareJSON(){
let data = await fetch('https://1.1.1.1/cdn-cgi/trace').then(res=>res.text())
let arr = data.trim().split('\n').map(e=>e.split('='))
return Object.fromEntries(arr)
}
getCloudflareJSON().then(console.log)
use https://cf-ns.com/cdn-cgi/trace in China
fetch(`https://${location.hostname}/cdn-cgi/trace`)
.then(res => res.text() ).then(t => {
var data = t.replace(/[\r\n]+/g, '","').replace(/\=+/g, '":"');
data = '{"' + data.slice(0, data.lastIndexOf('","')) + '"}';
var jsondata = JSON.parse(data);
console.log(jsondata);
})
code if page is add to cloudflare
- /cdn-cgi/trace is work on every domain on cloudflare DNS or network
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't think CORS will be an issue on GET requests