generateIPList.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var http = require('https');
  2. var zlib = require('zlib');
  3. var fs = require('fs');
  4. var CDN_ASN_LIST = {
  5. 13335: 'Cloudflare',
  6. 15133: 'Verizon',
  7. 16625: 'Akamai',
  8. 20446: 'StackPath',
  9. 20940: 'Akamai',
  10. 22822: 'Limelight',
  11. 54113: 'Fastly'
  12. };
  13. // Downloads a file and ungzip ip directly
  14. function getGzipped(url, callback) {
  15. var buffer = [];
  16. http.get(url, function(res) {
  17. var gunzip = zlib.createGunzip();
  18. res.pipe(gunzip);
  19. gunzip.on('data', function(data) {
  20. buffer.push(data.toString())
  21. }).on('end', function() {
  22. callback(null, buffer.join(''));
  23. }).on('error', function(e) {
  24. callback(e);
  25. })
  26. }).on('error', function(e) {
  27. callback(e)
  28. });
  29. }
  30. // File provided here: https://iptoasn.com/
  31. console.log('Start downloading the file...');
  32. getGzipped('https://iptoasn.com/data/ip2asn-v4.tsv.gz', function(err, data) {
  33. if (err) {
  34. console.log(err);
  35. return;
  36. }
  37. console.log('Download complete, let\'s start parsing...');
  38. parseFile(data);
  39. });
  40. // Parse the CSV formated file and grag only the interesting IP ranges
  41. function parseFile(data) {
  42. var results = [];
  43. var allTextLines = data.split(/\r\n|\n/);
  44. for (var i = 0; i < allTextLines.length; i++) {
  45. var lineData = allTextLines[i].split('\t');
  46. var cdnName = CDN_ASN_LIST[lineData[2]];
  47. if (cdnName) {
  48. // We found an ASN number from our list. Let's save it!
  49. results.push({
  50. rangeStart: lineData[0],
  51. rangeEnd: lineData[1],
  52. cdn: cdnName
  53. });
  54. }
  55. }
  56. console.log('%d IP ranges found', results.length);
  57. var outputFile = __dirname + '/cdn-ip-list.json';
  58. fs.writeFileSync(outputFile, JSON.stringify(results, null, 2));
  59. console.log('File saved here: %s', outputFile);
  60. }