geoip-country 4.2.124 → 4.2.126

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -96,7 +96,7 @@ A npm script alias has been setup to make this process easy. Please keep in mind
96
96
  rate limits that amount of downloads on their servers.
97
97
 
98
98
  ```shell
99
- npm run-script updatedb --license_key=YOUR_GEOLITE2_LICENSE_KEY
99
+ npm run updatedb --license_key=YOUR_GEOLITE2_LICENSE_KEY
100
100
  or
101
101
  GEOLITE2_LICENSE_KEY=YOUR_GEOLITE2_LICENSE_KEY node scripts/updatedb.js
102
102
  ```
@@ -111,6 +111,13 @@ For creating or updating the database files in custom directory, you need to run
111
111
  If you have no write-access to the `geoip-country` directory, it would be better to set the environment `GEOTMPDATADIR` or CLI parameter `--geotmpdatadir=YYYYY` for temporary directory when updating the database files.
112
112
 
113
113
 
114
+ ## Use ip-location-db database
115
+
116
+ You can use [ip-location-db](https://github.com/sapics/ip-location-db) database with the environment variable `IP_LOCATION_DB` or CLI parameter `--ip_location_db=XXXXX`. For example, if you want to use `geolite2-geo-whois-asn-country`, you can update the database by executing `npm run updatedb --ip_location_db=geolite2-geo-whois-asn`.
117
+
118
+ If you cannot use `geolite2` licence for your use-case, the licence issue can be circumvented by replacing the database in [ip-location-db](https://github.com/sapics/ip-location-db), as there is also a [CC0](https://creativecommons.org/publicdomain/zero/1.0/deed) license database in [ip-location-db](https://github.com/sapics/ip-location-db).
119
+
120
+
114
121
  ## License and EULA
115
122
 
116
123
  Please carefully read the LICENSE and EULA files. This package comes with certain restrictions and obligations, most notably:
@@ -120,6 +127,7 @@ Please carefully read the LICENSE and EULA files. This package comes with certai
120
127
  - to identify specific households or individuals.
121
128
 
122
129
  You can read [the latest version of GeoLite2 EULA](https://www.maxmind.com/en/geolite2/eula).
130
+ GeoLite2 database is provided under [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) by [MaxMind](https://www.maxmind.com/), so, you must create attribusion to [MaxMind](https://www.maxmind.com/) for using GeoLite2 database.
123
131
 
124
132
 
125
133
  ## References
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geoip-country",
3
- "version": "4.2.124",
3
+ "version": "4.2.126",
4
4
  "description": "Less memory usage version of geoip-lite, by supporting only country lookup.",
5
5
  "keywords": [
6
6
  "geo",
@@ -7,13 +7,17 @@ var download_server = process.env.npm_config_geolite2_download_url || process.en
7
7
  var license_key = process.env.npm_config_license_key || process.env.GEOLITE2_LICENSE_KEY || null;
8
8
  var geodatadir = process.env.npm_config_geodatadir || process.env.GEODATADIR;
9
9
  var tmpdatadir = process.env.npm_config_geotmpdatadir || process.env.GEOTMPDATADIR;
10
+ var ip_location_db = process.env.npm_config_ip_location_db || process.env.IP_LOCATION_DB || null;
10
11
  for(var i = 0; i < process.argv.length; ++i){
11
- if(process.argv[i].indexOf('--license_key=') === 0 && !license_key){
12
- license_key = process.argv[i].slice(14);
13
- } else if(process.argv[i].indexOf('--geodatadir=') === 0 && !geodatadir){
14
- geodatadir = process.argv[i].slice(13);
15
- } else if(process.argv[i].indexOf('--geotmpdatadir=') === 0 && !tmpdatadir){
16
- tmpdatadir = process.argv[i].slice(16);
12
+ var arg = process.argv[i];
13
+ if(arg.indexOf('--license_key=') === 0 && !license_key){
14
+ license_key = arg.slice(14);
15
+ } else if(arg.indexOf('--geodatadir=') === 0 && !geodatadir){
16
+ geodatadir = arg.slice(13);
17
+ } else if(arg.indexOf('--geotmpdatadir=') === 0 && !tmpdatadir){
18
+ tmpdatadir = arg.slice(16);
19
+ } else if(arg.indexOf('--ip_location_db=') === 0) {
20
+ ip_location_db = arg.slice(17).replace('-country', '');
17
21
  }
18
22
  }
19
23
  var fs = require('fs');
@@ -148,18 +152,21 @@ function CSVtoArray(text) {
148
152
 
149
153
 
150
154
  function fetch(database, cb) {
151
-
152
- var downloadUrl = download_server + '?edition_id=' + database.edition + '&suffix=' + database.suffix + "&license_key=" + encodeURIComponent(license_key);
153
- var fileName = database.edition + '.' + database.suffix;
155
+ var downloadUrl, fileName;
156
+ if(typeof database === 'string') {
157
+ downloadUrl = database;
158
+ fileName = path.basename(downloadUrl);
159
+ } else {
160
+ downloadUrl = download_server + '?edition_id=' + database.edition + '&suffix=' + database.suffix + "&license_key=" + encodeURIComponent(license_key);
161
+ fileName = database.edition + '.' + database.suffix;
162
+ console.log('Fetching edition ' + database.edition + ' from ' + download_server);
163
+ }
154
164
 
155
165
  var tmpFile = path.join(tmpPath, fileName);
156
-
157
166
  if (fs.existsSync(tmpFile)) {
158
167
  return cb(null, tmpFile, fileName, database);
159
168
  }
160
169
 
161
- console.log('Fetching edition ' + database.edition + ' from ' + download_server);
162
-
163
170
  function getOptions(redirectUrl) {
164
171
  var options = url.parse(redirectUrl || downloadUrl);
165
172
  options.headers = {
@@ -173,7 +180,7 @@ function fetch(database, cb) {
173
180
  }
174
181
  catch (e) {
175
182
  console.error("Install https-proxy-agent to use an HTTP/HTTPS proxy");
176
- process.exit(-1)
183
+ process.exit(-1);
177
184
  }
178
185
  }
179
186
 
@@ -308,7 +315,7 @@ function processCountryData(src, dest, cb) {
308
315
  var nextSip = preEip;
309
316
  for(i = nextSip.length; i--; ) {
310
317
  ++nextSip[i];
311
- if(nextSip === 4294967296) {
318
+ if(nextSip[i] === 4294967296) {
312
319
  nextSip[i] = 0;
313
320
  } else {
314
321
  break;
@@ -398,6 +405,138 @@ function processCountryData(src, dest, cb) {
398
405
  .map(processLine)
399
406
  .on('pipe', function() {
400
407
  console.log(' DONE');
408
+ fs.closeSync(datFile);
409
+ cb();
410
+ });
411
+ }
412
+
413
+ function processCountryDataIpLocationDb(src, fileName, srcUrl, cb) {
414
+ var lines = 0, preCC, pos = 0, preEip = 0;
415
+ function processLine(line) {
416
+ var fields = line.split(',');
417
+
418
+ if (!fields || fields.length < 3) {
419
+ console.log("weird line: %s::", line);
420
+ return;
421
+ }
422
+ lines++;
423
+
424
+ var sip;
425
+ var eip;
426
+ var rngip;
427
+ var cc = fields[2];
428
+ var b;
429
+ var bsz;
430
+ var i;
431
+ if(cc){
432
+ if (src.indexOf('ipv6') > 0) {
433
+ // IPv6
434
+ bsz = 34;
435
+ sip = utils.aton6(fields[0]);
436
+ eip = utils.aton6(fields[1]);
437
+
438
+ if (cc === preCC) {
439
+ var nextSip = preEip;
440
+ for(i = nextSip.length; i--; ) {
441
+ ++nextSip[i];
442
+ if(nextSip[i] === 4294967296) {
443
+ nextSip[i] = 0;
444
+ } else {
445
+ break;
446
+ }
447
+ }
448
+ for (i = 0; i < nextSip.length; ++i) {
449
+ if (sip[i] !== nextSip[i]) {
450
+ preCC = null;
451
+ break;
452
+ }
453
+ }
454
+ }
455
+
456
+ if (cc !== preCC) {
457
+ b = Buffer.alloc(bsz);
458
+ b.fill(0);
459
+ for (i = 0; i < sip.length; i++) {
460
+ b.writeUInt32BE(sip[i], i * 4);
461
+ }
462
+
463
+ for (i = 0; i < eip.length; i++) {
464
+ b.writeUInt32BE(eip[i], 16 + (i * 4));
465
+ }
466
+ } else {
467
+ console.log('SAME!!! ipv6')
468
+ b = Buffer.alloc(16);
469
+ b.fill(0);
470
+ for (i = 0; i < eip.length; i++) {
471
+ b.writeUInt32BE(eip[i], i * 4);
472
+ }
473
+ }
474
+
475
+ } else {
476
+ // IPv4
477
+ bsz = 10;
478
+
479
+ rngip = new Address4(fields[0]);
480
+ sip = parseInt(rngip.bigInteger(),10);
481
+ rngip = new Address4(fields[1]);
482
+ eip = parseInt(rngip.bigInteger(),10);
483
+
484
+ if (preEip + 1 !== sip) {
485
+ preCC = null;
486
+ }
487
+ if (cc !== preCC) {
488
+ b = Buffer.alloc(bsz);
489
+ b.fill(0);
490
+ b.writeUInt32BE(sip, 0);
491
+ b.writeUInt32BE(eip, 4);
492
+ } else {
493
+ console.log('SAME!!! ipv4')
494
+ b = Buffer.alloc(4);
495
+ b.fill(0);
496
+ b.writeUInt32BE(eip, 0);
497
+ }
498
+ }
499
+
500
+ preEip = eip;
501
+ if (cc !== preCC) {
502
+ b.write(cc, bsz - 2);
503
+ fs.writeSync(datFile, b, 0, bsz, pos);
504
+ pos += bsz;
505
+ preCC = cc;
506
+ } else {
507
+ fs.writeSync(datFile, b, 0, bsz < 16 ? 4: 16, pos - bsz + (bsz < 16 ? 4: 16));
508
+ }
509
+ if(Date.now() - tstart > 5000) {
510
+ tstart = Date.now();
511
+ process.stdout.write('\nStill working (' + lines + ') ...');
512
+ }
513
+ }
514
+ }
515
+
516
+ var dest
517
+ if(src.indexOf('ipv4') > 0){
518
+ dest = 'geoip-country.dat';
519
+ } else {
520
+ dest = 'geoip-country6.dat';
521
+ }
522
+ var dataFile = path.join(dataPath, dest);
523
+ var tmpDataFile = src
524
+
525
+ rimraf(dataFile);
526
+
527
+ process.stdout.write('Processing Data (may take a moment) ...');
528
+ var tstart = Date.now();
529
+ var datFile = fs.openSync(dataFile, "w");
530
+
531
+ lazy(fs.createReadStream(tmpDataFile))
532
+ .lines
533
+ .map(function(byteArray) {
534
+ return iconv.decode(byteArray, 'latin1');
535
+ })
536
+ .map(processLine)
537
+ .on('pipe', function() {
538
+ console.log(' DONE (lines: ' + lines + ')');
539
+ fs.closeSync(datFile);
401
540
  cb();
402
541
  });
403
542
  }
@@ -420,6 +559,9 @@ function processCityData(src, dest, cb) {
420
559
  var locId;
421
560
  var b;
422
561
  var bsz;
562
+ var lat;
563
+ var lon;
564
+ var area;
423
565
 
424
566
  var i;
425
567
 
@@ -449,9 +591,9 @@ function processCityData(src, dest, cb) {
449
591
  }
450
592
  b.writeUInt32BE(locId>>>0, 32);
451
593
 
452
- var lat = Math.round(parseFloat(fields[7]) * 10000);
453
- var lon = Math.round(parseFloat(fields[8]) * 10000);
454
- var area = parseInt(fields[9], 10);
594
+ lat = Math.round(parseFloat(fields[7]) * 10000);
595
+ lon = Math.round(parseFloat(fields[8]) * 10000);
596
+ area = parseInt(fields[9], 10);
455
597
  b.writeInt32BE(lat,36);
456
598
  b.writeInt32BE(lon,40);
457
599
  b.writeInt32BE(area,44);
@@ -470,9 +612,9 @@ function processCityData(src, dest, cb) {
470
612
  b.writeUInt32BE(eip>>>0, 4);
471
613
  b.writeUInt32BE(locId>>>0, 8);
472
614
 
473
- var lat = Math.round(parseFloat(fields[7]) * 10000);
474
- var lon = Math.round(parseFloat(fields[8]) * 10000);
475
- var area = parseInt(fields[9], 10);
615
+ lat = Math.round(parseFloat(fields[7]) * 10000);
616
+ lon = Math.round(parseFloat(fields[8]) * 10000);
617
+ area = parseInt(fields[9], 10);
476
618
  b.writeInt32BE(lat,12);
477
619
  b.writeInt32BE(lon,16);
478
620
  b.writeInt32BE(area,20);
@@ -594,28 +736,49 @@ function processData(database, cb) {
594
736
  }
595
737
  }
596
738
 
597
- if (!license_key || license_key === "true") {
598
- console.log('No GeoLite2 License Key Provided, Please Provide Argument: `--license_key=`');
599
- process.exit(1);
600
- }
601
-
602
739
  rimraf(tmpPath);
603
740
  mkdir(tmpPath);
604
741
 
605
- console.log('Fetching new databases from MaxMind...');
606
- console.log('Storing files at ' + dataPath);
607
-
608
- async.eachSeries(databases, function(database, nextDatabase) {
609
- if (database.type.indexOf('city') !== -1) return nextDatabase();
610
- async.seq(fetch, extract, processData)(database, nextDatabase);
611
- }, function(err) {
612
- if (err) {
613
- console.log('Failed to Update Databases from MaxMind.');
742
+ if(ip_location_db){
743
+ var preUrl = 'https://cdn.jsdelivr.net/npm/@ip-location-db/'+ip_location_db+'-country/'+ip_location_db+'-country'
744
+ var ipv4Url = preUrl+'-ipv4.csv'
745
+ var ipv6Url = preUrl+'-ipv6.csv'
746
+ async.seq(fetch, processCountryDataIpLocationDb)(ipv4Url, function(err){
747
+ if(err){
748
+ console.log('Failed to Update Databases ip-location-db/' + ip_location_db);
749
+ process.exit(1);
750
+ }
751
+ async.seq(fetch, processCountryDataIpLocationDb)(ipv6Url, function(err){
752
+ if(err){
753
+ console.log('Failed to Update Databases ip-location-db/' + ip_location_db);
754
+ process.exit(1);
755
+ }
756
+ console.log('Successfully Updated Database.');
757
+ // rimraf(tmpPath);
758
+ process.exit(0);
759
+ });
760
+ })
761
+ } else {
762
+ if (!license_key || license_key === "true") {
763
+ console.log('No GeoLite2 License Key Provided, Please Provide Argument: `--license_key=`');
614
764
  process.exit(1);
615
- } else {
616
- console.log('Successfully Updated Databases from MaxMind.');
617
- if (process.argv.indexOf('debug') !== -1) console.log('Notice: temporary files are not deleted for debug purposes.');
618
- else rimraf(tmpPath);
619
- process.exit(0);
620
765
  }
621
- });
766
+
767
+ console.log('Fetching new databases from MaxMind...');
768
+ console.log('Storing files at ' + dataPath);
769
+
770
+ async.eachSeries(databases, function(database, nextDatabase) {
771
+ if (database.type.indexOf('city') !== -1) return nextDatabase();
772
+ async.seq(fetch, extract, processData)(database, nextDatabase);
773
+ }, function(err) {
774
+ if (err) {
775
+ console.log('Failed to Update Databases from MaxMind.');
776
+ process.exit(1);
777
+ } else {
778
+ console.log('Successfully Updated Databases from MaxMind.');
779
+ if (process.argv.indexOf('debug') !== -1) console.log('Notice: temporary files are not deleted for debug purposes.');
780
+ else rimraf(tmpPath);
781
+ process.exit(0);
782
+ }
783
+ });
784
+ }