From f7dcaf24ba9b72536f5b654c1b0dd0a6dd40ee8d Mon Sep 17 00:00:00 2001 From: IP2Location Date: Fri, 10 Nov 2023 12:41:55 +0800 Subject: [PATCH] Fixed wrong value returned by cidrToIPV6 function --- package.json | 4 ++-- src/ip2location.js | 53 +++++++++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index fdb54ec..815110e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ip2location-nodejs", - "version": "9.6.0", + "version": "9.6.1", "description": "IP2Location geolocation component", "keywords": [ "ip2location", @@ -26,7 +26,7 @@ }, "repository": { "type": "git", - "url": "https://github.com/ip2location/ip2location-nodejs.git" + "url": "git+https://github.com/ip2location/ip2location-nodejs.git" }, "devDependencies": { "prettier": "2.4.0" diff --git a/src/ip2location.js b/src/ip2location.js index d2d2ceb..b4db902 100644 --- a/src/ip2location.js +++ b/src/ip2location.js @@ -5,7 +5,7 @@ const https = require("https"); const csv = require("csv-parser"); // For BIN queries -const VERSION = "9.6.0"; +const VERSION = "9.6.1"; const MAX_INDEX = 65536; const COUNTRY_POSITION = [ 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, @@ -2295,34 +2295,43 @@ class IPTools { ip = arr[0]; prefix = parseInt(arr[1]); - let hexStartAddress = this.expandIPV6(ip).replaceAll(":", ""); - let hexEndAddress = hexStartAddress; + let parts = this.expandIPV6(ip).split(":"); - let bits = 128 - prefix; - let x = 0; - let y = ""; - let pos = 31; + let bitStart = "1".repeat(prefix) + "0".repeat(128 - prefix); + let bitEnd = "0".repeat(prefix) + "1".repeat(128 - prefix); - while (bits > 0) { - x = parseInt(hexEndAddress.charAt(pos), 16); - y = (x | (Math.pow(2, Math.min(4, bits)) - 1)).toString(16); // single hex char + let floors = bitStart.match(/.{1,16}/g) ?? []; + let ceilings = bitEnd.match(/.{1,16}/g) ?? []; - // replace char - hexEndAddress = - hexEndAddress.substring(0, pos) + - y + - hexEndAddress.substring(pos + y.length); + let start = []; + let end = []; - bits -= 4; - pos -= 1; + for (let x = 0; x < 8; x++) { + start.push( + ( + parseInt(parts[x], 16) & + parseInt(this.baseConvert(floors[x], 2, 16), 16) + ).toString(16) + ); + end.push( + ( + parseInt(parts[x], 16) | + parseInt(this.baseConvert(ceilings[x], 2, 16), 16) + ).toString(16) + ); } - hexStartAddress = hexStartAddress.replaceAll(/(.{4})/g, "$1:"); - hexStartAddress = hexStartAddress.substring(0, hexStartAddress.length - 1); - hexEndAddress = hexEndAddress.replaceAll(/(.{4})/g, "$1:"); - hexEndAddress = hexEndAddress.substring(0, hexEndAddress.length - 1); + return [this.expandIPV6(start.join(":")), this.expandIPV6(end.join(":"))]; + } + + baseConvert(num, fromBase, toBase) { + // Parse the num from the source base to base 10 + let base10Number = parseInt(num, fromBase); + + // Convert the base 10 number to the target base + let result = base10Number.toString(toBase); - return [hexStartAddress, hexEndAddress]; + return result; } }