Jump to content
Akinix

How to Get IP, Location and Cloudflare Trace in JSON (Ready-to-Use JS Code)


Recommended Posts

Here is a super simple JavaScript code you could use on all websites which are using Cloudflare CDN.

It allows you to quickly retrieve some connection data, such as IP, location, CF server, whether or not the user uses WARP VPN, etc.

async function getCloudflareTraceJSON() {
    try {
      const response = await fetch(`https://${location.hostname}/cdn-cgi/trace`);
      const traceData = await response.text();
      const traceEntries = traceData.trim().split('\n').map(e => e.split('='));
      return Object.fromEntries(traceEntries);
    } catch (error) {
      console.error(error);
      return null;
    }
  }
  
  getCloudflareTraceJSON().then(console.log);

Usage example:

getCloudflareTraceJSON().then(
    traceData => {
        if (traceData.loc === 'US') {
            console.log('Location: USA');
        } else {
            console.log('Location: Not USA');
        }
    }
);

 

Edited by Everlasting Summer
Link to comment
Share on other sites

×
×
  • Create New...