252 lines
6 KiB
JavaScript
252 lines
6 KiB
JavaScript
import IPCIDR from "ip-cidr";
|
|
import IPAddress from "ip-address";
|
|
|
|
export function isValidMacAddress(mac) {
|
|
try {
|
|
const regex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
|
|
return regex.test(mac);
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isValidCidr(cidr, type = 0) {
|
|
let [isV4, isV6] = [false, false];
|
|
let tempCidr = null;
|
|
|
|
try {
|
|
if (type === 4) {
|
|
tempCidr = new IPCIDR(cidr);
|
|
if (tempCidr.address.v4) {
|
|
isV4 = true;
|
|
}
|
|
}
|
|
|
|
if (type === 6) {
|
|
tempCidr = new IPAddress.Address6(cidr);
|
|
if (!tempCidr.address.v4) {
|
|
isV6 = true;
|
|
}
|
|
}
|
|
|
|
if (type === 0) {
|
|
tempCidr = new IPCIDR(cidr);
|
|
if (tempCidr.address.v4) {
|
|
isV4 = true;
|
|
} else {
|
|
isV6 = true;
|
|
}
|
|
}
|
|
|
|
return isV4 || isV6;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function clickSelect2Dropdown(page, selectSelector) {
|
|
const select2ContainerSelector = `${selectSelector} + .select2-container`;
|
|
await page.waitForTimeout(2000);
|
|
const select2ContainerExists =
|
|
(await page.$(select2ContainerSelector)) !== null;
|
|
|
|
if (!select2ContainerExists) {
|
|
throw new Error("Select2 container not found");
|
|
}
|
|
|
|
const select2Selection = `${select2ContainerSelector} .select2-selection`;
|
|
await page.click(select2Selection);
|
|
await page.waitForSelector(".select2-dropdown .select2-results__option");
|
|
}
|
|
|
|
export async function hasToastText(page, text) {
|
|
try {
|
|
await page.waitForSelector(".swal2-popup", { state: "visible" });
|
|
const message = await page.waitForSelector("#swal2-html-container", {
|
|
state: "visible",
|
|
});
|
|
|
|
const textContent = await message.textContent();
|
|
return textContent.includes(text);
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function hasToastInput(page, text) {
|
|
try {
|
|
await page.waitForSelector(".swal2-popup", { state: "visible" });
|
|
|
|
const inputField = await page.waitForSelector(".swal2-input", {
|
|
state: "visible",
|
|
});
|
|
|
|
const okButton = await page.waitForSelector(".swal2-confirm", {
|
|
state: "visible",
|
|
});
|
|
|
|
const inputIsVisible = await inputField.isVisible();
|
|
|
|
const okButtonIsEnabled = await okButton.isEnabled();
|
|
|
|
const heading = await page.waitForSelector("#swal2-title", {
|
|
state: "visible",
|
|
});
|
|
const textContent = await heading.textContent();
|
|
const hasText = textContent.includes(text);
|
|
|
|
return inputIsVisible && okButtonIsEnabled && hasText;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function hasToastQRcode(page, text) {
|
|
try {
|
|
await page.waitForSelector(".swal2-popup", { state: "visible" });
|
|
|
|
const heading = await page.waitForSelector("#swal2-title", {
|
|
state: "visible",
|
|
});
|
|
|
|
const imgElement = await page.waitForSelector("#swal2-html-container img", {
|
|
state: "visible",
|
|
});
|
|
|
|
if (imgElement) {
|
|
const style = await imgElement.evaluate((el) => {
|
|
const computedStyle = window.getComputedStyle(el);
|
|
return {
|
|
display: computedStyle.getPropertyValue("display"),
|
|
};
|
|
});
|
|
|
|
if (style.display === "none") {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
const textContent = await heading.textContent();
|
|
return textContent.includes(text);
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function sleep(ms) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
export function isValidDate(date) {
|
|
try {
|
|
const dateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/;
|
|
return dateRegex.test(date);
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isValidPrivateIPV4(ip) {
|
|
try {
|
|
const privateRanges = [
|
|
["10.0.0.0", "10.255.255.255"],
|
|
["172.16.0.0", "172.31.255.255"],
|
|
["192.168.0.0", "192.168.255.255"],
|
|
];
|
|
|
|
const ipNumber = ip
|
|
.split(".")
|
|
.reduce(
|
|
(acc, octet, index) => acc + (parseInt(octet) << ((3 - index) * 8)),
|
|
0
|
|
);
|
|
|
|
for (const [start, end] of privateRanges) {
|
|
const startNumber = start
|
|
.split(".")
|
|
.reduce(
|
|
(acc, octet, index) => acc + (parseInt(octet) << ((3 - index) * 8)),
|
|
0
|
|
);
|
|
const endNumber = end
|
|
.split(".")
|
|
.reduce(
|
|
(acc, octet, index) => acc + (parseInt(octet) << ((3 - index) * 8)),
|
|
0
|
|
);
|
|
|
|
if (ipNumber >= startNumber && ipNumber <= endNumber) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function getDefaultNetmask(ip) {
|
|
const firstOctet = parseInt(ip.split(".")[0], 10);
|
|
|
|
if (firstOctet >= 1 && firstOctet <= 126) {
|
|
return "255.0.0.0";
|
|
} else if (firstOctet >= 128 && firstOctet <= 191) {
|
|
return "255.255.0.0";
|
|
} else if (firstOctet >= 192 && firstOctet <= 223) {
|
|
return "255.255.255.0";
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function calculateDhcpLimits(ip, mask) {
|
|
function toBinary(octet) {
|
|
return ("00000000" + parseInt(octet, 10).toString(2)).substr(-8);
|
|
}
|
|
|
|
const maskBinary = mask
|
|
.split(".")
|
|
.map((octet) => toBinary(octet))
|
|
.join("");
|
|
const subnetBits = maskBinary.split("1").length - 1;
|
|
const totalHosts = Math.pow(2, 32 - subnetBits) - 2;
|
|
|
|
return {
|
|
minStart: 1,
|
|
maxStart: totalHosts,
|
|
maxLeases: totalHosts,
|
|
};
|
|
}
|
|
|
|
export function isValidateDhcpInput(ip, mask, dhcpStart, maxLeases) {
|
|
const limits = calculateDhcpLimits(ip, mask);
|
|
const isValidStart =
|
|
dhcpStart >= limits.minStart && dhcpStart <= limits.maxStart;
|
|
const isValidLeases =
|
|
maxLeases <= limits.maxLeases &&
|
|
dhcpStart + maxLeases - 1 <= limits.maxStart;
|
|
|
|
return isValidStart && isValidLeases;
|
|
}
|
|
|
|
export function splitIPv6(ipv6Prefixes) {
|
|
if (ipv6Prefixes == "") {
|
|
return false;
|
|
}
|
|
|
|
const splitPrefixes = ipv6Prefixes.split(/(?<=\/\d+)/);
|
|
|
|
const formattedPrefixes = [];
|
|
|
|
for (let i = 0; i < splitPrefixes.length; i += 2) {
|
|
const prefix = splitPrefixes[i].trim();
|
|
const subnet = splitPrefixes[i + 1] ? splitPrefixes[i + 1].trim() : "";
|
|
|
|
if (prefix && subnet) {
|
|
formattedPrefixes.push(`${prefix}${subnet}`);
|
|
}
|
|
}
|
|
|
|
return formattedPrefixes;
|
|
}
|