@@ -4,7 +4,7 @@ const fsp = fs.promises;
44const https = require ( "https" ) ;
55
66// For BIN queries
7- const VERSION = "4.3.1 " ;
7+ const VERSION = "4.3.2 " ;
88const MAX_INDEX = 65536 ;
99const COUNTRY_POSITION = [ 0 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 ] ;
1010const REGION_POSITION = [ 0 , 0 , 0 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 ] ;
@@ -25,6 +25,8 @@ const TO_6TO4 = BigInt("42550872755692912415807417417958686719");
2525const FROM_TEREDO = BigInt ( "42540488161975842760550356425300246528" ) ;
2626const TO_TEREDO = BigInt ( "42540488241204005274814694018844196863" ) ;
2727const LAST_32_BITS = BigInt ( "4294967295" ) ;
28+ const FROM_IPV4_MAPPED_IPV6 = BigInt ( "281470681743360" ) ;
29+ const TO_IPV4_MAPPED_IPV6 = BigInt ( "281474976710655" ) ;
2830const MODES = {
2931 COUNTRY_SHORT : 1 ,
3032 COUNTRY_LONG : 2 ,
@@ -127,6 +129,7 @@ class IP2Proxy {
127129 fileSize : 0 ,
128130 } ;
129131 #fd;
132+ #fh;
130133
131134 constructor ( ) { }
132135
@@ -396,8 +399,8 @@ class IP2Proxy {
396399
397400 try {
398401 if ( this . #binFile && this . #binFile != "" ) {
399- let fh = await fsp . open ( this . #binFile, "r" ) ;
400- this . #fd = fh . fd ;
402+ this . # fh = await fsp . open ( this . #binFile, "r" ) ;
403+ this . #fd = this . # fh. fd ;
401404
402405 let len = 64 ; // 64-byte header
403406 let row = await this . readRowAsync ( len , 1 ) ;
@@ -549,7 +552,7 @@ class IP2Proxy {
549552 }
550553 }
551554
552- // Reset everything (do not use in async case due to race conditions)
555+ // Reset everything
553556 close ( ) {
554557 try {
555558 this . #myDB. baseAddress = 0 ;
@@ -568,7 +571,33 @@ class IP2Proxy {
568571 this . #myDB. productCode = 0 ;
569572 this . #myDB. productType = 0 ;
570573 this . #myDB. fileSize = 0 ;
571- fs . closeSync ( this . #fd) ;
574+ fs . closeSync ( this . #fd) ; // normal file handle
575+ return 0 ;
576+ } catch ( err ) {
577+ return - 1 ;
578+ }
579+ }
580+
581+ // Reset everything
582+ async closeAsync ( ) {
583+ try {
584+ this . #myDB. baseAddress = 0 ;
585+ this . #myDB. dbCount = 0 ;
586+ this . #myDB. dbColumn = 0 ;
587+ this . #myDB. dbType = 0 ;
588+ this . #myDB. dbDay = 0 ;
589+ this . #myDB. dbMonth = 0 ;
590+ this . #myDB. dbYear = 0 ;
591+ this . #myDB. baseAddressIPV6 = 0 ;
592+ this . #myDB. dbCountIPV6 = 0 ;
593+ this . #myDB. indexed = 0 ;
594+ this . #myDB. indexedIPV6 = 0 ;
595+ this . #myDB. indexBaseAddress = 0 ;
596+ this . #myDB. indexBaseAddressIPV6 = 0 ;
597+ this . #myDB. productCode = 0 ;
598+ this . #myDB. productType = 0 ;
599+ this . #myDB. fileSize = 0 ;
600+ await this . #fh. close ( ) ; // FileHandler object opened by fs.Promises
572601 return 0 ;
573602 } catch ( err ) {
574603 return - 1 ;
@@ -615,7 +644,8 @@ class IP2Proxy {
615644
616645 if (
617646 ( ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4 ) ||
618- ( ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO )
647+ ( ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO ) ||
648+ ( ipNumber >= FROM_IPV4_MAPPED_IPV6 && ipNumber <= TO_IPV4_MAPPED_IPV6 )
619649 ) {
620650 ipType = 4 ;
621651 MAX_IP_RANGE = MAX_IPV4_RANGE ;
@@ -625,15 +655,21 @@ class IP2Proxy {
625655
626656 if ( ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4 ) {
627657 ipNumber = Number ( ( ipNumber >> BigInt ( 80 ) ) & LAST_32_BITS ) ;
628- } else {
658+ } else if ( ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO ) {
629659 ipNumber = Number ( ~ ipNumber & LAST_32_BITS ) ;
660+ } else {
661+ ipNumber = Number ( ipNumber - FROM_IPV4_MAPPED_IPV6 ) ;
630662 }
631663 if ( this . #myDB. indexed == 1 ) {
632664 indexAddress = ipNumber >>> 16 ;
633665 low = this . #indexArrayIPV4[ indexAddress ] [ 0 ] ;
634666 high = this . #indexArrayIPV4[ indexAddress ] [ 1 ] ;
635667 }
636668 } else {
669+ if ( this . #myDB. dbCountIPV6 == 0 ) {
670+ loadMesg ( data , MSG_IPV6_UNSUPPORTED ) ;
671+ return ;
672+ }
637673 firstCol = 16 ; // IPv6 is 16 bytes
638674 if ( this . #myDB. indexedIPV6 == 1 ) {
639675 indexAddress = Number ( ipNumber >> BigInt ( 112 ) ) ;
@@ -835,7 +871,8 @@ class IP2Proxy {
835871
836872 if (
837873 ( ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4 ) ||
838- ( ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO )
874+ ( ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO ) ||
875+ ( ipNumber >= FROM_IPV4_MAPPED_IPV6 && ipNumber <= TO_IPV4_MAPPED_IPV6 )
839876 ) {
840877 ipType = 4 ;
841878 MAX_IP_RANGE = MAX_IPV4_RANGE ;
@@ -845,15 +882,21 @@ class IP2Proxy {
845882
846883 if ( ipNumber >= FROM_6TO4 && ipNumber <= TO_6TO4 ) {
847884 ipNumber = Number ( ( ipNumber >> BigInt ( 80 ) ) & LAST_32_BITS ) ;
848- } else {
885+ } else if ( ipNumber >= FROM_TEREDO && ipNumber <= TO_TEREDO ) {
849886 ipNumber = Number ( ~ ipNumber & LAST_32_BITS ) ;
887+ } else {
888+ ipNumber = Number ( ipNumber - FROM_IPV4_MAPPED_IPV6 ) ;
850889 }
851890 if ( this . #myDB. indexed == 1 ) {
852891 indexAddress = ipNumber >>> 16 ;
853892 low = this . #indexArrayIPV4[ indexAddress ] [ 0 ] ;
854893 high = this . #indexArrayIPV4[ indexAddress ] [ 1 ] ;
855894 }
856895 } else {
896+ if ( this . #myDB. dbCountIPV6 == 0 ) {
897+ loadMesg ( data , MSG_IPV6_UNSUPPORTED ) ;
898+ return ;
899+ }
857900 firstCol = 16 ; // IPv6 is 16 bytes
858901 if ( this . #myDB. indexedIPV6 == 1 ) {
859902 indexAddress = Number ( ipNumber >> BigInt ( 112 ) ) ;
@@ -1059,9 +1102,6 @@ class IP2Proxy {
10591102 } else if ( this . #myDB. dbType == 0 ) {
10601103 loadMesg ( data , MSG_MISSING_FILE ) ;
10611104 return data ;
1062- } else if ( ipType == 6 && this . #myDB. dbCountIPV6 == 0 ) {
1063- loadMesg ( data , MSG_IPV6_UNSUPPORTED ) ;
1064- return data ;
10651105 } else {
10661106 this . proxyQueryData ( myIP , ipType , data , mode ) ;
10671107 return data ;
@@ -1110,9 +1150,6 @@ class IP2Proxy {
11101150 } else if ( this . #myDB. dbType == 0 ) {
11111151 loadMesg ( data , MSG_MISSING_FILE ) ;
11121152 return data ;
1113- } else if ( ipType == 6 && this . #myDB. dbCountIPV6 == 0 ) {
1114- loadMesg ( data , MSG_IPV6_UNSUPPORTED ) ;
1115- return data ;
11161153 } else {
11171154 await this . proxyQueryDataAsync ( myIP , ipType , data , mode ) ;
11181155 return data ;
0 commit comments