Skip to content

Commit

Permalink
Make host parameter optional
Browse files Browse the repository at this point in the history
  • Loading branch information
elmccd authored and laggingreflex committed Nov 18, 2016
1 parent 2c73a12 commit eb345da
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ portscanner.findAPortNotInUse(3000, 3010, '127.0.0.1', function(error, port) {
portscanner.findAPortInUse(3000, 3010, '127.0.0.1', function(error, port) {
console.log('PORT IN USE AT: ' + port)
})

// And skip host param. Default is '127.0.0.1'
portscanner.findAPortNotInUse(3000, 4000, function(error, port) {
console.log('PORT IN USE AT: ' + port)
})
```

The example directory contains a more detailed example.
Expand Down
37 changes: 31 additions & 6 deletions lib/portscanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,22 +80,27 @@ portscanner.findAPortNotInUse = function(portList, host, callback) {
* Checks the status of an individual port.
*
* @param {Number} port - Port to check status on.
* @param {String|Object} options - host or options
* @param {String|Object|Function} arg2 - host or options or function
* - {String} host - Host of where to scan. Defaults to '127.0.0.1'.
* - {Object} options
* - {String} host - Host of where to scan. Defaults to '127.0.0.1'.
* - {Number} timeout - Connection timeout. Defaults to 400ms.
* @param {Function} callback - function (error, port) { ... }
* @param {Function} arg3 - function (error, port) { ... }
* - {Object|null} error - Any errors that occurred while port scanning.
* - {String} status - 'open' if the port is in use.
* 'closed' if the port is available.
*/
portscanner.checkPortStatus = function(port, options, callback) {
if (typeof options === 'string') {
portscanner.checkPortStatus = function(port, arg2, arg3) {
var options = {}
, callback

if (typeof arg2 === 'string') {
// Assume this param is the host option
options = {host: options}
options = {host: arg2}
}

callback = Array.prototype.slice.call(arguments).slice(-1)[0];

var host = options.host || '127.0.0.1'
var timeout = options.timeout || 400
var connectionRefused = false;
Expand Down Expand Up @@ -183,7 +188,27 @@ function findAPortWithStatus(status, startPort, endPort, host, callback) {
})
}

function findAPortWithStatus(status, portList, host, callback) {
function findAPortWithStatus(status, params) {
var host;
var callback;

//use array of ports
if (typeof params[0] === 'object') {
var ports = params[0];
host = typeof params[1] === 'string' ? params[1] : null;

//use startPort and endPort
} else if (typeof params[0] === 'number') {
var startPort = params[0];
var endPort = params[1];
host = typeof params[2] === 'string' ? params[2] : null;
}

//callback always at the end
callback = Array.prototype.slice.call(params).slice(-1)[0];

endPort = endPort || 65535

var foundPort = false
var portIndex = 0
var port = portList[portIndex]
Expand Down

0 comments on commit eb345da

Please sign in to comment.