|
2 | 2 | const net = require('net');
|
3 | 3 |
|
4 | 4 | // scan range of ports for status (open|closed)
|
5 |
| -const nodePortScanner = (host, ports, status, callback) => { |
| 5 | +module.exports = nodePortScanner = (host, ports, status) => { |
6 | 6 |
|
7 |
| - // all ports that exist |
8 |
| - const allPorts = Array.from({length: 65535}, (_, i) => i + 1); |
9 |
| - |
10 |
| - // connect to a single port and get the status |
11 |
| - const connectToPort = (host, port, callback) => { |
12 |
| - |
13 |
| - // error checking args |
14 |
| - if (!Number.isInteger(port)) throw new Error('port must be an integer'); |
15 |
| - if (port < 1 || port > 65535) throw new Error('port must be in range [1-65535]'); |
16 |
| - |
17 |
| - let socket = new net.Socket(); |
18 |
| - // increase this if y'all on dial up |
19 |
| - let timeout = 200; |
20 |
| - |
21 |
| - // new properties & events for port scanner |
22 |
| - socket._scan = {}; |
23 |
| - socket._scan.status = 'initialized'; |
24 |
| - socket._scan.host = host; |
25 |
| - socket._scan.port = port; |
26 |
| - socket._scan._events = { complete : callback }; |
27 |
| - |
28 |
| - // events for socket |
29 |
| - socket.on('connect', function () { |
30 |
| - this._scan.status = 'connect'; |
31 |
| - socket.destroy(); |
32 |
| - }); |
33 |
| - socket.on('timeout', function () { |
34 |
| - this._scan.status = 'timeout'; |
35 |
| - socket.destroy(); |
36 |
| - }); |
37 |
| - socket.on('error', function (exception) { |
38 |
| - this._scan.status = 'error'; |
39 |
| - socket.destroy(); |
40 |
| - }); |
41 |
| - socket.on('close', function (exception) { |
42 |
| - this._scan._events.complete(this._scan); |
43 |
| - }); |
| 7 | + return new Promise((resolve, reject) => { |
44 | 8 |
|
45 |
| - socket.setTimeout(timeout); |
46 |
| - socket.connect(port, host); |
47 |
| - |
48 |
| - }; |
49 |
| - |
50 |
| - // recursive function to check all port status one after the other is complete |
51 |
| - const connectToPorts = (host, ports, status, scanResults, callback) => { |
52 |
| - |
53 |
| - let port = ports.shift(); |
54 |
| - |
55 |
| - connectToPort(host, port, function (result) { |
| 9 | + // all ports that exist |
| 10 | + const allPorts = Array.from({length: 65535}, (_, i) => i + 1); |
| 11 | + |
| 12 | + // connect to a single port and get the status |
| 13 | + const connectToPort = (host, port, callback) => { |
56 | 14 |
|
57 |
| - // add to our results based on the status of the result and scan |
58 |
| - if ((result.status == 'connect' && status == 'open') || |
59 |
| - (result.status != 'connect' && status == 'closed')) { |
60 |
| - scanResults.ports.push(result.port); |
61 |
| - } |
| 15 | + // error checking args |
| 16 | + if (!Number.isInteger(port)) reject('port must be an integer'); |
| 17 | + if (port < 1 || port > 65535) reject('port must be in range [1-65535]'); |
62 | 18 |
|
63 |
| - // recursivly go through all the ports |
64 |
| - if (ports.length) { |
65 |
| - connectToPorts(host, ports, status, scanResults, callback); |
66 |
| - } |
67 |
| - // when ports are done then complete callback from the scan |
68 |
| - else { |
69 |
| - callback(scanResults); |
70 |
| - } |
| 19 | + let socket = new net.Socket(); |
| 20 | + // increase this if y'all on dial up |
| 21 | + let timeout = 200; |
71 | 22 |
|
72 |
| - }); |
73 |
| - |
74 |
| - }; |
75 |
| - |
76 |
| - // error checking args |
77 |
| - if (host == undefined || !host) throw new Error('host is required'); |
78 |
| - if (ports == undefined || !ports) throw new Error('ports is required'); |
79 |
| - if (!Array.isArray(ports)) throw new Error('ports must be an array'); |
80 |
| - if (status == undefined || !status) throw new Error('status is required'); |
81 |
| - if (status.toLowerCase() != 'open' && status.toLowerCase() != 'closed') throw new Error('status must be open or closed'); |
82 |
| - if (callback == undefined || !callback || typeof callback != 'function') throw new Error('callback is required'); |
| 23 | + // new properties & events for port scanner |
| 24 | + socket._scan = {}; |
| 25 | + socket._scan.status = 'initialized'; |
| 26 | + socket._scan.host = host; |
| 27 | + socket._scan.port = port; |
| 28 | + socket._scan._events = { complete : callback }; |
| 29 | + |
| 30 | + // events for socket |
| 31 | + socket.on('connect', function () { |
| 32 | + this._scan.status = 'connect'; |
| 33 | + socket.destroy(); |
| 34 | + }); |
| 35 | + socket.on('timeout', function () { |
| 36 | + this._scan.status = 'timeout'; |
| 37 | + socket.destroy(); |
| 38 | + }); |
| 39 | + socket.on('error', function (exception) { |
| 40 | + this._scan.status = 'error'; |
| 41 | + socket.destroy(); |
| 42 | + }); |
| 43 | + socket.on('close', function (exception) { |
| 44 | + this._scan._events.complete(this._scan); |
| 45 | + }); |
| 46 | + |
| 47 | + socket.setTimeout(timeout); |
| 48 | + socket.connect(port, host); |
| 49 | + |
| 50 | + }; |
83 | 51 |
|
84 |
| - // scan results will be and array of port numbers matching status |
85 |
| - let scanResults = { host : host, status : status, ports : [] }; |
| 52 | + // recursive function to check all port status one after the other is complete |
| 53 | + const connectToPorts = (host, ports, status, scanResults) => { |
| 54 | + |
| 55 | + let port = ports.shift(); |
| 56 | + |
| 57 | + connectToPort(host, port, function (result) { |
| 58 | + |
| 59 | + // add to our results based on the status of the result and scan |
| 60 | + if ((result.status == 'connect' && status == 'open') || |
| 61 | + (result.status != 'connect' && status == 'closed')) { |
| 62 | + scanResults.ports.push(result.port); |
| 63 | + } |
| 64 | + |
| 65 | + // recursivly go through all the ports |
| 66 | + if (ports.length) { |
| 67 | + connectToPorts(host, ports, status, scanResults); |
| 68 | + } |
| 69 | + // when ports are done resolve the promise |
| 70 | + else { |
| 71 | + resolve(scanResults); |
| 72 | + } |
| 73 | + |
| 74 | + }); |
| 75 | + |
| 76 | + }; |
| 77 | + |
| 78 | + // error checking args |
| 79 | + if (host == undefined || !host) reject('host is required'); |
| 80 | + if (ports == undefined || !ports) reject('ports is required'); |
| 81 | + if (!Array.isArray(ports)) reject('ports must be an array'); |
| 82 | + if (status == undefined || !status) reject('status is required'); |
| 83 | + if (status.toLowerCase() != 'open' && status.toLowerCase() != 'closed') reject('status must be open or closed'); |
86 | 84 |
|
87 |
| - // no ports = all ports |
88 |
| - if (!ports.length) ports = allPorts; |
| 85 | + // scan results will be and array of port numbers matching status |
| 86 | + let scanResults = { host : host, status : status, ports : [] }; |
89 | 87 |
|
90 |
| - connectToPorts(host, ports, status, scanResults, callback); |
| 88 | + // no ports = all ports |
| 89 | + if (!ports.length) ports = allPorts; |
| 90 | + |
| 91 | + connectToPorts(host, ports, status, scanResults); |
| 92 | + |
| 93 | + }); |
91 | 94 |
|
92 | 95 | };
|
93 |
| - |
94 |
| -// encapsulate methods |
95 |
| -module.exports = { nodePortScanner : nodePortScanner }; |
0 commit comments