lib/ip2proxy_ruby.rb in ip2proxy_ruby-3.2.1 vs lib/ip2proxy_ruby.rb in ip2proxy_ruby-3.3.0

- old
+ new

@@ -9,11 +9,11 @@ require_relative 'ip2proxy_ruby/ip2proxy_record' class Ip2proxy 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 - VERSION = '3.2.1' + VERSION = '3.3.0' FIELD_NOT_SUPPORTED = 'NOT SUPPORTED' INVALID_IP_ADDRESS = 'INVALID IP ADDRESS' INVALID_BIN_DATABASE = 'Incorrect IP2Proxy BIN file format. Please make sure that you are using the latest IP2Proxy BIN file.' def open(url) @@ -99,12 +99,11 @@ # if ipnum reach MAX_IPV6_RANGE if realipno == 340282366920938463463374607431768211455 ipnum = realipno - 1 end end - low = read32(indexpos) - high = read32(indexpos + 4) + low, high = read32x2(indexpos) return self.record = bsearch(low, high, ipnum, self.base_addr, col_length) else return self.record = bsearch(0, self.count, ipnum, self.base_addr, col_length) end end @@ -417,15 +416,16 @@ end end end def get_from_to(mid, base_addr, col_length) - from_base = ( base_addr + mid * (col_length + (v4 ? 0 : 12))) + from_base = (base_addr + mid * (col_length + (v4 ? 0 : 12))) + data_length = col_length + (v4 ? 4 : (12 + 16)) file.seek(from_base) - ip_from = v4 ? file.read(4).unpack('V').first : readipv6(file) - file.seek(from_base + col_length + (v4 ? 0 : 12)) - ip_to = v4 ? file.read(4).unpack('V').first : readipv6(file) + data_read = file.read(data_length) + ip_from = v4 ? data_read[0..3].unpack('V').first : readipv6(data_read[0..15].unpack('V*')) + 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*')) [ip_from, ip_to] end def validateip(ip) if ip.ipv4? @@ -457,20 +457,22 @@ end end [ipv, ipnum] end - def read32(indexp) + def read32x2(indexp) file.seek(indexp - 1) - return file.read(4).unpack('V').first + data_read = file.read(8) + data1 = data_read[0..3].unpack('V').first + data2 = data_read[4..7].unpack('V').first + return [data1, data2] end - def readipv6(filer) - parts = filer.read(16).unpack('V*') + def readipv6(parts) return parts[0] + parts[1] * 4294967296 + parts[2] * 4294967296**2 + parts[3] * 4294967296**3 end - private :get_record, :bsearch, :get_from_to, :read32, :readipv6 + private :get_record, :bsearch, :get_from_to, :read32x2, :readipv6 end class Ip2proxyWebService attr_accessor :ws_api_key, :ws_package, :ws_use_ssl \ No newline at end of file