2018-11-18 23:50:21 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-01 18:16:45 -04:00
|
|
|
require 'maxminddb'
|
|
|
|
require 'resolv'
|
2018-10-09 10:21:41 -04:00
|
|
|
|
|
|
|
class DiscourseIpInfo
|
|
|
|
include Singleton
|
|
|
|
|
|
|
|
def initialize
|
2018-10-25 06:54:01 -04:00
|
|
|
open_db(File.join(Rails.root, 'vendor', 'data'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_db(path)
|
2018-10-30 18:08:57 -04:00
|
|
|
@loc_mmdb = mmdb_load(File.join(path, 'GeoLite2-City.mmdb'))
|
|
|
|
@asn_mmdb = mmdb_load(File.join(path, 'GeoLite2-ASN.mmdb'))
|
2018-10-30 21:38:57 -04:00
|
|
|
@cache = LruRedux::ThreadSafeCache.new(2000)
|
2018-10-30 18:08:57 -04:00
|
|
|
end
|
|
|
|
|
2019-04-10 05:37:29 -04:00
|
|
|
def self.mmdb_path(name)
|
|
|
|
File.join(Rails.root, 'vendor', 'data', "#{name}.mmdb")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.mmdb_download(name)
|
|
|
|
require 'rubygems/package'
|
|
|
|
require 'zlib'
|
|
|
|
|
|
|
|
uri = URI("https://geolite.maxmind.com/download/geoip/database/#{name}.tar.gz")
|
|
|
|
|
|
|
|
begin
|
2019-04-10 06:37:04 -04:00
|
|
|
tar_gz_file = Tempfile.new
|
2019-04-10 05:37:29 -04:00
|
|
|
tar_gz_file.binmode
|
|
|
|
tar_gz_file.write(Net::HTTP.get(uri))
|
|
|
|
tar_gz_file.close
|
|
|
|
|
2019-04-10 06:37:04 -04:00
|
|
|
begin
|
|
|
|
extractor = Gem::Package::TarReader.new(Zlib::GzipReader.open(tar_gz_file.path))
|
|
|
|
extractor.rewind
|
2019-04-10 05:37:29 -04:00
|
|
|
|
2019-04-10 06:37:04 -04:00
|
|
|
extractor.each do |entry|
|
|
|
|
next unless entry.full_name.ends_with?(".mmdb")
|
|
|
|
File.open(mmdb_path(name), "wb") { |f| f.write(entry.read) }
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
extractor.close
|
2019-04-10 05:37:29 -04:00
|
|
|
end
|
2019-04-10 06:37:04 -04:00
|
|
|
|
2019-04-10 05:37:29 -04:00
|
|
|
ensure
|
|
|
|
tar_gz_file.close
|
|
|
|
tar_gz_file.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-30 18:08:57 -04:00
|
|
|
def mmdb_load(filepath)
|
2018-10-09 10:21:41 -04:00
|
|
|
begin
|
2018-10-30 18:08:57 -04:00
|
|
|
MaxMindDB.new(filepath, MaxMindDB::LOW_MEMORY_FILE_READER)
|
2018-10-09 10:21:41 -04:00
|
|
|
rescue Errno::ENOENT => e
|
2018-10-30 21:57:18 -04:00
|
|
|
Rails.logger.warn("MaxMindDB (#{filepath}) could not be found: #{e}")
|
|
|
|
nil
|
|
|
|
rescue => e
|
|
|
|
Discourse.warn_exception(e, "MaxMindDB (#{filepath}) could not be loaded.")
|
|
|
|
nil
|
2018-10-09 10:21:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-30 21:38:57 -04:00
|
|
|
def lookup(ip, locale: :en, resolve_hostname: false)
|
2018-10-30 18:08:57 -04:00
|
|
|
ret = {}
|
2018-11-18 23:50:21 -05:00
|
|
|
return ret if ip.blank?
|
2018-10-09 10:21:41 -04:00
|
|
|
|
2018-10-30 18:08:57 -04:00
|
|
|
if @loc_mmdb
|
|
|
|
begin
|
|
|
|
result = @loc_mmdb.lookup(ip)
|
|
|
|
if result&.found?
|
|
|
|
ret[:country] = result.country.name(locale) || result.country.name
|
|
|
|
ret[:country_code] = result.country.iso_code
|
|
|
|
ret[:region] = result.subdivisions.most_specific.name(locale) || result.subdivisions.most_specific.name
|
|
|
|
ret[:city] = result.city.name(locale) || result.city.name
|
|
|
|
ret[:latitude] = result.location.latitude
|
|
|
|
ret[:longitude] = result.location.longitude
|
2018-12-14 07:47:59 -05:00
|
|
|
ret[:location] = ret.values_at(:city, :region, :country).reject(&:blank?).uniq.join(", ")
|
2018-10-30 18:08:57 -04:00
|
|
|
end
|
2018-10-30 21:57:18 -04:00
|
|
|
rescue => e
|
2018-10-30 22:37:54 -04:00
|
|
|
Discourse.warn_exception(e, message: "IP #{ip} could not be looked up in MaxMind GeoLite2-City database.")
|
2018-10-30 18:08:57 -04:00
|
|
|
end
|
2018-10-09 10:21:41 -04:00
|
|
|
end
|
|
|
|
|
2018-10-30 18:08:57 -04:00
|
|
|
if @asn_mmdb
|
|
|
|
begin
|
|
|
|
result = @asn_mmdb.lookup(ip)
|
|
|
|
if result&.found?
|
|
|
|
result = result.to_hash
|
|
|
|
ret[:asn] = result["autonomous_system_number"]
|
|
|
|
ret[:organization] = result["autonomous_system_organization"]
|
|
|
|
end
|
2018-10-30 21:57:18 -04:00
|
|
|
rescue => e
|
2018-10-30 22:37:54 -04:00
|
|
|
Discourse.warn_exception(e, message: "IP #{ip} could not be looked up in MaxMind GeoLite2-ASN database.")
|
2018-10-30 18:08:57 -04:00
|
|
|
end
|
|
|
|
end
|
2018-10-09 10:21:41 -04:00
|
|
|
|
2018-10-30 21:38:57 -04:00
|
|
|
# this can block for quite a while
|
|
|
|
# only use it explicitly when needed
|
|
|
|
if resolve_hostname
|
|
|
|
begin
|
|
|
|
result = Resolv::DNS.new.getname(ip)
|
|
|
|
ret[:hostname] = result&.to_s
|
|
|
|
rescue Resolv::ResolvError
|
|
|
|
end
|
2018-10-30 18:08:57 -04:00
|
|
|
end
|
2018-10-25 06:54:01 -04:00
|
|
|
|
2018-10-30 18:08:57 -04:00
|
|
|
ret
|
2018-10-09 10:21:41 -04:00
|
|
|
end
|
|
|
|
|
2018-10-30 21:38:57 -04:00
|
|
|
def get(ip, locale: :en, resolve_hostname: false)
|
2018-10-25 05:45:31 -04:00
|
|
|
ip = ip.to_s
|
2018-10-30 18:08:57 -04:00
|
|
|
locale = locale.to_s.sub('_', '-')
|
|
|
|
|
2018-10-30 21:38:57 -04:00
|
|
|
@cache["#{ip}-#{locale}-#{resolve_hostname}"] ||=
|
|
|
|
lookup(ip, locale: locale, resolve_hostname: resolve_hostname)
|
2018-10-25 06:54:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.open_db(path)
|
|
|
|
instance.open_db(path)
|
2018-10-09 10:21:41 -04:00
|
|
|
end
|
|
|
|
|
2018-10-30 21:38:57 -04:00
|
|
|
def self.get(ip, locale: :en, resolve_hostname: false)
|
|
|
|
instance.get(ip, locale: locale, resolve_hostname: resolve_hostname)
|
2018-10-09 10:21:41 -04:00
|
|
|
end
|
|
|
|
end
|