Skip to content

Commit e232efc

Browse files
committed
Checks a range of ports for first open or closed port.
1 parent 8568c23 commit e232efc

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-31
lines changed

example/portscan.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ portscanner.checkPortStatus('3000', 'localhost', function(error, status) {
44
console.log(status)
55
})
66

7-
portscanner.findAnAvailablePort(3000, 3010, 'localhost', function(error, port) {
8-
console.log('AVAILABLE PORT AT ' + port)
7+
portscanner.findAnOpenPort(3000, 3010, 'localhost', function(error, port) {
8+
console.log('OPEN PORT AT ' + port)
9+
})
10+
11+
portscanner.findAClosedPort(3000, 3010, 'localhost', function(error, port) {
12+
console.log('CLOSED PORT AT ' + port)
913
})
1014

1115
setTimeout(function() { }, 10000)

lib/portscanner.js

+38-29
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,14 @@ var net = require('net')
33

44
var portscanner = exports
55

6-
portscanner.findAnAvailablePort = function(startPort, endPort, host, callback) {
7-
var that = this
8-
var foundAvailablePort = false
9-
var numberOfPortsChecked = 0
106

11-
var check = function(port) {
12-
that.checkPortStatus(port, host, function(error, status) {
13-
numberOfPortsChecked++
14-
// Only callback once
15-
if (foundAvailablePort === false) {
16-
if (error) {
17-
foundAvailablePort = true
18-
callback(error)
19-
}
20-
else {
21-
if (status === 'open') {
22-
foundAvailablePort = true
23-
callback(null, port)
24-
}
25-
// All port checks have returned unavailable
26-
else if (numberOfPortsChecked === (endPort - startPort + 1)) {
27-
callback(null, false)
28-
}
29-
}
30-
}
31-
})
32-
}
7+
portscanner.findAnOpenPort = function(startPort, endPort, host, callback) {
8+
findAPortWithStatus('open', startPort, endPort, host, callback)
9+
}
10+
11+
portscanner.findAClosedPort = function(startPort, endPort, host, callback) {
12+
findAPortWithStatus('closed', startPort, endPort, host, callback)
3313

34-
for (var port = startPort; port <= endPort; port++) {
35-
check(port)
36-
}
3714
}
3815

3916
portscanner.checkPortStatus = function(port, host, callback) {
@@ -67,3 +44,35 @@ portscanner.checkPortStatus = function(port, host, callback) {
6744
socket.connect(port, host)
6845
}
6946

47+
function findAPortWithStatus(status, startPort, endPort, host, callback) {
48+
var foundPort = false
49+
var numberOfPortsChecked = 0
50+
51+
var check = function(port) {
52+
portscanner.checkPortStatus(port, host, function(error, statusOfPort) {
53+
numberOfPortsChecked++
54+
// Only callback once
55+
if (foundPort === false) {
56+
if (error) {
57+
foundPort = true
58+
callback(error)
59+
}
60+
else {
61+
if (statusOfPort === status) {
62+
foundPort = true
63+
callback(null, port)
64+
}
65+
// All port checks have returned unavailable
66+
else if (numberOfPortsChecked === (endPort - startPort + 1)) {
67+
callback(null, false)
68+
}
69+
}
70+
}
71+
})
72+
}
73+
74+
for (var port = startPort; port <= endPort; port++) {
75+
check(port)
76+
}
77+
}
78+

0 commit comments

Comments
 (0)