function checkvpcV4CIDRAndV6CIDR (ipAddress,type) {
console.log("type",type);
if (ipAddress && ipAddress != undefined) { //为Ipv4带掩码
let ipInfo = analyzeIpAndMask(ipAddress);
if (!ipInfo.MASK) {
return false;
}
let legitimateAddress = validateIp(ipInfo.IP,type);
console.log(legitimateAddress);
if (!legitimateAddress) {
return false;
}
return true;
}
}
//分离ip
function DecompositionIpString (iplist,type) {
let V4CIDRInfo = false;
if (iplist.endsWith(";")) {
return V4CIDRInfo
} else if (iplist.indexOf(";") > 0) {
let ipList = iplist.split(";");
for (let j = 0; j < ipList.length; j++) {
let ipMask = ipList[j];
V4CIDRInfo = checkvpcV4CIDRAndV6CIDR(ipMask,type);
}
} else {
V4CIDRInfo = checkvpcV4CIDRAndV6CIDR(iplist,type);
}
return V4CIDRInfo
}
function analyzeIpAndMask(ip = '') {
let index = ip.lastIndexOf("/");
let IP = null;
let MASK = null;
let host = {
IP: IP,
MASK: MASK
};
if (index > 0) {
IP = ip.substring(0, index);
MASK = ip.substr(index + 1);
host = {
IP: IP,
MASK: MASK
}
}
console.log(host);
return host
}
//校验IP格式
function validateIp (ip,type) {
let reg = null;
console.log(tpye);
if (type == "IPv4") {
reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
} else {
reg = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;
}
return reg.test(ip)
}
function removeMaskCode(ip = '') {
let index = ip.lastIndexOf("/");
if (index > 0) {
return ip.substring(0, index);
} else {
return ip;
}
}
console.log(DecompositionIpString("192.168.1.10/16","IPv4"));
console