From 2b19744683f11253cc0646ce73529c754eac4ab7 Mon Sep 17 00:00:00 2001 From: Jonathan Kolyer Date: Sun, 3 Nov 2013 02:45:54 -0600 Subject: [PATCH] vrfs-774: replaced original max_geo_isp db cols --- web/lib/max_mind_manager.rb | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/web/lib/max_mind_manager.rb b/web/lib/max_mind_manager.rb index 422413f23..0bf48ee4d 100644 --- a/web/lib/max_mind_manager.rb +++ b/web/lib/max_mind_manager.rb @@ -14,6 +14,7 @@ class MaxMindManager < BaseManager unless ip_address.nil? || ip_address !~ /^\d+\.\d+\.\d+\.\d+$/ ActiveRecord::Base.connection_pool.with_connection do |connection| pg_conn = connection.instance_variable_get("@connection") + ip_as_int = ip_address_to_int(ip_address) pg_conn.exec("SELECT country, region, city FROM max_mind_geo WHERE ip_start <= $1 AND ip_end >= $2", [ip_address, ip_address]) do |result| if !result.nil? && result.ntuples > 0 country = result.getvalue(0, 0) @@ -39,7 +40,8 @@ class MaxMindManager < BaseManager unless ip_address.nil? || ip_address !~ /^\d+\.\d+\.\d+\.\d+$/ ActiveRecord::Base.connection_pool.with_connection do |connection| pg_conn = connection.instance_variable_get("@connection") - pg_conn.exec("SELECT isp FROM max_mind_isp WHERE ip_star <= $1 AND ip_end >= $2", [ip_address, ip_address]) do |result| + ip_as_int = ip_address_to_int(ip_address) + pg_conn.exec("SELECT isp FROM max_mind_isp WHERE ip_bottom <= $1 AND ip_top >= $2", [ip_as_int, ip_as_int]) do |result| if !result.nil? && result.ntuples > 0 isp = result.getvalue(0, 0) end @@ -105,10 +107,10 @@ class MaxMindManager < BaseManager clear_isp_table (0..255).each do |top_octet| - @pg_conn.exec("INSERT INTO max_mind_isp (ip_start, ip_end, isp, country) VALUES ($1, $2, $3, $4)", + @pg_conn.exec("INSERT INTO max_mind_isp (ip_bottom, ip_top, isp, country) VALUES ($1, $2, $3, $4)", [ - "#{top_octet}.0.0.0", - "#{top_octet}.255.255.255", + self.class.ip_address_to_int("#{top_octet}.0.0.0"), + self.class.ip_address_to_int("#{top_octet}.255.255.255"), "ISP #{top_octet}", "US" ]).clear @@ -118,6 +120,13 @@ class MaxMindManager < BaseManager end private + + # Make an IP address fit in a signed int. Just divide it by 2, as the least significant part + # just can't possibly matter. We can verify this if needed. My guess is the entire bottom octet is + # actually irrelevant + def self.ip_address_to_int(ip) + ip.split('.').inject(0) {|total,value| (total << 8 ) + value.to_i} / 2 + end def clear_location_table @pg_conn.exec("DELETE FROM max_mind_geo").clear