diff --git a/README.md b/README.md index d334fa4..bdc0380 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Coverage Status](https://coveralls.io/repos/github/bcoe/which-cloud/badge.svg?branch=master)](https://coveralls.io/github/bcoe/which-cloud?branch=master) [![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version) -given an IP address, return which cloud provider it belongs to (EC2, GCE, etc) +given an ip address, return which cloud provider it belongs to (EC2, GCE, etc) ```sh > which-cloud 104.196.27.39 diff --git a/lib/which.js b/lib/which.js new file mode 100644 index 0000000..5b767fd --- /dev/null +++ b/lib/which.js @@ -0,0 +1,42 @@ +const parallel = require('async').parallel +const ipRangeCheck = require('ip-range-check') +const azure = require('./azure') +const aws = require('./aws') + +module.exports = function (ip, done) { + var name = 'unknown' + parallel([ + function (cb) { + check(ip, azure(), function (err, _name) { + if (err) return cb(err) + else { + if (_name) name = _name + return cb() + } + }) + }, + function (cb) { + check(ip, aws(), function (err, _name) { + if (err) return cb(err) + else { + if (_name) name = _name + return cb() + } + }) + } + ], function (err) { + if (err) return done(err) + else return done(null, name) + }) +} + +function check (ip, cloud, cb) { + cloud.list(function (err, ranges) { + if (err) return cb(err) + else if (ipRangeCheck(ip, ranges)) { + return cb(null, cloud.name) + } else { + return cb() + } + }) +} diff --git a/package.json b/package.json index dcfee3a..aa87ac0 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "which-cloud", "version": "1.0.0", - "description": "given an IP address, return which cloud provider it belongs to (EC2, GCE, etc)", + "description": "given an ip address, return which cloud provider it belongs to (AWS, GCE, etc)", "main": "index.js", "global": true, "scripts": { "pretest": "standard", - "test": "nyc mocha test/*.js", + "test": "nyc mocha --timeout=2500 test/*.js", "coverage": "nyc report --reporter=text-lcov | coveralls", "release": "standard-version" }, diff --git a/test/aws.js b/test/aws.js index 67f9f46..cec380d 100644 --- a/test/aws.js +++ b/test/aws.js @@ -1,13 +1,13 @@ -/* global describe, it */ +/* global describe, it, after */ require('chai').should() -var AWS = require('../lib/aws') -var nock = require('nock') +const AWS = require('../lib/aws') +const nock = require('nock') describe('AWS', function () { it('returns a list of IP ranges for AWS', function (done) { - var aws = AWS() + const aws = AWS() aws.list(function (err, ranges) { if (err) return done(err) ranges.should.include('23.20.0.0/14') @@ -16,8 +16,8 @@ describe('AWS', function () { }) it('handles an upstream error', function (done) { - var aws = AWS() - var getRanges = nock('https://ip-ranges.amazonaws.com') + const aws = AWS() + const getRanges = nock('https://ip-ranges.amazonaws.com') .get('/ip-ranges.json') .reply(500) aws.list(function (err, ranges) { @@ -26,4 +26,9 @@ describe('AWS', function () { return done() }) }) + + after(function () { + nock.cleanAll() + nock.enableNetConnect() + }) }) diff --git a/test/azure.js b/test/azure.js index 406e159..71cd9c1 100644 --- a/test/azure.js +++ b/test/azure.js @@ -2,11 +2,11 @@ require('chai').should() -var Azure = require('../lib/azure') +const Azure = require('../lib/azure') describe('Azure', function () { it('returns a list of IP ranges for Azure', function (done) { - var azure = Azure() + const azure = Azure() azure.list(function (err, ranges) { if (err) return done(err) ranges.should.include('40.112.124.0/24') diff --git a/test/which.js b/test/which.js new file mode 100644 index 0000000..1db63fb --- /dev/null +++ b/test/which.js @@ -0,0 +1,31 @@ +/* global describe, it */ + +require('chai').should() + +const which = require('../lib/which') + +describe('which', function () { + it('returns appropriate response for azure ip', function (done) { + which('94.245.97.0', function (err, name) { + if (err) return done(err) + name.should.equal('azure') + return done() + }) + }) + + it('returns appropriate response for aws ip', function (done) { + which('54.173.231.160', function (err, name) { + if (err) return done(err) + name.should.equal('aws') + return done() + }) + }) + + it("returns 'unknown' if ip is not recognized", function (done) { + which('9.9.9.9', function (err, name) { + if (err) return done(err) + name.should.equal('unknown') + return done() + }) + }) +})