Class: DoroParser::Geoinfo

Inherits:
Object
  • Object
show all
Defined in:
lib/dorothy2/DEM.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip) ⇒ Geoinfo

Returns a new instance of Geoinfo.


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/dorothy2/DEM.rb', line 112

def initialize(ip)
  @updated = 'null' #TODO take the creation file date of the .dat archive

  #year = geoip.database_info.grep(/(\S+) (\d{4})(\d{2})(\d{2})/){$2}
  #month = geoip.database_info.grep(/(\S+) (\d{4})(\d{2})(\d{2})/){$3}
  #day = geoip.database_info.grep(/(\S+) (\d{4})(\d{2})(\d{2})/){$4}
  #@updated = year.to_s + "/" + month.to_s + "/" + day.to_s
  localnetwork = IPAddr.new(DoroSettings.sandbox[:network])

  if !localnetwork.include?(ip)
    begin

      geoip = GeoIP.new(DoroSettings.env[:geoip])
      geoasn = GeoIP.new(DoroSettings.env[:geoasn])

      if geoip.country(ip)
        @city = geoip.country(ip).city_name.force_encoding('UTF-8').gsub(/"|'|\\/, "-") #xcode bug ->>> ")
        @country = geoip.country(ip).country_code2
        @coord = geoip.country(ip).latitude.to_s.gsub(/\(|\)/,'') + "," + geoip.country(ip).longitude.to_s.gsub(/\(|\)/,'')
      else
        @city, @country, @coord = "null", "null", "null"
      end

      @asn = (geoasn.asn(ip) ? geoasn.asn(ip).as_num[2..-1]	: "null" )

      @isp = "null"

      if File.exists?(DoroSettings.env[:geoisp])
        geoisp = GeoIP.new(DoroSettings.env[:geoisp])
        @isp = geoisp.isp(ip) ? geoisp.isp(ip) : "null"
      end

    rescue => e
      LOGGER_PARSER.fatal "GEO", "Error while fetching GeoIP dat file for IP: " + ip
      LOGGER_PARSER.fatal "GEO", e.inspect
      LOGGER_PARSER.fatal "GEO" ,e.backtrace
      @city, @country, @coord, @asn, @isp = "null", "null", "null", "null", "null"
    end

  else
    @city, @country, @coord, @asn, @isp = "null", "null", "null", "null", "null"

  end
end

Instance Attribute Details

#asnObject (readonly)

Returns the value of attribute asn


105
106
107
# File 'lib/dorothy2/DEM.rb', line 105

def asn
  @asn
end

#cityObject (readonly)

Returns the value of attribute city


107
108
109
# File 'lib/dorothy2/DEM.rb', line 107

def city
  @city
end

#coordObject (readonly)

Returns the value of attribute coord


104
105
106
# File 'lib/dorothy2/DEM.rb', line 104

def coord
  @coord
end

#countryObject (readonly)

Returns the value of attribute country


103
104
105
# File 'lib/dorothy2/DEM.rb', line 103

def country
  @country
end

#ispObject (readonly)

Returns the value of attribute isp


106
107
108
# File 'lib/dorothy2/DEM.rb', line 106

def isp
  @isp
end

#updatedObject (readonly)

Returns the value of attribute updated


102
103
104
# File 'lib/dorothy2/DEM.rb', line 102

def updated
  @updated
end