Skip to content

Commit

Permalink
feat: fallback to whois lookup (#10)
Browse files Browse the repository at this point in the history
* feat: fallback to whois lookup

* docs: update README.md

* nit: fix typo in README
  • Loading branch information
bcoe committed Jun 4, 2016
1 parent da2b02e commit fef40f4
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ npm i which-cloud -g

* Amazon Web Services (aws).
* Google Compute Engine (gce).
* Azure (azure)
* Azure (azure).
* _fallback to [whois](https://www.npmjs.com/package/whois) lookup._

## Patches Welcome!

I would love help adding support for more cloud services.

## License

Expand Down
2 changes: 1 addition & 1 deletion bin/which-cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ip = argv._[0]

which(ip, function (err, cloud) {
if (err) {
console.log('unknown')
console.log(which.default)
process.exit(1)
} else {
console.log(cloud)
Expand Down
20 changes: 17 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const ipRangeCheck = require('ip-range-check')
const azure = require('./lib/azure')
const aws = require('./lib/aws')
const gce = require('./lib/gce')
const whois = require('./lib/whois')

module.exports = function (ip, done) {
var name = 'unknown'
function WhichCloud (ip, done) {
var name = WhichCloud.default
parallel([
function (cb) {
check(ip, azure(), function (err, _name) {
Expand Down Expand Up @@ -36,10 +37,23 @@ module.exports = function (ip, done) {
}
], function (err) {
if (err) return done(err)
else return done(null, name)

if (name === WhichCloud.default) {
whois(ip).org(function (err, _name) {
if (err) return done(err)
if (_name) name = _name
return done(null, name)
})
} else {
return done(null, name)
}
})
}

WhichCloud.default = 'unknown'

module.exports = WhichCloud

function check (ip, cloud, cb) {
cloud.list(function (err, ranges) {
if (err) return cb(err)
Expand Down
23 changes: 23 additions & 0 deletions lib/whois.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const whois = require('whois')
const entryRegex = /^(network:)?Organization(;I)?:\W*(.*)/

function WhoIs (ip) {
this.ip = ip
}

WhoIs.prototype.org = function (cb) {
whois.lookup(this.ip, function (err, data) {
if (err) return cb(err)

var org = data.split('\n').filter(function (entry) {
return entryRegex.test(entry)
})

if (org.length) return cb(null, org[0].replace(entryRegex, '$3'))
else return cb()
})
}

module.exports = function (ip) {
return new WhoIs(ip)
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@
"ip-range-check": "0.0.1",
"lodash.assign": "^4.0.9",
"request": "^2.72.0",
"whois": "^2.1.6",
"xml2js": "^0.4.16",
"yargs": "^4.7.1"
},
"files": [
"lib/aws.js",
"lib/azure.js",
"lib/gce.js",
"lib/whois.js",
"LICENSE.txt",
"data/PublicIPs.xml",
"index.js",
Expand Down
10 changes: 9 additions & 1 deletion test/which.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ describe('which', function () {
})
})

it('falls back to whois organization', function (done) {
which('208.43.118.0', function (err, name) {
if (err) return done(err)
name.should.equal('Softlayer Corporate C')
return done()
})
})

it("returns 'unknown' if ip is not recognized", function (done) {
which('9.9.9.9', function (err, name) {
which('999.999.999.999', function (err, name) {
if (err) return done(err)
name.should.equal('unknown')
return done()
Expand Down

0 comments on commit fef40f4

Please sign in to comment.