diff --git a/src/lib/detect.ts b/src/lib/detect.ts index 2e78a1f35..643ed01a2 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -77,8 +77,10 @@ function decodeHeader(s: string | undefined | null): string | undefined | null { } export async function getLocation(ip: string = '', headers: Headers, skipHeaders: boolean) { - // Ignore local ips - if (!ip || (await isLocalhost(ip))) { + const cleanIp = stripPort(ip); + + // Ignore local or invalid ips + if (!cleanIp || !ipaddr.isValid(cleanIp) || (await isLocalhost(cleanIp))) { return null; } @@ -108,7 +110,7 @@ export async function getLocation(ip: string = '', headers: Headers, skipHeaders ); } - const result = globalThis[MAXMIND]?.get(stripPort(ip)); + const result = globalThis[MAXMIND]?.get(cleanIp); if (result) { const country = result.country?.iso_code ?? result?.registered_country?.iso_code; diff --git a/src/lib/ip.ts b/src/lib/ip.ts index a78c7e56e..08e77c65e 100644 --- a/src/lib/ip.ts +++ b/src/lib/ip.ts @@ -84,9 +84,7 @@ export function getIpAddress(headers: Headers) { if (header === 'forwarded') { const match = ip.match(/for=(\[?[0-9a-fA-F:.]+]?)/); - if (match) { - return resolveIp(match[1]); - } + return match ? resolveIp(match[1]) : undefined; } return resolveIp(ip);