Skip to content

Commit 1a83427

Browse files
committed
Changed to native BigInt
1 parent 7fea770 commit 1a83427

File tree

4 files changed

+49
-58
lines changed

4 files changed

+49
-58
lines changed

LICENSE.TXT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 IP2Location.com
3+
Copyright (c) 2023 IP2Location.com
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ip2proxy-nodejs",
3-
"version": "4.2.1",
3+
"version": "4.2.2",
44
"description": "IP2Proxy proxy detection component",
55
"keywords": [
66
"vpn-detection",
@@ -27,9 +27,6 @@
2727
"main": "src/ip2proxy.js",
2828
"types": "src/ip2proxy.d.ts",
2929
"license": "MIT",
30-
"dependencies": {
31-
"big-integer": ">=1.6.8"
32-
},
3330
"repository": {
3431
"type": "git",
3532
"url": "https://github.com/ip2location/ip2proxy-nodejs"

src/ip2proxy.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class IP2Proxy {
1313
* @param readBytes The number of bytes to read.
1414
* @param position The file offset to start reading.
1515
* @param readType The data type to convert the bytes to. (Valid values: int8|int32|uint32|float|str|int128)
16-
* @param isBigInt Whether to convert to BigInteger object.
16+
* @param isBigInt Whether to convert to BigInt.
1717
* @returns The value of the specified data type.
1818
*/
1919
readBin(readBytes: number, position: number, readType: string, isBigInt: boolean): any;
@@ -36,7 +36,7 @@ export class IP2Proxy {
3636
* Reads unsigned 32-bit integer from file.
3737
*
3838
* @param position The file offset to start reading.
39-
* @param isBigInt Whether to convert to BigInteger object.
39+
* @param isBigInt Whether to convert to BigInt.
4040
* @returns Unsigned 32-bit integer.
4141
*/
4242
read32(position: number, isBigInt: boolean): number;
@@ -53,7 +53,7 @@ export class IP2Proxy {
5353
*
5454
* @param position The buffer offset to start reading.
5555
* @param buffer The buffer containing the data.
56-
* @returns BigInteger object.
56+
* @returns BigInt.
5757
*/
5858
read128Row(position: number, buffer: any): any;
5959
/**
@@ -62,22 +62,22 @@ export class IP2Proxy {
6262
* @param position The buffer offset to start reading.
6363
* @param buffer The buffer containing the data.
6464
* @param len The number of bytes to read.
65-
* @returns BigInteger object or unsigned 32-bit integer.
65+
* @returns BigInt or unsigned 32-bit integer.
6666
*/
6767
read32Or128Row(position: number, buffer: any, len: number): any;
6868
/**
6969
* Reads either unsigned 32-bit or 128-bit integer from file.
7070
*
7171
* @param position The file offset to start reading.
7272
* @param ipType 4 for IPv4 or 6 for IPv6.
73-
* @returns BigInteger object or unsigned 32-bit integer.
73+
* @returns BigInt or unsigned 32-bit integer.
7474
*/
7575
read32Or128(position: number, ipType: number): any;
7676
/**
7777
* Reads unsigned 128-bit integer from file.
7878
*
7979
* @param position The file offset to start reading.
80-
* @returns BigInteger object.
80+
* @returns BigInt.
8181
*/
8282
read128(position: number): any;
8383
/**

src/ip2proxy.js

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
var net = require("net");
22
var fs = require("fs");
3-
var bigInt = require("big-integer");
43
var https = require("https");
54

65
// For BIN queries
7-
const VERSION = "4.2.1";
6+
const VERSION = "4.2.2";
87
const MAX_INDEX = 65536;
98
const COUNTRY_POSITION = [0, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3];
109
const REGION_POSITION = [0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4];
@@ -18,13 +17,13 @@ const AS_POSITION = [0, 0, 0, 0, 0, 0, 0, 10, 10, 10, 10, 10];
1817
const LAST_SEEN_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 11, 11];
1918
const THREAT_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 12];
2019
const PROVIDER_POSITION = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13];
21-
const MAX_IPV4_RANGE = bigInt(4294967295);
22-
const MAX_IPV6_RANGE = bigInt("340282366920938463463374607431768211455");
23-
const FROM_6TO4 = bigInt("42545680458834377588178886921629466624");
24-
const TO_6TO4 = bigInt("42550872755692912415807417417958686719");
25-
const FROM_TEREDO = bigInt("42540488161975842760550356425300246528");
26-
const TO_TEREDO = bigInt("42540488241204005274814694018844196863");
27-
const LAST_32_BITS = bigInt("4294967295");
20+
const MAX_IPV4_RANGE = BigInt(4294967295);
21+
const MAX_IPV6_RANGE = BigInt("340282366920938463463374607431768211455");
22+
const FROM_6TO4 = BigInt("42545680458834377588178886921629466624");
23+
const TO_6TO4 = BigInt("42550872755692912415807417417958686719");
24+
const FROM_TEREDO = BigInt("42540488161975842760550356425300246528");
25+
const TO_TEREDO = BigInt("42540488241204005274814694018844196863");
26+
const LAST_32_BITS = BigInt("4294967295");
2827
const MODES = {
2928
COUNTRY_SHORT: 1,
3029
COUNTRY_LONG: 2,
@@ -139,7 +138,7 @@ class IP2Proxy {
139138
break;
140139
case "uint32":
141140
return isBigInt
142-
? bigInt(buffer.readUInt32LE(0))
141+
? BigInt(buffer.readUInt32LE(0))
143142
: buffer.readUInt32LE(0);
144143
break;
145144
case "float":
@@ -149,12 +148,11 @@ class IP2Proxy {
149148
return buffer.toString("utf8");
150149
break;
151150
case "int128":
152-
let myBig = bigInt(); // zero
151+
let myBig = BigInt(0); // zero
153152
let bitShift = 8;
154153
for (let x = 0; x < 16; x++) {
155-
myBig = myBig.add(
156-
bigInt(buffer.readUInt8(x)).shiftLeft(bitShift * x)
157-
);
154+
myBig =
155+
myBig + (BigInt(buffer.readUInt8(x)) << BigInt(bitShift * x));
158156
}
159157
return myBig;
160158
break;
@@ -188,13 +186,12 @@ class IP2Proxy {
188186

189187
// Read 128 bits integer in the buffer
190188
read128Row(position, buffer) {
191-
let myBig = bigInt(); // zero
189+
let myBig = BigInt(0); // zero
192190
let bitShift = 8;
193191
for (let x = 0; x < 16; x++) {
194192
let pos = position + x;
195-
myBig = myBig.add(
196-
bigInt(this.read8Row(pos, buffer)).shiftLeft(bitShift * x)
197-
);
193+
myBig =
194+
myBig + (BigInt(this.read8Row(pos, buffer)) << BigInt(bitShift * x));
198195
}
199196
return myBig;
200197
}
@@ -440,19 +437,19 @@ class IP2Proxy {
440437
ipNumber = ip2No(myIP);
441438

442439
if (
443-
(ipNumber.geq(FROM_6TO4) && ipNumber.leq(TO_6TO4)) ||
444-
(ipNumber.geq(FROM_TEREDO) && ipNumber.leq(TO_TEREDO))
440+
(ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4) ||
441+
(ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO)
445442
) {
446443
ipType = 4;
447444
MAX_IP_RANGE = MAX_IPV4_RANGE;
448445
high = this.#myDB.dbCount;
449446
baseAddress = this.#myDB.baseAddress;
450447
columnSize = this.#ipV4ColumnSize;
451448

452-
if (ipNumber.geq(FROM_6TO4) && ipNumber.leq(TO_6TO4)) {
453-
ipNumber = ipNumber.shiftRight(80).and(LAST_32_BITS).toJSNumber();
449+
if (ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4) {
450+
ipNumber = Number((ipNumber >> BigInt(80)) & LAST_32_BITS);
454451
} else {
455-
ipNumber = ipNumber.not().and(LAST_32_BITS).toJSNumber();
452+
ipNumber = Number(~ipNumber & LAST_32_BITS);
456453
}
457454
if (this.#myDB.indexed == 1) {
458455
indexAddress = ipNumber >>> 16;
@@ -462,18 +459,18 @@ class IP2Proxy {
462459
} else {
463460
firstCol = 16; // IPv6 is 16 bytes
464461
if (this.#myDB.indexedIPV6 == 1) {
465-
indexAddress = ipNumber.shiftRight(112).toJSNumber();
462+
indexAddress = Number(ipNumber >> BigInt(112));
466463
low = this.#indexArrayIPV6[indexAddress][0];
467464
high = this.#indexArrayIPV6[indexAddress][1];
468465
}
469466
}
470467
}
471468

472469
data.ip = myIP;
473-
ipNumber = bigInt(ipNumber);
470+
ipNumber = BigInt(ipNumber);
474471

475-
if (ipNumber.geq(MAX_IP_RANGE)) {
476-
ipNumber = MAX_IP_RANGE.minus(1);
472+
if (ipNumber >= MAX_IP_RANGE) {
473+
ipNumber = MAX_IP_RANGE - BigInt(1);
477474
}
478475

479476
data.ipNo = ipNumber.toString();
@@ -488,10 +485,10 @@ class IP2Proxy {
488485
ipFrom = this.read32Or128Row(0, fullRow, firstCol);
489486
ipTo = this.read32Or128Row(columnSize, fullRow, firstCol);
490487

491-
ipFrom = bigInt(ipFrom);
492-
ipTo = bigInt(ipTo);
488+
ipFrom = BigInt(ipFrom);
489+
ipTo = BigInt(ipTo);
493490

494-
if (ipFrom.leq(ipNumber) && ipTo.gt(ipNumber)) {
491+
if (ipFrom <= ipNumber && ipTo >= ipNumber) {
495492
loadMesg(data, MSG_NOT_SUPPORTED); // load default message
496493

497494
let rowLen = columnSize - firstCol;
@@ -611,7 +608,7 @@ class IP2Proxy {
611608
}
612609
return;
613610
} else {
614-
if (ipFrom.gt(ipNumber)) {
611+
if (ipFrom > ipNumber) {
615612
high = mid - 1;
616613
} else {
617614
low = mid + 1;
@@ -801,37 +798,34 @@ function ip2No(ipV6) {
801798
let sectionBits = 16; // 16 bits per section
802799
let m = ipV6.split("::");
803800

804-
let total = bigInt(); // zero
801+
let total = BigInt(0); // zero
805802

806803
if (m.length == 2) {
807804
let myArrLeft = m[0] != "" ? m[0].split(":") : [];
808805
let myArrRight = m[1] != "" ? m[1].split(":") : [];
809806
let myArrMid = maxSections - myArrLeft.length - myArrRight.length;
810807

811808
for (let x = 0; x < myArrLeft.length; x++) {
812-
total = total.add(
813-
bigInt(parseInt("0x" + myArrLeft[x])).shiftLeft(
814-
(maxSections - (x + 1)) * sectionBits
815-
)
816-
);
809+
total =
810+
total +
811+
(BigInt(parseInt("0x" + myArrLeft[x])) <<
812+
BigInt((maxSections - (x + 1)) * sectionBits));
817813
}
818814

819815
for (let x = 0; x < myArrRight.length; x++) {
820-
total = total.add(
821-
bigInt(parseInt("0x" + myArrRight[x])).shiftLeft(
822-
(myArrRight.length - (x + 1)) * sectionBits
823-
)
824-
);
816+
total =
817+
total +
818+
(BigInt(parseInt("0x" + myArrRight[x])) <<
819+
BigInt((myArrRight.length - (x + 1)) * sectionBits));
825820
}
826821
} else if (m.length == 1) {
827822
let myArr = m[0].split(":");
828823

829824
for (let x = 0; x < myArr.length; x++) {
830-
total = total.add(
831-
bigInt(parseInt("0x" + myArr[x])).shiftLeft(
832-
(maxSections - (x + 1)) * sectionBits
833-
)
834-
);
825+
total =
826+
total +
827+
(BigInt(parseInt("0x" + myArr[x])) <<
828+
BigInt((maxSections - (x + 1)) * sectionBits));
835829
}
836830
}
837831

0 commit comments

Comments
 (0)