Class: DiscourseIpInfo
- Inherits:
-
Object
- Object
- DiscourseIpInfo
- Includes:
- Singleton
- Defined in:
- lib/discourse_ip_info.rb
Class Method Summary collapse
- .get(ip, locale: :en, resolve_hostname: false) ⇒ Object
- .mmdb_download(name) ⇒ Object
- .mmdb_path(name) ⇒ Object
- .open_db(path) ⇒ Object
- .path ⇒ Object
Instance Method Summary collapse
- #get(ip, locale: :en, resolve_hostname: false) ⇒ Object
-
#initialize ⇒ DiscourseIpInfo
constructor
A new instance of DiscourseIpInfo.
- #lookup(ip, locale: :en, resolve_hostname: false) ⇒ Object
- #mmdb_load(filepath) ⇒ Object
- #open_db(path) ⇒ Object
Constructor Details
#initialize ⇒ DiscourseIpInfo
Returns a new instance of DiscourseIpInfo.
9 10 11 |
# File 'lib/discourse_ip_info.rb', line 9 def initialize open_db(DiscourseIpInfo.path) end |
Class Method Details
.get(ip, locale: :en, resolve_hostname: false) ⇒ Object
174 175 176 |
# File 'lib/discourse_ip_info.rb', line 174 def self.get(ip, locale: :en, resolve_hostname: false) instance.get(ip, locale: locale, resolve_hostname: resolve_hostname) end |
.mmdb_download(name) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/discourse_ip_info.rb', line 27 def self.mmdb_download(name) extra_headers = {} url = if GlobalSetting.maxmind_mirror_url.present? File.join(GlobalSetting.maxmind_mirror_url, "#{name}.tar.gz").to_s else license_key = GlobalSetting.maxmind_license_key if license_key.blank? STDERR.puts "MaxMind IP database download requires an account ID and a license key" STDERR.puts "Please set DISCOURSE_MAXMIND_ACCOUNT_ID and DISCOURSE_MAXMIND_LICENSE_KEY. See https://meta.discourse.org/t/configure-maxmind-for-reverse-ip-lookups/173941 for more details." return end account_id = GlobalSetting.maxmind_account_id if account_id.present? extra_headers[ "Authorization" ] = "Basic #{Base64.strict_encode64("#{account_id}:#{license_key}")}" "https://download.maxmind.com/geoip/databases/#{name}/download?suffix=tar.gz" else # This URL is not documented by MaxMind, but it works but we don't know when it will stop working. Therefore, # we are deprecating this in 3.3 and will remove it in 3.4. An admin dashboard warning has been added to inform # site admins about this deprecation. See `ProblemCheck::MaxmindDbConfiguration` for more information. "https://download.maxmind.com/app/geoip_download?license_key=#{license_key}&edition_id=#{name}&suffix=tar.gz" end end gz_file = FileHelper.download( url, max_file_size: 100.megabytes, tmp_file_name: "#{name}.gz", validate_uri: false, follow_redirect: true, extra_headers:, ) filename = File.basename(gz_file.path) dir = "#{Dir.tmpdir}/#{SecureRandom.hex}" Discourse::Utils.execute_command("mkdir", "-p", dir) Discourse::Utils.execute_command("cp", gz_file.path, "#{dir}/#{filename}") Discourse::Utils.execute_command("tar", "-xzvf", "#{dir}/#{filename}", chdir: dir) Dir["#{dir}/**/*.mmdb"].each { |f| FileUtils.mv(f, mmdb_path(name)) } rescue => e Discourse.warn_exception(e, message: "MaxMind database #{name} download failed.") ensure FileUtils.rm_r(dir, force: true) if dir gz_file&.close! end |
.mmdb_path(name) ⇒ Object
23 24 25 |
# File 'lib/discourse_ip_info.rb', line 23 def self.mmdb_path(name) File.join(path, "#{name}.mmdb") end |
.open_db(path) ⇒ Object
170 171 172 |
# File 'lib/discourse_ip_info.rb', line 170 def self.open_db(path) instance.open_db(path) end |
.path ⇒ Object
19 20 21 |
# File 'lib/discourse_ip_info.rb', line 19 def self.path @path ||= File.join(Rails.root, "vendor", "data") end |
Instance Method Details
#get(ip, locale: :en, resolve_hostname: false) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/discourse_ip_info.rb', line 159 def get(ip, locale: :en, resolve_hostname: false) ip = ip.to_s locale = locale.to_s.sub("_", "-") @cache["#{ip}-#{locale}-#{resolve_hostname}"] ||= lookup( ip, locale: locale, resolve_hostname: resolve_hostname, ) end |
#lookup(ip, locale: :en, resolve_hostname: false) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/discourse_ip_info.rb', line 96 def lookup(ip, locale: :en, resolve_hostname: false) ret = {} return ret if ip.blank? 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 ret[:location] = ret.values_at(:city, :region, :country).reject(&:blank?).uniq.join(", ") # used by plugins or API to locate users more accurately ret[:geoname_ids] = [ result.continent.geoname_id, result.country.geoname_id, result.city.geoname_id, *result.subdivisions.map(&:geoname_id), ] ret[:geoname_ids].compact! end rescue => e Discourse.warn_exception( e, message: "IP #{ip} could not be looked up in MaxMind GeoLite2-City database.", ) end end 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 rescue => e Discourse.warn_exception( e, message: "IP #{ip} could not be looked up in MaxMind GeoLite2-ASN database.", ) end end # 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 end ret end |
#mmdb_load(filepath) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/discourse_ip_info.rb', line 84 def mmdb_load(filepath) begin MaxMindDB.new(filepath, MaxMindDB::LOW_MEMORY_FILE_READER) rescue Errno::ENOENT => e Rails.logger.warn("MaxMindDB (#{filepath}) could not be found: #{e}") nil rescue => e Discourse.warn_exception(e, message: "MaxMindDB (#{filepath}) could not be loaded.") nil end end |
#open_db(path) ⇒ Object
13 14 15 16 17 |
# File 'lib/discourse_ip_info.rb', line 13 def open_db(path) @loc_mmdb = mmdb_load(File.join(path, "GeoLite2-City.mmdb")) @asn_mmdb = mmdb_load(File.join(path, "GeoLite2-ASN.mmdb")) @cache = LruRedux::ThreadSafeCache.new(2000) end |