Skip to content

Instantly share code, notes, and snippets.

@n-johnson
Created September 3, 2015 12:02
Show Gist options
  • Select an option

  • Save n-johnson/62e0e5ca6a868eb33ed4 to your computer and use it in GitHub Desktop.

Select an option

Save n-johnson/62e0e5ca6a868eb33ed4 to your computer and use it in GitHub Desktop.
const subnetMap = { 8: 16777214, 9: 8388606, 10: 4194302, 11: 2097150, 12: 1048574, 13: 524286, 14: 262142, 15: 131070, 16: 65534, 17: 32766, 18: 16382, 19: 8190, 20: 4094, 21: 2046, 22: 1022, 23: 510, 24: 254, 25: 126, 26: 62, 27: 30, 28: 14, 29: 6, 30: 2, 31: 1};
const AWS_IP_RANGE_URL = 'https://ip-ranges.amazonaws.com/ip-ranges.json';
const CORS_URL = 'https://jsonp.afeld.me/?url=' + AWS_IP_RANGE_URL;
function filterUnique(p, c, i) { ~~p.indexOf(c) && p.push(c); return p; };
fetch(CORS_URL)
.then(function(response) {
response
.json()
.then(function(data) {
let ipList = data.prefixes
.map(function(p) { return p.ip_prefix; } )
.reduce(filterUnique, []);
let totalIpCount = ipList
.map(function(ip) { return subnetMap[ip.split('/')[1]]; } )
.reduce(function(p, c, i) { return p + c; }, 0 );
console.log('Total count: %s', totalIpCount);
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment