feat: use ipinfo.io for ip lookup, simplify truncate function

This commit is contained in:
Gottfried Mayer 2024-04-12 14:20:28 +00:00
parent 328744abe4
commit cf0066e405
1 changed files with 7 additions and 11 deletions

View File

@ -5,12 +5,8 @@ function isValidIpAddr(ip) {
return /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/.test(ip);
}
function truncate(ip, maxlen = 20) {
if (ip.length > maxlen) {
return ip.slice(0, 5) + '…' + ip.slice(-maxlen + 5);
} else {
return ip;
}
function truncate(ip) {
return ip.length > 20 ? ip.slice(0, 5) + '…' + ip.slice(-20 + 5) : ip;
}
const openIpLink = (event) => {
@ -25,26 +21,26 @@ const openIpLink = (event) => {
}
};
addEventListener("DOMContentLoaded", async (event) => {
addEventListener('DOMContentLoaded', async () => {
try {
const resp = await fetch('/myip');
const respText = await resp.text();
const e = document.getElementById('ip');
if (isValidIpAddr(respText)) {
e.href = "https://apps.db.ripe.net/db-web-ui/query?rflag=true&source=GRS&bflag=true&searchtext=" + respText;
e.href = 'https://ipinfo.io/' + respText;
e.textContent = truncate(respText);
e.title = respText;
e.parentNode.onclick = openIpLink;
} else {
e.textContent = "invalid ip.";
e.title = "invalid ip.";
e.textContent = 'invalid ip.';
e.title = 'invalid ip.';
}
} catch (ex) {
console.error(ex);
}
});
addEventListener('load', (event) => {
addEventListener('load', () => {
// display document load time
window.setTimeout(() => {
if (window && window.performance && window.performance.timing) {