Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
protected async initGeoCoder(): Promise {
this.geocoder = await maxmind.open(this.config.geoBasePath);
}
const loadFile = res => maxmind.open(ipFile)
.then(lookup => res(ip => (ip ? lookup.get(ip) : false)))
.catch(err => log(err))
return new Promise(resolve => {
export const getGeolocationFromIp = async (
ipAddress: string
): Promise => {
const cachedLookup = getItemFromCache(CacheCategories.IP_LOOKUP, ipAddress);
if (cachedLookup) return cachedLookup as GeoLocation;
const lookup = await maxmind.open(geoLite2.paths.city);
const ipLookup = lookup.get(ipAddress);
if (!ipLookup) return;
const location: any = {};
if (ipLookup.city) location.city = ipLookup.city.names.en;
if (ipLookup.continent) location.continent = ipLookup.continent.names.en;
if (ipLookup.country) location.country_code = ipLookup.country.iso_code;
if (ipLookup.location) location.latitude = ipLookup.location.latitude;
if (ipLookup.location) location.longitude = ipLookup.location.longitude;
if (ipLookup.location) location.time_zone = ipLookup.location.time_zone;
if (ipLookup.location)
location.accuracy_radius = ipLookup.location.accuracy_radius;
if (ipLookup.postal) location.zip_code = ipLookup.postal.code;
if (ipLookup.subdivisions)
location.region_name = ipLookup.subdivisions[0].names.en;
if (ipLookup.subdivisions)
location.region_code = ipLookup.subdivisions[0].iso_code;
utils.getCountry = function (ip, callback) {
maxmind.open(nconf.get('mmdb:path'), (err, orgLookup) => {
if (err) return callback(err);
return callback(null, orgLookup.get(ip));
});
};