-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (56 loc) · 1.9 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
const net = require('net');
const fs = require('fs');
const Promise = require('bluebird');
const yaml = require('js-yaml');
const dns = require('dns');
const configFile = process.argv[2] || './hosts.yml'
console.log(`Using config file: ${configFile}`);
function tcpTest(addr) {
return new Promise(function(resolve, reject) {
const client = new net.Socket();
client.connect(addr, () => {
client.destroy();
resolve(addr);
});
client.setTimeout(10000, function() {
client.destroy();
resolve(Object.assign({}, addr, { error: "Timeout" }));
});
client.on('error', (err) => {
client.destroy();
resolve(Object.assign({}, addr, { error: err }));
});
});
}
function resolveAddress(addr) {
return new Promise((resolve) => {
dns.lookup(addr.host, (err, resolvedAddresses) => {
resolve(Object.assign({}, addr, {host: `${addr.host}(${err || resolvedAddresses})`}));
});
});
}
function printResult({host, port, error}) {
console.log(`\t${host}:${port} - ${error ? 'ERR' : 'OK'}`);
}
function toHostObject(line) {
const [host, port = 80] = line.split(":");
return {host, port};
}
function retrieveHostList(data) {
return data.split("\n")
.filter(line => !line.startsWith("#") && line.length > 0)
.map(toHostObject)
.filter(addr => addr.host && addr.port);
}
function testByCategory(category, hosts) {
const promises = hosts.map(toHostObject)
.map(addr => tcpTest(addr).then(resolveAddress));
Promise.all(promises).then((tcpResults) => {
console.log(`Testing ${category}:`);
tcpResults.forEach(printResult);
});
}
const config = yaml.safeLoad(fs.readFileSync(configFile, 'utf8'));
for (category in config) {
testByCategory(category, config[category]);
}