Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept lists of ports #28

Merged
merged 4 commits into from
Feb 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this backwards compatible? You could detect the number of arguments to determine which logic to use.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duck typing would be another way (check if the first argument has .length but is not a string or function - or use the more strict Array.isArray() method). But yeah, I would really like to use the same API surface, since it still has the same intention, just with a slightly different set of data to start the search.

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)
}
})
}