diff --git a/db/migrate/20150322000001_update_geo_ip_config.rb b/db/migrate/20150322000001_update_geo_ip_config.rb new file mode 100644 index 000000000..0a036d2b7 --- /dev/null +++ b/db/migrate/20150322000001_update_geo_ip_config.rb @@ -0,0 +1,30 @@ +class UpdateGeoIpConfig < ActiveRecord::Migration + def up + Setting.create_or_update( + :title => 'Geo IP Backend', + :name => 'geo_ip_backend', + :area => 'System::Geo', + :description => 'Defines the backend for geo ip lookups.', + :options => { + :form => [ + { + :display => '', + :null => true, + :name => 'geo_ip_backend', + :tag => 'select', + :options => { + '' => '-', + 'GeoIp::ZammadGeoIp' => 'Zammad GeoIP Service', + }, + }, + ], + }, + :state => 'GeoIp::ZammadGeoIp', + :frontend => false + ) + end + + def down + end + +end \ No newline at end of file diff --git a/db/seeds.rb b/db/seeds.rb index d3ec2c25b..82d72ddf4 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -214,12 +214,12 @@ Setting.create_if_not_exists( :tag => 'select', :options => { '' => '-', - 'GeoIp::Freegeoip' => 'freegeoip.net', + 'GeoIp::ZammadGeoIp' => 'Zammad GeoIP Service', }, }, ], }, - :state => 'GeoIp::Freegeoip', + :state => 'GeoIp::ZammadGeoIp', :frontend => false ) diff --git a/lib/geo_ip/freegeoip.rb b/lib/geo_ip/freegeoip.rb deleted file mode 100644 index d3158734e..000000000 --- a/lib/geo_ip/freegeoip.rb +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ - -require 'faraday' -require 'cache' - -class GeoIp::Freegeoip - def self.location(address) - - # check cache - cache_key = "freegeoip::#{address}" - cache = Cache.get( cache_key ) - return cache if cache - - # do lookup - host = "http://freegeoip.net" - url = "/json/#{CGI::escape address}" - data = {} - begin - conn = Faraday.new( :url => host, :options => { :open_timeout => 2, :timeout => 5 } ) - response = conn.get url - data = JSON.parse( response.body ) - Cache.write( cache_key, data, { :expires_in => 90.days } ) - rescue - Cache.write( cache_key, data, { :expires_in => 60.minutes } ) - end - data - end -end diff --git a/lib/geo_ip/zammad_geo_ip.rb b/lib/geo_ip/zammad_geo_ip.rb new file mode 100644 index 000000000..ad85c5244 --- /dev/null +++ b/lib/geo_ip/zammad_geo_ip.rb @@ -0,0 +1,38 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +require 'cache' + +class GeoIp::ZammadGeoIp + def self.location(address) + + # check cache + cache_key = "zammadgeoip::#{address}" + cache = Cache.get( cache_key ) + return cache if cache + + # do lookup + host = "http://geo.zammad.com" + url = "/lookup?ip=#{CGI::escape address}" + data = {} + begin + response = UserAgent.request( + "#{host}#{url}", + { + :method => 'get', + :open_timeout => 2, + :read_timeout => 4, + }, + ) + if !response.success? && response.code.to_s !~ /^40.$/ + raise "ERROR: #{response.code.to_s}/#{response.body}" + end + + data = JSON.parse( response.body ) + Cache.write( cache_key, data, { :expires_in => 90.days } ) + rescue => e + puts "ERROR: #{host}#{url}: " + e.inspect + Cache.write( cache_key, data, { :expires_in => 60.minutes } ) + end + data + end +end \ No newline at end of file diff --git a/lib/user_agent.rb b/lib/user_agent.rb index 82404e4ce..d9598a6cc 100644 --- a/lib/user_agent.rb +++ b/lib/user_agent.rb @@ -17,7 +17,24 @@ http/https/ftp calls result = UserAgent.request( 'https://host/some_dir/some_file.bin' ) - result = UserAgent.request( 'http://host/some_dir/some_file.bin', { :method => 'post', :data => { :param1 => 123 } } ) + # post request + result = UserAgent.request( + 'http://host/some_dir/some_file.bin', + { + :method => 'post', + :data => { :param1 => 123 }, + }, + ) + + # get request + result = UserAgent.request( + 'http://host/some_dir/some_file?param1=123', + { + :method => 'get', + :open_timeout => 2, + :read_timeout => 4, + }, + ) returns @@ -43,8 +60,8 @@ returns http = Net::HTTP.new(uri.host, uri.port) - http.open_timeout = 8 - http.read_timeout = 8 + http.open_timeout = options[:open_timeout] || 8 + http.read_timeout = options[:read_timeout] || 8 if uri.scheme =~ /https/i http.use_ssl = true