优化
This commit is contained in:
parent
9f6b860f5f
commit
9933d56250
@ -61,15 +61,23 @@ function SubnetItem({index, subnets, setSubnets} : SubnetProps) : ReactNode {
|
|||||||
toast.error("没有选择有效的节点");
|
toast.error("没有选择有效的节点");
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
const nodeId = selectedOption.value;
|
const nodeId = selectedOption.value;
|
||||||
|
|
||||||
|
if(subnetInfo.nodes.some(node => node.nodeId === nodeId)) {
|
||||||
|
toast.error(`节点已添加`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cidr = undefined;
|
||||||
|
if(nodeSubnet) {
|
||||||
const nodecidr = (() => {
|
const nodecidr = (() => {
|
||||||
if(nodeSubnet.includes("/")) return nodeSubnet;
|
if(nodeSubnet.includes("/")) return nodeSubnet;
|
||||||
return `${nodeSubnet}/${nodeSubnet.includes(".") ? "32" : "128"}`;
|
return `${nodeSubnet}/${nodeSubnet.includes(".") ? "32" : "128"}`;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const result = IPUtils.parse(nodecidr);
|
const result = IPUtils.parse(nodecidr);
|
||||||
const cidr = result.cidr;
|
cidr = result.cidr;
|
||||||
if(!cidr) {
|
if(!cidr) {
|
||||||
toast.error(`无法解析子网:${result.error}`);
|
toast.error(`无法解析子网:${result.error}`);
|
||||||
return ;
|
return ;
|
||||||
@ -79,10 +87,6 @@ function SubnetItem({index, subnets, setSubnets} : SubnetProps) : ReactNode {
|
|||||||
toast.error("不在子网范围内");
|
toast.error("不在子网范围内");
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(subnetInfo.nodes.some(node => node.nodeId === nodeId)) {
|
|
||||||
toast.error(`节点已添加`);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setSubnets(subnets.map((info, idx) => {
|
setSubnets(subnets.map((info, idx) => {
|
||||||
|
|||||||
@ -24,12 +24,36 @@ export class CIDR {
|
|||||||
}
|
}
|
||||||
return `${octets.join('.')}/${this.mask}`;
|
return `${octets.join('.')}/${this.mask}`;
|
||||||
} else {
|
} else {
|
||||||
// Split 128 bits into 8 blocks of 16 bits
|
// IPv6: split into 8 hex blocks (no leading zeros) and apply :: compression
|
||||||
const blocks = [];
|
const blocks: string[] = [];
|
||||||
for (let i = 0; i < 128; i += 16) {
|
for (let i = 0; i < 128; i += 16) {
|
||||||
blocks.push(parseInt(this.binary.slice(i, i + 16), 2).toString(16));
|
blocks.push(parseInt(this.binary.slice(i, i + 16), 2).toString(16));
|
||||||
}
|
}
|
||||||
return `${blocks.join(':')}/${this.mask}`;
|
|
||||||
|
// find longest run of '0' blocks
|
||||||
|
let bestStart = -1, bestLen = 0;
|
||||||
|
for (let i = 0; i < blocks.length; ) {
|
||||||
|
if (blocks[i] !== '0') { i++; continue; }
|
||||||
|
let j = i + 1;
|
||||||
|
while (j < blocks.length && blocks[j] === '0') j++;
|
||||||
|
const len = j - i;
|
||||||
|
if (len > bestLen) { bestStart = i; bestLen = len; }
|
||||||
|
i = j;
|
||||||
|
}
|
||||||
|
|
||||||
|
let compact: string;
|
||||||
|
if (bestLen >= 2) {
|
||||||
|
const left = blocks.slice(0, bestStart).join(':');
|
||||||
|
const right = blocks.slice(bestStart + bestLen).join(':');
|
||||||
|
if (left && right) compact = `${left}::${right}`;
|
||||||
|
else if (left) compact = `${left}::`;
|
||||||
|
else if (right) compact = `::${right}`;
|
||||||
|
else compact = '::';
|
||||||
|
} else {
|
||||||
|
compact = blocks.join(':');
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${compact}/${this.mask}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user