-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
69 lines (62 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const parallel = require('async').parallel
const ipRangeCheck = require('ip-range-check')
const providers = [
require('./lib/azure'),
require('./lib/aws'),
require('./lib/gce')
]
const whois = require('./lib/whois')
const publicIp = require('public-ip')
function WhichCloud (ip, done) {
var name = WhichCloud.default
function checkersGenerator (ip) {
const checkers = []
providers.forEach(function (provider) {
checkers.push(function (cb) {
check(ip, provider(), function (err, _name) {
if (err) return cb(err)
else {
if (_name) name = _name
return cb()
}
})
})
})
return checkers
}
function runCheckers (ip, done) {
parallel(checkersGenerator(ip), function (err) {
if (err) return done(err)
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)
}
})
}
if (typeof ip === 'function') {
done = ip
publicIp.v4(function (err, hostIp) {
if (err) return done(err)
runCheckers(hostIp, done)
})
} else {
runCheckers(ip, done)
}
}
WhichCloud.default = 'unknown'
module.exports = WhichCloud
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()
}
})
}