Skip to content

Commit

Permalink
fix: should double check 0.0.0.0 and localhost (#20)
Browse files Browse the repository at this point in the history
should check current ip too

- closes #17
- closes #19
  • Loading branch information
fengmk2 authored May 11, 2017
1 parent 0e852e3 commit 0765f0e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 16 deletions.
55 changes: 43 additions & 12 deletions lib/detect-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const debug = require('debug')('detect-port');
const net = require('net');
const address = require('address');

module.exports = (port, callback) => {
if (typeof port === 'function') {
Expand All @@ -26,28 +27,58 @@ module.exports = (port, callback) => {
};

function tryListen(port, maxPort, callback) {
const server = new net.Server();

server.on('error', err => {
debug('listen %s error: %s', port, err);
if (port === 0) {
return callback(err);
}

function handleError() {
port++;
if (port >= maxPort) {
debug('port: %s >= maxPort: %s, give up and use random port', port, maxPort);
port = 0;
maxPort = 0;
}
tryListen(port, maxPort, callback);
}

// 1. check 0.0.0.0
listen(port, null, (err, realPort) => {
// ignore random listening
if (port === 0) {
return callback(err, realPort);
}

if (err) {
return handleError(err);
}

// 2. check localhost
listen(port, 'localhost', err => {
if (err) {
return handleError(err);
}

// 3. check current ip
listen(port, address.ip(), (err, realPort) => {
if (err) {
return handleError(err);
}

callback(null, realPort);
});
});
});
}

function listen(port, hostname, callback) {
const server = new net.Server();

server.on('error', err => {
debug('listen %s:%s error: %s', hostname, port, err);
server.close();
return tryListen(port, maxPort, callback);
return callback(err);
});

server.listen({ port }, () => {
server.listen(port, hostname, () => {
port = server.address().port;
server.close();
debug('get free port: %s', port);
callback(null, port);
debug('get free %s:%s', hostname, port);
return callback(null, port);
});
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"url": "git://github.com/node-modules/detect-port.git"
},
"dependencies": {
"address": "^1.0.1",
"debug": "^2.6.0"
},
"devDependencies": {
Expand Down
28 changes: 24 additions & 4 deletions test/detect-port.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,30 @@
const assert = require('assert');
const net = require('net');
const pedding = require('pedding');
const address = require('address');
const detectPort = require('..');

describe('detect port test', () => {
const servers = [];
before(done => {
done = pedding(11, done);
done = pedding(12, done);
const server = new net.Server();
server.listen(3000, done);
server.listen(3000, 'localhost', done);
servers.push(server);

const server2 = new net.Server();
server2.listen(4000, address.ip(), done);
servers.push(server2);

for (let port = 7000; port < 7010; port++) {
const server = new net.Server();
server.listen(port, done);
if (port % 3 === 0) {
server.listen(port, done);
} else if (port % 3 === 1) {
server.listen(port, 'localhost', done);
} else {
server.listen(port, address.ip(), done);
}
servers.push(server);
}
});
Expand All @@ -37,14 +49,22 @@ describe('detect port test', () => {
});
});

it('work with listening next port 3001', done => {
it('work with listening next port 3001 because 3000 was listen by localhost', done => {
const port = 3000;
detectPort(port, (_, realPort) => {
assert(realPort === 3001);
done();
});
});

it('work with listening next port 4001 because 4000 was listen by ' + address.ip(), done => {
const port = 4000;
detectPort(port, (_, realPort) => {
assert(realPort === 4001);
done();
});
});

it('work with listening random port when try port hit maxPort', done => {
const port = 7000;
detectPort(port, (_, realPort) => {
Expand Down

0 comments on commit 0765f0e

Please sign in to comment.