Socket
Socket
Sign inDemoInstall

portscanner

Package Overview
Dependencies
Maintainers
4
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

portscanner - npm Package Compare versions

Comparing version 1.1.0 to 1.1.1

.npmignore

245

lib/portscanner.js

@@ -1,38 +0,24 @@

var net = require('net')
var Socket = net.Socket
var async = require('async')
var promisify = require('./promisify')
var net = require('net')
, Socket = net.Socket
, async = require('async')
var portscanner = exports
/**
* 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 {Number} startPort - Port to begin status check on (inclusive).
* @param {Number} endPort - Last port to check status on (inclusive).
* Defaults to 65535.
* @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.
*/
/**
* @param {Number} startPort - Port to begin status check on (inclusive).
* @param {Number} [endPort=65535] - Last port to check status on (inclusive).
* @param {String} [host='127.0.0.1'] - Host of where to scan.
* @param {findPortCallback} [callback] - Function to call back with error or results.
* @returns {Promise}
* @example
* // scans through 3000 to 3002 (inclusive)
* portscanner.findAPortInUse(3000, 3002, '127.0.0.1', console.log)
* // returns a promise in the absence of a callback
* portscanner.findAPortInUse(3000, 3002, '127.0.0.1').then(console.log)
* @example
* // scans through 3000 to 65535 on '127.0.0.1'
* portscanner.findAPortInUse(3000, console.log)
*/
/**
* @param {Array} postList - Array of ports to check status on.
* @param {String} [host='127.0.0.1'] - Host of where to scan.
* @param {findPortCallback} [callback] - Function to call back with error or results.
* @returns {Promise}
* @example
* // scans 3000 and 3002 only, not 3001.
* portscanner.findAPortInUse([3000, 3002], console.log)
*/
function findAPortInUse () {
var params = [].slice.call(arguments)
params.unshift('open')
return findAPortWithStatus.apply(null, params)
portscanner.findAPortInUse = function(startPort, endPort, host, callback) {
findAPortWithStatus('open', startPort, endPort, host, callback)
}

@@ -42,8 +28,17 @@

* Finds the first port with a status of 'closed', implying the port is not in
* use. Accepts identical parameters as {@link findAPortInUse}
* use.
*
* @param {Number} startPort - Port to begin status check on (inclusive).
* @param {Number} endPort - Last port to check status on (inclusive).
* Defaults to 65535.
* @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.
*/
function findAPortNotInUse () {
var params = [].slice.call(arguments)
params.unshift('closed')
return findAPortWithStatus.apply(null, params)
portscanner.findAPortNotInUse = function(startPort, endPort, host, callback) {
findAPortWithStatus('closed', startPort, endPort, host, callback)
}

@@ -53,55 +48,30 @@

* Checks the status of an individual port.
*
* @param {Number} port - Port to check status on.
* @param {String|Object} options - host or options
* - {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) { ... }
* - {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.
*/
/**
* @param {Number} port - Port to check status on.
* @param {String} [host='127.0.0.1'] - Host of where to scan.
* @param {checkPortCallback} [callback] - Function to call back with error or results.
* @returns {Promise}
*/
/**
* @param {Number} port - Port to check status on.
* @param {Object} [opts={}] - Options object.
* @param {String} [opts.host='127.0.0.1'] - Host of where to scan.
* @param {Number} [opts.timeout=400] - Connection timeout in ms.
* @param {checkPortCallback} [callback] - Function to call back with error or results.
* @returns {Promise}
*/
function checkPortStatus (port) {
var args, host, opts, callback
args = [].slice.call(arguments, 1)
if (typeof args[0] === 'string') {
host = args[0]
} else if (typeof args[0] === 'object') {
opts = args[0]
} else if (typeof args[0] === 'function') {
callback = args[0]
portscanner.checkPortStatus = function(port, options, callback) {
if (typeof options === 'string') {
// Assume this param is the host option
options = {host: options}
}
if (typeof args[1] === 'object') {
opts = args[1]
} else if (typeof args[1] === 'function') {
callback = args[1]
}
var host = options.host || '127.0.0.1'
var timeout = options.timeout || 400
var connectionRefused = false;
if (typeof args[2] === 'function') {
callback = args[2]
}
if (!callback) return promisify(checkPortStatus, arguments)
opts = opts || {}
host = host || opts.host || '127.0.0.1'
var timeout = opts.timeout || 400
var connectionRefused = false
var socket = new Socket()
var status = null
var error = null
, status = null
, error = null
// Socket connection established, port is open
socket.on('connect', function () {
socket.on('connect', function() {
status = 'open'

@@ -113,3 +83,3 @@ socket.destroy()

socket.setTimeout(timeout)
socket.on('timeout', function () {
socket.on('timeout', function() {
status = 'closed'

@@ -122,8 +92,8 @@ error = new Error('Timeout (' + timeout + 'ms) occurred waiting for ' + host + ':' + port + ' to be available')

// exception
socket.on('error', function (exception) {
if (exception.code !== 'ECONNREFUSED') {
socket.on('error', function(exception) {
if(exception.code !== "ECONNREFUSED") {
error = exception
} else {
connectionRefused = true
}
else
connectionRefused = true;
status = 'closed'

@@ -133,4 +103,7 @@ })

// Return after the socket has closed
socket.on('close', function (exception) {
if (exception && !connectionRefused) { error = error || exception } else { error = null }
socket.on('close', function(exception) {
if(exception && !connectionRefused)
error = exception;
else
error = null;
callback(error, status)

@@ -141,70 +114,18 @@ })

}
/**
* Callback for {@link checkPortStatus}
* @callback checkPortCallback
* @param {Error|null} error - Any error that occurred while port scanning, or null.
* @param {String} status - Status: 'open' if the port is in use, 'closed' if the port is available.
*/
/**
* Internal helper function used by {@link findAPortInUse} and {@link findAPortNotInUse}
* to find a port from a range or a list with a specific status.
*/
/**
* @param {String} status - Status to check.
* @param {...params} params - Params as passed exactly to {@link findAPortInUse} and {@link findAPortNotInUse}.
*/
function findAPortWithStatus (status) {
var params, startPort, endPort, portList, host, callback
params = [].slice.call(arguments, 1)
if (typeof params[0] === 'number') {
startPort = params[0]
} else if (params[0] instanceof Array) {
portList = params[0]
}
if (typeof params[1] === 'number') {
endPort = params[1]
} else if (typeof params[1] === 'string') {
host = params[1]
} else if (typeof params[1] === 'function') {
callback = params[1]
}
if (typeof params[2] === 'string') {
host = params[2]
} else if (typeof params[2] === 'function') {
callback = params[2]
}
if (typeof params[3] === 'function') {
callback = params[3]
}
if (!callback) return promisify(findAPortWithStatus, arguments)
if (startPort && endPort && endPort < startPort) {
// WARNING: endPort less than startPort. Using endPort as startPort & vice versa.
var tempStartPort = startPort
startPort = endPort
endPort = tempStartPort
}
function findAPortWithStatus(status, startPort, endPort, host, callback) {
endPort = endPort || 65535
var foundPort = false
var numberOfPortsChecked = 0
var port = portList ? portList[0] : startPort
var port = startPort
// Returns true if a port with matching status has been found or if checked
// the entire range of ports
var hasFoundPort = function () {
return foundPort || numberOfPortsChecked === (portList ? portList.length : endPort - startPort + 1)
var hasFoundPort = function() {
return foundPort || numberOfPortsChecked === (endPort - startPort + 1)
}
// Checks the status of the port
var checkNextPort = function (callback) {
checkPortStatus(port, host, function (error, statusOfPort) {
var checkNextPort = function(callback) {
portscanner.checkPortStatus(port, host, function(error, statusOfPort) {
numberOfPortsChecked++

@@ -214,4 +135,5 @@ if (statusOfPort === status) {

callback(error)
} else {
port = portList ? portList[numberOfPortsChecked] : port + 1
}
else {
port++
callback(null)

@@ -224,8 +146,10 @@ }

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

@@ -235,17 +159,2 @@ }

}
/**
* Callback for {@link findAPortWithStatus}, and by that extension, for {@link findAPortInUse} and {@link findAPortNotInUse}.
* @callback findPortCallback
* @param {Error|null} error - Any error that occurred while port scanning, or null.
* @param {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.
*/
/**
* @exports portscanner
*/
module.exports = {
findAPortInUse: findAPortInUse,
findAPortNotInUse: findAPortNotInUse,
checkPortStatus: checkPortStatus
}
{
"name": "portscanner",
"description": "Asynchronous port scanner for Node.js",
"scripts": {
"test": "ava",
"lint": "standard"
},
"keywords": [

@@ -15,3 +11,3 @@ "portscanner",

],
"version": "1.1.0",
"version": "1.1.1",
"preferGlobal": false,

@@ -35,10 +31,5 @@ "homepage": "https://github.com/baalexander/node-portscanner",

"dependencies": {
"async": "1.5.2"
"async": "0.1.15"
},
"devDependencies": {
"ava": "^0.4.2",
"eslint": "^3.10.2",
"eslint-config-standard": "^6.2.1",
"standard": "^8.5.0"
},
"devDependencies": {},
"engines": {

@@ -48,3 +39,8 @@ "node": ">=0.4",

},
"license": "MIT"
"licenses": [
{
"type": "MIT",
"url": "https://github.com/baalexander/node-portscanner/raw/master/LICENSE"
}
]
}

@@ -1,6 +0,3 @@

# portscanner
# node-portscanner
[![npm](https://img.shields.io/npm/v/portscanner.svg)](https://www.npmjs.com/package/portscanner)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
The portscanner module is

@@ -13,4 +10,2 @@ an asynchronous JavaScript port scanner for Node.js.

[Looking for maintainer](https://github.com/baalexander/node-portscanner/issues/25)!
## Install

@@ -46,17 +41,2 @@

})
// You can also pass array of ports to check
portscanner.findAPortInUse([3000, 3005, 3006], '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)
})
// And use promises
portscanner.findAPortNotInUse(3000, 4000).then(function(port) {
console.log('PORT IN USE AT: ' + port)
})
```

@@ -68,5 +48,5 @@

```sh
npm test
```
There are currently no tests.
If you have ideas,
please open an issue.

@@ -73,0 +53,0 @@ ## Future

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc