Determining Country, City, Postal Code, and ASN from IP Address [GeoIP2 + Node.js]
I’ll introduce how to determine country, city, postal code, ASN and more from IP addresses using GeoIP2’s Node.js library.
When developing web services for a global audience, there’s a need to determine countries from IP addresses.
geoip2-node
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.
Here’s sample shell script code for downloading MaxMind DB binary:
#!/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-*
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.
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': '澳大利亚'
}
}
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'
});
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'
});
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.'
});
That’s all from the Gemba.