Determining Country, City, Postal Code, and ASN from IP Address [GeoIP2 + Node.js]

Tadashi Shigeoka ·  Sat, November 16, 2019

I’ll introduce how to determine country, city, postal code, ASN and more from IP addresses using GeoIP2’s Node.js library.

GeoIP

Background: Want to Determine Country/Region from IP Address

When developing web services for a global audience, there’s a need to determine countries from IP addresses.

About GeoIP2, GeoIP2-node

geoip2-node

GeoLite2 Free Downloadable Database

The GeoLite2 database is provided in two formats from GeoLite2 Free Downloadable Databases « MaxMind Developer Site: MaxMind DB binary and CSV format.

This time, I’ll download the MaxMind DB binary for use with GeoIP2-node. If you’re creating your own library, you should download and use the CSV format.

MaxMind DB binary Download Shell Script

Here’s sample shell script code for downloading MaxMind DB binary:

bin/download_geolite2_mmdb

#!/bin/sh
# Download geoip databases
# @see: https://dev.maxmind.com/geoip/geoip2/geolite2/
cd tmp/

# GeoLite2-City.mmdb
curl https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz -O
tar -zxvf GeoLite2-City.tar.gz
rm GeoLite2-City.tar.gz
mv GeoLite2-City_*/GeoLite2-City.mmdb ../data/

# GeoLite2-Country.mmdb
curl https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz -O
tar -zxvf GeoLite2-Country.tar.gz
rm GeoLite2-Country.tar.gz
mv GeoLite2-Country_*/GeoLite2-Country.mmdb ../data/

# GeoLite2-ASN.mmdb
curl https://geolite.maxmind.com/download/geoip/database/GeoLite2-ASN.tar.gz -O
tar -zxvf GeoLite2-ASN.tar.gz
rm GeoLite2-ASN.tar.gz
mv GeoLite2-ASN_*/GeoLite2-ASN.mmdb ../data/

# Remove unused files
rm -rf GeoLite2-*

GeoIP2-node Sample Code

I’ll introduce sample code that uses geoip2-node with the previously downloaded GeoLite2-City.mmdb, GeoLite2-Country.mmdb, and GeoLite2-ASN.mmdb to determine country, city, postal code, ASN, etc. from IP addresses.

Determining Country Code from IP Address (Buffer Version)

Sample code using GeoLite2-Country.mmdb and Buffer:

const Reader = require('@maxmind/geoip2-node').Reader;

const dbBuffer = fs.readFileSync('data/GeoLite2-Country.mmdb');
const reader = Reader.openBuffer(dbBuffer);

let res = reader.country('1.1.1.1');

console.log(res.country.isoCode); // AU

res.country

// console.log(res.country);
{
  geonameId: 2077456,
  isoCode: 'AU',
  names: {
    de: 'Australien',
    en: 'Australia',
    es: 'Australia',
    fr: 'Australie',
    ja: 'オーストラリア',
    'pt-BR': 'Austrália',
    ru: 'Австралия',
    'zh-CN': '澳大利亚'
  }
}

Using a Buffer | GeoIP2-node

Determining Country Code from IP Address (Promise Version)

Sample code using GeoLite2-Country.mmdb and Promise:

const Reader = require('@maxmind/geoip2-node').Reader;

Reader.open('data/GeoLite2-Country.mmdb').then(reader => {
  const res = reader.country('1.1.1.1');

  console.log(res.country.isoCode); // 'AU'
});

Country Example | GeoIP2-node

Determining City Name and Postal Code from IP Address

Sample code for determining city name and postal code using GeoLite2-City.mmdb:

const Reader = require('@maxmind/geoip2-node').Reader;
Reader.open('data/GeoLite2-City.mmdb').then(reader => {
  const res = reader.city('128.101.101.101');

  console.log(res.city.names.en); // 'Minneapolis'

  console.log(res.postal.code); // '55407'
});

City Example | GeoIP2-node

Determining ASN (Autonomous System Number) from IP Address

Sample code using GeoLite2-ASN.mmdb and Promise:

const Reader = require('@maxmind/geoip2-node').Reader;
Reader.open('data/GeoLite2-ASN.mmdb').then(reader => {
  const res = reader.asn('1.1.1.1');

  console.log(res.autonomousSystemNumber); // 13335

  console.log(res.autonomousSystemOrganization); // 'Cloudflare, Inc.'
});

ASN Example | GeoIP2-node

That’s all from the Gemba.

Reference Information