IP アドレスから国・都市・郵便番号・ASNの判定 [GeoIP2 + Node.js]

GeoIP2 の Node.js 向けライブラリで IP アドレスから国 (country)、都市 (city)、郵便番号 (postal code)、ASN などを判定する方法をご紹介します。

GeoIP

背景 IP アドレスから国・地域を判定したい

全世界向け Web サービスを開発していると IP アドレスから国判定をしたいというニーズがでてきます。

GeoIP2, GeoIP2-node について

geoip2-node

GeoLite2 Free Downloadable Database

GeoLite2 のデータベースは、GeoLite2 Free Downloadable Databases « MaxMind Developer Site から MaxMind DB binary, CSV format の2種類のフォーマットで提供されています。

今回は GeoIP2-node で利用する MaxMind DB binary をダウンロードします。ライブラリを自作する場合は CSV format の方をダウンロードして使うとよさそうです。

MaxMind DB binary ダウンロード shell script

MaxMind DB binary をダウンロードする shell script のサンプルコードは以下のとおりです。

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 サンプルコード

geoip2-node と事前にダウンロードした GeoLite2-City.mmdb, GeoLite2-Country.mmdb, GeoLite2-ASN.mmdb を利用して、IP アドレスから国、都市、郵便番号、ASN などを判定するサンプルコードを紹介していきます。

IP アドレスから国コードの判定 (Buffer 編)

GeoLite2-Country.mmdb と 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

IP アドレスから国コードの判定 (Promise 編)

GeoLite2-Country.mmdb と 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

IP アドレスから都市名、郵便番号の判定

GeoLite2-City.mmdb にて都市名 (city name)、郵便番号 (postal code) を判定するサンプルコードです。

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

IP アドレスから ASN (Autonomous System Number) の判定

GeoLite2-ASN.mmdb と 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

以上、GeoIP2 で IP アドレスから国・都市・郵便番号・ASNなどを判定したい、現場からお送りしました。

参考情報