-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
70 lines (57 loc) · 1.63 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
const { Socket } = require("net");
const regex = /^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/;
function validation({ host, port, timeout, callback }) {
if (regex.test(host) === false) {
return { valid: false, error: "host format is invalid" };
}
if (
typeof port !== "undefined" &&
(port > 65536 || port < 0 || typeof port !== "number")
) {
return {
valid: false,
error: "Port should be >= 0 and < 65536 and type of number",
};
}
if (
typeof timeout !== "undefined" &&
(timeout < 0 || typeof timeout !== "number")
) {
return {
valid: false,
error: "timeout should be larger than 0 and type of number",
};
}
if (typeof callback !== "undefined" && typeof callback !== "function") {
return { valid: false, error: "callback should be a function" };
}
return { valid: true, error: null };
}
function portScanner({ host, port, timeout = 200, callback }) {
return new Promise((resolve, reject) => {
const vResult = validation({ host, port, timeout, callback });
if (vResult.valid === false) {
reject(vResult.error);
return;
}
const socket = new Socket();
socket.on("connect", () => {
socket.destroy();
resolve(true);
callback && callback(true, port);
});
socket.on("error", () => {
socket.destroy();
resolve(false);
callback && callback(false, port);
});
socket.on("timeout", () => {
socket.destroy();
resolve(false);
callback && callback(false, port);
});
socket.setTimeout(timeout);
socket.connect(port, host);
});
}
module.exports = portScanner;