fawazahmed0 / cloudflare-trace-api

Get IP Address, TimeStamp, User Agent, Country Code, IATA, HTTP Version, TLS/SSL Version & more by Cloudflare
https://one.one.one.one/cdn-cgi/trace
101 stars 6 forks source link

Javascript Example to JSON #3

Open fawazahmed0 opened 3 years ago

fawazahmed0 commented 3 years ago
async function getCloudflareJSON(){
let data = await fetch('https://1.0.0.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)

Output:

{fl: "202f225", h: "1.0.0.1", ip: "47.37.137.777", ts: "1625581799.09", visit_scheme: "https", …}
Wamy-Dev commented 2 years ago

How would you take this and filter out everything but "ip" and save that ip value as a variable to use later?

fawazahmed0 commented 2 years ago
async function somefunc(){
let cloudflareJSON = await getCloudflareJSON()
console.log(cloudflareJSON.ip)
}