From 7d9fe30626a0251884cc33a31d408cd93117e2ed Mon Sep 17 00:00:00 2001 From: Panagiotis Date: Sun, 21 Sep 2025 22:56:59 +0300 Subject: [PATCH] Resolve IPv6 address destruction --- src/lib/detect.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/detect.ts b/src/lib/detect.ts index ee9d2603..17fb574c 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -108,6 +108,14 @@ function decodeHeader(s: string | undefined | null): string | undefined | null { return Buffer.from(s, 'latin1').toString('utf-8'); } +function removePortFromIP(ip: string = "") { + const split = ip.split(":"); + + // Assuming ip is a valid IPv4/IPv6 address, 3 colons is the minumum for IPv6 + const ipv4 = split.length - 1 < 3; + return ipv4 ? split[0] : ip; +} + export async function getLocation(ip: string = '', headers: Headers, hasPayloadIP: boolean) { // Ignore local ips if (await isLocalhost(ip)) { @@ -141,7 +149,7 @@ export async function getLocation(ip: string = '', headers: Headers, hasPayloadI } // When the client IP is extracted from headers, sometimes the value includes a port - const cleanIp = ip?.split(':')[0]; + const cleanIp = removePortFromIP(ip); const result = global[MAXMIND].get(cleanIp); if (result) {