From 95595dafcb9e18d3d30cab590d678e9728aeca4c Mon Sep 17 00:00:00 2001 From: ThatOneCalculator Date: Tue, 24 Jan 2023 15:13:25 -0800 Subject: [PATCH] fix: :bug: signin with ipv6 co-authored-by: Syuilo --- packages/backend/src/misc/get-ip-hash.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/misc/get-ip-hash.ts b/packages/backend/src/misc/get-ip-hash.ts index caf832f904..3bafaee5df 100644 --- a/packages/backend/src/misc/get-ip-hash.ts +++ b/packages/backend/src/misc/get-ip-hash.ts @@ -1,9 +1,14 @@ import IPCIDR from "ip-cidr"; export function getIpHash(ip: string) { - // because a single person may control many IPv6 addresses, - // only a /64 subnet prefix of any IP will be taken into account. - // (this means for IPv4 the entire address is used) - const prefix = IPCIDR.createAddress(ip).mask(64); - return `ip-${BigInt(`0b${prefix}`).toString(36)}`; + try { + // because a single person may control many IPv6 addresses, + // only a /64 subnet prefix of any IP will be taken into account. + // (this means for IPv4 the entire address is used) + const prefix = IPCIDR.createAddress(ip).mask(64); + return `ip-${BigInt(`0b${prefix}`).toString(36)}`; + } catch (e) { + const prefix = IPCIDR.createAddress(ip.replace(/:[0-9]+$/, "")).mask(64); + return `ip-${BigInt(`0b${prefix}`).toString(36)}`; + } }