11var net = require ( "net" ) ;
22var fs = require ( "fs" ) ;
3- var bigInt = require ( "big-integer" ) ;
43var https = require ( "https" ) ;
54
65// For BIN queries
7- const VERSION = "4.2.1 " ;
6+ const VERSION = "4.2.2 " ;
87const MAX_INDEX = 65536 ;
98const COUNTRY_POSITION = [ 0 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 ] ;
109const 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];
1817const LAST_SEEN_POSITION = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 11 , 11 , 11 , 11 ] ;
1918const THREAT_POSITION = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 12 , 12 , 12 ] ;
2019const 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" ) ;
2827const 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