-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcheckPort.js
36 lines (34 loc) · 1.17 KB
/
checkPort.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
// Copyright 2018-2022 BadAimWeeb. All rights reserved. MIT license.
var net = require('net');
/**
* Check if that port on that address can be listenable
*
* @param {number} port A port you want to check
* @param {string} address An address you want to check on
*
* @return {Promise<Boolean>} A promise that will resolve with either true/false (listenable/not listenable)
*/
module.exports = function checkPort(port, address) {
typeof port == "string" ? port = parseInt(port) : "";
isNaN(port) ? port = 80 : "";
typeof port != "number" ? port = 80 : "";
typeof address != "string" ? address = "0.0.0.0" : "";
return new Promise((resolve, reject) => {
try {
var server = net.createServer(function(socket) {
socket.write('Testing port...');
socket.pipe(socket);
});
server.listen(port, address);
server.on('error', function () {
resolve(true);
});
server.on('listening', function () {
server.close();
resolve(false);
});
} catch (ex) {
reject(ex);
}
});
};