From 8a3d34b737a4894f9eeed0807130fe22fa92c21f Mon Sep 17 00:00:00 2001 From: lanrenwo Date: Mon, 4 Dec 2023 18:32:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96isValidCIDR=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3=E9=83=A8=E5=88=86=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=E6=9C=89=E8=AF=AF=EF=BC=8C=E5=B9=B6=E7=BB=99?= =?UTF-8?q?=E4=BA=88=E5=BB=BA=E8=AE=AE=E6=8F=90=E7=A4=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/pages/group/List.vue | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/web/src/pages/group/List.vue b/web/src/pages/group/List.vue index 97fdaae..6d5975c 100644 --- a/web/src/pages/group/List.vue +++ b/web/src/pages/group/List.vue @@ -712,10 +712,11 @@ export default { pushToArr(); continue; } - if (!this.isValidCIDR(ip[0])) { - this.$message.error("错误:CIDR格式错误 " + item); - this.ipEditLoading = false; - return; + let valid = this.isValidCIDR(ip[0]); + if (!valid.valid) { + this.$message.error("错误:CIDR格式错误,建议 " + ip[0] + " 改为 " + valid.suggestion); + this.ipEditLoading = false; + return; } pushToArr(); } @@ -723,9 +724,28 @@ export default { this.ipEditLoading = false; this.ipListDialog = false; }, - isValidCIDR(str) { - const cidrRegex = /^([0-9]{1,3}\.){3}[0-9]{1,3}\/([0-9]|[1-2][0-9]|3[0-2])$/; - return cidrRegex.test(str); + isValidCIDR(input) { + const cidrRegex = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)\/([12]?\d|3[0-2])$/; + if (!cidrRegex.test(input)) { + return { valid: false, suggestion: null }; + } + const [ip, mask] = input.split('/'); + const maskNum = parseInt(mask); + const ipParts = ip.split('.').map(part => parseInt(part)); + const binaryIP = ipParts.map(part => part.toString(2).padStart(8, '0')).join(''); + for (let i = maskNum; i < 32; i++) { + if (binaryIP[i] === '1') { + const binaryNetworkPart = binaryIP.substring(0, maskNum).padEnd(32, '0'); + const networkIPParts = []; + for (let j = 0; j < 4; j++) { + const octet = binaryNetworkPart.substring(j * 8, (j + 1) * 8); + networkIPParts.push(parseInt(octet, 2)); + } + const suggestedIP = networkIPParts.join('.'); + return { valid: false, suggestion: `${suggestedIP}/${mask}` }; + } + } + return { valid: true, suggestion: null }; }, resetForm(formName) { this.$refs[formName].resetFields();