-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
129 lines (106 loc) · 3.67 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
const net = require('net');
const util = require('util');
const path = require('path');
const shell = require('child_process');
let SOCKET_TIMEOUT = 1000; //Setting 1s as max acceptable timeout
//source - http://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
var ValidHostnameRegex = new RegExp("^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$");
function isValidHostNameOrIP(host) {
return net.isIP(host) || ValidHostnameRegex.test(host);
}
function testSync(host, port, connectTimeout) {
const nodeBinary = process.execPath;
const scriptPath = path.join(__dirname, "./scripts/connection-tester");
const cmd = util.format('"%s" "%s" %s %s %s', nodeBinary, scriptPath, host, port, connectTimeout);
const shellOut = shell.execSync(cmd).toString();
const output = {
success: false,
error: null
};
if (shellOut) {
if (shellOut.match(/true/)) {
output.success = true;
} else {
output.error = shellOut;
}
} else {
output.error = "No output from connection test";
}
return output;
}
function testAsync(host, port, connectTimeout, callback) {
const socket = new net.Socket();
const output = {
success: false,
error: null
};
socket.connect(port, host);
socket.setTimeout(connectTimeout);
//if able to establish the connection, returns `true`
socket.on('connect', function () {
socket.destroy();
output.success = true;
return callback(null, output);
});
//on connection error, returns error
socket.on('error', function (err) {
socket.destroy();
output.error = err && err.message || err;
return callback(err, output);
});
//on connection timeout, returns error
socket.on('timeout', function (err) {
socket.destroy();
output.error = err && err.message || err || 'socket TIMEOUT';
return callback(err, output);
});
}
module.exports = {
timeout: function (socketTimeout) {
if (!!socketTimeout) {
SOCKET_TIMEOUT = socketTimeout;
}
return SOCKET_TIMEOUT;
},
test: function ConnectionTester(host, port, connectTimeout, callback) {
// validate host
if (!isValidHostNameOrIP(host)) {
console.error('[connection-tester] invalid host: ', host);
host = undefined;
}
// validate port
var originalPort = port;
port = +port;
if (!port || port < 0 || port > 65535) {
console.error('[connection-tester] invalid port: ', originalPort);
port = undefined;
}
if (typeof connectTimeout === 'function') {
console.error('deprecated: Please migrate to the new interface ConnectionTester\(host, port, timeout, callback\)');
callback = connectTimeout;
connectTimeout = SOCKET_TIMEOUT;
}
if (connectTimeout === undefined) {
connectTimeout = SOCKET_TIMEOUT;
}
if (typeof connectTimeout === 'number') {
if (!port || !host) {
var output = {
success: false,
error: 'invalid host/port'
};
if (callback) {
return callback(null, output);
} else {
return output;
}
}
if (callback) {
return testAsync(host, port, connectTimeout, callback);
} else {
return testSync(host, port, connectTimeout);
}
}
}
};