GeoIP

From YobiWiki
Jump to navigation Jump to search

GeoIP install

GeoIP Country detection

apt-get install geoip-bin

To get city level geolocalisation we can get the free city db:

mkdir /usr/local/share/GeoIP
cd /usr/local/share/GeoIP
wget http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
ln -s /usr/local/share/GeoIP/GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
geoiplookup 85.234.207.107 
GeoIP Country Edition: BE, Belgium
GeoIP City Edition, Rev 1: BE, 02, Ixelles, (null), 50.833302, 4.366700, 0, 0
GeoIP City Edition, Rev 0: BE, 02, Ixelles, (null), 50.833302, 4.366700

Usage with AWFFull

cf AWFFull page

in /etc/awffull/awffull.conf:

GeoIP          yes

Actually awffull is not compiled with GeoIP support but we can rebuild the package

awffull -v (interrupt it, it's at the beginning)
=> GeoIP is not available in this binary. Ignoring request to use.

ok, don't be impressed ;-)
the building process of awffull is pretty well done, if it detects the headers of GeoIP it uses it, as simply as that.

apt-get build-dep awffull
apt-get source awffull
cd awffull-3.7.1/
apt-get install libgeoip1 libgeoip-dev
dpkg-buildpackage -uc -b 

Et voilà! You can install the new .deb freshly created.
Now you can run it, it will give e.g.

awffull -v
=> awffull 3.7.1 (Linux 2.6.17-2-vserver-amd64) English
=> Using GeoIP for IP Address Lookups: /usr/share/GeoIP/GeoIP.dat

One bad point: we didn't fix the dependancies of our new package, for a local use this is ok but for larger use we should have added the libgeoip1 package in the list of dependencies of awffull.

Usage with Zope

cf Zope page

First import the database on your MySQL db as <db_name>.csv as descibed here
Add a Python Script in zope (e.g. geoip):

from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE =  request.RESPONSE
ip = context.REQUEST.REMOTE_ADDR
line = string.split(ip,'.')
ip0 = line[0]
ip1 = line[1]
ip2 = line[2]
ip3 = line[3]
ipnum = int(ip0) * 16777216 + int(ip1) * 65536 + int(ip2) * 256 + int(ip3) * 1
print ipnum
return printed

Add a Z Mysql Method (e.g. select_GeoIP) with as Arguments ipnum

SELECT cn 
from <db_name>.csv 
where start < <dtml-sqlvar ipnum type="string"> and end > <dtml-sqlvar ipnum type="string">

In a DTML Method, test :

<dtml-in expr="select_GeoIPs(ipnum=geoip())">Hoo, you are from <dtml-var cn>, Welcome !</dtml-in>

Same for latitude and longitude of the country Country Geolocalization