Use local geo database instead of npm package.

This commit is contained in:
Mike Cao 2020-09-26 20:21:16 -07:00
parent 6db386a455
commit ad95da63b7
6 changed files with 65 additions and 24 deletions

View file

@ -1,8 +1,8 @@
import path from 'path';
import requestIp from 'request-ip';
import { browserName, detectOS } from 'detect-browser';
import isLocalhost from 'is-localhost-ip';
import maxmind from 'maxmind';
import geolite2 from 'geolite2-redist';
import {
DESKTOP_OS,
MOBILE_OS,
@ -60,15 +60,11 @@ export async function getCountry(req, ip) {
}
// Database lookup
const lookup = await geolite2.open('GeoLite2-Country', path => {
return maxmind.open(path);
});
const lookup = await maxmind.open(path.resolve(__dirname, '../geo/GeoLite2-Country.mmdb'));
const result = lookup.get(ip);
lookup.close();
return result.country.iso_code;
return result?.country?.iso_code;
}
export async function getClientInfo(req, { screen }) {