46 lines
865 B
JavaScript
46 lines
865 B
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;
|
||
|
}
|
||
|
}
|