From 68de633143fce9de8950d14b85c9901d8601aaa9 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Thu, 4 Jan 2024 09:03:35 +1100 Subject: [PATCH] work on #523 - ip v6 ordering support 0.1 --- front/js/pialert_common.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/front/js/pialert_common.js b/front/js/pialert_common.js index 9fff72637..97a30ce40 100755 --- a/front/js/pialert_common.js +++ b/front/js/pialert_common.js @@ -515,28 +515,33 @@ function getNameByMacAddress(macAddress) { // ----------------------------------------------------------------------------- // A function used to make the IP address orderable +function isValidIPv6(ipAddress) { + // Regular expression for IPv6 validation + const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,7}:|^([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}$|^([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}$|^([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}$|^([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}$|^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})$/; + + return ipv6Regex.test(ipAddress); +} + function formatIPlong(ipAddress) { - if (ipAddress.includes(':')) { - // IPv6 address + if (ipAddress.includes(':') && isValidIPv6(ipAddress)) { const parts = ipAddress.split(':'); - if (parts.length !== 8) { - throw new Error('Invalid IPv6 address format'); - } - return parts.reduce((acc, part, index) => { - const hexValue = parseInt(part, 16); - if (isNaN(hexValue) || hexValue < 0 || hexValue > 0xFFFF) { - throw new Error('Invalid IPv6 address format'); + if (part === '') { + const remainingGroups = 8 - parts.length + 1; + return acc << (16 * remainingGroups); } + + const hexValue = parseInt(part, 16); return acc | (hexValue << (112 - index * 16)); }, 0); } else { - // IPv4 address + // Handle IPv4 address const parts = ipAddress.split('.'); if (parts.length !== 4) { - throw new Error('Invalid IPv4 address format'); + console.log("⚠ Invalid IPv4 address: " + ipAddress); + return -1; // or any other default value indicating an error } return (parseInt(parts[0]) << 24) |