Skip to content

Commit

Permalink
Scan ports.
Browse files Browse the repository at this point in the history
First commit related to scanning ports. This includes an actual scan of
the ports on a system with success and failure callbacks. Also, this
will fire a complete callback which happens at the end of the scan.

There are a number of options, undocumented at the moment.

There are a handful of tests but nearly extensive.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
  • Loading branch information
ncb000gt committed May 25, 2012
0 parents commit 147f404
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.sw[a-z]
node_modules
60 changes: 60 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var net = require('net');


function scan(port, opts, accum) {
if (!port) port = 1;
if (accum == undefined) accum = 0;
if (!opts) opts = {};
if (opts.startPort == undefined) opts.startPort = port;
if (!(opts.maxPort)) opts.maxPort = 65537; //exclusionary
if (!(opts.host)) opts.host = 'localhost';
if (opts.findActive == undefined) opts.findActive = true;
if (opts.findOne == undefined) opts.findOne = false;
if (!(opts.success)) opts.success = function(host, port) {
}
if (!(opts.failure)) opts.failure = function(host, port) {
}
if (!(opts.complete)) opts.complete = function() {
}

function handleResponse(active, port, opts) {
var success = (opts.findActive === active);

if (success) {
accum++;
opts.success(opts.host, port);
} else {
opts.failure(opts.host, port);
}

if ((!success || (success && !(opts.findOne))) && port < opts.maxPort) {
return scan(++port, opts, accum);
} else {
return opts.complete({
count: (port - opts.startPort),
successes: accum,
failures: (((opts.maxPort > opts.startPort)?(opts.maxPort):opts.startPort+1)-opts.startPort-accum)
});
}
}

var _socket = new net.Socket();
var handled = false;
_socket.setTimeout(3000);
_socket.on('error', function() {
handled = true;
handleResponse(false, port, opts);
});
_socket.on('end', function() {
if (!handled) {
handleResponse(false, port, opts);
}
});
_socket.connect(port, opts.host, function() {
handled = true;
_socket.end();
handleResponse(true, port, opts);
});
}

module.exports = scan;
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "portscan",
"description": "port scanner in node, useful for finding an open port.",
"version": "0.0.1",
"keywords": ["port","scanner","scan","portscan","portscanner","port scan", "port scanner"],
"main": "./index",
"author": "Nick Campbell <nicholas.j.campbell@gmail.com>",
"repository": "https://github.com/ncb000gt/node-portscan.git",
"licenses": ["MIT"],
"bugs": "https://github.com/ncb000gt/node-portscan/issues",
"devDependencies": {
"mocha": "*"
}
}
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--timeout 5000
55 changes: 55 additions & 0 deletions test/suite/scan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var assert = require('assert'),
scan = require('../../index');

describe('scan', function() {
it('calls back with active port', function(done) {
var successes = 0;
scan(0, {
findOne: true,
success: function() {
successes++;
},
complete: function(stats) {
assert.equal(stats.successes, 1);
assert.equal(stats.successes, successes);
done();
}
});
});

it('calls back with first inactive port', function(done) {
var successes = 0;
scan(0, {
findActive: false,
findOne: true,
success: function() {
successes++;
},
complete: function(stats) {
assert.equal(stats.successes, 1);
assert.equal(stats.successes, successes);
done();
}
});
});

it('calls back with high failures and no successes due to artificially large inactive port', function(done) {
var successes = 0;
var failures = 0;
scan(1000000, {
success: function() {
successes++;
},
failure: function() {
failures++;
},
complete: function(stats) {
assert.equal(stats.successes, 0);
assert.ok(stats.failures >= 1);
assert.equal(successes, 0);
assert.equal(stats.failures, failures);
done();
}
});
});
});

0 comments on commit 147f404

Please sign in to comment.