Skip to content

Instantly share code, notes, and snippets.

@aldovelasco-acme
Created November 29, 2022 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldovelasco-acme/308cbbec025f9bfe2dc5240aabb3f178 to your computer and use it in GitHub Desktop.
Save aldovelasco-acme/308cbbec025f9bfe2dc5240aabb3f178 to your computer and use it in GitHub Desktop.
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