Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

A function for checking local port availability #6

Closed
wants to merge 1 commit into from

2 participants

@jdpaton

This local-only function has the additional check of establishing
a listening socket on the intended port to check availability, which
is destroyed as soon as it is in listening status.

This solved an issue I was having where a port established by SSH was being returned as 'open' by portscanner, i.e:

root     43716     1  0 Nov06 ?        00:00:00 ssh -L 0.0.0.0:35005:192.168.3.218:4101 -f stackato@localhost -N -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no 

@jdpaton jdpaton Add a findALocalPortNotInUse function
This local-only function has the additional check of establishing
a listening socket on the intended port to check availability, which
is destroyed as soon as it is in listening status.
c89cfe3
@EndangeredMassa EndangeredMassa commented on the diff
lib/portscanner.js
((9 lines not shown))
+ * @param {Number} endPort - Last port to check status on (inclusive).
+ * Defaults to 65535.
+ * @param {Function} callback - function (error, port) { ... }
+ * - {Object|null} error - Any errors that occurred while port scanning.
+ * - {Number|Boolean} port - The first closed port found. Note, this is the
+ * first port that returns status as 'closed', not
+ * necessarily the first closed port checked. If no
+ * closed port is found, the value is false.
+ */
+portscanner.findALocalPortNotInUse = function(startPort, endPort, callback){
+ findAPortWithStatus('closed', startPort, endPort, '127.0.0.1', function(err, port){
+ var server = net.createServer();
+ server.listen(port);
+ server.on('error', function (e) {
+ if (e.code == 'EADDRINUSE') {
+ portscanner.findLocalPortNotInUse(port, endPort, callback);
@EndangeredMassa Collaborator

Shouldn't this be portscanner.findLocalPortNotInUse(port+1, endPort, callback); (port+1 instead of port)? It appears that if the first port that hits EADDRINUSE will cause an infinite loop here.

It also appears that this line references findLocalPortNotInUse instead of findALocalPortNotInUse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@EndangeredMassa
Collaborator

I'm closing this old PR. I apologize for the delay in addressing it.

If this is still something you would like to pursue, please reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 7, 2012
  1. @jdpaton

    Add a findALocalPortNotInUse function

    jdpaton authored
    This local-only function has the additional check of establishing
    a listening socket on the intended port to check availability, which
    is destroyed as soon as it is in listening status.
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 0 deletions.
  1. +33 −0 lib/portscanner.js
View
33 lib/portscanner.js
@@ -43,6 +43,39 @@ portscanner.findAPortNotInUse = function(startPort, endPort, host, callback) {
}
/**
+ * Finds the first port with a status of 'closed', implying the port is not in
+ * use, additionally attempting to establish a listening socket (local only).
+ * Wraps `findAPortWithStatus` until it finds one that is free.
+ *
+ * @param {Number} startPort - Port to begin status check on (inclusive).
+ * @param {Number} endPort - Last port to check status on (inclusive).
+ * Defaults to 65535.
+ * @param {Function} callback - function (error, port) { ... }
+ * - {Object|null} error - Any errors that occurred while port scanning.
+ * - {Number|Boolean} port - The first closed port found. Note, this is the
+ * first port that returns status as 'closed', not
+ * necessarily the first closed port checked. If no
+ * closed port is found, the value is false.
+ */
+portscanner.findALocalPortNotInUse = function(startPort, endPort, callback){
+ findAPortWithStatus('closed', startPort, endPort, '127.0.0.1', function(err, port){
+ var server = net.createServer();
+ server.listen(port);
+ server.on('error', function (e) {
+ if (e.code == 'EADDRINUSE') {
+ portscanner.findLocalPortNotInUse(port, endPort, callback);
@EndangeredMassa Collaborator

Shouldn't this be portscanner.findLocalPortNotInUse(port+1, endPort, callback); (port+1 instead of port)? It appears that if the first port that hits EADDRINUSE will cause an infinite loop here.

It also appears that this line references findLocalPortNotInUse instead of findALocalPortNotInUse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ }else{
+ callback(e);
+ }
+ });
+ server.on('listening', function(e) {
+ server.close();
+ callback(null, port);
+ });
+ });
+}
+
+/**
* Checks the status of an individual port.
*
* @param {Number} port - Port to check status on.
Something went wrong with that request. Please try again.