|
11 | 11 | class Ip2proxy |
12 | 12 | attr_accessor :record_class4, :record_class6, :v4, :file, :db_index, :count, :base_addr, :ipno, :record, :database, :columns, :ip_version, :ipv4databasecount, :ipv4databaseaddr, :ipv4indexbaseaddr, :ipv6databasecount, :ipv6databaseaddr, :ipv6indexbaseaddr, :databaseyear, :databasemonth, :databaseday, :last_err_msg |
13 | 13 |
|
14 | | - VERSION = '3.2.1' |
| 14 | + VERSION = '3.3.0' |
15 | 15 | FIELD_NOT_SUPPORTED = 'NOT SUPPORTED' |
16 | 16 | INVALID_IP_ADDRESS = 'INVALID IP ADDRESS' |
17 | 17 | INVALID_BIN_DATABASE = 'Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file.' |
@@ -101,8 +101,7 @@ def get_record(ip) |
101 | 101 | ipnum = realipno - 1 |
102 | 102 | end |
103 | 103 | end |
104 | | - low = read32(indexpos) |
105 | | - high = read32(indexpos + 4) |
| 104 | + low, high = read32x2(indexpos) |
106 | 105 | return self.record = bsearch(low, high, ipnum, self.base_addr, col_length) |
107 | 106 | else |
108 | 107 | return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length) |
@@ -419,11 +418,12 @@ def bsearch(low, high, ipnum, base_addr, col_length) |
419 | 418 | end |
420 | 419 |
|
421 | 420 | def get_from_to(mid, base_addr, col_length) |
422 | | - from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12))) |
| 421 | + from_base = (base_addr + mid * (col_length + (v4 ? 0 : 12))) |
| 422 | + data_length = col_length + (v4 ? 4 : (12 + 16)) |
423 | 423 | file.seek(from_base) |
424 | | - ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file) |
425 | | - file.seek(from_base + col_length + (v4 ? 0 : 12)) |
426 | | - ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file) |
| 424 | + data_read = file.read(data_length) |
| 425 | + ip_from = v4 ? data_read[0..3].unpack('V').first : readipv6(data_read[0..15].unpack('V*')) |
| 426 | + ip_to = v4 ? data_read[(data_length - 4)..(data_length - 1)].unpack('V').first : readipv6(data_read[(data_length - 16)..(data_length - 1)].unpack('V*')) |
427 | 427 | [ip_from, ip_to] |
428 | 428 | end |
429 | 429 |
|
@@ -459,17 +459,19 @@ def validateip(ip) |
459 | 459 | [ipv, ipnum] |
460 | 460 | end |
461 | 461 |
|
462 | | - def read32(indexp) |
| 462 | + def read32x2(indexp) |
463 | 463 | file.seek(indexp - 1) |
464 | | - return file.read(4).unpack('V').first |
| 464 | + data_read = file.read(8) |
| 465 | + data1 = data_read[0..3].unpack('V').first |
| 466 | + data2 = data_read[4..7].unpack('V').first |
| 467 | + return [data1, data2] |
465 | 468 | end |
466 | 469 |
|
467 | | - def readipv6(filer) |
468 | | - parts = filer.read(16).unpack('V*') |
| 470 | + def readipv6(parts) |
469 | 471 | return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3 |
470 | 472 | end |
471 | 473 |
|
472 | | - private :get_record, :bsearch, :get_from_to, :read32, :readipv6 |
| 474 | + private :get_record, :bsearch, :get_from_to, :read32x2, :readipv6 |
473 | 475 |
|
474 | 476 | end |
475 | 477 |
|
|
0 commit comments