-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
0 parents
commit 147f404
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.sw[a-z] | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--timeout 5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}); | ||
}); |