Skip to content

Commit f7dcaf2

Browse files
committed
Fixed wrong value returned by cidrToIPV6 function
1 parent 1a170a4 commit f7dcaf2

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ip2location-nodejs",
3-
"version": "9.6.0",
3+
"version": "9.6.1",
44
"description": "IP2Location geolocation component",
55
"keywords": [
66
"ip2location",
@@ -26,7 +26,7 @@
2626
},
2727
"repository": {
2828
"type": "git",
29-
"url": "https://github.com/ip2location/ip2location-nodejs.git"
29+
"url": "git+https://github.com/ip2location/ip2location-nodejs.git"
3030
},
3131
"devDependencies": {
3232
"prettier": "2.4.0"

src/ip2location.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const https = require("https");
55
const csv = require("csv-parser");
66

77
// For BIN queries
8-
const VERSION = "9.6.0";
8+
const VERSION = "9.6.1";
99
const MAX_INDEX = 65536;
1010
const COUNTRY_POSITION = [
1111
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 {
22952295
ip = arr[0];
22962296
prefix = parseInt(arr[1]);
22972297

2298-
let hexStartAddress = this.expandIPV6(ip).replaceAll(":", "");
2299-
let hexEndAddress = hexStartAddress;
2298+
let parts = this.expandIPV6(ip).split(":");
23002299

2301-
let bits = 128 - prefix;
2302-
let x = 0;
2303-
let y = "";
2304-
let pos = 31;
2300+
let bitStart = "1".repeat(prefix) + "0".repeat(128 - prefix);
2301+
let bitEnd = "0".repeat(prefix) + "1".repeat(128 - prefix);
23052302

2306-
while (bits > 0) {
2307-
x = parseInt(hexEndAddress.charAt(pos), 16);
2308-
y = (x | (Math.pow(2, Math.min(4, bits)) - 1)).toString(16); // single hex char
2303+
let floors = bitStart.match(/.{1,16}/g) ?? [];
2304+
let ceilings = bitEnd.match(/.{1,16}/g) ?? [];
23092305

2310-
// replace char
2311-
hexEndAddress =
2312-
hexEndAddress.substring(0, pos) +
2313-
y +
2314-
hexEndAddress.substring(pos + y.length);
2306+
let start = [];
2307+
let end = [];
23152308

2316-
bits -= 4;
2317-
pos -= 1;
2309+
for (let x = 0; x < 8; x++) {
2310+
start.push(
2311+
(
2312+
parseInt(parts[x], 16) &
2313+
parseInt(this.baseConvert(floors[x], 2, 16), 16)
2314+
).toString(16)
2315+
);
2316+
end.push(
2317+
(
2318+
parseInt(parts[x], 16) |
2319+
parseInt(this.baseConvert(ceilings[x], 2, 16), 16)
2320+
).toString(16)
2321+
);
23182322
}
23192323

2320-
hexStartAddress = hexStartAddress.replaceAll(/(.{4})/g, "$1:");
2321-
hexStartAddress = hexStartAddress.substring(0, hexStartAddress.length - 1);
2322-
hexEndAddress = hexEndAddress.replaceAll(/(.{4})/g, "$1:");
2323-
hexEndAddress = hexEndAddress.substring(0, hexEndAddress.length - 1);
2324+
return [this.expandIPV6(start.join(":")), this.expandIPV6(end.join(":"))];
2325+
}
2326+
2327+
baseConvert(num, fromBase, toBase) {
2328+
// Parse the num from the source base to base 10
2329+
let base10Number = parseInt(num, fromBase);
2330+
2331+
// Convert the base 10 number to the target base
2332+
let result = base10Number.toString(toBase);
23242333

2325-
return [hexStartAddress, hexEndAddress];
2334+
return result;
23262335
}
23272336
}
23282337

0 commit comments

Comments
 (0)