Skip to content

Commit

Permalink
Implement JavaScript Standard Style Guide
Browse files Browse the repository at this point in the history
  • Loading branch information
laggingreflex committed Nov 19, 2016
1 parent 1bf0d6c commit 0beeca8
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 145 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extends": "standard"}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
22 changes: 11 additions & 11 deletions example/portscan.js
Original file line number Diff line number Diff line change
@@ -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)
})
})

102 changes: 47 additions & 55 deletions lib/portscanner.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -74,56 +74,53 @@ 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()
})

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

Expand All @@ -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

Expand All @@ -174,38 +171,33 @@ 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)
}
})
}

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

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "portscanner",
"description": "Asynchronous port scanner for Node.js",
"scripts": {
"test": "ava"
"test": "ava",
"lint": "standard"
},
"keywords": [
"portscanner",
Expand Down Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 0beeca8

Please sign in to comment.