FIX: use tar -xzvf to extract maxmind vs Ruby wrappers

The tar/gz extractor in Ruby is somewhat buggy, instead rely on battle
tested `tar` and `gzip` Linux commands
This commit is contained in:
Sam Saffron 2019-05-21 10:48:18 +10:00
parent edc6ac0c66
commit 0f76e3090c

View File

@ -7,7 +7,7 @@ class DiscourseIpInfo
include Singleton
def initialize
open_db(File.join(Rails.root, 'vendor', 'data'))
open_db(DiscourseIpInfo.path)
end
def open_db(path)
@ -16,38 +16,43 @@ class DiscourseIpInfo
@cache = LruRedux::ThreadSafeCache.new(2000)
end
def self.path
@path ||= File.join(Rails.root, 'vendor', 'data')
end
def self.mmdb_path(name)
File.join(Rails.root, 'vendor', 'data', "#{name}.mmdb")
File.join(path, "#{name}.mmdb")
end
def self.mmdb_download(name)
require 'rubygems/package'
require 'zlib'
FileUtils.mkdir_p(path)
uri = URI("https://geolite.maxmind.com/download/geoip/database/#{name}.tar.gz")
begin
tar_gz_file = Tempfile.new
tar_gz_file.binmode
tar_gz_file.write(Net::HTTP.get(uri))
tar_gz_file.close
tar_gz_file = Tempfile.new
tar_gz_file.binmode
tar_gz_file.write(Net::HTTP.get(uri))
tar_gz_file.close
begin
extractor = Gem::Package::TarReader.new(Zlib::GzipReader.open(tar_gz_file.path))
extractor.rewind
dest = File.join(Dir.tmpdir, "maxmind_#{SecureRandom.hex}")
FileUtils.mkdir_p(dest)
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
Discourse::Utils.execute_command('tar', '-xzvf', tar_gz_file.path, "-C", dest)
Dir.glob("#{dest}/**/*.mmdb").each do |path|
if path.include?(name)
FileUtils.mv(path, mmdb_path(name))
else
Rails.logger.warn("Skipping unknown mmdb file during ip database update #{path}")
end
ensure
tar_gz_file.close
tar_gz_file.unlink
end
ensure
FileUtils.rm_rf(dest) if dest
FileUtils.rm(tar_gz_file) if tar_gz_file
end
def mmdb_load(filepath)