Skip to content

Commit

Permalink
Fixed wrong value returned by cidrToIPV6 function
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Nov 10, 2023
1 parent 1a170a4 commit f7dcaf2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ip2location-nodejs",
"version": "9.6.0",
"version": "9.6.1",
"description": "IP2Location geolocation component",
"keywords": [
"ip2location",
Expand All @@ -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"
Expand Down
53 changes: 31 additions & 22 deletions src/ip2location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit f7dcaf2

Please sign in to comment.