Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

geoip-lite

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

geoip-lite - npm Package Compare versions

Comparing version 1.2.2 to 1.3.0

60

lib/geoip.js

@@ -41,3 +41,3 @@ var fs = require('fs');

locationBuffer: null,
locationRecordSize: 64,
locationRecordSize: 96,
mainBuffer: null,

@@ -52,3 +52,3 @@ recordSize: 12

mainBuffer: null,
recordSize: 58
recordSize: 36
};

@@ -79,2 +79,4 @@

region: '',
eu:'',
timezone:'',
city: '',

@@ -107,10 +109,12 @@ ll: [0, 0]

} else {
locId = buffer.readUInt32BE((line * recordSize) + 8) - 1;
locId = buffer.readUInt32BE((line * recordSize) + 8);
geodata.country = locBuffer.toString('utf8', (locId * locRecordSize) + 0, (locId * locRecordSize) + 2).replace(/\u0000.*/, '');
geodata.region = locBuffer.toString('utf8', (locId * locRecordSize) + 2, (locId * locRecordSize) + 4).replace(/\u0000.*/, '');
geodata.ll = [locBuffer.readInt32BE((locId * locRecordSize) + 4) / 10000, locBuffer.readInt32BE((locId * locRecordSize) + 8) / 10000];
geodata.metro = locBuffer.readInt32BE((locId * locRecordSize) + 12);
geodata.zip = locBuffer.readInt32BE((locId * locRecordSize) + 16);
geodata.city = locBuffer.toString('utf8', (locId * locRecordSize) + 20, (locId * locRecordSize) + locRecordSize).replace(/\u0000.*/, '');
geodata.metro = locBuffer.readInt32BE((locId * locRecordSize) + 4);
//geodata.ll[0] = locBuffer.readInt32BE((locId * locRecordSize) + 8);
//geodata.ll[1]= locBuffer.readInt32BE((locId * locRecordSize) + 12);
geodata.eu = locBuffer.toString('utf8', (locId * locRecordSize) + 16, (locId * locRecordSize) + 17).replace(/\u0000.*/, '');
geodata.timezone = locBuffer.toString('utf8', (locId * locRecordSize) + 17, (locId * locRecordSize) + 41).replace(/\u0000.*/, '');
geodata.city = locBuffer.toString('utf8', (locId * locRecordSize) + 41, (locId * locRecordSize) + locRecordSize).replace(/\u0000.*/, '');
}

@@ -138,2 +142,4 @@

var recordSize = cache6.recordSize;
var locBuffer = cache4.locationBuffer;
var locRecordSize = cache4.locationRecordSize;

@@ -147,8 +153,2 @@ var geodata = {

};
// XXX We only use the first 8 bytes of an IPv6 address
// This identifies the network, but not the host within
// the network. Unless at some point of time we have a
// global peace treaty and single subnets span multiple
// countries, this should not be a problem.
function readip(line, offset) {

@@ -187,7 +187,13 @@ var ii = 0;

} else {
geodata.range = [floor, ceil];
geodata.country = buffer.toString('utf8', (line * recordSize) + 32, (line * recordSize) + 34).replace(/\u0000.*/, '');
geodata.region = buffer.toString('utf8', (line * recordSize) + 34, (line * recordSize) + 36).replace(/\u0000.*/, '');
geodata.ll = [buffer.readInt32BE((line * recordSize) + 36) / 10000, buffer.readInt32BE((line * recordSize) + 40) / 10000];
geodata.city = buffer.toString('utf8', (line * recordSize) + 44, (line * recordSize) + recordSize).replace(/\u0000.*/, '');
locId = buffer.readUInt32BE((line * recordSize) + 32);
geodata.country = locBuffer.toString('utf8', (locId * locRecordSize) + 0, (locId * locRecordSize) + 2).replace(/\u0000.*/, '');
geodata.region = locBuffer.toString('utf8', (locId * locRecordSize) + 2, (locId * locRecordSize) + 4).replace(/\u0000.*/, '');
geodata.metro = locBuffer.readInt32BE((locId * locRecordSize) + 4);
//geodata.ll[0] = locBuffer.readInt32BE((locId * locRecordSize) + 8);
//geodata.ll[1]= locBuffer.readInt32BE((locId * locRecordSize) + 12);
geodata.eu = locBuffer.toString('utf8', (locId * locRecordSize) + 16, (locId * locRecordSize) + 17).replace(/\u0000.*/, '');
geodata.timezone = locBuffer.toString('utf8', (locId * locRecordSize) + 17, (locId * locRecordSize) + 41).replace(/\u0000.*/, '');
geodata.city = locBuffer.toString('utf8', (locId * locRecordSize) + 41, (locId * locRecordSize) + locRecordSize).replace(/\u0000.*/, '');
}

@@ -214,11 +220,11 @@

function get4mapped(ip) {
var ipv6 = ip.toUpperCase();
var v6prefixes = ['0:0:0:0:0:FFFF:', '::FFFF:'];
for (var i = 0; i < v6prefixes.length; i++) {
var v6prefix = v6prefixes[i];
if (ipv6.indexOf(v6prefix) == 0) {
return ipv6.substring(v6prefix.length);
}
}
return null;
var ipv6 = ip.toUpperCase();
var v6prefixes = ['0:0:0:0:0:FFFF:', '::FFFF:'];
for (var i = 0; i < v6prefixes.length; i++) {
var v6prefix = v6prefixes[i];
if (ipv6.indexOf(v6prefix) == 0) {
return ipv6.substring(v6prefix.length);
}
}
return null;
}

@@ -225,0 +231,0 @@

{
"name" : "geoip-lite",
"version" : "1.2.2",
"version" : "1.3.0",
"description" : "A light weight native JavaScript implementation of GeoIP API from MaxMind",

@@ -22,4 +22,6 @@ "keywords" : ["geo", "geoip", "ip", "ipv4", "ipv6", "geolookup", "maxmind", "geolite"],

"iconv-lite": "^0.4.13",
"ip-address": "^5.8.9",
"lazy": "^1.0.11",
"rimraf": "^2.5.2",
"save": "^2.3.2",
"yauzl": "^2.9.2"

@@ -31,5 +33,5 @@ },

"devDependencies": {
"nodeunit": "^0.10.2"
"nodeunit": "^0.11.2"
},
"license": "Apache-2.0"
}

@@ -171,3 +171,3 @@ GeoIP-lite

`geoip-lite` is Copyright 2011-2012 Philip Tellis <philip@bluesmoon.info> and the latest version of the code is
`geoip-lite` is Copyright 2011-2018 Philip Tellis <philip@bluesmoon.info> and the latest version of the code is
available at https://github.com/bluesmoon/node-geoip

@@ -174,0 +174,0 @@

@@ -23,36 +23,37 @@ // fetches and converts maxmind lite databases

var utils = require('../lib/utils');
var Address6 = require('ip-address').Address6;
var Address4 = require('ip-address').Address4;
var dataPath = path.join(__dirname, '..', 'data');
var tmpPath = path.join(__dirname, '..', 'tmp');
var countryLookup = {};
var cityLookup = {};
var databases = [
{
type: 'country',
url: 'https://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip',
src: 'GeoIPCountryWhois.csv',
dest: 'geoip-country.dat'
url: 'https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip',
src: [
'GeoLite2-Country-Locations-en.csv',
'GeoLite2-Country-Blocks-IPv4.csv',
'GeoLite2-Country-Blocks-IPv6.csv'
],
dest: [
'',
'geoip-country.dat',
'geoip-country6.dat'
]
},
{
type: 'country',
url: 'https://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz',
src: 'GeoIPv6.csv',
dest: 'geoip-country6.dat'
},
{
type: 'city-extended',
url: 'https://geolite.maxmind.com/download/geoip/database/GeoLiteCity_CSV/GeoLiteCity-latest.zip',
type: 'city',
url: 'https://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip',
src: [
'GeoLiteCity-Blocks.csv',
'GeoLiteCity-Location.csv'
'GeoLite2-City-Locations-en.csv',
'GeoLite2-City-Blocks-IPv4.csv',
'GeoLite2-City-Blocks-IPv6.csv'
],
dest: [
'geoip-city-names.dat',
'geoip-city.dat',
'geoip-city-names.dat'
'geoip-city6.dat'
]
},
{
type: 'city',
url: 'https://geolite.maxmind.com/download/geoip/database/GeoLiteCityv6-beta/GeoLiteCityv6.csv.gz',
src: 'GeoLiteCityv6.csv',
dest: 'geoip-city6.dat'
}

@@ -70,22 +71,47 @@ ];

// Return array of string values, or NULL if CSV string not well formed.
function CSVtoArray(text) {
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
var re_value = /(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g;
// Return NULL if input string is not well formed CSV string.
if (!re_valid.test(text)) return null;
var a = []; // Initialize array to receive values.
text.replace(re_value, // "Walk" the string using replace with callback.
function(m0, m1, m2, m3) {
// Remove backslash from \' in single quoted values.
if (m1 !== undefined) a.push(m1.replace(/\\'/g, "'"));
// Remove backslash from \" in double quoted values.
else if (m2 !== undefined) a.push(m2.replace(/\\"/g, '"'));
else if (m3 !== undefined) a.push(m3);
return ''; // Return empty string.
});
// Handle special case of empty last value.
if (/,\s*$/.test(text)) a.push('');
return a;
// Return array of string values, or NULL if CSV string not well formed.
function try_fixing_line(line) {
var pos1 = 0;
var pos2 = -1;
line = line.replace(/'/g,"\\'");
while(pos1 < line.length && pos2 < line.length) {
pos1 = pos2;
pos2 = line.indexOf(',', pos1 + 1);
if(pos2 < 0) pos2 = line.length;
if(line.indexOf("'", (pos1 || 0)) > -1 && line.indexOf("'", pos1) < pos2 && line[pos1 + 1] != '"' && line[pos2 - 1] != '"') {
line = line.substr(0, pos1 + 1) + '"' + line.substr(pos1 + 1, pos2 - pos1 - 1) + '"' + line.substr(pos2, line.length - pos2);
pos2 = line.indexOf(',', pos2 + 1);
if(pos2 < 0) pos2 = line.length;
}
}
return line;
}
function CSVtoArray(text) {
var re_valid = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;
var re_value = /(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g;
// Return NULL if input string is not well formed CSV string.
if (!re_valid.test(text)){
text = try_fixing_line(text);
if(!re_valid.test(text))
return null;
}
var a = []; // Initialize array to receive values.
text.replace(re_value, // "Walk" the string using replace with callback.
function(m0, m1, m2, m3) {
// Remove backslash from \' in single quoted values.
if (m1 !== undefined) a.push(m1.replace(/\\'/g, "'"));
// Remove backslash from \" in double quoted values.
else if (m2 !== undefined) a.push(m2.replace(/\\"/g, '"').replace(/\\'/g, "'"));
else if (m3 !== undefined) a.push(m3);
return ''; // Return empty string.
});
// Handle special case of empty last value.
if (/,\s*$/.test(text)) a.push('');
return a;
};
function fetch(database, cb) {

@@ -198,3 +224,30 @@

}
function processLookupCountry(src, cb){
var lines=0;
function processLine(line) {
var fields = CSVtoArray(line);
if (!fields || fields.length < 6) {
console.log("weird line: %s::", line);
return;
}
countryLookup[fields[0]] = fields[4];
}
var tmpDataFile = path.join(tmpPath, src);
process.stdout.write('Processing Lookup Data (may take a moment) ...');
var tstart = Date.now();
lazy(fs.createReadStream(tmpDataFile))
.lines
.map(function(byteArray) {
return iconv.decode(byteArray, 'latin1');
})
.skip(1)
.map(processLine)
.on('pipe', function() {
console.log(' DONE'.green);
cb();
});
}
function processCountryData(src, dest, cb) {

@@ -213,41 +266,45 @@ var lines=0;

var eip;
var cc = fields[4].replace(/"/g, '');
var rngip;
var cc = countryLookup[fields[1]];
var b;
var bsz;
var i;
if (fields[0].match(/:/)) {
// IPv6
bsz = 34;
sip = utils.aton6(fields[0]);
eip = utils.aton6(fields[1]);
b = new Buffer(bsz);
for (i = 0; i < sip.length; i++) {
b.writeUInt32BE(sip[i], i * 4);
if(cc){
if (fields[0].match(/:/)) {
// IPv6
bsz = 34;
rngip = new Address6(fields[0]);
sip = utils.aton6(rngip.startAddress().correctForm());
eip = utils.aton6(rngip.endAddress().correctForm());
b = new Buffer(bsz);
for (i = 0; i < sip.length; i++) {
b.writeUInt32BE(sip[i], i * 4);
}
for (i = 0; i < eip.length; i++) {
b.writeUInt32BE(eip[i], 16 + (i * 4));
}
} else {
// IPv4
bsz = 10;
rngip = new Address4(fields[0]);
sip = parseInt(rngip.startAddress().bigInteger(),10);
eip = parseInt(rngip.endAddress().bigInteger(),10);
b = new Buffer(bsz);
b.fill(0);
b.writeUInt32BE(sip, 0);
b.writeUInt32BE(eip, 4);
}
for (i = 0; i < eip.length; i++) {
b.writeUInt32BE(eip[i], 16 + (i * 4));
b.write(cc, bsz - 2);
fs.writeSync(datFile, b, 0, bsz, null);
if(Date.now() - tstart > 5000) {
tstart = Date.now();
process.stdout.write('\nStill working (' + lines + ') ...');
}
} else {
// IPv4
bsz = 10;
sip = parseInt(fields[2].replace(/"/g, ''), 10);
eip = parseInt(fields[3].replace(/"/g, ''), 10);
b = new Buffer(bsz);
b.fill(0);
b.writeUInt32BE(sip, 0);
b.writeUInt32BE(eip, 4);
}
b.write(cc, bsz - 2);
fs.writeSync(datFile, b, 0, bsz, null);
if(Date.now() - tstart > 5000) {
tstart = Date.now();
process.stdout.write('\nStill working (' + lines + ') ...');
}
}

@@ -286,4 +343,9 @@

var fields = CSVtoArray(line);
if (!fields) {
console.log("weird line: %s::", line);
return;
}
var sip;
var eip;
var rngip;
var locId;

@@ -300,13 +362,9 @@ var b;

var offset = 0;
bsz = 36;
rngip = new Address6(fields[0]);
sip = utils.aton6(rngip.startAddress().correctForm());
eip = utils.aton6(rngip.endAddress().correctForm());
locId = parseInt(fields[1], 10);
locId = cityLookup[locId];
var cc = fields[4];
var city = fields[6];
var lat = Math.round(parseFloat(fields[7]) * 10000);
var lon = Math.round(parseFloat(fields[8]) * 10000);
var rg = fields[5];
bsz = 58;
sip = utils.aton6(fields[0]);
eip = utils.aton6(fields[1]);
b = new Buffer(bsz);

@@ -324,8 +382,3 @@ b.fill(0);

}
b.write(cc, offset);
b.write(rg, offset + 2);
b.writeInt32BE(lat, offset + 4);
b.writeInt32BE(lon, offset + 8);
b.write(city, offset + 12);
b.writeUInt32BE(locId>>>0, 32);
} else {

@@ -335,6 +388,7 @@ // IPv4

sip = parseInt(fields[0], 10);
eip = parseInt(fields[1], 10);
locId = parseInt(fields[2], 10);
rngip = new Address4(fields[0]);
sip = parseInt(rngip.startAddress().bigInteger(),10);
eip = parseInt(rngip.endAddress().bigInteger(),10);
locId = parseInt(fields[1], 10);
locId = cityLookup[locId];
b = new Buffer(bsz);

@@ -375,3 +429,3 @@ b.fill(0);

var locId = null;
var linesCount = 0;
function processLine(line, i, a) {

@@ -383,40 +437,44 @@ if (line.match(/^Copyright/) || !line.match(/\d/)) {

var b;
var sz = 64;
var sz = 96;
var fields = CSVtoArray(line);
if (locId === null)
locId = parseInt(fields[0]);
else {
if (parseInt(fields[0]) - 1 > locId) {
b = new Buffer(sz);
b.fill(0);
fs.writeSync(datFile, b, 0, b.length, null);
}
locId = parseInt(fields[0]);
if (!fields) {
//lot's of cities contain ` or ' in the name and can't be parsed correctly with current method
console.log("weird line: %s::", line);
return;
}
var cc = fields[1];
locId = parseInt(fields[0]);
cityLookup[locId] = linesCount;
var cc = fields[4];
var rg = fields[2];
var city = fields[3];
var zip = parseInt(fields[4]);
var lat = Math.round(parseFloat(fields[5]) * 10000);
var lon = Math.round(parseFloat(fields[6]) * 10000);
var metro = parseInt(fields[7]);
var city = fields[10];
var metro = parseInt(fields[11]);
//other possible fields to include
var tz = fields[12];
var eu = fields[13];
//coming back later
/*
var lat = Math.round(parseFloat(fields[7]) * 10000);
var lon = Math.round(parseFloat(fields[8]) * 10000);
*/
b = new Buffer(sz);
b.fill(0);
b.write(cc, 0);
b.write(rg, 2);
b.writeInt32BE(lat, 4);
b.writeInt32BE(lon, 8);
b.write(cc, 0);//country code
b.write(rg, 2);//region
if(metro){
b.writeInt32BE(metro, 12);
if(metro) {
b.writeInt32BE(metro, 4);
}
/* remove comment when lat and long is back
b.writeInt32BE(lat,8);
b.writeInt32BE(lon,12);
*/
b.write(eu,16);//is in eu
b.write(tz,17);//timezone
b.write(city, 41);//cityname
if(zip){
b.writeInt32BE(zip, 16);
}
b.write(city, 20);
fs.writeSync(datFile, b, 0, b.length, null);
linesCount++;
}

@@ -434,3 +492,3 @@

.map(function(byteArray) {
return iconv.decode(byteArray, 'latin1');
return iconv.decode(byteArray, 'utf-8');
})

@@ -448,15 +506,22 @@ .skip(1)

if (type === 'country') {
processCountryData(src, dest, cb);
} else if (type === 'city-extended') {
processCityData(src[0], dest[0], function() {
processCityDataNames(src[1], dest[1], function() {
console.log(' DONE'.green);
cb();
if(Array.isArray(src)){
processLookupCountry(src[0], function() {
processCountryData(src[1], dest[1], function() {
processCountryData(src[2], dest[2], cb);
});
});
}
else{
processCountryData(src, dest, cb);
}
} else if (type === 'city') {
processCityDataNames(src[0], dest[0], function() {
processCityData(src[1], dest[1], function() {
console.log("city data processed");
processCityData(src[2], dest[2], function() {
console.log(' DONE'.green);
cb();
});
});
});
} else {
processCityData(src, dest, function() {
console.log(' DONE'.green);
cb();
});
}

@@ -478,3 +543,3 @@ }

console.log('Successfully Updated Databases from MaxMind.'.green);
if (process.argv[2]=='debug') console.log('Notice: temporary files are not deleted for debug purposes.'.bold.yellow);
if (process.argv[2] == 'debug') console.log('Notice: temporary files are not deleted for debug purposes.'.bold.yellow);
else rimraf(tmpPath);

@@ -481,0 +546,0 @@ process.exit(0);

@@ -39,3 +39,3 @@ var geoip = require('../lib/geoip');

test.equal(actual.city, "Santa Ana");
test.equal(actual.city, "Nuevo");//checked files, correct result
test.equal(actual.metro, 803);

@@ -42,0 +42,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc