Skip to content
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

fix: should double check 0.0.0.0 and localhost #20

Merged
merged 2 commits into from
May 11, 2017
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
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