What is maxmind?
The maxmind npm package provides tools for IP geolocation and other related functionalities using MaxMind's GeoIP2 and GeoLite2 databases. It allows developers to determine the geographical location, ISP, and other details of an IP address.
What are maxmind's main functionalities?
IP Geolocation
This feature allows you to determine the geographical location of an IP address. The code sample demonstrates how to use the GeoLite2-City database to get the city information for the IP address '8.8.8.8'.
const maxmind = require('maxmind');
const lookup = async (ip) => {
const cityLookup = await maxmind.open('GeoLite2-City.mmdb');
const city = cityLookup.get(ip);
console.log(city);
};
lookup('8.8.8.8');
ISP Information
This feature allows you to get the ISP information of an IP address. The code sample demonstrates how to use the GeoIP2-ISP database to get the ISP details for the IP address '8.8.8.8'.
const maxmind = require('maxmind');
const lookup = async (ip) => {
const ispLookup = await maxmind.open('GeoIP2-ISP.mmdb');
const isp = ispLookup.get(ip);
console.log(isp);
};
lookup('8.8.8.8');
Anonymous IP Detection
This feature allows you to detect if an IP address is associated with anonymous networks such as VPNs, proxies, or Tor. The code sample demonstrates how to use the GeoIP2-Anonymous-IP database to get anonymous IP details for the IP address '8.8.8.8'.
const maxmind = require('maxmind');
const lookup = async (ip) => {
const anonLookup = await maxmind.open('GeoIP2-Anonymous-IP.mmdb');
const anon = anonLookup.get(ip);
console.log(anon);
};
lookup('8.8.8.8');
Other packages similar to maxmind
ipstack
The ipstack package provides IP geolocation services similar to maxmind. It offers detailed information about the location, currency, timezone, and connection of an IP address. Unlike maxmind, ipstack is a cloud-based service and requires an API key for access.
geoip-lite
The geoip-lite package offers a lightweight IP geolocation solution using a local database. It provides basic geolocation information such as country, region, and city. Compared to maxmind, geoip-lite is less comprehensive but is easier to set up and use for simple geolocation needs.
ipinfo
The ipinfo package provides IP address information including geolocation, ASN, and company details. It uses the IPinfo API and requires an API key. Compared to maxmind, ipinfo offers additional data points such as company information and is also cloud-based.
node-maxmind
Pure Javascript module for Geo IP lookup using Maxmind binary databases (aka mmdb or geoip2).
Fastest Maxmind lookup library available - up to 17,000% faster than other libraries. Module has 100% test coverage with comprehensive test suite. It natively works with binary Maxmind database format and doesn't require any "CSV - {specific lib format}" conversions as some other modules do. Maxmind binary databases are highly optimized for size and performance so there's no point working with other than that format.
GEO databases
You might want to use geolite2 module with free geo databases. Alternatively, free databases available for download here. If you need better accuracy you should consider buying commercial subscription.
Installation
npm i maxmind
Usage
var maxmind = require('maxmind');
maxmind.open('/path/to/GeoLite2-City.mmdb', (err, cityLookup) => {
var city = cityLookup.get('66.6.44.4');
});
maxmind.open('/path/to/GeoOrg.mmdb', (err, orgLookup) => {
var city = orgLookup.get('66.6.44.4');
});
var cityLookup = maxmind.openSync('/path/to/GeoLite2-City.mmdb');
var city = cityLookup.get('66.6.44.4');
var orgLookup = maxmind.openSync('/path/to/GeoOrg.mmdb');
var organization = orgLookup.get('66.6.44.4');
V6 Support
Module is fully compatible with IPv6. There are no differences in API between IPv4 and IPv6.
var lookup = maxmind.openSync('/path/to/GeoLite2.mmdb');
var location = lookup.get('2001:4860:0:1001::3004:ef68');
Options
cache
Module uses tiny-lru. You can configure its settings by doing following:
var lookup = maxmind.openSync('/path/to/GeoLite2.mmdb', {
cache: {
max: 500,
}
});
lookup.get('1.1.1.1');
watchForUpdates
Supports reloading the reader when changes occur to the database that is loaded. (default false
). Only supported by Node v0.5.10+.
var lookup = maxmind.openSync('/path/to/GeoLite2.mmdb', { watchForUpdates: true });
lookup.get('1.1.1.1');
You also can specify wether the watcher should be persistent or not. If it is persistent, a node process will be blocked in watching state if the watcher is the only thing still running in the program. You can use watchForUpdatesNonPersistent
option (default false
) to prevent this behavior.
var lookup = maxmind.openSync('/path/to/GeoLite2.mmdb', {
watchForUpdates: true,
watchForUpdatesNonPersistent: true,
});
lookup.get('1.1.1.1');
Also, you can specify custom hook function on database update.
var opts = {
watchForUpdates: true,
watchForUpdatesHook: () => { console.log('database updated!'); },
};
var lookup = maxmind.openSync('/path/to/GeoLite2.mmdb', opts);
lookup.get('1.1.1.1');
IP addresses validation
Module supports validation for both IPv4 and IPv6:
maxmind.validate('66.6.44.4');
maxmind.validate('66.6.44.boom!');
maxmind.validate('2001:4860:0:1001::3004:ef68');
maxmind.validate('2001:4860:0:1001::3004:boom!');
GeoIP Legacy binary format
In case you want to use legacy GeoIP binary databases you should use maxmind@0.6.
References
License
MIT