diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..185ff2e --- /dev/null +++ b/.eslintrc @@ -0,0 +1 @@ +{"extends": "standard"} diff --git a/README.md b/README.md index 8a7b70e..7e10f03 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # node-portscanner +[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) + The portscanner module is an asynchronous JavaScript port scanner for Node.js. diff --git a/example/portscan.js b/example/portscan.js index 0eeace6..247c23f 100644 --- a/example/portscan.js +++ b/example/portscan.js @@ -1,35 +1,35 @@ -var http = require('http') - , portscanner = require('../lib/portscanner.js') +var http = require('http') +var portscanner = require('../lib/portscanner.js') // Sets up an HTTP server listening on port 3005 var server = http.createServer(function (request, response) { }) -server.listen(3005, '127.0.0.1', function() { +server.listen(3005, '127.0.0.1', function () { // Checks the status of an individual port. - portscanner.checkPortStatus(3005, '127.0.0.1', function(error, status) { + portscanner.checkPortStatus(3005, '127.0.0.1', function (error, status) { // Status should be 'open' since the HTTP server is listening on that port console.log('Status at port 3005 is ' + status) - if (error) console.error(error); + if (error) console.error(error) }) - portscanner.checkPortStatus(3000, '127.0.0.1', function(error, status) { + portscanner.checkPortStatus(3000, '127.0.0.1', function (error, status) { // Status should be 'closed' since no service is listening on that port. console.log('Status at port 3000 is ' + status) - if (error) console.error(error); + if (error) console.error(error) }) // Finds a port that a service is listening on - portscanner.findAPortInUse(3000, 3010, '127.0.0.1', function(error, port) { + portscanner.findAPortInUse(3000, 3010, '127.0.0.1', function (error, port) { // Port should be 3005 as the HTTP server is listening on that port console.log('Found an open port at ' + port) - if (error) console.error(error); + if (error) console.error(error) }) // Finds a port no service is listening on - portscanner.findAPortNotInUse(3000, 3010, '127.0.0.1', function(error, port) { + portscanner.findAPortNotInUse(3000, 3010, '127.0.0.1', function (error, port) { // Will return any number between 3000 and 3010 (inclusive), that's not 3005. // The order is unknown as the port status checks are asynchronous. console.log('Found a closed port at ' + port) - if (error) console.error(error); + if (error) console.error(error) }) }) diff --git a/lib/portscanner.js b/lib/portscanner.js index 6db4d20..98da576 100644 --- a/lib/portscanner.js +++ b/lib/portscanner.js @@ -1,6 +1,6 @@ -var net = require('net') - , Socket = net.Socket - , async = require('async') +var net = require('net') +var Socket = net.Socket +var async = require('async') var portscanner = exports @@ -29,9 +29,9 @@ var portscanner = exports * portscanner.findAPortInUse([3000, 3001, 3002], '127.0.0.1', callback) * portscanner.findAPortInUse(3000, 3002, '127.0.0.1', callback) */ -portscanner.findAPortInUse = function(params) { +portscanner.findAPortInUse = function (params) { findAPortWithStatus('open', arguments) -}; +} /** * Finds the first port with a status of 'closed', implying the port is not in @@ -57,7 +57,7 @@ portscanner.findAPortInUse = function(params) { * portscanner.findAPortNotInUse([3000, 3001, 3002], '127.0.0.1', callback) * portscanner.findAPortNotInUse(3000, 3002, '127.0.0.1', callback) */ -portscanner.findAPortNotInUse = function(params) { +portscanner.findAPortNotInUse = function (params) { findAPortWithStatus('closed', arguments) } @@ -74,34 +74,34 @@ portscanner.findAPortNotInUse = function(params) { * - {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, arg2, arg3) { +portscanner.checkPortStatus = function (port, arg2, arg3) { var options = {} - , callback + var callback if (typeof arg2 === 'string') { // Assume this param is the host option - options = {host: arg2} + options = { host: arg2 } } - callback = Array.prototype.slice.call(arguments).slice(-1)[0]; + 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; + var connectionRefused = false var socket = new Socket() - , status = null - , error = null + var status = null + var error = null // Socket connection established, port is open - socket.on('connect', function() { + socket.on('connect', function () { status = 'open' socket.destroy() }) // If no response, assume port is not listening socket.setTimeout(timeout) - socket.on('timeout', function() { + socket.on('timeout', function () { status = 'closed' error = new Error('Timeout (' + timeout + 'ms) occurred waiting for ' + host + ':' + port + ' to be available') socket.destroy() @@ -109,21 +109,18 @@ portscanner.checkPortStatus = function(port, arg2, arg3) { // Assuming the port is not open if an error. May need to refine based on // 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' }) // 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 = error || exception } else { error = null } callback(error, status) }) @@ -136,35 +133,35 @@ portscanner.checkPortStatus = function(port, arg2, arg3) { * @param {Number} to * @return {Array} Array of integers from @from to @to inclusive */ -function range(from, to) { - const arr = []; +function range (from, to) { + const arr = [] - while(from <= to) { - arr.push(~~from); - from += 1; + while (from <= to) { + arr.push(~~from) + from += 1 } - return arr; + return arr } -function findAPortWithStatus(status, params) { - var host; - var callback; +function findAPortWithStatus (status, params) { + var host + var callback - //use array of ports + // use array of ports if (typeof params[0] === 'object') { - var ports = params[0]; - host = typeof params[1] === 'string' ? params[1] : null; + var ports = params[0] + host = typeof params[1] === 'string' ? params[1] : null - //use startPort and endPort + // 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; + 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]; + // callback always at the end + callback = Array.prototype.slice.call(params).slice(-1)[0] endPort = endPort || 65535 @@ -174,21 +171,19 @@ function findAPortWithStatus(status, params) { // Returns true if a port with matching status has been found or if checked // the entire range of ports - var hasFoundPort = function() { + var hasFoundPort = function () { return foundPort || numberOfPortsChecked === (ports ? ports.length : endPort - startPort + 1) } // Checks the status of the port - var checkNextPort = function(callback) { - - portscanner.checkPortStatus(port, host, function(error, statusOfPort) { + var checkNextPort = function (callback) { + portscanner.checkPortStatus(port, host, function (error, statusOfPort) { numberOfPortsChecked++ if (statusOfPort === status) { foundPort = true callback(error) - } - else { - port = ports ? ports[numberOfPortsChecked] : port + 1; + } else { + port = ports ? ports[numberOfPortsChecked] : port + 1 callback(null) } }) @@ -196,16 +191,13 @@ function findAPortWithStatus(status, params) { // 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) { + 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) } }) } - diff --git a/package.json b/package.json index 8c9f5ca..228caae 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "portscanner", "description": "Asynchronous port scanner for Node.js", "scripts": { - "test": "ava" + "test": "ava", + "lint": "standard" }, "keywords": [ "portscanner", @@ -33,7 +34,10 @@ "async": "1.5.2" }, "devDependencies": { - "ava": "^0.4.2" + "ava": "^0.4.2", + "eslint": "^3.10.2", + "eslint-config-standard": "^6.2.1", + "standard": "^8.5.0" }, "engines": { "node": ">=0.4", diff --git a/test.js b/test.js index 3ee0714..93e161a 100644 --- a/test.js +++ b/test.js @@ -1,116 +1,111 @@ -import path from 'path'; -import fs from 'fs'; -import net from 'net'; +import net from 'net' -import test from 'ava'; +import test from 'ava' import portScanner from './lib/portscanner.js' test.before('Set #1 test server', t => { - const server = net.createServer(); - server.listen(3000, '127.0.0.1', () => t.end()); -}); + const server = net.createServer() + server.listen(3000, '127.0.0.1', () => t.end()) +}) test.before('Set #2 test server', t => { - const server2 = net.createServer(); - server2.listen(2999, '127.0.0.1', () => t.end()); -}); - + const server2 = net.createServer() + server2.listen(2999, '127.0.0.1', () => t.end()) +}) /* checkPortStatus */ test('checkPortStatus - taken', t => { - t.plan(2); + t.plan(2) - portScanner.checkPortStatus(3000, '127.0.0.1', (error, port) => { - t.is(error, null); - t.is(port, 'open'); - }); -}); + portScanner.checkPortStatus(3000, '127.0.0.1', (error, port) => { + t.is(error, null) + t.is(port, 'open') + }) +}) test('checkPortStatus - free', t => { - t.plan(2); - - portScanner.checkPortStatus(3001, '127.0.0.1', (error, port) => { - t.is(error, null); - t.is(port, 'closed'); - }); -}); + t.plan(2) + portScanner.checkPortStatus(3001, '127.0.0.1', (error, port) => { + t.is(error, null) + t.is(port, 'closed') + }) +}) /* findPortInUse */ test('findPortInUse - taken port in range', t => { - t.plan(2); - - portScanner.findAPortInUse(3000, 3010, '127.0.0.1', (error, port) => { - t.is(error, null); - t.is(port, 3000); - }); -}); - + t.plan(2) + + portScanner.findAPortInUse(3000, 3010, '127.0.0.1', (error, port) => { + t.is(error, null) + t.is(port, 3000) + }) +}) + test('findPortInUse - taken port in range - ports as array', t => { - t.plan(2); - - portScanner.findAPortInUse([2999, 3000, 3001], '127.0.0.1', (error, port) => { - t.is(error, null); - t.is(port, 2999); - }); -}); - + t.plan(2) + + portScanner.findAPortInUse([2999, 3000, 3001], '127.0.0.1', (error, port) => { + t.is(error, null) + t.is(port, 2999) + }) +}) + test('findPortInUse - all ports in range free', t => { - t.plan(2); + t.plan(2) - portScanner.findAPortInUse(3001, 3010, '127.0.0.1', (error, port) => { - t.is(error, null); - t.false(port); - }); -}); + portScanner.findAPortInUse(3001, 3010, '127.0.0.1', (error, port) => { + t.is(error, null) + t.false(port) + }) +}) test('findPortInUse - all ports in range free - ports as array', t => { - t.plan(2); - - portScanner.findAPortInUse([3001, 3005, 3008], '127.0.0.1', (error, port) => { - t.is(error, null); - t.false(port); - }); -}); + t.plan(2) + portScanner.findAPortInUse([3001, 3005, 3008], '127.0.0.1', (error, port) => { + t.is(error, null) + t.false(port) + }) +}) /* findPortNotInUse */ test('findAPortNotInUse - start from free port', t => { - t.plan(2); + t.plan(2) - portScanner.findAPortNotInUse(3001, 3010, '127.0.0.1', (error, port) => { - t.is(error, null); - t.is(port, 3001); - }); -}); + portScanner.findAPortNotInUse(3001, 3010, '127.0.0.1', (error, port) => { + t.is(error, null) + t.is(port, 3001) + }) +}) test('findAPortNotInUse - start from taken port', t => { - t.plan(2); + t.plan(2) - portScanner.findAPortNotInUse(3000, 3010, '127.0.0.1', (error, port) => { - t.is(error, null); - t.is(port, 3001); - }); -}); + portScanner.findAPortNotInUse(3000, 3010, '127.0.0.1', (error, port) => { + t.is(error, null) + t.is(port, 3001) + }) +}) test('findAPortNotInUse - all ports in range taken', t => { - t.plan(2); + t.plan(2) - portScanner.findAPortNotInUse(2999, 3000, '127.0.0.1', (error, port) => { - t.is(error, null); - t.false(port); - }); -}); + portScanner.findAPortNotInUse(2999, 3000, '127.0.0.1', (error, port) => { + t.is(error, null) + t.false(port) + }) +}) test('findAPortNotInUse - with array as parameter', t => { - t.plan(2); + t.plan(2) - portScanner.findAPortNotInUse([3000, 3002, 2999], '127.0.0.1', (error, port) => { - t.is(error, null); - t.is(port, 3002); - }); -}); + portScanner.findAPortNotInUse([3000, 3002, 2999], '127.0.0.1', (error, port) => { + t.is(error, null) + t.is(port, 3002) + }) +})