const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const validNetNumber = []; let i = 7; while (i >= 0) { let j = 7; let sum = 0; while (j >= 0) { if (j >= i) { sum += Math.pow(2, j); } j--; } validNetNumber.push(sum + ""); i--; } const isValidSubnet = (subnet) => { let flag = true; let countType1 = 0; let countType2 = 0; const nets = subnet.split("."); let hasSpcial = false; let specialIndexs = []; nets.forEach((net, index) => { if (net === "255") { countType1++; } else if (net === "0") { countType2++; } else if (validNetNumber.includes(net)) { hasSpcial = true; specialIndexs.push(index); } else { flag = false; } }); if (specialIndexs.length > 1) { flag = false; } // 二进制下全是1或者全是0均为非法子网掩码 if (countType1 === 4 || countType2 === 4) { flag = false; } if (hasSpcial && specialIndexs.length === 1 && flag) { let specialIndex = specialIndexs[0]; let indexAfter = specialIndex; let indexBefore = specialIndex; indexAfter++; indexBefore--; while (indexAfter <= 3) { if (nets[indexAfter] !== "0") { flag = false; } indexAfter++; } while (indexBefore >= 0) { if (nets[indexBefore] !== "255") { flag = false; } indexBefore--; } } return flag; }; let ipA = 0; let ipB = 0; let ipC = 0; let ipD = 0; let ipE = 0; let ipFault = 0; let ipPrivate = 0; const subnets = []; const faultIps = []; let setTimeoutId; const callback = () => { console.log( ipA + " " + ipB + " " + ipC + " " + ipD + " " + ipE + " " + ipFault + " " + ipPrivate ); // test-8 // if (ipA === 50) { // console.log("subnets:", subnets); // console.log("faultIps:", faultIps); // } }; rl.on("line", function (line) { if (setTimeoutId) { clearTimeout(setTimeoutId); } const tokens = line.split("~"); const ipArray = tokens[0].split("."); // 过滤首个ip段值为0或127的数据 if (["0", "127"].includes(ipArray[0])) { setTimeoutId = setTimeout(callback, 0); return; } // ip地址非法 if (ipArray.some((item) => item === "")) { faultIps.push(ipArray); ipFault++; setTimeoutId = setTimeout(callback, 0); return; } const subnet = tokens[1]; // 子网掩码非法 if (!isValidSubnet(subnet)) { subnets.push(subnet); ipFault++; setTimeoutId = setTimeout(callback, 0); return; } const ip_1 = +ipArray[0]; const ip_2 = +ipArray[1]; // 是否是A|B|C|D|E类IP地址 if (ip_1 >= 1 && ip_1 <= 126) { ipA++; } else if (ip_1 >= 128 && ip_1 <= 191) { ipB++; } else if (ip_1 >= 192 && ip_1 <= 223) { ipC++; } else if (ip_1 >= 224 && ip_1 <= 239) { ipD++; } else if (ip_1 >= 240 && ip_1 <= 255) { ipE++; } // 是否是私有IP地址 if ( ip_1 === 10 || (ip_1 === 172 && ip_2 >= 16 && ip_2 <= 31) || (ip_1 === 192 && ip_2 === 168) ) { ipPrivate++; } setTimeoutId = setTimeout(callback, 0); });