Skip to content

Commit

Permalink
feat: adds support for AWS/EC2
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Jun 3, 2016
0 parents commit 9938517
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
.nyc_output
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js

os:
- linux

after_success: npm run coverage

node_js:
- "0.10"
- "4"
- "node"
14 changes: 14 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2016, Contributors

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice
appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE
LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# which-cloud

[![Build Status](https://travis-ci.org/bcoe/gce-ips.svg)](https://travis-ci.org/bcoe/gce-ips)
[![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)

```sh
> which-cloud 104.196.27.39
> gce
```

## Installing

```sh
npm i which-cloud -g
```

## Supported Clouds

* Amazon Web Services (aws).
* Google Compute Engine (gce).
* Azure (azure)

## License

ISC
33 changes: 33 additions & 0 deletions lib/aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const assign = require('lodash.assign')
const request = require('request')

function AWS (opts) {
assign(this, {
rangesURL: 'https://ip-ranges.amazonaws.com/ip-ranges.json',
name: 'aws'
}, opts)
}

AWS.prototype.list = function (cb) {
request.get({
url: this.rangesURL,
json: true
}, function (err, res, obj) {
if (res && res.statusCode >= 400) {
err = Error('unexpected status = ' + res.statusCode)
err.statusCode = res.statusCode
}
if (err) return cb(err)
else {
return cb(null, obj.prefixes.map(
function (p) {
return p.ip_prefix
}
))
}
})
}

module.exports = function (opts) {
return new AWS(opts)
}
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "which-cloud",
"version": "1.0.0",
"description": "given an IP address, return which cloud provider it belongs to (EC2, GCE, etc)",
"main": "index.js",
"global": true,
"scripts": {
"pretest": "standard",
"test": "nyc mocha test/*.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"release": "standard-version"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/bcoe/which-cloud.git"
},
"keywords": [
"cloud",
"lookup",
"ip"
],
"author": "Ben Coe <ben@npmjs.com>",
"license": "ISC",
"bugs": {
"url": "https://github.com/bcoe/which-cloud/issues"
},
"homepage": "https://github.com/bcoe/which-cloud#readme",
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"mocha": "^2.5.3",
"nock": "^8.0.0",
"nyc": "^6.4.4",
"standard": "^7.1.1",
"standard-version": "^2.3.0"
},
"dependencies": {
"async": "^1.5.2",
"ip-range-check": "0.0.1",
"lodash.assign": "^4.0.9",
"request": "^2.72.0",
"yargs": "^4.7.1"
}
}
29 changes: 29 additions & 0 deletions test/aws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* global describe, it */

require('chai').should()

var AWS = require('../lib/aws')
var nock = require('nock')

describe('AWS', function () {
it('returns a list of IP ranges for the AWS service', function (done) {
var aws = AWS()
aws.list(function (err, ranges) {
if (err) return done(err)
ranges.should.include('23.20.0.0/14')
return done()
})
})

it('handles an upstream error', function (done) {
var aws = AWS()
var getRanges = nock('https://ip-ranges.amazonaws.com')
.get('/ip-ranges.json')
.reply(500)
aws.list(function (err, ranges) {
getRanges.done()
err.statusCode.should.equal(500)
return done()
})
})
})

0 comments on commit 9938517

Please sign in to comment.