Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
AX: { flag: 'aland-islands', name: 'Aland Islands' },
BQ: { flag: 'bonaire-sint-eustatius-saba', name: 'Bonaire, Sint Eustatius, Saba' },
CD: { flag: 'congo-the-democratic-republic-of-the', name: 'Congo The Democratic Republic of The' },
CW: { flag: 'curacao', name: 'Curacao' },
EU: { flag: 'european-union', name: 'European Union' },
GG: { flag: 'guernsey', name: 'Guernsey' },
MF: { flag: 'saint-martin', name: 'Saint Martin' },
SS: { flag: 'south-sudan', name: 'South Sudan' },
SX: { flag: 'sint-maarten', name: 'Sint Maarten' },
TL: { flag: 'timor-leste', name: 'Timor-leste' },
YT: { flag: 'mayotte', name: 'Mayotte' },
ZZ: { flag: 'unknown', name: 'Unknown' }
};
const mmdbPath = path.resolve(isDev ? './files/GeoLite2-City.mmdb' : './resources/files/GeoLite2-City.mmdb');
const reader = new Reader(readFileSync(mmdbPath));
const extractCity = city => {
try {
return city.names.en;
} catch {
return '';
}
};
export const lookup = ip => {
try {
const { city: city = '', country } = reader.get(ip);
return {
city: extractCity(city),
...codes[country.iso_code]
maxmindUpdate(true, geoIpStatus.maxmindDbDir, function (err, fileName) {
if (geoIpStatus.debug) {
consoleLogger.debug('GeoIP: init GeoIP DB ' + fileName)
}
if (err) {
consoleLogger.error('GeoIP: error ' + err)
geoIpStatus.geoIpFailed = true
return
}
try {
fs.statSync(fileName)
cityLookup = new MaxMindReader(fs.readFileSync(fileName), { watchForUpdates: true })
if (cityLookup) {
consoleLogger.debug('GeoIP: open ' + fileName)
} else {
consoleLogger.log('GeoIP: error ' + err)
}
if (geoIpStatus.fields) {
consoleLogger.log('GeoIP: lookup enabled for fields ' + geoIpStatus.fields)
} else {
consoleLogger.log('GeoIP: lookup enabled ')
}
} catch (fsStatError) {
geoIpStatus.geoIpFailed = true
consoleLogger.error('GeoIP: error ' + fsStatError)
}
})
}
import path from 'path';
import fs from 'fs';
import { Reader } from 'maxmind';
import { assetsDir } from 'consts/paths';
const buffer = fs.readFileSync(
path.join(assetsDir, 'GeoLite2-City', 'GeoLite2-City.mmdb')
);
const geoip = new Reader(buffer);
export default geoip;