Include IP conflict detection.

This commit is contained in:
Herbert Wolverson 2024-05-01 15:41:56 -05:00
parent d21f4fa8e0
commit 386003214b

View File

@ -1165,6 +1165,72 @@
shapedDevices();
}
function checkIpv4(ip) {
const ipv4Pattern =
/^(\d{1,3}\.){3}\d{1,3}$/;
if (ip.indexOf('/') === -1) {
return ipv4Pattern.test(ip);
} else {
let parts = ip.split('/');
return ipv4Pattern.test(parts[0]);
}
}
function checkIpv6(ip) {
const ipv6Pattern =
/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/;
if (ip.indexOf('/') === -1) {
return ipv6Pattern.test(ip);
} else {
let parts = ip.split('/');
return ipv6Pattern.test(parts[0]);
}
}
function checkIpv4Duplicate(ip, index) {
ip = ip.trim();
for (let i=0; i < shaped_devices.length; i++) {
if (i !== index) {
let sd = shaped_devices[i];
for (let j=0; j<sd.ipv4.length; j++) {
let formatted = "";
if (ip.indexOf('/') > 0) {
formatted = sd.ipv4[j][0] + "/" + sd.ipv4[j][1];
} else {
formatted = sd.ipv4[j][0];
}
if (formatted === ip) {
return index;
}
}
}
}
return -1;
}
function checkIpv6Duplicate(ip, index) {
ip = ip.trim();
for (let i=0; i < shaped_devices.length; i++) {
if (i !== index) {
let sd = shaped_devices[i];
for (let j=0; j<sd.ipv6.length; j++) {
let formatted = "";
if (ip.indexOf('/') > 0) {
formatted = sd.ipv6[j][0] + "/" + sd.ipv6[j][1];
} else {
formatted = sd.ipv6[j][0];
}
if (formatted === ip) {
return index;
}
}
}
}
return -1;
}
function validateSd() {
let valid = true;
let errors = [];
@ -1242,10 +1308,80 @@
// IPv4
controlId = "#" + rowPrefix(i, "ipv4");
let ipv4 = $(controlId).val();
if (ipv4.length > 0) {
// We have IP addresses
if (ipv4.indexOf(',') !== -1) {
// We have multiple addresses
let ips = ipv4.replace(' ', '').split(',');
for (let j=0; j<ips.length; j++) {
if (!checkIpv4(ips[j].trim())) {
valid = false;
errors.push(ips[j] + "is not a valid IPv4 address");
$(controlId).addClass("invalid");
}
let dupes = checkIpv4Duplicate(ips[j], i);
if (dupes > 0 && dupes !== i) {
valid = false;
errors.push(ips[j] + " is a duplicate IP");
$(controlId).addClass("invalid");
$("#" + rowPrefix(dupes, "ipv4")).addClass("invalid");
}
}
} else {
// Just the one
if (!checkIpv4(ipv4)) {
valid = false;
errors.push(ipv4 + "is not a valid IPv4 address");
$(controlId).addClass("invalid");
}
let dupes = checkIpv4Duplicate(ipv4, i);
if (dupes > 0) {
valid = false;
errors.push(ipv4 + " is a duplicate IP");
$(controlId).addClass("invalid");
$("#" + rowPrefix(dupes, "ipv4")).addClass("invalid");
}
}
}
// IPv6
controlId = "#" + rowPrefix(i, "ipv6");
let ipv6 = $(controlId).val();
if (ipv6.length > 0) {
// We have IP addresses
if (ipv6.indexOf(',') !== -1) {
// We have multiple addresses
let ips = ipv6.replace(' ', '').split(',');
for (let j=0; j<ips.length; j++) {
if (!checkIpv6(ips[j].trim())) {
valid = false;
errors.push(ips[j] + "is not a valid IPv6 address");
$(controlId).addClass("invalid");
}
let dupes = checkIpv6Duplicate(ips[j], i);
if (dupes > 0 && dupes !== i) {
valid = false;
errors.push(ips[j] + " is a duplicate IP");
$(controlId).addClass("invalid");
$("#" + rowPrefix(dupes, "ipv6")).addClass("invalid");
}
}
} else {
// Just the one
if (!checkIpv6(ipv6)) {
valid = false;
errors.push(ipv6 + "is not a valid IPv6 address");
$(controlId).addClass("invalid");
}
let dupes = checkIpv6Duplicate(ipv6, i);
if (dupes > 0 && dupes !== i) {
valid = false;
errors.push(ipv6 + " is a duplicate IP");
$(controlId).addClass("invalid");
$("#" + rowPrefix(dupes, "ipv6")).addClass("invalid");
}
}
}
// Combined - must be an address between them
if (ipv4.length === 0 && ipv6.length === 0) {