Giter Club home page Giter Club logo

geoip's Introduction

geoip

github.com/cjheath/geoip

DESCRIPTION:

GeoIP searches a GeoIP database for a given host or IP address, and returns information about the country where the IP address is allocated, and the city, ISP and other information, if you have that database version.

FEATURES/PROBLEMS:

Does not support the new Maxmind v2 database formats.

Includes support for ASN data files, thanks to Roland Matiz. This release adds support for timezone names, thanks to Tonni Aagesen.

If you have required ‘io/extra’ and have IO#pread, cross-process file-descriptor sharing is enabled. Each GeoIP instance keeps the file descriptor open, with a Mutex for thread-safety. You should consider this if your process will fork without exec, as modrails does

SYNOPSIS:

require 'geoip'

# Use the country database:
c = GeoIP.new('GeoIP.dat').country('www.nokia.com')
=> ["www.nokia.com", "147.243.3.83", 69, "FI", "FIN", "Finland", "EU"]
c.country_code3
=> "FIN"
c.to_hash
=> {:country_code3=>"FIN", :country_name=>"Finland", :continent_code=>"EU", :request=>"www.nokia.com", :country_code=>69, :country_code2=>"FI", :ip=>"147.243.3.83"}

Returned values are the requested hostname, the IP address as a dotted quad, Maxmind’s country code, the ISO3166-1 alpha-2 country code, the ISO3166-2 alpha-3 country code, the ISO3166 country name, and the continent code.

# Use the city database:
c = GeoIP.new('GeoLiteCity.dat').city('github.com')
=> ["github.com", "207.97.227.239", "US", "USA", "United States", "NA", "CA", "San Francisco", "94110", 37.7484, -122.4156, 807, 415, "America/Los_Angeles"]
>> c.longitude
=> -122.4156
>> c.timezone
=> "America/Los_Angeles"

GeoIP.new('GeoCity.dat').city('github.com')
=> ["github.com", "207.97.227.239", "US", "USA", "United States", "NA", "CA", "San Francisco", "94110", 37.7484, -122.4156, 807, 415, "America/Los_Angeles"]

# Use the city ipv6 database:
GeoIP.new('GeoLiteCityv6.dat').city('::151.38.39.114')
=> ["::151.38.39.114", "::151.38.39.114", "IT", "ITA", "Italy", "EU", "05", "Piacenza", "", 45.016699999999986, 9.666699999999992, nil, nil, "Europe/Rome"]

Returned values are the requested hostname, the IP address as a dotted quad, the ISO3166-1 alpha-2 country code, the ISO3166-2 alpha-3 country code, the ISO3166 country name, the continent code, the region (state or territory) name, city name, postal_code/zipcode, latitude, longitude, USA DMA code, USA area code, timezone name.

Result arrays from both city and country have mixed-in accessor methods as appropriate: request, ip, country_code, country_code2, country_code3, country_name, continent_code, region_name, city_name, postal_code, latitude, longitude, dma_code, area_code, timezone

GeoIP.new('GeoIPASNum.dat').asn("www.fsb.ru")
=> ["AS8342", "RTComm.RU Autonomous System"]

For testing or other purposes, you might wish to treat localhost IP addresses as some other address - an external one. You can do this by assigning the desired external IP address to the attribute local_ip_alias:

g = GeoIP.new('GeoIP.dat')
g.city("127.0.0.1")
=> nil
g.local_ip_alias = "173.194.112.35"
g.city("127.0.0.1") 
#<struct GeoIP::City request="127.0.0.1", ip="173.194.112.35", country_code2="US", country_code3="USA", country_name="United States", continent_code="NA", region_name="CA", city_name="Mountain View", postal_code="94043", latitude=37.41919999999999, longitude=-122.0574, dma_code=807, area_code=650, timezone="America/Los_Angeles", real_region_name="California">

REQUIREMENTS:

You need one of the free GeoLite country, city or ASN binary databases, or a subscription database version. Maxmind have unfortunately stopped updating databases in the original GeoLite format - only v2 is currently available, and the CSV form. There is no available compiler from CSV to the legacy database format, so this gem has started to go stale and needs a path forwards. Any volunteers? The download location for the GeoLite2 databases is <dev.maxmind.com/geoip/geoip2/geolite2/>.

This API requires a gzipped binary file to be decompressed for searching. Other versions of this database are available for purchase which contain more detailed information, but not all this information is returned by this implementation. See www.maxmind.com for more information.

INSTALL:

sudo gem install geoip

LICENSE:

This version Copyright © 2005 Clifford Heath Derived from the C version, Copyright © 2003 MaxMind LLC

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This follows the license applied by Maxmind to their C library, for example in the version here: <www.maxmind.com/download/geoip/api/c/GeoIP-1.4.2.tar.gz>.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

geoip's People

Contributors

2called-chaos avatar agerauer avatar amatsuda avatar bernd avatar blakemesdag avatar breunigs avatar bwalding avatar byroot avatar cjheath avatar disaacs avatar dubek avatar dv avatar ghazel avatar ktdreyer avatar leemhenson avatar markus avatar mseifert avatar postmodern avatar rakoth avatar reiz avatar rmoriz avatar secretfader avatar shovtyuk avatar wmspanel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

geoip's Issues

GeoIp.dat on heroku

Hello,

where do I have to save this file and how to access it to get it work on Heroku. On my local machine I can access it, but on Heroku I have tried to put it in lib/assets, public, in the root but I always get that there is not such file.

Thanks

Missing Hash Outputs for different lookups

The synopsis gives pretty good detail about the Hash values provided as a response object but does not give key names. Would be really awesome to see a little more documentation in the README regarding this.

NoMethodError: undefined method `bytes' for "ved":String

Hi,

on both win and debian systems and two GeoIP.dat's I get this error:
geo = GeoIP.new("/usr/share/GeoIP/GeoIP.dat")
NoMethodError: undefined method bytes' for "ved":String from /usr/lib/ruby/gems/1.8/gems/geoip-0.8.9/lib/geoip.rb:672:ininitialize'

Any ideas or hints?

Thanks and regards, Ph.

GeoIP#asn always returns nil

I'm using the example GeoIPASNum.dat DB from the README, but cannot get any ASN information from it for any IP or HostName. It seems the regepx at the end of GeoIP#asn isn't matching.

History.rdoc file not updated

there appear to be a 1.4 release, but the History.rdoc file is only up to date to 1.2, so it's hard to know if I can safely upgrade or not.

Doesn't work with ruby 1.9

I was able to get this working by just changing every instance of 'getc' with 'getc.ord', but then of course it won't work with 1.8 any more (needs a detection).

I also had to make another small change but can't find it any more :/

NameError: uninitialized constant GeoIp

I'd love to use geoip, but I'm doing something terribly wrong, I guess. Here's my irb printout, leading up to the error.

gerard@ubuntu:/Documents/geoBrainz/src$ which ruby
/home/gerard/.rvm/rubies/ruby-2.0.0-p353/bin/ruby
gerard@ubuntu:
/Documents/geoBrainz/src$ which geoip
/home/gerard/.rvm/gems/ruby-2.0.0-p353/bin/geoip
gerard@ubuntu:~/Documents/geoBrainz/src$ irb
2.0.0p353 :001 > require 'geoip'
=> true
2.0.0p353 :002 > c = GeoIp.new('db/GeoLiteCity.dat').city('google.com')
NameError: uninitialized constant GeoIp
from (irb):2
from /home/gerard/.rvm/rubies/ruby-2.0.0-p353/bin/irb:12:in `

'

I uninstalled and reinstalled the geoip gem, trying to eliminate a corrupted gem install.

Where am I going wrong?

Geoip#country raises undefined method when sent a string that is not an IP address

If you pass in a string that is blank or just contains numbers and dots (but is not an IP address) then GeoIP raises a NoMethodError: undefined method `&' for "1.2":String exception

For example

> GeoIP.new('the-data-file.dat').country('')
NoMethodError: undefined method `&' for "1.2":String
    from /[redacted]/geoip-1.1.1/lib/geoip.rb:518:in `seek_record'

or

> GeoIP.new('the-data-file.dat').country('1.2.3')
NoMethodError: undefined method `&' for "1.2":String
    from /[redacted]/geoip-1.1.1/lib/geoip.rb:518:in `seek_record'

Better behaviour would be to return nil OR to raise an exception complaining that the argument is invalid

Support for GeoIPOrg.dat

Same issue as https://logstash.jira.com/browse/LOGSTASH-1394 it seems.

but for organisations in GeoIPOrg.dat.

Maybe same fix?

{:timestamp=>"2013-10-28T16:06:13.964000+0000", :message=>"Exception in filterworker", "exception"=>#<NoMethodError: undefined method to_hash' for "Teraspace GmbH":String>, "backtrace"=>["file:/opt/logstash/logstash-1.2.2-flatjar.jar!/logstash/filters/geoip.rb:104:in filter'", "(eval):220:ininitialize'", "org/jruby/RubyProc.java:271:in call'", "file:/opt/logstash/logstash-1.2.2-flatjar.jar!/logstash/pipeline.rb:250:infilter'", "file:/opt/logstash/logstash-1.2.2-flatjar.jar!/logstash/pipeline.rb:191:in filterworker'", "file:/opt/logstash/logstash-1.2.2-flatjar.jar!/logstash/pipeline.rb:134:in`start_filters'"], :level=>:error}

GeoIP.asn incorrect parsing of some records

irb(main):008:0>     @mm_asn = GeoIP.new( 'GeoIPASNum.dat' )
=> #<GeoIP:0x2b1a1e0 @mutex=#<Mutex:0x2b1a1b0>, @use_pread=false, @options={}, @database_type=9, @record_length=3, @file=#<File:GeoIPASNum.dat>, @ip_bits=32, @database_segments=[365984]>

# Good: Returns ASN struct
irb(main):009:0> x = @mm_asn.asn( "195.238.18.26")
=> #<struct GeoIP::ASN number="AS5432", asn="BELGACOM S.A.">

# Bad: Returns the raw record (because of leading *)
irb(main):010:0> x = @mm_asn.asn( "195.238.181.26")
=> "*AS18144 Energia Communications,Inc."

Add a lookup for connection type

I'm currently trying to retrieve the connection type (a.k.a netspeed) and ISP based on an IP from two purchased legacy databases that I bought from MaxMind. Doing a lookup for an ISP works perfectly well with the isp method from the appropriate db.

However, even though in the code you check if the db type is "GEOIP_NETSPEED_EDITION" (which is a purchased MaxMind legacy db), there is no way to actually look up the netspeed/connection type from it.

Do you have any ideas on how to go about this? Does a new lookup method have to be added?

Thanks!

uninitialized constant GeoIP

Hi, I am trying to use this gem in an app, but for some reason the GeoIP class cannot be found:

ree-1.8.7-2011.03 :001 > require 'geoip'
=> true
ree-1.8.7-2011.03 :002 > GeoIP
NameError: uninitialized constant GeoIP
from (irb):2

Am I missing something?

Many thanks in advance
Vito

Return list of countries and cities

I know it is not directly related to this particular gem, but it would be quite nice to have all the cities and countries fetched from the .dat file, to be usable, for example, in forms (select fields). Just to avoid duplication in code / databases.

utf-8 read incorrect

I converts MaxMind CSV files to .dat files use
https://github.com/mteodoro/mmutils/blob/master/csv2dat.py
And translate some city name to local language(Fuzhou=> Fuzhou福州)before convert.

# encoding: UTF-8
require "geoip"
@database="mmcity.dat"
geoip_initialize = ::GeoIP.new(@database)
info=geoip_initialize.city("218.85.*.*")
print info.city_name

# output Fuzhouç¦�å·

I modify some code in "geoip.rb"
line 781

# city = city.force_encoding('iso-8859-1').encode('utf-8') if city.respond_to?(:force_encoding)
city.force_encoding(Encoding::UTF_8) # add this just for [logstash geoip filter ](https://github.com/logstash-plugins/logstash-filter-geoip/blob/master/lib/logstash/filters/geoip.rb) line 192

#output:Fuzhou福州

Test on jruby 1.9 in logstash and ruby 2.0.0

undefined method `slice!' for nil:NilClass

I have this error on production:

NoMethodError: undefined method `slice!' for nil:NilClass
[GEM_ROOT]/gems/geoip-1.2.1/lib/geoip.rb:535:in `block in seek_record'
[GEM_ROOT]/gems/geoip-1.2.1/lib/geoip.rb:531:in `downto'
[GEM_ROOT]/gems/geoip-1.2.1/lib/geoip.rb:531:in `seek_record'
[GEM_ROOT]/gems/geoip-1.2.1/lib/geoip.rb:239:in `city'
[GEM_ROOT]/gems/geoip-1.2.1/lib/geoip.rb:183:in `country'

I can't reproduce the same error with the same IP, so it seems to have something to do with its internal state? Not sure.

It is with Ruby 2.0.0p0.

Reporting Abuse on GeoIP

Hello,

I found your contact information on this website.

GeoIP and GeoIP2 are registered trademarks of MaxMind, Inc. Trademark number 2897141. The usage of MaxMind's trademark on this website is infringing on our trademark rights. We ask that you remove this usage from your website and any related collateral.

Thank you,
Tristen Howell
Operations Associate
MaxMind, Inc.

test/csvORG2dat.py does not support python3

I don't think this affects anyone else than the maintainer but:

  File "/csvORG2dat.py", line 139
    print node.segment, [self.dump_node(node.lhs), self.dump_node(node.rhs)]
          ^
SyntaxError: invalid syntax

Missing countries

Hi,

the list of country names and country codes is outdated. It doesn't include some newer countries, such as:

BONAIRE, SINT EUSTATIUS AND SABA
CURAÇAO
SOUTH SUDAN
SINT MAARTEN (DUTCH PART)

Best, Max

.each method always returns empty Enumerable for GeoIPCity.dat database

Using a recently downloaded GeoIPCity.dat legacy database, calling the each method always returns an Enumerable with zero elements. However, if you use the GeoLiteCity.dat database, a non-empty Enumerable is returned.

#!/usr/bin/env ruby

require 'geoip'  # v 1.6.1

@maxmind = GeoIP.new("./GeoLiteCity.dat")

puts "Cities found in GeoLiteCity.dat:  #{@maxmind.each.count}"

@maxmind = GeoIP.new("./GeoIPCity.dat")

puts "Cities found in GeoIPCity.dat:  #{@maxmind.each.count}"
Cities found in GeoLiteCity.dat:  12098
Cities found in GeoIPCity.dat:  0

Timezone incorrect for Canada/Ontario

The time_zone.yml maps CAON (that is, Canada/Ontario) to America/Rainy_River. This is incorrect, since America/Rainy_River is designated as in Central Time. The only part of Ontario for which America/Rainy_River is correct is the small slice that lies west of Thunder Bay.

Since Ontario covers 2 timezones, the simple mapping provided by time_zone.yml cannot be 100% accurate. It is, however, far more accurate to designate CAON as America/Toronto, since this covers the majority of the province and probably 99% of the population.

I am curious as to where this mapping data came from.

Cheers!

Dave I

Broken for >= 0.8.5

I have a simple GeoIP rack app using GeoIPCity.dat that has worked fine through version 0.8.4, but fails with geoip 0.8.5 and 0.8.6.

result = db.country(env['REMOTE_ADDR'])

will set 'result' to nil for legitimate IP's.

Haven't had a chance to dig into this yet, but wanted to let you know that the latest versions are causing some issues.

include LICENSE file in geoip gem

Hi Clifford,

Thank you very much for your work on geoip. I'm working on packaging this gem for Fedora, and the Fedora Packaging Guidelines suggest that I ask you to include a LICENSE file in the root of the project. By including a copy of the exact LGPL license text, you will make the terms of the geoip license very clear.

Also, in the specific case of geoip's LGPL license, all derivative copies of geoip are required to contain the full text of the LGPL license (as alluded in the sentence in README.rdoc: "You should have received a copy of the GNU Lesser General Public License along with this library...") So in order to ease development and distribution of geoip, it makes sense to simply include this text in the upstream project.

More details about this can be found on Fedora's wiki, under the "License Text" section: https://fedoraproject.org/wiki/Packaging:LicensingGuidelines#License_Text

Would you mind adding a LICENSE file with the LGPL license text to the git repo and the gem?

Out of range latitude and longitude

I'm using this gem to retrieve lat/lon from the user's IP, like so:

city = GeoIP::DB.city(@user.ip)
@user.lat = city.try(:latitude)
@user.lng = city.try(:longitude)

But I'm getting incorrect values sporadically.

@user.ip => "182.249.223.182",
@user.lat => 1387.872 - #<BigDecimal:f5f9c04,'0.1387872E4',18(45)>,
@user.lng => 1387.9004 - #<BigDecimal:f5f93d0,'0.13879004E4',18(45)>

If I run the same query from console, it works as expected:

> city = GeoIP::DB.city('182.249.223.182')
> city.try(:latitude)
=> 36.0
> city.try(:longitude)
=> 138.0

So, I'm guessing it has something to do with long-running reuse of the same GeoIP instance? GeoIP::DB is initialized at boot, only once:

class GeoIP
  DB = GeoIP.new(Rails.root.join('some/path/to/GeoLiteCity.dat'))
end

Thoughts?

GeoIP database global accuracy

Hi,

I've read on MaxMind's website that the accuracy of their database decrease at the rate of about 1.5% per month and I was wondering if their is a way to evaluate the overall accuracy of the database once the GeoIP object is created.

If the creation date of the .dat file is available, it could be interesting to access it and read it through th GeoIP model, or even having access to a method, say #accuracy, returning directly the evaluated accuracy:

def accuracy
  0.985**((Time.now - database_publication_date)/2592000).to_i
end

Doesn't work with recent version of db

I'm using GeoLiteCountry database from debian package https://packages.debian.org/jessie/geoip-database - currently in version of 1.6.2-4 which depends on geoip-database in version 20150317-1

When I lookup for a country I'm getting 0 as country code.

g = GeoIP.new('tmp/GeoIP.dat')
g.country("91.193.208.126").to_hash
=> {:request=>"91.193.208.126", :ip=>"91.193.208.126", :country_code=>0, :country_code2=>"--", :country_code3=>"--", :country_name=>"N/A", :continent_code=>"--"}

As there is some binary search going on, I don't know how to debug it https://github.com/cjheath/geoip/blob/master/lib/geoip.rb#L873-L887

dnsmasq

Because of Performance reasons I want to use a local dnsmasq deamon with Caching enabled to Speed up lookups by geoip on hostnames. It seems that geoip does dns-lookups directly bypasing dnsmasq.

  1. Am I right?

2 What can I do?

Rails can't access struct variables

Hello,

I am using Rails 4.0 and Ruby 2.0, in terminal I can easily access a IP's information, but on Rails this code gives an error to the structs variable.

<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat.country_code2 %>

In this code, "country_code2" gives an "undefined method `country_code2' for nil:NilClass" error, but when I do

<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat %>

It prints the struct properly. Is this a gem issue or a Rails misconfiguration?

Closing database file

I have not found a place where the file is closed in the code. If so - is it kept open deliberately?
I am doing GeoIP.new("GeoIP.dat") on each page request in Rails. If it opens the file but never closes on each page load, it will soon use all file descriptors in the system. Am I missing something?

GeoIP crashes when looking up for IP

Hi,

I have a weird behaviour when using the GeoIP::country method: when trying to geolocate some address, through a script, a resque job or the IRB console, a crash occurs kicking me out the IRB console.

I wasn't able to find out exactly why. All I know is that the crash occurs while calling the IPSocket::getaddress method in geoip.rb on line 508. I'm not sure why as I logged requests to this method and retried the requests with the failed addresses but I wasn't able to reproduce the crash.

All I got is the core dump, but I'm not sure I can easily send you about 2500 lines of debug messages. Here is at least the backtrace of the crash

-- Ruby level backtrace information ----------------------------------------
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/bin/rake:23:in `<main>'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/bin/rake:23:in `load'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/tasks.rb:41:in `block (2 levels) in <top (required)>'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/worker.rb:134:in `work'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/worker.rb:134:in `loop'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/worker.rb:143:in `block in work'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/worker.rb:266:in `fork'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/worker.rb:266:in `fork'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/worker.rb:146:in `block (2 levels) in work'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/worker.rb:195:in `perform'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/job.rb:145:in `perform'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/job.rb:145:in `call'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/job.rb:133:in `block (2 levels) in perform'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-job-stats-0.3.0/lib/resque/plugins/job_stats/duration.rb:24:in `around_perform_job_stats_duration'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/job.rb:133:in `block (3 levels) in perform'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/job.rb:133:in `call'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/job.rb:137:in `block (2 levels) in perform'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-lock-timeout-0.4.1/lib/resque/plugins/lock_timeout.rb:263:in `around_perform_lock'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/resque-1.24.1/lib/resque/job.rb:138:in `block (3 levels) in perform'
/var/www/my_app/releases/20130809163951/app/workers/fetch_alerts.rb:33:in `perform'
/var/www/my_app/releases/20130809163951/app/models/xxx/source.rb:93:in `import'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/persistence.rb:241:in `create'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/threaded/lifecycle.rb:173:in `_creating'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/persistence.rb:246:in `block in create'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/persistence.rb:80:in `save'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/persistence.rb:51:in `insert'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/persistence/insertion.rb:23:in `prepare'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/callbacks.rb:130:in `run_callbacks'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:414:in `_run__246524166145139314__save__4173549135879904193__callbacks'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/persistence/insertion.rb:24:in `block in prepare'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/mongoid-3.1.4/lib/mongoid/callbacks.rb:130:in `run_callbacks'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_create_callbacks'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:429:in `_run__246524166145139314__create__4173549135879904193__callbacks'
/var/www/my_app/releases/20130809163951/app/models/xxx/alert.rb:71:in `block in <class:Alert>'
/var/www/my_app/releases/20130809163951/app/models/xxx/alert.rb:192:in `geolocate'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/geoip-1.2.1/lib/geoip.rb:183:in `country'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/geoip-1.2.1/lib/geoip.rb:233:in `city'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/geoip-1.2.1/lib/geoip.rb:508:in `lookup_ip'
/var/www/my_app/releases/20130809163951/vendor/bundle/ruby/1.9.1/gems/geoip-1.2.1/lib/geoip.rb:508:in `getaddress'

The geolocate method in xxx/alert.rb looks like this:

  def geolocate
    begin
      source_loc = Dash::Application.config.geoip_db.country(self.sourceip)
      if !source_loc.nil?
        self.source_country_code = source_loc[:country_code2]
        self.source_country_name = source_loc[:country_name]
        self.source_city_name = source_loc[:city_name]
        self.source_latitude = source_loc[:latitude]
        self.source_longitude = source_loc[:longitude]
      end
    rescue
    end

    begin
      destination_loc = Dash::Application.config.geoip_db.country(self.destinationip) # this is line 192
      if !destination_loc.nil?
        self.destination_country_code = destination_loc[:country_code2]
        self.destination_country_name = destination_loc[:country_name]
        self.destination_city_name = destination_loc[:city_name]
        self.destination_latitude = destination_loc[:latitude]
        self.destination_longitude = destination_loc[:longitude]
      end
    rescue
    end
    return nil
  end

Does some of you have an idea about what this could come from? What could I give you to help you finding out what is happening?

Very weird bug. Sometime GEO_IP_CITY.city returns incorrect info

I've been using this gem for a long time. It worked just fine until recently I found a very weird bug when I try to track requests from Africa to my website.

My ruby code is like this:

# In class ApplicationController < ActionController::Base

after_filter :monitor_african_requests

def monitor_african_requests
    request_ip = request.ip
    geo_city = GEO_IP_CITY.city(request_ip)
    user = current_user
    condition = user && geo_city.is_a?(GeoIP::City) && geo_city.continent_code == 'AF'
    if condition
      log = "LOG for African Request: geo_city: #{geo_city.inspect}; IP: #{request_ip.inspect}"
      Rails.logger.info log
    end
end

This works for some request but can not find the correct location info for some other requests. For example, this is one of the output in my log file:

LOG for African Request: geo_city: #<struct GeoIP::City request="135.0.174.69", ip="135.0.174.69", country_code2="DZ", country_code3="DZA", country_name="Algeria", continent_code="AF", region_name="09", city_name="Oran", postal_code="", latitude=35.6911, longitude=-0.641699999999986, dma_code=nil, area_code=nil, timezone="Africa/Algiers">; IP: "135.0.174.69"

It shows this request is from Africa, but in my Rails console, I tried the same code, it turned out to be a Canadian IP.

GEO_IP_CITY.city("135.0.174.69")
=> #<struct GeoIP::City request="135.0.174.69", ip="135.0.174.69", country_code2="CA", country_code3="CAN", country_name="Canada", continent_code="NA", region_name="ON", city_name="Toronto", postal_code="M2J", latitude=43.7685, longitude=-79.3584, dma_code=nil, area_code=nil, timezone="America/Rainy_River">

Any idea why this could happen? I'll appreciate any help.

My Rails version is 2.3.18 and the geoip version is "~> 4.0.1".

unclear license: GPL or MIT?

I'm interested in packaging this for Fedora. Would you mind clarifying the license of this package? README.rdoc and geoip.rb say "GPL v2.1 or later", but the Rakefile says "MIT".

encoding issue

with recent GeoLiteCity.dat files I think the text encoding has changed, i.e.

>>geoip.city("201.68.235.228")
=> #<struct GeoIP::City
 request="201.68.235.228",
 ip="201.68.235.228",
 country_code2="BR",
 country_code3="BRA",
 country_name="Brazil",
 continent_code="SA",
 region_name="27",
 city_name="Guaruj\xE1",
 postal_code="",
 latitude=-24.0,
 longitude=-46.266699999999986,
 dma_code=nil,
 area_code=nil,
 timezone="America/Sao_Paulo",
 real_region_name="Sao Paulo">

notice that city_name is badly encoded. Calling .encode('utf-8') on it correctly converts it.
in #6 the code was updated to force encoding of the strings to iso-8859-1, so that seems to cause this issue.
This is a problem because the string is in fact utf-8 but considered latin-1 and when concatenated with a string in utf8 it causes a runtime error. I can fix this in user code by re-encoding in utf-8 but it seems wasteful.

I don't know if there is a way to detect the encoding of the database file, but I think we might want to provide a flag to the db constructor or default to utf8 if necessary.

Defaults to the Congo for private IP's

Looking up 127.0.0.1 or 10.0.0.1 results in ["127.0.0.1", "127.0.0.1", "CG", "COG", "Congo", "AF", "\341", "", "", 38.0, -97.0] from the GeoLiteCity.dat file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.