Skip to content

Commit

Permalink
Checks a range of ports for first open or closed port.
Browse files Browse the repository at this point in the history
  • Loading branch information
baalexander committed Aug 11, 2011
1 parent 8568c23 commit e232efc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
8 changes: 6 additions & 2 deletions example/portscan.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ portscanner.checkPortStatus('3000', 'localhost', function(error, status) {
console.log(status)
})

portscanner.findAnAvailablePort(3000, 3010, 'localhost', function(error, port) {
console.log('AVAILABLE PORT AT ' + port)
portscanner.findAnOpenPort(3000, 3010, 'localhost', function(error, port) {
console.log('OPEN PORT AT ' + port)
})

portscanner.findAClosedPort(3000, 3010, 'localhost', function(error, port) {
console.log('CLOSED PORT AT ' + port)
})

setTimeout(function() { }, 10000)
Expand Down
67 changes: 38 additions & 29 deletions lib/portscanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,14 @@ var net = require('net')

var portscanner = exports

portscanner.findAnAvailablePort = function(startPort, endPort, host, callback) {
var that = this
var foundAvailablePort = false
var numberOfPortsChecked = 0

var check = function(port) {
that.checkPortStatus(port, host, function(error, status) {
numberOfPortsChecked++
// Only callback once
if (foundAvailablePort === false) {
if (error) {
foundAvailablePort = true
callback(error)
}
else {
if (status === 'open') {
foundAvailablePort = true
callback(null, port)
}
// All port checks have returned unavailable
else if (numberOfPortsChecked === (endPort - startPort + 1)) {
callback(null, false)
}
}
}
})
}
portscanner.findAnOpenPort = function(startPort, endPort, host, callback) {
findAPortWithStatus('open', startPort, endPort, host, callback)
}

portscanner.findAClosedPort = function(startPort, endPort, host, callback) {
findAPortWithStatus('closed', startPort, endPort, host, callback)

for (var port = startPort; port <= endPort; port++) {
check(port)
}
}

portscanner.checkPortStatus = function(port, host, callback) {
Expand Down Expand Up @@ -67,3 +44,35 @@ portscanner.checkPortStatus = function(port, host, callback) {
socket.connect(port, host)
}

function findAPortWithStatus(status, startPort, endPort, host, callback) {
var foundPort = false
var numberOfPortsChecked = 0

var check = function(port) {
portscanner.checkPortStatus(port, host, function(error, statusOfPort) {
numberOfPortsChecked++
// Only callback once
if (foundPort === false) {
if (error) {
foundPort = true
callback(error)
}
else {
if (statusOfPort === status) {
foundPort = true
callback(null, port)
}
// All port checks have returned unavailable
else if (numberOfPortsChecked === (endPort - startPort + 1)) {
callback(null, false)
}
}
}
})
}

for (var port = startPort; port <= endPort; port++) {
check(port)
}
}

0 comments on commit e232efc

Please sign in to comment.