4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'app/models/fortifier/max_mind.rb', line 4
def self.valid_ip?(auth_log)
return true unless Rails.env=="production"
ip_address = auth_log[:remote_addr]
return true if local_ip?(ip_address)
return true if previously_validated_ip?(ip_address)
return true if auth_log.auth_user.try(:matching_whitelist_ip?,ip_address)
return false if previously_rejected_ip?(ip_address)
fields = [:country_code, :error]
options = { :license => "KqA72c39qtQs", :ip => ip_address }
uri = URI::HTTP.build(:scheme => 'http',
:host => 'geoip.maxmind.com',
:path => '/a',
:query => URI.encode_www_form(:l => options[:license],
:i => ip_address))
begin
response = Net::HTTP.get_response(uri)
raise Exception.new("MaxMind Request failed with status #{response.code}") unless response.is_a?(Net::HTTPSuccess)
info = Hash[fields.zip(response.body.encode('utf-8', 'iso-8859-1').parse_csv)]
country = info[:country_code]
raise Exception.new("MaxMind Request failed - IP not found: #{ip_address}") if info[:error]=="IP_NOT_FOUND"
if country=="CA" || country=="US" || country=="UM" || country=="PR" || country==nil
MaxMindReferenceIp.create(ip_address: ip_address, country: country, allowed: true)
return true
else
NotifierMailer.foreign_access(auth_log.auth_user, auth_log).deliver
MaxMindReferenceIp.create(ip_address: ip_address, country: country, allowed: false)
return false
end
rescue Exception=>e
NotifierMailer.task_exception(e).deliver
return true
end
end
|