Skip to content

Commit 0313d1f

Browse files
committed
Updated to use promise so async/await can be used.
1 parent ae7e7d2 commit 0313d1f

File tree

5 files changed

+240
-145
lines changed

5 files changed

+240
-145
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: 2.1
22
jobs:
33
build:
44
docker:
5-
- image: circleci/node:7.10
5+
- image: circleci/node:12.19
66

77
working_directory: ~/node-port-scanner
88

README.md

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,56 @@ npm install node-port-scanner
1717
## Usage
1818

1919
```javascript
20-
const { nodePortScanner } = require('node-port-scanner');
21-
22-
// scan for open common ports
23-
nodePortScanner('127.0.0.1', [21, 22, 25, 80, 110, 123, 443], 'open', function (results) {
24-
console.log(results);
25-
});
26-
27-
// scan for open common ports
28-
nodePortScanner('github.com', [21, 22, 25, 80, 110, 123, 443], 'open', function (results) {
29-
console.log(results);
30-
});
31-
32-
// scan for closed common ports
33-
nodePortScanner('127.0.0.1', [21, 22, 25, 80, 110, 123, 443], 'closed', function (results) {
34-
console.log(results);
35-
});
36-
37-
// scan for closed common ports
38-
nodePortScanner('github.com', [21, 22, 25, 80, 110, 123, 443], 'closed', function (results) {
39-
console.log(results);
40-
});
41-
42-
// scan for all open ports - not recommended on remote hosts
43-
nodePortScanner('127.0.0.1', [], 'open', function (results) {
44-
console.log(results);
45-
});
46-
47-
// scan for all closed ports - not recommended on remote hosts
48-
nodePortScanner('127.0.0.1', [], 'closed', function (results) {
49-
console.log(results);
50-
});
20+
const nodePortScanner = require('node-port-scanner');
21+
22+
// scan for open local common ports
23+
nodePortScanner('127.0.0.1', [21, 22, 23, 25, 80, 110, 123, 443], 'open')
24+
.then(results => {
25+
console.log(results);
26+
})
27+
.catch(error => {
28+
console.log('Error: ' + error);
29+
});
30+
31+
// scan for open remote common ports
32+
nodePortScanner('github.com', [21, 22, 23, 25, 80, 110, 123, 443], 'open')
33+
.then(results => {
34+
console.log(results);
35+
})
36+
.catch(error => {
37+
console.log('Error: ' + error);
38+
});
39+
40+
//scan for closed local common ports
41+
nodePortScanner('127.0.0.1', [21, 22, 23, 25, 80, 110, 123, 443], 'closed')
42+
.then(results => {
43+
console.log(results);
44+
})
45+
.catch(error => {
46+
console.log('Error: ' + error);
47+
});
48+
49+
// scan for closed remote common ports
50+
nodePortScanner('github.com', [21, 22, 23, 25, 80, 110, 123, 443], 'closed')
51+
.then(results => {
52+
console.log(results);
53+
})
54+
.catch(error => {
55+
console.log('Error: ' + error);
56+
});
57+
58+
59+
// make calls in parallel - remote port checking not recommended
60+
async function checkLocalPorts () {
61+
62+
const openPorts = nodePortScanner('127.0.0.1', [], 'open');
63+
const closedPorts = nodePortScanner('127.0.0.1', [], 'closed');
64+
65+
console.log(await openPorts);
66+
console.log(await closedPorts);
67+
68+
}
69+
checkLocalPorts();
5170
```
5271

5372
## Test

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-port-scanner",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"description": "Simple JavaScript port scanner for open/closed ports",
55
"keywords": [
66
"portscanner",

src/node-port-scanner.js

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,94 @@
22
const net = require('net');
33

44
// scan range of ports for status (open|closed)
5-
const nodePortScanner = (host, ports, status, callback) => {
5+
module.exports = nodePortScanner = (host, ports, status) => {
66

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) => {
448

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) => {
5614

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]');
6218

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;
7122

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+
};
8351

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');
8684

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 : [] };
8987

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+
});
9194

9295
};
93-
94-
// encapsulate methods
95-
module.exports = { nodePortScanner : nodePortScanner };

0 commit comments

Comments
 (0)