geoip-lite2
Advanced tools
Comparing version 2.1.21 to 2.1.22
@@ -1,1 +0,1 @@ | ||
const n=require('node:fs'),e=require('node:path'),t={};function o(n){t[n].close()}module.exports.makeFsWatchFilter=function(u,l,c,r,i){let s=null;function f(){s=null,i()}'function'==typeof r&&(i=r,r=c,c=null),t[u]&&o(u),t[u]=n.watch(l,(function(t,o){if(!o)return;const u=e.join(l,o);c&&c!==o||n.access(u,n.constants.F_OK,(n=>{if(n)return console.error(n);null!==s&&(clearTimeout(s),s=null),s=setTimeout(f,r)}))}))},module.exports.stopWatching=o; | ||
const n=require('fs'),t=require('path'),e={};function o(n){e[n].close()}module.exports.makeFsWatchFilter=function(u,l,c,r,i){let s=null;function f(){s=null,i()}'function'==typeof r&&(i=r,r=c,c=null),e[u]&&o(u),e[u]=n.watch(l,(function(e,o){if(!o)return;const u=t.join(l,o);c&&c!==o||n.access(u,n.constants.F_OK,(n=>{if(n)return console.error(n);null!==s&&(clearTimeout(s),s=null),s=setTimeout(f,r)}))}))},module.exports.stopWatching=o; |
@@ -1,1 +0,1 @@ | ||
const e=require('node:fs'),t=require('node:net'),r=require('node:path'),n=require('async'),i=require('./utils.js'),o=require('./fsWatcher.js'),{version:a}=require('../package.json');e.existsSync=e.existsSync||r.existsSync;const c='dataWatcher',f=r.resolve(__dirname,global.geoDataDir||process.env.geoDataDIR||'../data/'),s={city:r.join(f,'geoip-city.dat'),city6:r.join(f,'geoip-city6.dat'),cityNames:r.join(f,'geoip-city-names.dat'),country:r.join(f,'geoip-country.dat'),country6:r.join(f,'geoip-country6.dat')},l=[[i.aton4('10.0.0.0'),i.aton4('10.255.255.255')],[i.aton4('172.16.0.0'),i.aton4('172.31.255.255')],[i.aton4('192.168.0.0'),i.aton4('192.168.255.255')]],u={firstIP:null,lastIP:null,lastLine:0,locationBuffer:null,locationRecordSize:88,mainBuffer:null,recordSize:24},d={firstIP:null,lastIP:null,lastLine:0,mainBuffer:null,recordSize:48};let y=JSON.parse(JSON.stringify(u)),S=JSON.parse(JSON.stringify(d));const p=e=>{let t,r,n=0,i=y.lastIP,o=y.lastLine,a=y.firstIP;const c=y.mainBuffer,f=y.locationBuffer,s=l,u=y.recordSize,d=y.locationRecordSize;let S;const p={range:'',country:'',region:'',eu:'',timezone:'',city:'',ll:[0,0]};if(e>y.lastIP||e<y.firstIP)return null;for(S=0;S<s.length;S++)if(e>=s[S][0]&&e<=s[S][1])return null;for(;;){if(t=Math.round((o-n)/2)+n,i=c.readUInt32BE(t*u),a=c.readUInt32BE(t*u+4),i<=e&&a>=e)return p.range=[i,a],10===u?p.country=c.toString('utf8',t*u+8,t*u+10):(r=c.readUInt32BE(t*u+8),-1>>>0>r&&(p.country=f.toString('utf8',r*d,r*d+2).replace(/\u0000.*/,''),p.region=f.toString('utf8',r*d+2,r*d+5).replace(/\u0000.*/,''),p.metro=f.readInt32BE(r*d+5),p.ll[0]=c.readInt32BE(t*u+12)/1e4,p.ll[1]=c.readInt32BE(t*u+16)/1e4,p.area=c.readUInt32BE(t*u+20),p.eu=f.toString('utf8',r*d+9,r*d+10).replace(/\u0000.*/,''),p.timezone=f.toString('utf8',r*d+10,r*d+42).replace(/\u0000.*/,''),p.city=f.toString('utf8',r*d+42,r*d+d).replace(/\u0000.*/,''))),p;if(n===o)return null;n===o-1?t===n?n=o:o=n:i>e?o=t:a<e&&(n=t)}};function B(t){let r,i;const o=JSON.parse(JSON.stringify(u));if('function'==typeof arguments[0])n.series([t=>{n.series([t=>{e.open(s.cityNames,'r',((e,n)=>{r=n,t(e)}))},t=>{e.fstat(r,((e,r)=>{i=r.size,o.locationBuffer=Buffer.alloc(i),t(e)}))},t=>{e.read(r,o.locationBuffer,0,i,0,t)},t=>{e.close(r,t)},t=>{e.open(s.city,'r',((e,n)=>{r=n,t(e)}))},t=>{e.fstat(r,((e,r)=>{i=r.size,t(e)}))}],(n=>{if(n){if('ENOENT'!==n.code&&'EBADF'!==n.code)throw n;e.open(s.country,'r',((n,a)=>{n?t(n):(r=a,e.fstat(r,((e,r)=>{i=r.size,o.recordSize=10,t()})))}))}else t()}))},()=>{o.mainBuffer=Buffer.alloc(i),n.series([t=>{e.read(r,o.mainBuffer,0,i,0,t)},t=>{e.close(r,t)}],(e=>{e||(o.lastLine=i/o.recordSize-1,o.lastIP=o.mainBuffer.readUInt32BE(o.lastLine*o.recordSize+4),o.firstIP=o.mainBuffer.readUInt32BE(0),y=o),t(e)}))}]);else{try{if(r=e.openSync(s.cityNames,'r'),i=e.fstatSync(r).size,0===i)throw{code:'EMPTY_FILE'};y.locationBuffer=Buffer.alloc(i),e.readSync(r,y.locationBuffer,0,i,0),e.closeSync(r),r=e.openSync(s.city,'r'),i=e.fstatSync(r).size}catch(t){if('ENOENT'!==t.code&&'EBADF'!==t.code&&'EMPTY_FILE'!==t.code)throw t;r=e.openSync(s.country,'r'),i=e.fstatSync(r).size,y.recordSize=10}y.mainBuffer=Buffer.alloc(i),e.readSync(r,y.mainBuffer,0,i,0),e.closeSync(r),y.lastLine=i/y.recordSize-1,y.lastIP=y.mainBuffer.readUInt32BE(y.lastLine*y.recordSize+4),y.firstIP=y.mainBuffer.readUInt32BE(0)}}function g(t){let r,i;const o=JSON.parse(JSON.stringify(d));if('function'==typeof arguments[0])n.series([t=>{n.series([t=>{e.open(s.city6,'r',((e,n)=>{r=n,t(e)}))},t=>{e.fstat(r,((e,r)=>{i=r.size,t(e)}))}],(n=>{if(n){if('ENOENT'!==n.code&&'EBADF'!==n.code)throw n;e.open(s.country6,'r',((n,a)=>{n?t(n):(r=a,e.fstat(r,((e,r)=>{i=r.size,o.recordSize=34,t()})))}))}else t()}))},()=>{o.mainBuffer=Buffer.alloc(i),n.series([t=>{e.read(r,o.mainBuffer,0,i,0,t)},t=>{e.close(r,t)}],(e=>{e||(o.lastLine=i/o.recordSize-1,S=o),t(e)}))}]);else{try{if(r=e.openSync(s.city6,'r'),i=e.fstatSync(r).size,0===i)throw{code:'EMPTY_FILE'}}catch(t){if('ENOENT'!==t.code&&'EBADF'!==t.code&&'EMPTY_FILE'!==t.code)throw t;r=e.openSync(s.country6,'r'),i=e.fstatSync(r).size,S.recordSize=34}S.mainBuffer=Buffer.alloc(i),e.readSync(r,S.mainBuffer,0,i,0),e.closeSync(r),S.lastLine=i/S.recordSize-1}}module.exports={cmp:i.cmp,lookup:e=>{if(!e)return null;if('number'==typeof e)return p(e);if(4===t.isIP(e))return p(i.aton4(e));if(6===t.isIP(e)){const t=(e=>{const t=e.toUpperCase(),r=['0:0:0:0:0:FFFF:','::FFFF:'];for(let e=0;e<r.length;e++){const n=r[e];if(0===t.indexOf(n))return t.substring(n.length)}return null})(e);return t?p(i.aton4(t)):(e=>{const t=S.mainBuffer,r=S.recordSize,n=y.locationBuffer,o=y.locationRecordSize,a={range:'',country:'',region:'',city:'',ll:[0,0]},c=(e,n)=>{let i;const o=[];for(i=0;i<2;i++)o.push(t.readUInt32BE(e*r+16*n+4*i));return o};S.lastIP=c(S.lastLine,1),S.firstIP=c(0,0);let f,s,l=0,u=S.lastIP,d=S.lastLine,p=S.firstIP;if(i.cmp6(e,S.lastIP)>0||i.cmp6(e,S.firstIP)<0)return null;for(;;){if(f=Math.round((d-l)/2)+l,u=c(f,0),p=c(f,1),i.cmp6(u,e)<=0&&i.cmp6(p,e)>=0)return 34===r?a.country=t.toString('utf8',f*r+32,f*r+34).replace(/\u0000.*/,''):(s=t.readUInt32BE(f*r+32),-1>>>0>s&&(a.country=n.toString('utf8',s*o,s*o+2).replace(/\u0000.*/,''),a.region=n.toString('utf8',s*o+2,s*o+5).replace(/\u0000.*/,''),a.metro=n.readInt32BE(s*o+5),a.ll[0]=t.readInt32BE(f*r+36)/1e4,a.ll[1]=t.readInt32BE(f*r+40)/1e4,a.area=t.readUInt32BE(f*r+44),a.eu=n.toString('utf8',s*o+9,s*o+10).replace(/\u0000.*/,''),a.timezone=n.toString('utf8',s*o+10,s*o+42).replace(/\u0000.*/,''),a.city=n.toString('utf8',s*o+42,s*o+o).replace(/\u0000.*/,''))),a;if(l===d)return null;l===d-1?f===l?l=d:d=l:i.cmp6(u,e)>0?d=f:i.cmp6(p,e)<0&&(l=f)}})(i.aton6(e))}return null},pretty:e=>'string'==typeof e?e:'number'==typeof e?i.ntoa4(e):Array.isArray(e)?i.ntoa6(e):e,startWatchingDataUpdate:e=>{o.makeFsWatchFilter(c,f,6e4,(()=>{n.series([e=>{B(e)},e=>{g(e)}],e)}))},stopWatchingDataUpdate:()=>{o.stopWatching(c)},clear:()=>{y=JSON.parse(JSON.stringify(u)),S=JSON.parse(JSON.stringify(d))},reloadDataSync:()=>{B(),g()},reloadData:e=>{n.series([e=>{B(e)},e=>{g(e)}],e)},version:a},B(),g(); | ||
const e=require('fs'),t=require('net'),r=require('path'),n=require('async'),i=require('./utils.js'),o=require('./fsWatcher.js'),{version:a}=require('../package.json');e.existsSync=e.existsSync||r.existsSync;const c='dataWatcher',f=r.resolve(__dirname,global.geoDataDir||process.env.geoDataDIR||'../data/'),s={city:r.join(f,'geoip-city.dat'),city6:r.join(f,'geoip-city6.dat'),cityNames:r.join(f,'geoip-city-names.dat'),country:r.join(f,'geoip-country.dat'),country6:r.join(f,'geoip-country6.dat')},l=[[i.aton4('10.0.0.0'),i.aton4('10.255.255.255')],[i.aton4('172.16.0.0'),i.aton4('172.31.255.255')],[i.aton4('192.168.0.0'),i.aton4('192.168.255.255')]],u={firstIP:null,lastIP:null,lastLine:0,locationBuffer:null,locationRecordSize:88,mainBuffer:null,recordSize:24},d={firstIP:null,lastIP:null,lastLine:0,mainBuffer:null,recordSize:48};let y=JSON.parse(JSON.stringify(u)),S=JSON.parse(JSON.stringify(d));const p=e=>{let t,r,n=0,i=y.lastIP,o=y.lastLine,a=y.firstIP;const c=y.mainBuffer,f=y.locationBuffer,s=l,u=y.recordSize,d=y.locationRecordSize;let S;const p={range:'',country:'',region:'',eu:'',timezone:'',city:'',ll:[0,0]};if(e>y.lastIP||e<y.firstIP)return null;for(S=0;S<s.length;S++)if(e>=s[S][0]&&e<=s[S][1])return null;for(;;){if(t=Math.round((o-n)/2)+n,i=c.readUInt32BE(t*u),a=c.readUInt32BE(t*u+4),i<=e&&a>=e)return p.range=[i,a],10===u?p.country=c.toString('utf8',t*u+8,t*u+10):(r=c.readUInt32BE(t*u+8),-1>>>0>r&&(p.country=f.toString('utf8',r*d,r*d+2).replace(/\u0000.*/,''),p.region=f.toString('utf8',r*d+2,r*d+5).replace(/\u0000.*/,''),p.metro=f.readInt32BE(r*d+5),p.ll[0]=c.readInt32BE(t*u+12)/1e4,p.ll[1]=c.readInt32BE(t*u+16)/1e4,p.area=c.readUInt32BE(t*u+20),p.eu=f.toString('utf8',r*d+9,r*d+10).replace(/\u0000.*/,''),p.timezone=f.toString('utf8',r*d+10,r*d+42).replace(/\u0000.*/,''),p.city=f.toString('utf8',r*d+42,r*d+d).replace(/\u0000.*/,''))),p;if(n===o)return null;n===o-1?t===n?n=o:o=n:i>e?o=t:a<e&&(n=t)}};function B(t){let r,i;const o=JSON.parse(JSON.stringify(u));if('function'==typeof arguments[0])n.series([t=>{n.series([t=>{e.open(s.cityNames,'r',((e,n)=>{r=n,t(e)}))},t=>{e.fstat(r,((e,r)=>{i=r.size,o.locationBuffer=Buffer.alloc(i),t(e)}))},t=>{e.read(r,o.locationBuffer,0,i,0,t)},t=>{e.close(r,t)},t=>{e.open(s.city,'r',((e,n)=>{r=n,t(e)}))},t=>{e.fstat(r,((e,r)=>{i=r.size,t(e)}))}],(n=>{if(n){if('ENOENT'!==n.code&&'EBADF'!==n.code)throw n;e.open(s.country,'r',((n,a)=>{n?t(n):(r=a,e.fstat(r,((e,r)=>{i=r.size,o.recordSize=10,t()})))}))}else t()}))},()=>{o.mainBuffer=Buffer.alloc(i),n.series([t=>{e.read(r,o.mainBuffer,0,i,0,t)},t=>{e.close(r,t)}],(e=>{e||(o.lastLine=i/o.recordSize-1,o.lastIP=o.mainBuffer.readUInt32BE(o.lastLine*o.recordSize+4),o.firstIP=o.mainBuffer.readUInt32BE(0),y=o),t(e)}))}]);else{try{if(r=e.openSync(s.cityNames,'r'),i=e.fstatSync(r).size,0===i)throw{code:'EMPTY_FILE'};y.locationBuffer=Buffer.alloc(i),e.readSync(r,y.locationBuffer,0,i,0),e.closeSync(r),r=e.openSync(s.city,'r'),i=e.fstatSync(r).size}catch(t){if('ENOENT'!==t.code&&'EBADF'!==t.code&&'EMPTY_FILE'!==t.code)throw t;r=e.openSync(s.country,'r'),i=e.fstatSync(r).size,y.recordSize=10}y.mainBuffer=Buffer.alloc(i),e.readSync(r,y.mainBuffer,0,i,0),e.closeSync(r),y.lastLine=i/y.recordSize-1,y.lastIP=y.mainBuffer.readUInt32BE(y.lastLine*y.recordSize+4),y.firstIP=y.mainBuffer.readUInt32BE(0)}}function g(t){let r,i;const o=JSON.parse(JSON.stringify(d));if('function'==typeof arguments[0])n.series([t=>{n.series([t=>{e.open(s.city6,'r',((e,n)=>{r=n,t(e)}))},t=>{e.fstat(r,((e,r)=>{i=r.size,t(e)}))}],(n=>{if(n){if('ENOENT'!==n.code&&'EBADF'!==n.code)throw n;e.open(s.country6,'r',((n,a)=>{n?t(n):(r=a,e.fstat(r,((e,r)=>{i=r.size,o.recordSize=34,t()})))}))}else t()}))},()=>{o.mainBuffer=Buffer.alloc(i),n.series([t=>{e.read(r,o.mainBuffer,0,i,0,t)},t=>{e.close(r,t)}],(e=>{e||(o.lastLine=i/o.recordSize-1,S=o),t(e)}))}]);else{try{if(r=e.openSync(s.city6,'r'),i=e.fstatSync(r).size,0===i)throw{code:'EMPTY_FILE'}}catch(t){if('ENOENT'!==t.code&&'EBADF'!==t.code&&'EMPTY_FILE'!==t.code)throw t;r=e.openSync(s.country6,'r'),i=e.fstatSync(r).size,S.recordSize=34}S.mainBuffer=Buffer.alloc(i),e.readSync(r,S.mainBuffer,0,i,0),e.closeSync(r),S.lastLine=i/S.recordSize-1}}module.exports={cmp:i.cmp,lookup:e=>{if(!e)return null;if('number'==typeof e)return p(e);if(4===t.isIP(e))return p(i.aton4(e));if(6===t.isIP(e)){const t=(e=>{const t=e.toUpperCase(),r=['0:0:0:0:0:FFFF:','::FFFF:'];for(let e=0;e<r.length;e++){const n=r[e];if(0===t.indexOf(n))return t.substring(n.length)}return null})(e);return t?p(i.aton4(t)):(e=>{const t=S.mainBuffer,r=S.recordSize,n=y.locationBuffer,o=y.locationRecordSize,a={range:'',country:'',region:'',city:'',ll:[0,0]},c=(e,n)=>{let i;const o=[];for(i=0;i<2;i++)o.push(t.readUInt32BE(e*r+16*n+4*i));return o};S.lastIP=c(S.lastLine,1),S.firstIP=c(0,0);let f,s,l=0,u=S.lastIP,d=S.lastLine,p=S.firstIP;if(i.cmp6(e,S.lastIP)>0||i.cmp6(e,S.firstIP)<0)return null;for(;;){if(f=Math.round((d-l)/2)+l,u=c(f,0),p=c(f,1),i.cmp6(u,e)<=0&&i.cmp6(p,e)>=0)return 34===r?a.country=t.toString('utf8',f*r+32,f*r+34).replace(/\u0000.*/,''):(s=t.readUInt32BE(f*r+32),-1>>>0>s&&(a.country=n.toString('utf8',s*o,s*o+2).replace(/\u0000.*/,''),a.region=n.toString('utf8',s*o+2,s*o+5).replace(/\u0000.*/,''),a.metro=n.readInt32BE(s*o+5),a.ll[0]=t.readInt32BE(f*r+36)/1e4,a.ll[1]=t.readInt32BE(f*r+40)/1e4,a.area=t.readUInt32BE(f*r+44),a.eu=n.toString('utf8',s*o+9,s*o+10).replace(/\u0000.*/,''),a.timezone=n.toString('utf8',s*o+10,s*o+42).replace(/\u0000.*/,''),a.city=n.toString('utf8',s*o+42,s*o+o).replace(/\u0000.*/,''))),a;if(l===d)return null;l===d-1?f===l?l=d:d=l:i.cmp6(u,e)>0?d=f:i.cmp6(p,e)<0&&(l=f)}})(i.aton6(e))}return null},pretty:e=>'string'==typeof e?e:'number'==typeof e?i.ntoa4(e):Array.isArray(e)?i.ntoa6(e):e,startWatchingDataUpdate:e=>{o.makeFsWatchFilter(c,f,6e4,(()=>{n.series([e=>{B(e)},e=>{g(e)}],e)}))},stopWatchingDataUpdate:()=>{o.stopWatching(c)},clear:()=>{y=JSON.parse(JSON.stringify(u)),S=JSON.parse(JSON.stringify(d))},reloadDataSync:()=>{B(),g()},reloadData:e=>{n.series([e=>{B(e)},e=>{g(e)}],e)},version:a},B(),g(); |
{ | ||
"name": "geoip-lite2", | ||
"version": "2.1.21", | ||
"version": "2.1.22", | ||
"description": "A light weight native JavaScript implementation of GeoIP API from MaxMind. Improved and faster version by Sefinek.", | ||
@@ -66,3 +66,2 @@ "keywords": [ | ||
"ip-address": "^9.0.5", | ||
"lazy": "1.0.11", | ||
"rimraf": "^5.0.5" | ||
@@ -75,4 +74,4 @@ }, | ||
"engines": { | ||
"node": ">=18.18.0" | ||
"node": ">=16.20.2" | ||
} | ||
} |
<div align="center"> | ||
<a href="https://cdn.sefinek.net/images/npm/geoip-lite2/banner.jpg" target="_blank" title="Full screen"> | ||
<img src="https://cdn.sefinek.net/images/npm/geoip-lite2/banner.jpg" alt="GeoIP-Lite 2.1 πΊοΈ Now even faster!"> | ||
<img src="https://cdn.sefinek.net/images/npm/geoip-lite2/banner.png" alt="GeoIP-Lite v2.1 banner"> | ||
</a> | ||
@@ -42,6 +42,6 @@ <br> | ||
---------------- | ||
You can see this module in action on my [official API](https://sefinek.net). Generally speaking, the API interface is public, and you can safely use it in your projects. Happy coding! | ||
You can see this module in action using my [official API](https://sefinek.net). Generally speaking, the API interface is public, and you can safely use it in your projects. Happy coding! | ||
> Docs: https://api.sefinek.net/docs/v2 | ||
> Example: https://api.sefinek.net/api/v2/geoip/109.207.159.255 | ||
> Example: https://api.sefinek.net/api/v2/geoip/109.207.159.255?validation=false | ||
@@ -48,0 +48,0 @@ |
@@ -1,1 +0,1 @@ | ||
const e=require('../lib/main.js');describe('GeoIP2',(()=>{describe('#testLookup()',(()=>{it('should return data about IPv4',(()=>{const t=e.lookup('1.1.1.1');expect(t).toBeTruthy()})),it('should return data about IPv6',(()=>{const t=e.lookup('2606:4700:4700::64');expect(t).toBeTruthy()}))})),describe('#testDataIP4()',(()=>{it('should match data for IPv4 - US',(()=>{const t=e.lookup('72.229.28.185');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('US'),expect(t.region).toBe('NY'),expect(t.eu).toBe('0'),expect(t.timezone).toBe('America/New_York'),expect(t.city).toBe('New York'),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(501),expect(t.area).toBe(5)})),it('should match data for IPv4 - JP',(()=>{const t=e.lookup('210.138.184.59');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('JP'),expect(t.region).toBe('13'),expect(t.eu).toBe('0'),expect(t.timezone).toBe('Asia/Tokyo'),expect(t.city).toBe(''),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(200)})),it('should match data for IPv4 - PL',(()=>{const t=e.lookup('104.113.255.255');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('PL'),expect(t.region).toBe('14'),expect(t.eu).toBe('1'),expect(t.timezone).toBe('Europe/Warsaw'),expect(t.city).toBe('Warsaw'),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(20)})),it('should match data for IPv4 - RU',(()=>{const t=e.lookup('109.108.63.255');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('RU'),expect(t.region).toBe('IVA'),expect(t.eu).toBe('0'),expect(t.timezone).toBe('Europe/Moscow'),expect(t.city).toBe('Kineshma'),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(200)}))})),describe('#testDataIP6()',(()=>{it('should match data for IPv6',(()=>{const t=e.lookup('2001:1c04:400::1');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('NL'),expect(t.region).toBe('NH'),expect(t.eu).toBe('1'),expect(t.timezone).toBe('Europe/Amsterdam'),expect(t.city).toBe('Zandvoort'),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(5)})),it('should match data for IPv4 - JP',(()=>{const t=e.lookup('2400:8500:1302:814:a163:44:173:238f');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('JP'),expect(t.region).toBe(''),expect(t.eu).toBe('0'),expect(t.timezone).toBe('Asia/Tokyo'),expect(t.city).toBe(''),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(500)})),it('should match data for IPv4 - JP',(()=>{const t=e.lookup('1.79.255.115');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('JP'),expect(t.region).toBe(''),expect(t.eu).toBe('0'),expect(t.timezone).toBe('Asia/Tokyo'),expect(t.city).toBe(''),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(500)}))})),describe('#testUTF8()',(()=>{it('should return UTF8 city name',(()=>{const t=e.lookup('2.139.175.1');expect(t).toBeTruthy(),expect(t.city).toBe('Barbera Del Valles')}))})),describe('#testMetro()',(()=>{it('should match metro data',(()=>{const t=e.lookup('23.240.63.68');expect(t.metro).toBe(803)}))})),describe('#testIPv4MappedIPv6()',(()=>{it('should match IPv4 mapped IPv6 data',(()=>{const t=e.lookup('195.16.170.74');expect(t.metro).toBe(0)}))})),describe('#testSyncReload()',(()=>{it('should reload data synchronously',(()=>{const t=e.lookup('75.82.117.180');expect(t).not.toBeNull();const o=e.lookup('::ffff:173.185.182.82');expect(o).not.toBeNull(),e.clear();const c=e.lookup('75.82.117.180');expect(c).toBeNull();const l=e.lookup('::ffff:173.185.182.82');expect(l).toBeNull(),e.reloadDataSync();const p=e.lookup('75.82.117.180');expect(t).toEqual(p);const r=e.lookup('::ffff:173.185.182.82');expect(o).toEqual(r)}))})),describe('#testAsyncReload()',(()=>{it('should reload data asynchronously',(t=>{const o=e.lookup('75.82.117.180');expect(o).not.toBeNull();const c=e.lookup('::ffff:173.185.182.82');expect(c).not.toBeNull(),e.clear();const l=e.lookup('75.82.117.180');expect(l).toBeNull();const p=e.lookup('::ffff:173.185.182.82');expect(p).toBeNull(),e.reloadData((()=>{const l=e.lookup('75.82.117.180');expect(o).toEqual(l);const p=e.lookup('::ffff:173.185.182.82');expect(c).toEqual(p),t()}))}))})),describe('#testInvalidIP()',(()=>{it('should return null for an invalid IP address',(()=>{const t=e.lookup('invalid_ip_address');expect(t).toBeNull()}))})),describe('#testEmptyIP()',(()=>{it('should return null for an empty IP address',(()=>{const t=e.lookup('');expect(t).toBeNull()}))})),describe('#testNullIP()',(()=>{it('should return null for a null IP address',(()=>{const t=e.lookup(null);expect(t).toBeNull()}))})),describe('#testUnknownIP()',(()=>{it('should return null for an unknown IP address',(()=>{const t=e.lookup('192.168.1.1');expect(t).toBeNull()}))})),describe('#testNoDataForIP()',(()=>{it('should return null for an IP address with no data',(()=>{const t=e.lookup('203.0.113.0');expect(t).toBeNull()}))})),describe('#testSpecialCharactersIP()',(()=>{it('should return null for an IP address with special characters',(()=>{const t=e.lookup('1.2.3.@');expect(t).toBeNull()}))}))})); | ||
const e=require('../lib/main.js');describe('GeoIP2',(()=>{describe('#testLookup()',(()=>{it('should return data about IPv4',(()=>{const t=e.lookup('1.1.1.1');expect(t).toBeTruthy()})),it('should return data about IPv6',(()=>{const t=e.lookup('2606:4700:4700::64');expect(t).toBeTruthy()}))})),describe('#testDataIP4()',(()=>{it('should match data for IPv4 - US',(()=>{const t=e.lookup('72.229.28.185');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('US'),expect(t.region).toBe('NY'),expect(t.eu).toBe('0'),expect(t.timezone).toBe('America/New_York'),expect(t.city).toBe('New York'),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(501),expect(t.area).toBe(5)})),it('should match data for IPv4 - JP',(()=>{const t=e.lookup('210.138.184.59');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('JP'),expect(t.region).toBe('13'),expect(t.eu).toBe('0'),expect(t.timezone).toBe('Asia/Tokyo'),expect(t.city).toBe(''),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(500)})),it('should match data for IPv4 - PL',(()=>{const t=e.lookup('104.113.255.255');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('PL'),expect(t.region).toBe('14'),expect(t.eu).toBe('1'),expect(t.timezone).toBe('Europe/Warsaw'),expect(t.city).toBe('Warsaw'),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(20)})),it('should match data for IPv4 - RU',(()=>{const t=e.lookup('109.108.63.255');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('RU'),expect(t.region).toBe('IVA'),expect(t.eu).toBe('0'),expect(t.timezone).toBe('Europe/Moscow'),expect(t.city).toBe('Kineshma'),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(200)}))})),describe('#testDataIP6()',(()=>{it('should match data for IPv6',(()=>{const t=e.lookup('2001:1c04:400::1');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('NL'),expect(t.region).toBe('NH'),expect(t.eu).toBe('1'),expect(t.timezone).toBe('Europe/Amsterdam'),expect(t.city).toBe('Zandvoort'),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(5)})),it('should match data for IPv4 - JP',(()=>{const t=e.lookup('2400:8500:1302:814:a163:44:173:238f');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('JP'),expect(t.region).toBe(''),expect(t.eu).toBe('0'),expect(t.timezone).toBe('Asia/Tokyo'),expect(t.city).toBe(''),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(500)})),it('should match data for IPv4 - JP',(()=>{const t=e.lookup('1.79.255.115');expect(void 0!==t.range).toBe(!0),expect(t.country).toBe('JP'),expect(t.region).toBe(''),expect(t.eu).toBe('0'),expect(t.timezone).toBe('Asia/Tokyo'),expect(t.city).toBe(''),expect(t.ll).toBeTruthy(),expect(t.metro).toBe(0),expect(t.area).toBe(500)}))})),describe('#testUTF8()',(()=>{it('should return UTF8 city name',(()=>{const t=e.lookup('2.139.175.1');expect(t).toBeTruthy(),expect(t.city).toBe('Madrid')}))})),describe('#testMetro()',(()=>{it('should match metro data',(()=>{const t=e.lookup('23.240.63.68');expect(t.metro).toBe(803)}))})),describe('#testIPv4MappedIPv6()',(()=>{it('should match IPv4 mapped IPv6 data',(()=>{const t=e.lookup('195.16.170.74');expect(t.metro).toBe(0)}))})),describe('#testSyncReload()',(()=>{it('should reload data synchronously',(()=>{const t=e.lookup('75.82.117.180');expect(t).not.toBeNull();const o=e.lookup('::ffff:173.185.182.82');expect(o).not.toBeNull(),e.clear();const c=e.lookup('75.82.117.180');expect(c).toBeNull();const l=e.lookup('::ffff:173.185.182.82');expect(l).toBeNull(),e.reloadDataSync();const p=e.lookup('75.82.117.180');expect(t).toEqual(p);const r=e.lookup('::ffff:173.185.182.82');expect(o).toEqual(r)}))})),describe('#testAsyncReload()',(()=>{it('should reload data asynchronously',(t=>{const o=e.lookup('75.82.117.180');expect(o).not.toBeNull();const c=e.lookup('::ffff:173.185.182.82');expect(c).not.toBeNull(),e.clear();const l=e.lookup('75.82.117.180');expect(l).toBeNull();const p=e.lookup('::ffff:173.185.182.82');expect(p).toBeNull(),e.reloadData((()=>{const l=e.lookup('75.82.117.180');expect(o).toEqual(l);const p=e.lookup('::ffff:173.185.182.82');expect(c).toEqual(p),t()}))}))})),describe('#testInvalidIP()',(()=>{it('should return null for an invalid IP address',(()=>{const t=e.lookup('invalid_ip_address');expect(t).toBeNull()}))})),describe('#testEmptyIP()',(()=>{it('should return null for an empty IP address',(()=>{const t=e.lookup('');expect(t).toBeNull()}))})),describe('#testNullIP()',(()=>{it('should return null for a null IP address',(()=>{const t=e.lookup(null);expect(t).toBeNull()}))})),describe('#testUnknownIP()',(()=>{it('should return null for an unknown IP address',(()=>{const t=e.lookup('192.168.1.1');expect(t).toBeNull()}))})),describe('#testNoDataForIP()',(()=>{it('should return null for an IP address with no data',(()=>{const t=e.lookup('203.0.113.0');expect(t).toBeNull()}))})),describe('#testSpecialCharactersIP()',(()=>{it('should return null for an IP address with special characters',(()=>{const t=e.lookup('1.2.3.@');expect(t).toBeNull()}))}))})); |
@@ -1,1 +0,1 @@ | ||
'use strict';const{name:e,version:t}=require('../package.json'),n=`Mozilla/5.0 (compatible; ${e}/${t}; +https://sefinek.net)`,r=require('node:fs'),o=require('node:http'),s=require('node:https'),i=require('node:path'),c=require('node:url'),a=require('node:zlib'),l=require('readline');r.existsSync=r.existsSync||i.existsSync;const d=require('async'),u=require('chalk'),p=require('iconv-lite'),f=require('lazy'),g=require('rimraf').sync,w=require('adm-zip'),h=require('../lib/utils.js'),{Address6:m,Address4:y}=require('ip-address'),I=process.argv.slice(2);let S=I.find((function(e){return null!==e.match(/^license_key=[a-zA-Z0-9]+/)}));void 0===S&&void 0!==process.env.LICENSE_KEY&&(S='license_key='+process.env.LICENSE_KEY);let v=I.find((e=>null!==e.match(/^geoDataDir=[\w./]+/)));void 0===v&&void 0!==process.env.GEODATADIR&&(v='geoDataDir='+process.env.GEODATADIR);let E=i.resolve(__dirname,'..','data');void 0!==v&&(E=i.resolve(process.cwd(),v.split('=')[1]),r.existsSync(E)||(console.log(u.red('ERROR')+': Directory doesn\'t exist: '+E),process.exit(1)));const x=process.env.GEOTMPDIR?process.env.GEOTMPDIR:i.resolve(__dirname,'..','tmp'),k={},D={NaN:-1},R=[{type:'country',url:'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country-CSV&suffix=zip&'+S,checksum:'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country-CSV&suffix=zip.sha256&'+S,fileName:'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:'city',url:'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City-CSV&suffix=zip&'+S,checksum:'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City-CSV&suffix=zip.sha256&'+S,fileName:'GeoLite2-City-CSV.zip',src:['GeoLite2-City-Locations-en.csv','GeoLite2-City-Blocks-IPv4.csv','GeoLite2-City-Blocks-IPv6.csv'],dest:['geoip-city-names.dat','geoip-city.dat','geoip-city6.dat']}];function C(e){const t=i.dirname(e);r.existsSync(t)||r.mkdirSync(t)}function B(e){const t=/^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/;if(!t.test(e)&&(e=function(e){let t=0,n=-1;for(e=e.replace(/""/,'\\"').replace(/'/g,'\\\'');t<e.length&&n<e.length;)t=n,n=e.indexOf(',',t+1),n<0&&(n=e.length),e.indexOf('\'',t||0)>-1&&e.indexOf('\'',t)<n&&'"'!=e[t+1]&&'"'!=e[n-1]&&(n=(e=e.substr(0,t+1)+'"'+e.substr(t+1,n-t-1)+'"'+e.substr(n,e.length-n)).indexOf(',',n+1),n<0&&(n=e.length));return e}(e),!t.test(e)))return null;const n=[];return e.replace(/(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g,(function(e,t,r,o){return void 0!==t?n.push(t.replace(/\\'/g,'\'')):void 0!==r?n.push(r.replace(/\\"/g,'"').replace(/\\'/g,'\'')):void 0!==o&&n.push(o),''})),/,\s*$/.test(e)&&n.push(''),n}function _(e){const t=c.parse(e);if(t.headers={'User-Agent':n},process.env.http_proxy||process.env.https_proxy)try{const e=require('node:https-proxy-agent');t.agent=new e(process.env.http_proxy||process.env.https_proxy)}catch(e){console.error('Install https-proxy-agent to use an HTTP/HTTPS proxy'),process.exit(-1)}return t}function b(e,t){if(-1!==I.indexOf('force'))return t(null,e);const n=e.checksum;if(void 0===n)return t(null,e);r.readFile(i.join(E,e.type+'.checksum'),{encoding:'utf8'},(function(r,i){!r&&i&&i.length&&(e.checkValue=i),console.log('Checking ',e.fileName);var c=s.get(_(n),(function(n){const r=n.statusCode;200!==r&&(console.error(u.red('ERROR')+n.data),console.error(u.red('ERROR')+': HTTP Request Failed [%d %s]',r,o.STATUS_CODES[r]),c.end(),process.exit(1));let s='';n.on('data',(function(e){s+=e})),n.on('end',(()=>{s&&s.length?s===e.checkValue?(console.log(u.green('Database "'+e.type+'" is up to date')),e.skip=!0):(console.log(u.green('Database '+e.type+' has new data')),e.checkValue=s):(console.error(u.red('ERROR')+': Could not retrieve checksum for',e.type,u.red('Aborting')),console.error('Run with "force" to update without checksum'),c.end(),process.exit(1)),t(null,e)}))}))}))}function O(e,t){if(e.skip)return t(null,null,null,e);const n=e.url;let c=e.fileName;const l='.gz'===i.extname(c);l&&(c=c.replace('.gz',''));const d=i.join(x,c);if(r.existsSync(d))return t(null,d,c,e);console.log('Fetching ',c),C(d);var p=s.get(_(n),(function(n){const s=n.statusCode;let i;200!==s&&(console.error(u.red('ERROR')+': HTTP Request Failed [%d %s]',s,o.STATUS_CODES[s]),p.end(),process.exit(1));const f=r.createWriteStream(d);i=l?n.pipe(a.createGunzip()).pipe(f):n.pipe(f),i.on('close',(()=>{console.log(u.green(' DONE')),t(null,d,c,e)}))}));process.stdout.write('Retrieving '+c+'...')}function A(e,t,n,o){if(n.skip)return o(null,n);if('.zip'!==i.extname(t))o(null,n);else{process.stdout.write('Extracting '+t+'...');new w(e).getEntries().forEach((e=>{if(e.isDirectory)return;const t=e.entryName.split('/'),n=t[t.length-1],o=i.join(x,n);r.writeFileSync(o,e.getData())})),console.log(u.green(' DONE')),o(null,n)}}async function q(e,t){let n=0;function o(e){const t=B(e);if(!t||t.length<6)return console.warn('weird line: %s::',e);let r,o,s;n++;const i=k[t[1]];let c,l,u;if(i){if(t[0].match(/:/)){for(l=34,s=new m(t[0]),r=h.aton6(s.startAddress().correctForm()),o=h.aton6(s.endAddress().correctForm()),c=Buffer.alloc(l),u=0;u<r.length;u++)c.writeUInt32BE(r[u],4*u);for(u=0;u<o.length;u++)c.writeUInt32BE(o[u],16+4*u)}else l=10,s=new y(t[0]),r=parseInt(s.startAddress().bigInteger(),10),o=parseInt(s.endAddress().bigInteger(),10),c=Buffer.alloc(l),c.fill(0),c.writeUInt32BE(r,0),c.writeUInt32BE(o,4);return c.write(i,l-2),Date.now()-a>5e3&&(a=Date.now(),process.stdout.write('\nStill working ('+n+')...')),d._writableState.needDrain?new Promise((e=>{d.write(c,e)})):d.write(c)}}const s=i.join(E,t),c=i.join(x,e);g(s),C(s),process.stdout.write('Processing Data (may take a moment) ...');var a=Date.now(),d=r.createWriteStream(s);const u=l.createInterface({input:r.createReadStream(c),crlfDelay:1/0});let p=0;for await(const e of u)p++,1!==p&&await o(e);d.close(),console.log(' DONE'.green)}async function G(e,t){let n=0;async function o(e){if(e.match(/^Copyright/)||!e.match(/\d/))return;const t=B(e);if(!t)return console.warn('Weird line: %s::',e);let r,o,s,i,c,l,u,p,f,g;if(n++,t[0].match(/:/)){let e=0;for(l=48,s=new m(t[0]),r=h.aton6(s.startAddress().correctForm()),o=h.aton6(s.endAddress().correctForm()),i=parseInt(t[1],10),i=D[i],c=Buffer.alloc(l),c.fill(0),g=0;g<r.length;g++)c.writeUInt32BE(r[g],e),e+=4;for(g=0;g<o.length;g++)c.writeUInt32BE(o[g],e),e+=4;c.writeUInt32BE(i>>>0,32),u=Math.round(1e4*parseFloat(t[7])),p=Math.round(1e4*parseFloat(t[8])),f=parseInt(t[9],10),c.writeInt32BE(u,36),c.writeInt32BE(p,40),c.writeInt32BE(f,44)}else l=24,s=new y(t[0]),r=parseInt(s.startAddress().bigInteger(),10),o=parseInt(s.endAddress().bigInteger(),10),i=parseInt(t[1],10),i=D[i],c=Buffer.alloc(l),c.fill(0),c.writeUInt32BE(r>>>0,0),c.writeUInt32BE(o>>>0,4),c.writeUInt32BE(i>>>0,8),u=Math.round(1e4*parseFloat(t[7])),p=Math.round(1e4*parseFloat(t[8])),f=parseInt(t[9],10),c.writeInt32BE(u,12),c.writeInt32BE(p,16),c.writeInt32BE(f,20);return Date.now()-a>5e3&&(a=Date.now(),process.stdout.write('\nStill working ('+n+')...')),d._writableState.needDrain?new Promise((e=>{d.write(c,e)})):d.write(c)}const s=i.join(E,t),c=i.join(x,e);g(s),process.stdout.write('Processing data (may take a moment) ...');var a=Date.now(),d=r.createWriteStream(s);const u=l.createInterface({input:r.createReadStream(c),crlfDelay:1/0});let p=0;for await(const e of u)p++,1!==p&&await o(e);d.close()}function L(e,t){if(e.skip)return t(null,e);const n=e.type,o=e.src,s=e.dest;'country'===n?Array.isArray(o)?function(e,t){const n=i.join(x,e);process.stdout.write('Processing Lookup Data (may take a moment)...'),f(r.createReadStream(n)).lines.map((e=>p.decode(e,'latin1'))).skip(1).map((function(e){const t=B(e);!t||t.length<6?console.log('weird line: %s::',e):k[t[0]]=t[4]})).on('pipe',(()=>{console.log(u.green(' DONE')),t()}))}(o[0],(()=>{q(o[1],s[1]).then((()=>q(o[2],s[2]))).then((()=>{t(null,e)}))})):q(o,s):'city'===n&&function(e,t,n){let o=null,s=0;const c=i.join(E,t),a=i.join(x,e);g(c);var l=r.openSync(c,'w');f(r.createReadStream(a)).lines.map((e=>p.decode(e,'utf-8'))).skip(1).map((function(e){if(e.match(/^Copyright/)||!e.match(/\d/))return;const t=Buffer.alloc(88),n=B(e);if(!n)return void console.warn('Weird line: %s::',e);o=parseInt(n[0]),D[o]=s;const i=n[4],c=n[6],a=n[10],d=parseInt(n[11]),u=n[12],p=n[13];t.fill(0),t.write(i,0),t.write(c,2),d&&t.writeInt32BE(d,5),t.write(p,9),t.write(u,10),t.write(a,42),r.writeSync(l,t,0,t.length,null),s++})).on('pipe',n)}(o[0],s[0],(()=>{G(o[1],s[1]).then((()=>(console.log('City data processed'),G(o[2],s[2])))).then((()=>{console.log(u.green(' DONE')),t(null,e)}))}))}function F(e,t){if(e.skip||!e.checkValue)return t();r.writeFile(i.join(E,e.type+'.checksum'),e.checkValue,'utf8',(function(n){n&&console.log(u.red('Failed to Update checksums.'),'Database:',e.type),t()}))}S||(console.error(u.red('ERROR')+': Missing license_key'),process.exit(1)),g(x),C(x),d.eachSeries(R,((e,t)=>{d.seq(b,O,A,L,F)(e,t)}),(e=>{e?(console.error(u.red('Failed to update databases from MaxMind.'),e),process.exit(1)):(console.log(u.green('Successfully updated databases from MaxMind.')),-1!==I.indexOf('debug')?console.debug(u.blue.bold('Notice: temporary files are not deleted for debug purposes.')):g(x),process.exit(0))})); | ||
'use strict';const{name:e,version:t}=require('../package.json'),n=`Mozilla/5.0 (compatible; ${e}/${t}; +https://sefinek.net)`,r=require('fs'),o=require('http'),s=require('https'),i=require('path'),c=require('zlib'),a=require('readline');r.existsSync=r.existsSync||i.existsSync;const l=require('async'),d=require('chalk'),u=require('iconv-lite'),p=require('rimraf').sync,f=require('adm-zip'),g=require('../lib/utils.js'),{Address6:h,Address4:w}=require('ip-address'),m=process.argv.slice(2);let y=m.find((e=>null!==e.match(/^license_key=[a-zA-Z0-9]+/)));void 0===y&&void 0!==process.env.LICENSE_KEY&&(y=`license_key=${process.env.LICENSE_KEY}`);let I=m.find((e=>null!==e.match(/^geoDataDir=[\w./]+/)));void 0===I&&void 0!==process.env.GEODATADIR&&(I=`geoDataDir=${process.env.GEODATADIR}`);let S=i.resolve(__dirname,'..','data');void 0!==I&&(S=i.resolve(process.cwd(),I.split('=')[1]),r.existsSync(S)||(console.log(d.red('ERROR')+': Directory doesn\'t exist: '+S),process.exit(1)));const v=process.env.GEOTMPDIR?process.env.GEOTMPDIR:i.resolve(__dirname,'..','tmp'),E={},x={NaN:-1},k=[{type:'country',url:`https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country-CSV&suffix=zip&${y}`,checksum:`https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country-CSV&suffix=zip.sha256&${y}`,fileName:'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:'city',url:`https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City-CSV&suffix=zip&${y}`,checksum:`https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City-CSV&suffix=zip.sha256&${y}`,fileName:'GeoLite2-City-CSV.zip',src:['GeoLite2-City-Locations-en.csv','GeoLite2-City-Blocks-IPv4.csv','GeoLite2-City-Blocks-IPv6.csv'],dest:['geoip-city-names.dat','geoip-city.dat','geoip-city6.dat']}];function D(e){const t=i.dirname(e);r.existsSync(t)||r.mkdirSync(t)}const R=/^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/,C=/(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g;function B(e){if(!R.test(e)&&(e=function(e){let t=0,n=-1;for(e=e.replace(/""/,'\\"').replace(/'/g,'\\\'');t<e.length&&n<e.length;)t=n,n=e.indexOf(',',t+1),n<0&&(n=e.length),e.indexOf('\'',t||0)>-1&&e.indexOf('\'',t)<n&&'"'!=e[t+1]&&'"'!=e[n-1]&&(n=(e=e.substr(0,t+1)+'"'+e.substr(t+1,n-t-1)+'"'+e.substr(n,e.length-n)).indexOf(',',n+1),n<0&&(n=e.length));return e}(e),!R.test(e)))return null;const t=[];return e.replace(C,((e,n,r,o)=>(void 0!==n?t.push(n.replace(/\\'/g,'\'')):void 0!==r?t.push(r.replace(/\\"/g,'"').replace(/\\'/g,'\'')):void 0!==o&&t.push(o),''))),/,\s*$/.test(e)&&t.push(''),t}function _(e){const t=new URL(e),r={protocol:t.protocol,host:t.host,path:t.pathname+t.search,headers:{'User-Agent':n}};if(process.env.http_proxy||process.env.https_proxy)try{const e=require('https-proxy-agent');r.agent=new e(process.env.http_proxy||process.env.https_proxy)}catch(e){console.error('Install https-proxy-agent to use an HTTP/HTTPS proxy'),process.exit(-1)}return r}function b(e,t){if(-1!==m.indexOf('force'))return t(null,e);const n=e.checksum;if(void 0===n)return t(null,e);r.readFile(i.join(S,`${e.type}.checksum`),{encoding:'utf8'},((r,i)=>{!r&&i&&i.length&&(e.checkValue=i),console.log('Checking',e.fileName);var c=s.get(_(n),(function(n){const r=n.statusCode;200!==r&&(console.error(d.red('ERROR')+n.data),console.error(d.red('ERROR')+': HTTP Request Failed [%d %s]',r,o.STATUS_CODES[r]),c.end(),process.exit(1));let s='';n.on('data',(e=>{s+=e})),n.on('end',(()=>{s&&s.length?s===e.checkValue?(console.log(d.green('Database "'+e.type+'" is up to date')),e.skip=!0):(console.log(d.green('Database '+e.type+' has new data')),e.checkValue=s):(console.error(d.red('ERROR')+': Could not retrieve checksum for',e.type,d.red('Aborting')),console.error('Run with "force" to update without checksum'),c.end(),process.exit(1)),t(null,e)}))}))}))}function O(e,t){if(e.skip)return t(null,null,null,e);const n=e.url;let a=e.fileName;const l='.gz'===i.extname(a);l&&(a=a.replace('.gz',''));const u=i.join(v,a);if(r.existsSync(u))return t(null,u,a,e);console.log('Fetching',a),D(u);var p=s.get(_(n),(function(n){const s=n.statusCode;let i;200!==s&&(console.error(d.red('ERROR')+': HTTP Request Failed [%d %s]',s,o.STATUS_CODES[s]),p.end(),process.exit(1));const f=r.createWriteStream(u);i=l?n.pipe(c.createGunzip()).pipe(f):n.pipe(f),i.on('close',(()=>{console.log(d.green(' DONE')),t(null,u,a,e)}))}));process.stdout.write(`Retrieving ${a}...`)}function A(e,t,n,o){if(n.skip)return o(null,n);if('.zip'!==i.extname(t))o(null,n);else{process.stdout.write('Extracting '+t+'...');new f(e).getEntries().forEach((e=>{if(e.isDirectory)return;const t=e.entryName.split('/'),n=t[t.length-1],o=i.join(v,n);r.writeFileSync(o,e.getData())})),console.log(d.green(' DONE')),o(null,n)}}function q(e,t){const n=i.join(v,e);process.stdout.write('Processing lookup data (may take a moment)...');const o=a.createInterface({input:r.createReadStream(n).pipe(u.decodeStream('latin1')),output:process.stdout,terminal:!1});let s=0;o.on('line',(e=>{s>0&&function(e){const t=B(e);!t||t.length<6?console.log('Weird line: %s::',e):E[t[0]]=t[4]}(e),s++})),o.on('close',(()=>{console.log(d.green(' DONE')),t()}))}async function G(e,t){let n=0;function o(e){const t=B(e);if(!t||t.length<6)return console.warn('weird line: %s::',e);let r,o,s;n++;const i=E[t[1]];let c,a,d;if(i){if(t[0].match(/:/)){for(a=34,s=new h(t[0]),r=g.aton6(s.startAddress().correctForm()),o=g.aton6(s.endAddress().correctForm()),c=Buffer.alloc(a),d=0;d<r.length;d++)c.writeUInt32BE(r[d],4*d);for(d=0;d<o.length;d++)c.writeUInt32BE(o[d],16+4*d)}else a=10,s=new w(t[0]),r=parseInt(s.startAddress().bigInteger(),10),o=parseInt(s.endAddress().bigInteger(),10),c=Buffer.alloc(a),c.fill(0),c.writeUInt32BE(r,0),c.writeUInt32BE(o,4);return c.write(i,a-2),Date.now()-l>5e3&&(l=Date.now(),process.stdout.write('\nStill working ('+n+')...')),u._writableState.needDrain?new Promise((e=>{u.write(c,e)})):u.write(c)}}const s=i.join(S,t),c=i.join(v,e);p(s),D(s),process.stdout.write('\nProcessing data (may take a moment)...');var l=Date.now(),u=r.createWriteStream(s);const f=a.createInterface({input:r.createReadStream(c),crlfDelay:1/0});let m=0;for await(const e of f)m++,1!==m&&await o(e);u.close(),console.log(d.green(' DONE'))}async function L(e,t){let n=0;async function o(e){if(e.match(/^Copyright/)||!e.match(/\d/))return;const t=B(e);if(!t)return console.warn('Weird line: %s::',e);let r,o,s,i,c,a,u,p,f,m;if(n++,t[0].match(/:/)){let e=0;for(a=48,s=new h(t[0]),r=g.aton6(s.startAddress().correctForm()),o=g.aton6(s.endAddress().correctForm()),i=parseInt(t[1],10),i=x[i],c=Buffer.alloc(a),c.fill(0),m=0;m<r.length;m++)c.writeUInt32BE(r[m],e),e+=4;for(m=0;m<o.length;m++)c.writeUInt32BE(o[m],e),e+=4;c.writeUInt32BE(i>>>0,32),u=Math.round(1e4*parseFloat(t[7])),p=Math.round(1e4*parseFloat(t[8])),f=parseInt(t[9],10),c.writeInt32BE(u,36),c.writeInt32BE(p,40),c.writeInt32BE(f,44)}else a=24,s=new w(t[0]),r=parseInt(s.startAddress().bigInteger(),10),o=parseInt(s.endAddress().bigInteger(),10),i=parseInt(t[1],10),i=x[i],c=Buffer.alloc(a),c.fill(0),c.writeUInt32BE(r>>>0,0),c.writeUInt32BE(o>>>0,4),c.writeUInt32BE(i>>>0,8),u=Math.round(1e4*parseFloat(t[7])),p=Math.round(1e4*parseFloat(t[8])),f=parseInt(t[9],10),c.writeInt32BE(u,12),c.writeInt32BE(p,16),c.writeInt32BE(f,20);return Date.now()-l>5e3&&(l=Date.now(),process.stdout.write('\nStill working ('+n+')...')),d._writableState.needDrain?new Promise((e=>{d.write(c,e)})):d.write(c)}const s=i.join(S,t),c=i.join(v,e);p(s),process.stdout.write('\nProcessing data (may take a moment)...');var l=Date.now(),d=r.createWriteStream(s);const u=a.createInterface({input:r.createReadStream(c),crlfDelay:1/0});let f=0;for await(const e of u)f++,1!==f&&await o(e);d.close()}function F(e,t,n){let o=null,s=0;const c=i.join(S,t),l=i.join(v,e);p(c);var d=r.openSync(c,'w');const f=a.createInterface({input:r.createReadStream(l).pipe(u.decodeStream('utf-8')),output:process.stdout,terminal:!1});let g=0;f.on('line',(e=>{g>0&&function(e){if(e.match(/^Copyright/)||!e.match(/\d/))return;const t=Buffer.alloc(88),n=B(e);if(!n)return void console.warn('Weird line: %s::',e);o=parseInt(n[0]),x[o]=s;const i=n[4],c=n[6],a=n[10],l=parseInt(n[11]),u=n[12],p=n[13];t.fill(0),t.write(i,0),t.write(c,2),l&&t.writeInt32BE(l,5),t.write(p,9),t.write(u,10),t.write(a,42),r.writeSync(d,t,0,t.length,null),s++}(e),g++})),f.on('close',n)}function T(e,t){if(e.skip)return t(null,e);const n=e.type,r=e.src,o=e.dest;'country'===n?Array.isArray(r)?q(r[0],(()=>{G(r[1],o[1]).then((()=>G(r[2],o[2]))).then((()=>{t(null,e)}))})):G(r,o):'city'===n&&F(r[0],o[0],(()=>{L(r[1],o[1]).then((()=>(console.log('\nCity data processed'),L(r[2],o[2])))).then((()=>{console.log(d.green(' DONE')),t(null,e)}))}))}function N(e,t){if(e.skip||!e.checkValue)return t();r.writeFile(i.join(S,e.type+'.checksum'),e.checkValue,'utf8',(n=>{n&&console.log(d.red('Failed to Update checksums!'),'Database:',e.type),t()}))}y||(console.error(d.red('ERROR:'),'Missing license_key'),process.exit(1)),p(v),D(v),l.eachSeries(k,((e,t)=>{l.seq(b,O,A,T,N)(e,t)}),(e=>{e?(console.error(d.red('Failed to update databases from MaxMind'),e),process.exit(1)):(console.log(d.green('Successfully updated databases from MaxMind')),-1!==m.indexOf('debug')?console.debug(d.blue.bold('Notice: temporary files are not deleted for debug purposes')):p(v),process.exit(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
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
Network access
Supply chain riskThis module accesses the network.
Found 4 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
160103736
6
14
9
- Removedlazy@1.0.11
- Removedlazy@1.0.11(transitive)