Skip to content

fix port reporting #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions example/portscan.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ var http = require('http')

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

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

// Finds a port that a service is listening on
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);
})

// Finds a port no service is listening on
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);
})

})

10 changes: 3 additions & 7 deletions lib/portscanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ portscanner.checkPortStatus = function(port, options, callback) {
})

// Return after the socket has closed
socket.on('close', function(exception) {
if (exception) error = exception
socket.on('close', function() {
callback(error, status)
})

Expand All @@ -117,14 +116,11 @@ function findAPortWithStatus(status, startPort, endPort, host, callback) {
numberOfPortsChecked++
if (statusOfPort === status) {
foundPort = true
callback(error, port)
}
else if (error) {
callback(error)
}
else {
port++
callback(null, false)
callback(null)
}
})
}
Expand All @@ -133,7 +129,7 @@ function findAPortWithStatus(status, startPort, endPort, host, callback) {
// found or the range of ports has been exhausted
async.until(hasFoundPort, checkNextPort, function(error) {
if (error) {
callback(error)
callback(error, port)
}
else if (foundPort) {
callback(null, port)
Expand Down