Feat: ipv6 support

This commit is contained in:
Lazy 2024-01-08 22:00:43 +08:00
parent f0795c83d9
commit a97efa50af

View File

@ -101,23 +101,50 @@ function leftZero4(str: string) {
return str || "";
}
const isIPv6 = (str: string) => {
return /\[([0-9a-fA-F:]+)\]/g.test(str);
};
export const dockerPortsParse = (ports: string[]) => {
let p1 = [];
let p2 = [];
let joinArr = ports.join(":");
let tempAddr: RegExpMatchArray | null;
const ipMaps = new Map();
for (let i = 0; i < ports.length; i++) {
if (isIPv6(joinArr)) {
tempAddr = joinArr.match(/\[([0-9a-fA-F:]+)\]/g);
for (let i = 0; i < tempAddr!.length; i++) {
joinArr = joinArr.replace(tempAddr![i], "IPv6_" + i);
ipMaps.set("IPv6_" + i, tempAddr![i]);
}
}
let p = isIPv6(ports.join(":")) ? joinArr.split(":") : ports;
let p1 = [],
p2 = [];
for (let i = 0; i < p.length; i++) {
if (
(isInt(ports[0]) && ports.length === 3 && i < 1) ||
(!isInt(ports[0]) && ports.length === 3 && i < 2) ||
(ports.length === 4 && i < 2)
(isInt(p[0]) && p.length === 3 && i < 1) ||
(!isInt(p[0]) && p.length === 3 && i < 2) ||
(p.length === 4 && i < 2)
) {
p1.push(ports[i]);
p1.push(p[i]);
} else if (p.length === 2) {
return { port1: p[0], port2: p[1] };
} else {
p2.push(ports[i]);
p2.push(p[i]);
}
}
return { port1: p1.join(":"), port2: p2.join(":") };
const v4 = { port1: p1.join(":"), port2: p2.join(":") };
const v6 = {
port1: p1.length === 1 ? p1[0] : p1.join(":").replace(p1[0], ipMaps.get(p1[0])),
port2: p2.length === 1 ? p2[0] : p2.join(":").replace(p2[0], ipMaps.get(p2[0]))
};
return isIPv6(ports.join(":")) ? v6 : v4;
};
export const dockerPortsArray = (ports: string[]) => {