Class: RemoteHost

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
FastTimestamp
Defined in:
lib/honeypot/remote_host.rb

Instance Method Summary collapse

Instance Method Details

#delayed_geolocateObject



53
54
55
56
57
58
59
60
61
# File 'lib/honeypot/remote_host.rb', line 53

def delayed_geolocate
  if (rand(20) == 1 or latitude.blank?) and (
       !timestamped?(:failed_to_geolocate) or
       timestamp_for(:failed_to_geolocate) < 5.days.ago
     )
    defined?(Delayed::Job) ? send_later(:geolocate) : geolocate
  end
  true
end

#delayed_lookup_hostnameObject



43
44
45
46
47
48
49
50
51
# File 'lib/honeypot/remote_host.rb', line 43

def delayed_lookup_hostname
  if (rand(20) == 1 or hostname.blank?) and (
       !timestamped?(:failed_to_lookup_hostname) or
       timestamp_for(:failed_to_lookup_hostname) < 5.days.ago
     )
    defined?(Delayed::Job) ? send_later(:lookup_hostname) : lookup_hostname
  end
  true
end

#geolocateObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/honeypot/remote_host.rb', line 18

def geolocate
  location = Geokit::Geocoders::MultiGeocoder.geocode ip_address
  
  # take what we can get
  if location.success
    self.latitude = location.lat if location.lat.present?
    self.longitude = location.lng if location.lng.present?
    self.country_code = location.country_code if location.country_code.present?
    # state -> state_name
    self.state_name = location.state if location.state.present?
    self.city = location.city if location.city.present?
    save!
  end
  
  # but only call it a success if we get latitude
  # this way if we don't, it will check again in 5 days
  if location.success and location.latitude.present?
    untimestamp! :failed_to_geolocate
  else
    timestamp! :failed_to_geolocate
  end
rescue
  timestamp! :failed_to_geolocate
end

#lookup_hostnameObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/honeypot/remote_host.rb', line 6

def lookup_hostname
  result = Resolv.getname ip_address
  if result.present?
    update_attribute :hostname, result
    untimestamp! :failed_to_lookup_hostname
  else
    timestamp! :failed_to_lookup_hostname
  end
rescue # Resolv::ResolvError
  timestamp! :failed_to_lookup_hostname
end