From a97efa50af1c5729225584c3a25693549a829592 Mon Sep 17 00:00:00 2001 From: Lazy <2211717435@qq.com> Date: Mon, 8 Jan 2024 22:00:43 +0800 Subject: [PATCH] Feat: ipv6 support --- frontend/src/tools/common.ts | 53 +++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/frontend/src/tools/common.ts b/frontend/src/tools/common.ts index 6163cb65..9fdd2f3d 100644 --- a/frontend/src/tools/common.ts +++ b/frontend/src/tools/common.ts @@ -101,23 +101,50 @@ function leftZero4(str: string) { return str || ""; } -export const dockerPortsParse = (ports: string[]) => { - let p1 = []; - let p2 = []; +const isIPv6 = (str: string) => { + return /\[([0-9a-fA-F:]+)\]/g.test(str); +}; - for (let i = 0; i < ports.length; i++) { - if ( - (isInt(ports[0]) && ports.length === 3 && i < 1) || - (!isInt(ports[0]) && ports.length === 3 && i < 2) || - (ports.length === 4 && i < 2) - ) { - p1.push(ports[i]); - } else { - p2.push(ports[i]); +export const dockerPortsParse = (ports: string[]) => { + let joinArr = ports.join(":"); + let tempAddr: RegExpMatchArray | null; + const ipMaps = new Map(); + + 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]); } } - return { port1: p1.join(":"), port2: p2.join(":") }; + let p = isIPv6(ports.join(":")) ? joinArr.split(":") : ports; + + let p1 = [], + p2 = []; + + for (let i = 0; i < p.length; i++) { + if ( + (isInt(p[0]) && p.length === 3 && i < 1) || + (!isInt(p[0]) && p.length === 3 && i < 2) || + (p.length === 4 && i < 2) + ) { + p1.push(p[i]); + } else if (p.length === 2) { + return { port1: p[0], port2: p[1] }; + } else { + p2.push(p[i]); + } + } + + 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[]) => {