Skip to content

Commit

Permalink
Merge pull request baalexander#28 from darthneko/master
Browse files Browse the repository at this point in the history
Accept lists of ports
  • Loading branch information
shinnn committed Feb 23, 2015
2 parents c2092ce + 5a714b2 commit 3a271ee
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
11 changes: 11 additions & 0 deletions example/portscan-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var http = require('http')
, portscanner = require('../lib/portscanner.js')

// Check if one of the ports in the list is available
portscanner.findAPortNotInUse([4443, 4444, 4447], '127.0.0.1', function(error, port) {
if (!error) {
console.log('Available port: ' + port);
} else {
console.log('There was an error');
}
});
76 changes: 75 additions & 1 deletion lib/portscanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ portscanner.findAPortInUse = function(startPort, endPort, host, callback) {
findAPortWithStatus('open', startPort, endPort, host, callback)
}

/**
* Finds the first port with a status of 'open', implying the port is in use and
* there is likely a service listening on it.
*
* @param {List} portList - List of ports to check on.
* @param {String} host - Where to scan. Defaults to '127.0.0.1'.
* @param {Function} callback - function (error, port) { ... }
* - {Object|null} error - Any errors that occurred while port scanning.
* - {Number|Boolean} port - The first open port found. Note, this is the
* first port that returns status as 'open', not
* necessarily the first open port checked. If no
* open port is found, the value is false.
*/
portscanner.findAPortInUse = function(portList, host, callback) {
findAPortWithStatus('open', portList, host, callback)
}

/**
* Finds the first port with a status of 'closed', implying the port is not in
* use.
Expand All @@ -42,6 +59,23 @@ portscanner.findAPortNotInUse = function(startPort, endPort, host, callback) {
findAPortWithStatus('closed', startPort, endPort, host, callback)
}

/**
* Finds the first port with a status of 'closed', implying the port is not in
* use.
*
* @param {List} portList - List of ports to check on.
* @param {String} host - Where to scan. Defaults to '127.0.0.1'.
* @param {Function} callback - function (error, port) { ... }
* - {Object|null} error - Any errors that occurred while port scanning.
* - {Number|Boolean} port - The first closed port found. Note, this is the
* first port that returns status as 'closed', not
* necessarily the first closed port checked. If no
* closed port is found, the value is false.
*/
portscanner.findAPortNotInUse = function(portList, host, callback) {
findAPortWithStatus('closed', portList, host, callback)
}

/**
* Checks the status of an individual port.
*
Expand Down Expand Up @@ -108,7 +142,7 @@ portscanner.checkPortStatus = function(port, options, callback) {
}

function findAPortWithStatus(status, startPort, endPort, host, callback) {
endPort = endPort || 65535
var endPort = endPort || 65535
var foundPort = false
var numberOfPortsChecked = 0
var port = startPort
Expand Down Expand Up @@ -149,3 +183,43 @@ function findAPortWithStatus(status, startPort, endPort, host, callback) {
})
}

function findAPortWithStatus(status, portList, host, callback) {
var foundPort = false
var portIndex = 0
var port = portList[portIndex]

// Returns true if a port with matching status has been found or if checked
// the entire range of ports
var hasFoundPort = function() {
return foundPort || portIndex === portList.length
}

// Checks the status of the port
var checkNextPort = function(callback) {
portscanner.checkPortStatus(port, host, function(error, statusOfPort) {
if (statusOfPort === status) {
foundPort = true
callback(error)
}
else {
port = portList[++portIndex]
callback(null)
}
})
}

// Check the status of each port until one with a matching status has been
// found or the range of ports has been exhausted
async.until(hasFoundPort, checkNextPort, function(error) {
if (error) {
callback(error, port)
}
else if (foundPort) {
callback(null, port)
}
else {
callback(null, false)
}
})
}

0 comments on commit 3a271ee

Please sign in to comment.