Skip to content

Instantly share code, notes, and snippets.

@ikawka
Last active February 25, 2026 19:22
Show Gist options
  • Select an option

  • Save ikawka/6efa4551ca765d7d14e00e9a5ea539f8 to your computer and use it in GitHub Desktop.

Select an option

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.
(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);
@marcelovani
Copy link

I don't think CORS will be an issue on GET requests

@fawazahmed0
Copy link

@MaheshCasiraghi
Copy link

MaheshCasiraghi commented Sep 30, 2022

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)

@consatan
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment