From e0df631848fde2b8800824c9da5844b629b5a648 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 1 Jan 2020 18:50:48 +0000 Subject: [PATCH] Switch GeoIP code to use GeoIPv2 databases --- Gemfile | 4 ++-- Gemfile.lock | 4 ++-- config/settings.yml | 4 ++-- lib/osm.rb | 12 ++++++------ 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Gemfile b/Gemfile index 60c155459..9a3fb1f6b 100644 --- a/Gemfile +++ b/Gemfile @@ -93,8 +93,8 @@ gem "SystemTimer", ">= 1.1.3", :require => "system_timer", :platforms => :ruby_1 # Load faraday for mockable HTTP client gem "faraday" -# Load geoip for querying Maxmind GeoIP database -gem "geoip" +# Load maxminddb for querying Maxmind GeoIP database +gem "maxminddb" # Load rotp to generate TOTP tokens gem "rotp" diff --git a/Gemfile.lock b/Gemfile.lock index e27ffda72..a0d50c15a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -208,7 +208,6 @@ GEM fspath (3.1.2) gd2-ffij (0.4.0) ffi (>= 1.0.0) - geoip (1.6.4) globalid (0.4.2) activesupport (>= 4.2.0) hashdiff (1.0.0) @@ -260,6 +259,7 @@ GEM mini_mime (>= 0.1.1) marcel (0.3.3) mimemagic (~> 0.3.2) + maxminddb (0.1.22) method_source (0.9.2) mimemagic (0.3.3) mini_magick (4.9.5) @@ -489,7 +489,6 @@ DEPENDENCIES faraday ffi-libarchive gd2-ffij (>= 0.4.0) - geoip htmlentities http_accept_language (~> 2.0.0) i18n-js (>= 3.0.0) @@ -502,6 +501,7 @@ DEPENDENCIES libxml-ruby (>= 2.0.5) listen logstasher + maxminddb mimemagic mini_magick minitest (~> 5.1) diff --git a/config/settings.yml b/config/settings.yml index 0f64382c9..3aa1d96c7 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -49,8 +49,8 @@ max_messages_per_hour: 60 #messages_domain: "messages.openstreetmap.org" # Geonames authentication details #geonames_username: "" -# GeoIP database -#geoip_database: "" +# MaxMind GeoIPv2 database +#maxmind_database: "" # Users to show as being nearby nearby_users: 30 # Max radius, in km, for nearby users diff --git a/lib/osm.rb b/lib/osm.rb index 743d1b1c6..bc3eb48c5 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -511,10 +511,10 @@ module OSM end def self.ip_to_country(ip_address) - ipinfo = geoip_database.country(ip_address) if Settings.key?(:geoip_database) + ipinfo = maxmind_database.lookup(ip_address) if Settings.key?(:maxmind_database) - if ipinfo - country = ipinfo.country_code2 + if ipinfo.found? + country = ipinfo.country.iso_code else country = http_client.get("https://api.hostip.info/country.php?ip=#{ip_address}").body country = "GB" if country == "UK" @@ -575,8 +575,8 @@ module OSM @http_client ||= Faraday.new end - # Return the GeoIP database handle - def self.geoip_database - @geoip_database ||= GeoIP.new(Settings.geoip_database) if Settings.key?(:geoip_database) + # Return the MaxMindDB database handle + def self.maxmind_database + @maxmind_database ||= MaxMindDB.new(Settings.maxmind_database) if Settings.key?(:maxmind_database) end end -- 2.45.2