@@ -4,7 +4,7 @@ var bigInt = require("big-integer");
44
55var fd ;
66
7- var version = "2.1 .0" ;
7+ var version = "2.2 .0" ;
88var binfile = "" ;
99var IPv4ColumnSize = 0 ;
1010var IPv6ColumnSize = 0 ;
@@ -93,6 +93,13 @@ var MSG_INVALID_IP = "INVALID IP ADDRESS";
9393var MSG_MISSING_FILE = "MISSING FILE" ;
9494var MSG_IPV6_UNSUPPORTED = "IPV6 ADDRESS MISSING IN IPV4 BIN" ;
9595
96+ // Read row data
97+ function readrow ( readbytes , pos ) {
98+ var buff = new Buffer . alloc ( readbytes ) ;
99+ totalread = fs . readSync ( fd , buff , 0 , readbytes , pos - 1 ) ;
100+ return buff ;
101+ }
102+
96103// Read binary data
97104function readbin ( readbytes , pos , readtype , isbigint ) {
98105 var buff = new Buffer . alloc ( readbytes ) ;
@@ -142,12 +149,22 @@ function read32(pos, isbigint) {
142149 return readbin ( readbytes , pos - 1 , "uint32" , isbigint ) ;
143150}
144151
152+ // Read 32 bits integer in the buffer
153+ function read32_row ( pos , buff ) {
154+ return buff . readUInt32LE ( pos ) ;
155+ }
156+
145157// Read 32 bits float in the database
146158function readfloat ( pos ) {
147159 readbytes = 4 ;
148160 return readbin ( readbytes , pos - 1 , "float" ) ;
149161}
150162
163+ // Read 32 bits float in the buffer
164+ function readfloat_row ( pos , buff ) {
165+ return buff . readFloatLE ( pos ) ;
166+ }
167+
151168function read32or128 ( pos , iptype ) {
152169 if ( iptype == 4 ) {
153170 return read32 ( pos , true ) ; // should be bigInt here already
@@ -236,16 +253,28 @@ function loadbin() {
236253 var dbt = mydb . _DBType ;
237254
238255 // since both IPv4 and IPv6 use 4 bytes for the below columns, can just do it once here
239- country_pos_offset = ( country_pos [ dbt ] != 0 ) ? ( country_pos [ dbt ] - 1 ) << 2 : 0 ;
240- region_pos_offset = ( region_pos [ dbt ] != 0 ) ? ( region_pos [ dbt ] - 1 ) << 2 : 0 ;
241- city_pos_offset = ( city_pos [ dbt ] != 0 ) ? ( city_pos [ dbt ] - 1 ) << 2 : 0 ;
242- isp_pos_offset = ( isp_pos [ dbt ] != 0 ) ? ( isp_pos [ dbt ] - 1 ) << 2 : 0 ;
243- proxytype_pos_offset = ( proxytype_pos [ dbt ] != 0 ) ? ( proxytype_pos [ dbt ] - 1 ) << 2 : 0 ;
244- domain_pos_offset = ( domain_pos [ dbt ] != 0 ) ? ( domain_pos [ dbt ] - 1 ) << 2 : 0 ;
245- usagetype_pos_offset = ( usagetype_pos [ dbt ] != 0 ) ? ( usagetype_pos [ dbt ] - 1 ) << 2 : 0 ;
246- asn_pos_offset = ( asn_pos [ dbt ] != 0 ) ? ( asn_pos [ dbt ] - 1 ) << 2 : 0 ;
247- as_pos_offset = ( as_pos [ dbt ] != 0 ) ? ( as_pos [ dbt ] - 1 ) << 2 : 0 ;
248- lastseen_pos_offset = ( lastseen_pos [ dbt ] != 0 ) ? ( lastseen_pos [ dbt ] - 1 ) << 2 : 0 ;
256+ // country_pos_offset = (country_pos[dbt] != 0) ? (country_pos[dbt] - 1) << 2 : 0;
257+ // region_pos_offset = (region_pos[dbt] != 0) ? (region_pos[dbt] - 1) << 2 : 0;
258+ // city_pos_offset = (city_pos[dbt] != 0) ? (city_pos[dbt] - 1) << 2 : 0;
259+ // isp_pos_offset = (isp_pos[dbt] != 0) ? (isp_pos[dbt] - 1) << 2 : 0;
260+ // proxytype_pos_offset = (proxytype_pos[dbt] != 0) ? (proxytype_pos[dbt] - 1) << 2 : 0;
261+ // domain_pos_offset = (domain_pos[dbt] != 0) ? (domain_pos[dbt] - 1) << 2 : 0;
262+ // usagetype_pos_offset = (usagetype_pos[dbt] != 0) ? (usagetype_pos[dbt] - 1) << 2 : 0;
263+ // asn_pos_offset = (asn_pos[dbt] != 0) ? (asn_pos[dbt] - 1) << 2 : 0;
264+ // as_pos_offset = (as_pos[dbt] != 0) ? (as_pos[dbt] - 1) << 2 : 0;
265+ // lastseen_pos_offset = (lastseen_pos[dbt] != 0) ? (lastseen_pos[dbt] - 1) << 2 : 0;
266+
267+ // slightly different offset for reading by row
268+ country_pos_offset = ( country_pos [ dbt ] != 0 ) ? ( country_pos [ dbt ] - 2 ) << 2 : 0 ;
269+ region_pos_offset = ( region_pos [ dbt ] != 0 ) ? ( region_pos [ dbt ] - 2 ) << 2 : 0 ;
270+ city_pos_offset = ( city_pos [ dbt ] != 0 ) ? ( city_pos [ dbt ] - 2 ) << 2 : 0 ;
271+ isp_pos_offset = ( isp_pos [ dbt ] != 0 ) ? ( isp_pos [ dbt ] - 2 ) << 2 : 0 ;
272+ proxytype_pos_offset = ( proxytype_pos [ dbt ] != 0 ) ? ( proxytype_pos [ dbt ] - 2 ) << 2 : 0 ;
273+ domain_pos_offset = ( domain_pos [ dbt ] != 0 ) ? ( domain_pos [ dbt ] - 2 ) << 2 : 0 ;
274+ usagetype_pos_offset = ( usagetype_pos [ dbt ] != 0 ) ? ( usagetype_pos [ dbt ] - 2 ) << 2 : 0 ;
275+ asn_pos_offset = ( asn_pos [ dbt ] != 0 ) ? ( asn_pos [ dbt ] - 2 ) << 2 : 0 ;
276+ as_pos_offset = ( as_pos [ dbt ] != 0 ) ? ( as_pos [ dbt ] - 2 ) << 2 : 0 ;
277+ lastseen_pos_offset = ( lastseen_pos [ dbt ] != 0 ) ? ( lastseen_pos [ dbt ] - 2 ) << 2 : 0 ;
249278
250279 country_enabled = ( country_pos [ dbt ] != 0 ) ? 1 : 0 ;
251280 region_enabled = ( region_pos [ dbt ] != 0 ) ? 1 : 0 ;
@@ -404,19 +433,25 @@ function proxyquery_data(myIP, iptype, data, mode) {
404433 if ( ipfrom . leq ( ipnum ) && ipto . gt ( ipnum ) ) {
405434 loadmesg ( data , MSG_NOT_SUPPORTED ) ; // load default message
406435
436+ var firstcol = 4 ;
407437 if ( iptype == 6 ) { // IPv6
408- rowoffset = rowoffset + 12 ; // coz below is assuming all columns are 4 bytes, so got 12 left to go to make 16 bytes total
438+ firstcol = 16 ;
439+ // rowoffset = rowoffset + 12; // coz below is assuming all columns are 4 bytes, so got 12 left to go to make 16 bytes total
409440 }
410441
442+ var row = readrow ( _ColumnSize - firstcol , rowoffset + firstcol ) ;
443+
411444 if ( proxytype_enabled ) {
412445 if ( mode == modes . ALL || mode == modes . PROXY_TYPE || mode == modes . IS_PROXY ) {
413- data . Proxy_Type = readstr ( read32 ( rowoffset + proxytype_pos_offset ) ) ;
446+ // data.Proxy_Type = readstr(read32(rowoffset + proxytype_pos_offset));
447+ data . Proxy_Type = readstr ( read32_row ( proxytype_pos_offset , row ) ) ;
414448 }
415449 }
416450
417451 if ( country_enabled ) {
418452 if ( mode == modes . ALL || mode == modes . COUNTRY_SHORT || mode == modes . COUNTRY_LONG || mode == modes . IS_PROXY ) {
419- countrypos = read32 ( rowoffset + country_pos_offset ) ;
453+ // countrypos = read32(rowoffset + country_pos_offset);
454+ countrypos = read32_row ( country_pos_offset , row ) ;
420455 }
421456 if ( mode == modes . ALL || mode == modes . COUNTRY_SHORT || mode == modes . IS_PROXY ) {
422457 data . Country_Short = readstr ( countrypos ) ;
@@ -428,43 +463,51 @@ function proxyquery_data(myIP, iptype, data, mode) {
428463
429464 if ( region_enabled ) {
430465 if ( mode == modes . ALL || mode == modes . REGION ) {
431- data . Region = readstr ( read32 ( rowoffset + region_pos_offset ) ) ;
466+ // data.Region = readstr(read32(rowoffset + region_pos_offset));
467+ data . Region = readstr ( read32_row ( region_pos_offset , row ) ) ;
432468 }
433469 }
434470
435471 if ( city_enabled ) {
436472 if ( mode == modes . ALL || mode == modes . CITY ) {
437- data . City = readstr ( read32 ( rowoffset + city_pos_offset ) ) ;
473+ // data.City = readstr(read32(rowoffset + city_pos_offset));
474+ data . City = readstr ( read32_row ( city_pos_offset , row ) ) ;
438475 }
439476 }
440477 if ( isp_enabled ) {
441478 if ( mode == modes . ALL || mode == modes . ISP ) {
442- data . ISP = readstr ( read32 ( rowoffset + isp_pos_offset ) ) ;
479+ // data.ISP = readstr(read32(rowoffset + isp_pos_offset));
480+ data . ISP = readstr ( read32_row ( isp_pos_offset , row ) ) ;
443481 }
444482 }
445483 if ( domain_enabled ) {
446484 if ( mode == modes . ALL || mode == modes . DOMAIN ) {
447- data . Domain = readstr ( read32 ( rowoffset + domain_pos_offset ) ) ;
485+ // data.Domain = readstr(read32(rowoffset + domain_pos_offset));
486+ data . Domain = readstr ( read32_row ( domain_pos_offset , row ) ) ;
448487 }
449488 }
450489 if ( usagetype_enabled ) {
451490 if ( mode == modes . ALL || mode == modes . USAGE_TYPE ) {
452- data . Usage_Type = readstr ( read32 ( rowoffset + usagetype_pos_offset ) ) ;
491+ // data.Usage_Type = readstr(read32(rowoffset + usagetype_pos_offset));
492+ data . Usage_Type = readstr ( read32_row ( usagetype_pos_offset , row ) ) ;
453493 }
454494 }
455495 if ( asn_enabled ) {
456496 if ( mode == modes . ALL || mode == modes . ASN ) {
457- data . ASN = readstr ( read32 ( rowoffset + asn_pos_offset ) ) ;
497+ // data.ASN = readstr(read32(rowoffset + asn_pos_offset));
498+ data . ASN = readstr ( read32_row ( asn_pos_offset , row ) ) ;
458499 }
459500 }
460501 if ( as_enabled ) {
461502 if ( mode == modes . ALL || mode == modes . AS ) {
462- data . AS = readstr ( read32 ( rowoffset + as_pos_offset ) ) ;
503+ // data.AS = readstr(read32(rowoffset + as_pos_offset));
504+ data . AS = readstr ( read32_row ( as_pos_offset , row ) ) ;
463505 }
464506 }
465507 if ( lastseen_enabled ) {
466508 if ( mode == modes . ALL || mode == modes . LAST_SEEN ) {
467- data . Last_Seen = readstr ( read32 ( rowoffset + lastseen_pos_offset ) ) ;
509+ // data.Last_Seen = readstr(read32(rowoffset + lastseen_pos_offset));
510+ data . Last_Seen = readstr ( read32_row ( lastseen_pos_offset , row ) ) ;
468511 }
469512 }
470513
0 commit comments