Jump to content
Akinix
Sign in to follow this  
Everlasting Summer

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

×
×
  • Create New...