|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const dns = require('dns'); |
| 4 | + |
| 5 | + |
| 6 | +const dnsJsonPath = path.join(__dirname, '../config', 'dnsTable.json'); |
| 7 | + |
| 8 | +const lookupArray = [ |
| 9 | + { |
| 10 | + "key":"udp", |
| 11 | + "hosts":[ |
| 12 | + "eks-udp-device-service-blue-static.us-east-1.eks-production-gotham.particle.io", |
| 13 | + "eks-udp-device-service-green-static.us-east-1.eks-production-gotham.particle.io" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "key":"tcp", |
| 18 | + "hosts":[ |
| 19 | + "eks-tcp-device-service-green-static.us-east-1.eks-production-gotham.particle.io", |
| 20 | + "eks-udp-device-service-green-static.us-east-1.eks-production-gotham.particle.io" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "key":"api", |
| 25 | + "hosts":[ |
| 26 | + "api.particle.io" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "key":"tools", |
| 31 | + "hosts":[ |
| 32 | + "build.particle.io", |
| 33 | + "console.particle.io", |
| 34 | + "docs.particle.io", |
| 35 | + "support.particle.io" |
| 36 | + ] |
| 37 | + } |
| 38 | +]; |
| 39 | + |
| 40 | +// console.log('generating...'); |
| 41 | +let results = {}; |
| 42 | + |
| 43 | +processLookupArray(function() { |
| 44 | + fs.writeFileSync(dnsJsonPath, JSON.stringify(results, null, 2)); |
| 45 | +}); |
| 46 | + |
| 47 | +function processLookupArray(completion) { |
| 48 | + |
| 49 | + lookupArray.forEach(function(lookupObj, outerIndex) { |
| 50 | + // Create empty keys for the results |
| 51 | + if (!results[lookupObj.key]) { |
| 52 | + results[lookupObj.key] = {}; |
| 53 | + } |
| 54 | + if (!results[lookupObj.key].addresses) { |
| 55 | + results[lookupObj.key].addresses = []; |
| 56 | + } |
| 57 | + |
| 58 | + const outerLast = (outerIndex + 1) >= lookupArray.length; |
| 59 | + |
| 60 | + lookupObj.hosts.forEach(function(hostName, innerIndex) { |
| 61 | + dns.lookup(hostName, {family:4, all:true}, function(err, addresses) { |
| 62 | + const innerLast = (innerIndex + 1) >= lookupObj.hosts.length; |
| 63 | + |
| 64 | + if (err) { |
| 65 | + throw err; |
| 66 | + } |
| 67 | + |
| 68 | + addresses.forEach(function(obj) { |
| 69 | + results[lookupObj.key].addresses.push(obj.address); |
| 70 | + }); |
| 71 | + |
| 72 | + |
| 73 | + if (innerLast) { |
| 74 | + //console.log('innerLast'); |
| 75 | + results[lookupObj.key].addresses.sort(ipAddrSort); |
| 76 | + // console.log('addresses', results[lookupObj.key].addresses); |
| 77 | + |
| 78 | + // results[lookupObj.key].md = arrayToMd(results[lookupObj.key].addresses); |
| 79 | + } |
| 80 | + |
| 81 | + if (outerLast && innerLast) { |
| 82 | + //console.log('outerLast && innerLast'); |
| 83 | + completion(); |
| 84 | + } |
| 85 | + }); |
| 86 | + }); |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +function ipAddrSort(a, b) { |
| 91 | + const aArray = a.split('.'); |
| 92 | + const bArray = b.split('.'); |
| 93 | + |
| 94 | + if (aArray.length != 4 || bArray.length != 4) { |
| 95 | + return a.localeCompare(b); |
| 96 | + } |
| 97 | + |
| 98 | + for(let ii = 0; ii < aArray.length; ii++) { |
| 99 | + const aInt = parseInt(aArray[ii]); |
| 100 | + const bInt = parseInt(bArray[ii]); |
| 101 | + |
| 102 | + if (aInt < bInt) { |
| 103 | + return -1; |
| 104 | + } |
| 105 | + else if (aInt > bInt) { |
| 106 | + return +1; |
| 107 | + } |
| 108 | + } |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +/* |
| 113 | +function arrayToMd(array) { |
| 114 | + var md = ''; |
| 115 | +
|
| 116 | + md += '| IP address |\n'; |
| 117 | + md += '| :-------------: |\n'; |
| 118 | +
|
| 119 | + array.forEach(function(addr) { |
| 120 | + md += '| ' + addr + ' '.substr(0, 15 - addr.length) + ' |\n'; |
| 121 | + }); |
| 122 | +
|
| 123 | + return md; |
| 124 | +} |
| 125 | +*/ |
0 commit comments