Moved to own geo ip service per default.

This commit is contained in:
Martin Edenhofer 2015-03-22 11:46:05 +01:00
parent 54b08ad334
commit 810391d8b7
5 changed files with 90 additions and 33 deletions

View file

@ -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

View file

@ -214,12 +214,12 @@ Setting.create_if_not_exists(
:tag => 'select', :tag => 'select',
:options => { :options => {
'' => '-', '' => '-',
'GeoIp::Freegeoip' => 'freegeoip.net', 'GeoIp::ZammadGeoIp' => 'Zammad GeoIP Service',
}, },
}, },
], ],
}, },
:state => 'GeoIp::Freegeoip', :state => 'GeoIp::ZammadGeoIp',
:frontend => false :frontend => false
) )

View file

@ -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

View file

@ -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

View file

@ -17,7 +17,24 @@ http/https/ftp calls
result = UserAgent.request( 'https://host/some_dir/some_file.bin' ) 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 returns
@ -43,8 +60,8 @@ returns
http = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 8 http.open_timeout = options[:open_timeout] || 8
http.read_timeout = 8 http.read_timeout = options[:read_timeout] || 8
if uri.scheme =~ /https/i if uri.scheme =~ /https/i
http.use_ssl = true http.use_ssl = true