Sha256: 343fff568524179cdbc4e1eeb7b149b03ec40097256c3c2fb02edf3093dc7adb

Contents?: true

Size: 1.9 KB

Versions: 8

Compression:

Stored size: 1.9 KB

Contents

require 'geoip'

module Dap
module Filter

module GeoIPLibrary
  GEOIP_DIRS = [ 
    File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "..", "data")),
    "/var/lib/geoip"
  ]
  GEOIP_CITY = %W{ geoip.dat geoip_city.dat GeoCity.dat IP_V4_CITY.dat GeoCityLite.dat }
  GEOIP_ORGS = %W{ geoip_org.dat IP_V4_ORG.dat }
  GEOIP_ASN = %W{ GeoIPASNum.dat }

  @@geo_city = nil
  @@geo_orgs = nil
  @@geo_asn = nil

  GEOIP_DIRS.each do |d|
    GEOIP_CITY.each do |f|
      path = File.join(d, f)
      if ::File.exist?(path)
        @@geo_city = GeoIP::City.new(path)
        break
      end
    end
    GEOIP_ORGS.each do |f|
      path = File.join(d, f)
      if ::File.exist?( path )
        @@geo_orgs = GeoIP::Organization.new(path)
        break
      end
    end
    GEOIP_ASN.each do |f|
      path = File.join(d, f)
      if ::File.exist?(path)
        @@geo_asn = GeoIP::Organization.new(path)
        break
      end
    end
  end  
end


#
# Add GeoIP tags using the MaxMind GeoIP::City 
#
class FilterGeoIP
  include BaseDecoder
  include GeoIPLibrary
  def decode(ip)
    return unless @@geo_city
    geo_hash = @@geo_city.look_up(ip)
    return unless geo_hash
    ret = {}
    geo_hash.each_pair do |k,v|
      next unless k
      ret[k.to_s] = v.to_s
    end
    
    ret
  end
end

#
# Add GeoIP tags using the MaxMind GeoIP::Organization database
#
class FilterGeoIPOrg
  include BaseDecoder
  include GeoIPLibrary
  def decode(ip)
    return unless @@geo_orgs
    geo_hash = @@geo_orgs.look_up(ip)
    return unless (geo_hash and geo_hash[:name])
    { :org => geo_hash[:name] }
  end
end

#
# Add GeoIP ASN tags using the MaxMind GeoIP::ASN database
# 
class FilterGeoIPAsn
  include BaseDecoder
  include GeoIPLibrary
  def decode(ip)
    return unless @@geo_asn
    geo_hash = @@geo_asn.look_up(ip)
    return unless (geo_hash and geo_hash[:name])
    { :asn => geo_hash[:name].split(' ')[0] }
  end
end

end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
dap-0.1.14 lib/dap/filter/geoip.rb
dap-0.1.13 lib/dap/filter/geoip.rb
dap-0.1.12 lib/dap/filter/geoip.rb
dap-0.1.11 lib/dap/filter/geoip.rb
dap-0.1.10 lib/dap/filter/geoip.rb
dap-0.1.9 lib/dap/filter/geoip.rb
dap-0.1.8 lib/dap/filter/geoip.rb
dap-0.1.7 lib/dap/filter/geoip.rb