11var net = require ( "net" ) ;
22var fs = require ( "fs" ) ;
3- var bigInt = require ( "big-integer" ) ;
43var https = require ( "https" ) ;
54const csv = require ( "csv-parser" ) ;
65
76// For BIN queries
8- const VERSION = "9.4.2 " ;
7+ const VERSION = "9.4.3 " ;
98const MAX_INDEX = 65536 ;
109const COUNTRY_POSITION = [
1110 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 ,
@@ -81,13 +80,13 @@ const ADDRESS_TYPE_POSITION = [
8180const CATEGORY_POSITION = [
8281 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 22 ,
8382] ;
84- const MAX_IPV4_RANGE = bigInt ( 4294967295 ) ;
85- const MAX_IPV6_RANGE = bigInt ( "340282366920938463463374607431768211455" ) ;
86- const FROM_6TO4 = bigInt ( "42545680458834377588178886921629466624" ) ;
87- const TO_6TO4 = bigInt ( "42550872755692912415807417417958686719" ) ;
88- const FROM_TEREDO = bigInt ( "42540488161975842760550356425300246528" ) ;
89- const TO_TEREDO = bigInt ( "42540488241204005274814694018844196863" ) ;
90- const LAST_32_BITS = bigInt ( "4294967295" ) ;
83+ const MAX_IPV4_RANGE = BigInt ( 4294967295 ) ;
84+ const MAX_IPV6_RANGE = BigInt ( "340282366920938463463374607431768211455" ) ;
85+ const FROM_6TO4 = BigInt ( "42545680458834377588178886921629466624" ) ;
86+ const TO_6TO4 = BigInt ( "42550872755692912415807417417958686719" ) ;
87+ const FROM_TEREDO = BigInt ( "42540488161975842760550356425300246528" ) ;
88+ const TO_TEREDO = BigInt ( "42540488241204005274814694018844196863" ) ;
89+ const LAST_32_BITS = BigInt ( "4294967295" ) ;
9190
9291const MODES = {
9392 COUNTRY_SHORT : 1 ,
@@ -239,7 +238,7 @@ class IP2Location {
239238 break ;
240239 case "uint32" :
241240 return isBigInt
242- ? bigInt ( buffer . readUInt32LE ( 0 ) )
241+ ? BigInt ( buffer . readUInt32LE ( 0 ) )
243242 : buffer . readUInt32LE ( 0 ) ;
244243 break ;
245244 case "float" :
@@ -249,12 +248,10 @@ class IP2Location {
249248 return buffer . toString ( "utf8" ) ;
250249 break ;
251250 case "int128" :
252- let myBig = bigInt ( ) ; // zero
251+ let myBig = BigInt ( 0 ) ; // zero
253252 let bitShift = 8 ;
254253 for ( let x = 0 ; x < 16 ; x ++ ) {
255- myBig = myBig . add (
256- bigInt ( buffer . readUInt8 ( x ) ) . shiftLeft ( bitShift * x )
257- ) ;
254+ myBig = myBig + ( BigInt ( buffer . readUInt8 ( x ) ) << ( bitShift * x ) ) ;
258255 }
259256 return myBig ;
260257 break ;
@@ -288,13 +285,12 @@ class IP2Location {
288285
289286 // Read 128 bits integer in the buffer
290287 read128Row ( position , buffer ) {
291- let myBig = bigInt ( ) ; // zero
288+ let myBig = BigInt ( 0 ) ; // zero
292289 let bitShift = 8 ;
293290 for ( let x = 0 ; x < 16 ; x ++ ) {
294291 let pos = position + x ;
295- myBig = myBig . add (
296- bigInt ( this . read8Row ( pos , buffer ) ) . shiftLeft ( bitShift * x )
297- ) ;
292+ myBig =
293+ myBig + ( BigInt ( this . read8Row ( pos , buffer ) ) << BigInt ( bitShift * x ) ) ;
298294 }
299295 return myBig ;
300296 }
@@ -571,19 +567,19 @@ class IP2Location {
571567 ipNumber = ip2No ( myIP ) ;
572568
573569 if (
574- ( ipNumber . geq ( FROM_6TO4 ) && ipNumber . leq ( TO_6TO4 ) ) ||
575- ( ipNumber . geq ( FROM_TEREDO ) && ipNumber . leq ( TO_TEREDO ) )
570+ ( ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4 ) ||
571+ ( ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO )
576572 ) {
577573 ipType = 4 ;
578574 MAX_IP_RANGE = MAX_IPV4_RANGE ;
579575 high = this . #myDB. dbCount ;
580576 baseAddress = this . #myDB. baseAddress ;
581577 columnSize = this . #ipV4ColumnSize;
582578
583- if ( ipNumber . geq ( FROM_6TO4 ) && ipNumber . leq ( TO_6TO4 ) ) {
584- ipNumber = ipNumber . shiftRight ( 80 ) . and ( LAST_32_BITS ) . toJSNumber ( ) ;
579+ if ( ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4 ) {
580+ ipNumber = Number ( ( ipNumber >> BigInt ( 80 ) ) & LAST_32_BITS ) ;
585581 } else {
586- ipNumber = ipNumber . not ( ) . and ( LAST_32_BITS ) . toJSNumber ( ) ;
582+ ipNumber = Number ( ~ ipNumber & LAST_32_BITS ) ;
587583 }
588584 if ( this . #myDB. indexed == 1 ) {
589585 indexAddress = ipNumber >>> 16 ;
@@ -593,17 +589,17 @@ class IP2Location {
593589 } else {
594590 firstCol = 16 ; // IPv6 is 16 bytes
595591 if ( this . #myDB. indexedIPV6 == 1 ) {
596- indexAddress = ipNumber . shiftRight ( 112 ) . toJSNumber ( ) ;
592+ indexAddress = Number ( ipNumber >> BigInt ( 112 ) ) ;
597593 low = this . #indexArrayIPV6[ indexAddress ] [ 0 ] ;
598594 high = this . #indexArrayIPV6[ indexAddress ] [ 1 ] ;
599595 }
600596 }
601597 }
602598 data . ip = myIP ;
603- ipNumber = bigInt ( ipNumber ) ;
599+ ipNumber = BigInt ( ipNumber ) ;
604600
605- if ( ipNumber . geq ( MAX_IP_RANGE ) ) {
606- ipNumber = MAX_IP_RANGE . minus ( 1 ) ;
601+ if ( ipNumber >= MAX_IP_RANGE ) {
602+ ipNumber = MAX_IP_RANGE - BigInt ( 1 ) ;
607603 }
608604
609605 data . ipNo = ipNumber . toString ( ) ;
@@ -618,10 +614,10 @@ class IP2Location {
618614 ipFrom = this . read32Or128Row ( 0 , fullRow , firstCol ) ;
619615 ipTo = this . read32Or128Row ( columnSize , fullRow , firstCol ) ;
620616
621- ipFrom = bigInt ( ipFrom ) ;
622- ipTo = bigInt ( ipTo ) ;
617+ ipFrom = BigInt ( ipFrom ) ;
618+ ipTo = BigInt ( ipTo ) ;
623619
624- if ( ipFrom . leq ( ipNumber ) && ipTo . gt ( ipNumber ) ) {
620+ if ( ipFrom <= ipNumber && ipTo > ipNumber ) {
625621 loadMesg ( data , MSG_NOT_SUPPORTED ) ; // load default message
626622
627623 let rowLen = columnSize - firstCol ;
@@ -790,7 +786,7 @@ class IP2Location {
790786 }
791787 return ;
792788 } else {
793- if ( ipFrom . gt ( ipNumber ) ) {
789+ if ( ipFrom > ipNumber ) {
794790 high = mid - 1 ;
795791 } else {
796792 low = mid + 1 ;
@@ -1032,37 +1028,34 @@ function ip2No(ipV6) {
10321028 let sectionBits = 16 ; // 16 bits per section
10331029 let m = ipV6 . split ( "::" ) ;
10341030
1035- let total = bigInt ( ) ; // zero
1031+ let total = BigInt ( 0 ) ; // zero
10361032
10371033 if ( m . length == 2 ) {
10381034 let myArrLeft = m [ 0 ] != "" ? m [ 0 ] . split ( ":" ) : [ ] ;
10391035 let myArrRight = m [ 1 ] != "" ? m [ 1 ] . split ( ":" ) : [ ] ;
10401036 let myArrMid = maxSections - myArrLeft . length - myArrRight . length ;
10411037
10421038 for ( let x = 0 ; x < myArrLeft . length ; x ++ ) {
1043- total = total . add (
1044- bigInt ( parseInt ( "0x" + myArrLeft [ x ] ) ) . shiftLeft (
1045- ( maxSections - ( x + 1 ) ) * sectionBits
1046- )
1047- ) ;
1039+ total =
1040+ total +
1041+ ( BigInt ( parseInt ( "0x" + myArrLeft [ x ] ) ) <<
1042+ BigInt ( ( maxSections - ( x + 1 ) ) * sectionBits ) ) ;
10481043 }
10491044
10501045 for ( let x = 0 ; x < myArrRight . length ; x ++ ) {
1051- total = total . add (
1052- bigInt ( parseInt ( "0x" + myArrRight [ x ] ) ) . shiftLeft (
1053- ( myArrRight . length - ( x + 1 ) ) * sectionBits
1054- )
1055- ) ;
1046+ total =
1047+ total +
1048+ ( BigInt ( parseInt ( "0x" + myArrRight [ x ] ) ) <<
1049+ BigInt ( ( myArrRight . length - ( x + 1 ) ) * sectionBits ) ) ;
10561050 }
10571051 } else if ( m . length == 1 ) {
10581052 let myArr = m [ 0 ] . split ( ":" ) ;
10591053
10601054 for ( let x = 0 ; x < myArr . length ; x ++ ) {
1061- total = total . add (
1062- bigInt ( parseInt ( "0x" + myArr [ x ] ) ) . shiftLeft (
1063- ( maxSections - ( x + 1 ) ) * sectionBits
1064- )
1065- ) ;
1055+ total =
1056+ total +
1057+ ( BigInt ( parseInt ( "0x" + myArr [ x ] ) ) <<
1058+ BigInt ( ( maxSections - ( x + 1 ) ) * sectionBits ) ) ;
10661059 }
10671060 }
10681061
@@ -1231,10 +1224,10 @@ class IPTools {
12311224 // Convert IP number to IPv6 address
12321225 decimalToIPV6 ( ipNum ) {
12331226 if ( typeof ipNum == "string" || typeof ipNum == "number" ) {
1234- ipNum = bigInt ( ipNum ) ;
1227+ ipNum = BigInt ( ipNum ) ;
12351228 }
12361229
1237- if ( ipNum . lt ( bigInt . zero ) || ipNum . gt ( MAX_IPV6_RANGE ) ) {
1230+ if ( ipNum < BigInt ( 0 ) || ipNum > MAX_IPV6_RANGE ) {
12381231 return null ;
12391232 }
12401233
@@ -1348,7 +1341,7 @@ class IPTools {
13481341 return null ;
13491342 }
13501343
1351- let ipNum = bigInt ( myBin , 2 ) ;
1344+ let ipNum = BigInt ( "0b" + myBin ) ;
13521345 let v6 = this . decimalToIPV6 ( ipNum ) ;
13531346
13541347 return v6 ;
0 commit comments