-
-
Save aldovelasco-acme/308cbbec025f9bfe2dc5240aabb3f178 to your computer and use it in GitHub Desktop.
Get IP Address
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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