Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var maxmind = require('maxmind');
maxmind.init(__dirname + '/GeoLiteCity.dat');
// The returned location object has the following properties:
// countryCode, countryName, region, city, latitude, longitude
// postalCode, metroCode, dmaCode, areaCode
console.log('Location:', maxmind.getLocation('66.6.44.4'));
var maxmind = require('maxmind');
maxmind.init(__dirname + '/GeoLiteCity.dat');
// The returned location object has the following properties:
// countryCode, countryName, region, city, latitude, longitude
// postalCode, metroCode, dmaCode, areaCode
console.log('Location:', maxmind.getLocation('66.6.44.4'));
function getDataFromDB (req, res, format, ip) {
const dbpath = __dirname + '/db/GeoLite2-City.mmdb';
maxmind.open(dbpath, function (err, cityLookup) {
if (err) {
console.log('Error => ', err);
let text = 'Error accessing database. Try again';
sendResult(res, 'json', text, 500);
return;
}
data = cityLookup.get(ip);
if (data === null) { // not in database
sendResult(res, format, geoData, 200);
return;
}
geoData = handleGeoData(ip, data);
sendResult(res, format, geoData, 200);
return;
});
}
async function addGeoIP(req: Request) {
let countryCode = '';
try {
const ip = req.header('X-Forwarded-For') || req.ip;
// https://github.com/runk/node-maxmind/releases/tag/v3.0.0
const lookup = await maxmind.open(geolite2.paths.country);
const result = lookup.get(ip);
if (result) {
countryCode = result.country.iso_code;
}
} catch (error) {
// It's okay to go without a detected country.
}
req.app.locals.country = countryCode;
}
/**
* Milliseconds in an hour, the duration which analytics data will be cached.
* @type {number}
*/
const CACHE_KEEP_TIME = 3600000
/**
* Name of the MMDB file, which is assumed to be in the server directory.
* @type {string}
*/
const MMDB_FILE = 'server/GeoLite2-City.mmdb'
const cache = {}
// eslint-disable-next-line no-sync
const maxmindDb = maxmind.openSync(MMDB_FILE)
/**
* Looks up an IP address in the maxmind database.
* @param {string} ip The IP address to look up.
* @return {?Object}
*/
const lookupIp = ip => {
return maxmindDb.get(ip || '0.0.0.0')
}
/**
* Fetches analytics on recent site traffic and returns a Promise
* @param {string} file file to fetch analytics data from.
* @return {Promise}
*/
const get = file => {
module.exports = function(options) {
'use strict';
options = options || {};
var dbPath = options.dbPath || DEFAULTS.DB_PATH;
const dbLookup = maxmind.openSync(dbPath);
return (ip, options = {}) => {
const userLocale = options.userLocale || DEFAULTS.USER_LOCALE;
// check if ip is valid
if (!maxmind.validate(ip)) {
throw new Error(ERRORS.IS_INVALID);
}
const locationData = dbLookup.get(ip);
if (locationData == null) {
throw new Error(ERRORS.UNABLE_TO_FETCH_DATA);
}
// return an object with city, country, continent,
// latitude, and longitude, and timezone
return (ip, options = {}) => {
const userLocale = options.userLocale || DEFAULTS.USER_LOCALE;
// check if ip is valid
if (!maxmind.validate(ip)) {
throw new Error(ERRORS.IS_INVALID);
}
const locationData = dbLookup.get(ip);
if (locationData == null) {
throw new Error(ERRORS.UNABLE_TO_FETCH_DATA);
}
// return an object with city, country, continent,
// latitude, and longitude, and timezone
return new Location(locationData, userLocale);
};
};
var vows = require('vows'),
geoip = require('geoip-lite'),
maxmind = require('maxmind'),
filter_helper = require('./filter_helper');
maxmind.init(['test/maxmind/GeoIPCity.dat', 'test/maxmind/GeoIPASNum.dat']);
var ip1 = '91.121.153.187';
var ip1_res = geoip.lookup(ip1);
var ip1_res_maxmind = maxmind.getLocation(ip1);
var ip2 = '82.66.65.173';
var ip2_res = geoip.lookup(ip2);
var ip2_res_maxmind = maxmind.getLocation(ip2);
vows.describe('Filter Geoip ').addBatch({
'normal': filter_helper.create('geoip', 'ip', [
{
'titi': 'tata'
},
{
'titi': 'tata',
'ip': 'toto'
},
{
'titi': 'tata',
var vows = require('vows'),
geoip = require('geoip-lite'),
maxmind = require('maxmind'),
filter_helper = require('./filter_helper');
maxmind.init(['test/maxmind/GeoIPCity.dat', 'test/maxmind/GeoIPASNum.dat']);
var ip1 = '91.121.153.187';
var ip1_res = geoip.lookup(ip1);
var ip1_res_maxmind = maxmind.getLocation(ip1);
var ip2 = '82.66.65.173';
var ip2_res = geoip.lookup(ip2);
var ip2_res_maxmind = maxmind.getLocation(ip2);
vows.describe('Filter Geoip ').addBatch({
'normal': filter_helper.create('geoip', 'ip', [
{
'titi': 'tata'
},
{
'titi': 'tata',
'ip': 'toto'
},
{
'titi': 'tata',
'ip': ip1
},
{
'titi': 'tata',
var maxmind = require('maxmind');
maxmind.init(__dirname + '/GeoLiteCity.dat');
var fromLocation = maxmind.getLocation('66.6.44.4');
var toLocation = maxmind.getLocation('213.180.193.3');
console.log('From:\t', fromLocation.countryName, fromLocation.city);
console.log('To:\t', toLocation.countryName, toLocation.city);
console.log('Dist:\t', toLocation.distance(fromLocation));