Skip to content

Instantly share code, notes, and snippets.

@ikawka
Last active December 31, 2023 12:29
Show Gist options
  • Save ikawka/6efa4551ca765d7d14e00e9a5ea539f8 to your computer and use it in GitHub Desktop.
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);
@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

@thedtvn
Copy link

thedtvn commented Jan 15, 2023

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