Skip to content

russellsimpkins/libvmod-maxmind-geoip

Repository files navigation

libvmod-maxmind-geoip

The vmod provides the ability to lookup location information based on IP address. The GeoIP database is a mmap'd red black tree implementation that's read-only. The code opens the database when the vmod is initialized and shutdown when the thread is terminated, leaving the database open for the life of the thread. The most expensive operation is opening the MaxMind database.

The vmod uses the MaxMind City database.

Usage

import geo
   // ....
   sub vcl_recv {
     set req.http.X-Country     = geo.country("170.149.100.10")
     set req.http.X-CountryCode = geo.country_code("170.149.100.10")
     set req.http.X-Region      = geo.region("170.149.100.10")
     set req.http.X-MetroCode   = geo.metro_code("170.149.100.10")
     set req.http.X-City        = geo.city("170.149.100.10")
     set req.http.X-Timezone    = geo.timezone("170.149.100.10")
     set req.http.X-Location    = geo.location("170.149.100.10")

     //# nytimes specific stuff
     set req.http.Weather-Code   = geo.weather_code("170.149.100.10")
     set req.http.Weather-Cookie = geo.get_weather_cookie(req.http.Cookie, "NYT_W2)
     set req.http.Cookie-Value   = geo.get_cookie(req.http.Cookie, "NYT_W2")

    }
    // ....
}

The location call generates json e.g. geo.location("199.254.0.98") would return

{"city":"Beverly Hills","state":"CA","country":"US"}"

Testing

There are unit tests and varnishtest scripts. The unit tests are in the tests folder. Edit tests/tests.c and rerun make && make test

The unit tests are done with unity https://mark-vandervoord-yxrv.squarespace.com/unity Varnishtest scripts are in src/tests. To run the tests:

make test

Installation

Below you'll find how to install on:

  • Fedora
  • Debian

Installation on Fedora (3 steps)

The vmod depends on having varnish 4.1 and libmaxmind installed as RPM.

http://maxmind.github.io/MaxMind-DB provides an indepth look at the MaxMind GeoIP database.

You can get a copy of the city database from here: https://dev.maxmind.com/geoip/geoip2/geolite2/

Step 1 - install varnish-cache

yum install -y varnish-cache

See https://github.com/nytm/varnish-cache

If you wanted to build from source, you could do something like this:

cd /usr/local/src
git clone -b 4.1 https://github.com/varnish/varnish-cache.git
cd varnish-cache
./autogen.sh
./configure --prefix=/usr --libdir=/usr/lib64 # <- change this to lib if your on a 32 bit system
make
sudo make install

NOTE: I received the following: after running make:

You need rst2man installed to make dist

I was able to get past this by installing python-docutils with:

yum install python-docutils

I then re-ran everything from ./autogen.sh onward. Varnish 4.1 publishes a package config file, so make sure you set libdir correctly or you will have to specify the PKG_CONFIG_PATH in step 3

Step 2 - install libmaxminddb

yum install -y libmaxinddb

If you wanted to install from source, I did the following:

cd ..
git clone --recursive https://github.com/maxmind/libmaxminddb.git
cd libmaxminddb
git submodule update
./bootstrap
./configure --prefix=/usr
make
sudo make install
cd ..

Step 3 - build the mddb vmod

git clone https://github.com/russellsimpkins/libvmod-maxmind-geoip.git
cd libvmod-maxmind-geoip
./autogen.sh
./configure --prefix=/usr --with-maxminddbfile=/mnt/mmdb/GeoIP2-City.mmdb VMODDIR=/usr/lib64/varnish/vmods
make
sudo make install

Make sure to read the NOTES below.

Installation on Debian (Jessie)

Considering you already have Varnish installed, else you may have a look here: https://varnish-cache.org/releases/install_debian.html#packages-from-repo-varnish-cache-org-5-0 (do use Packages from Varnish website, these are up-to-date).

Tested version below was Varnish 4.1.5 (with success!)

First install the backports for Jessie, if you don't already have it, by editing your /etc/apt/sources.list and adding this line (at the end of the file if you don't really know what sources.list is!):

deb http://ftp.debian.org/debian jessie-backports main

Update aptitude/apt (ruby is needed for the tests, but you can omit it if you really don't want it):

sudo aptitude update
sudo aptitude install libmaxminddb-dev ruby

Compile the VMOD:

git clone https://github.com/russellsimpkins/libvmod-maxmind-geoip.git
cd libvmod-maxmind-geoip
./autogen.sh
./configure --with-maxminddbfile=/usr/share/GeoIP/GeoLiteIP2-Country.mmdb
make
sudo make install

You may want to fix the mmdb path, and perhaps use https://github.com/maxmind/geoipupdate for automatic updates

Notes

NOTE I added support for a flag in autoconf: --with-maxminddbfile so that you can decide, when you build the module, where you're data file will live. If you don't specify a value the default will be used /mnt/mmdb/GeoIP2-City.mmdb See src/vmod_geo.h

NOTE Varnish 4.1 installs a package config. If varnish installs the varnish.pc file in the wrong directory, you will need to specify in the configure command e.g. PKG_CONFIG_PATH=/usr/lib/pkgconfig

#define MAX_CITY_DB "/mnt/mmdb/GeoLite2-City.mmdb"

About

A varnish module for geoid lookup

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •