Skip to content

Instantly share code, notes, and snippets.

@aldovelasco-acme
Created November 29, 2022 18:06
Embed
What would you like to do?
Get IP Address
/**
* @returns {Promise<{ ip: string | null; }>}
*/
function getIpAddress() {
return fetch("https://www.cloudflare.com/cdn-cgi/trace")
.then((response) => response.text())
.then((data) => {
const result = data
.trim()
.split("\n")
.reduce((obj, pair) => {
const [key, value] = pair.split("=");
obj[key] = value;
return obj;
}, {});
return { ip: result.ip || null };
})
.catch(() => {
return { ip: null };
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment