Process IPv6 display into short format including only the network prefix, and sharing the same code also for comparisons where needed.

This commit is contained in:
2025-12-09 16:33:16 +00:00
parent 03368b1b84
commit 8a21edf367
2 changed files with 36 additions and 24 deletions
+15
View File
@@ -26,3 +26,18 @@ export function getCookie(name) {
}
export const goBack = () => history.back() || window.close()
// IPv4 unchanged, IPv6 returns /64 network prefix in compact form
export const hostIP = ip => {
try {
if (!ip || !ip.includes(':')) return ip
const strip = s => s.replace(/^\[|\]$/g, '')
const norm = strip(new URL(`http://[${ip}]/`).hostname)
const [l, r] = norm.split('::').map(s => s ? s.split(':') : [])
const full = r ? [...l, ...Array(8 - l.length - r.length).fill('0'), ...r] : l
return strip(new URL(`http://[${full.slice(0, 4).join(':')}::]/`).hostname).replace(/::$/, '')
} catch (e) {
console.error('hostIP processing failed for:', ip, e)
return ip
}
}