| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | const debug = require('debug')('detect-port');
|
|---|
| 4 | const net = require('net');
|
|---|
| 5 | const address = require('address');
|
|---|
| 6 |
|
|---|
| 7 | module.exports = (port, host, callback) => {
|
|---|
| 8 | if (typeof port === 'function') {
|
|---|
| 9 | callback = port;
|
|---|
| 10 | port = null;
|
|---|
| 11 | } else if (typeof host === 'function') {
|
|---|
| 12 | callback = host;
|
|---|
| 13 | host = null;
|
|---|
| 14 | }
|
|---|
| 15 | port = parseInt(port) || 0;
|
|---|
| 16 | let maxPort = port + 10;
|
|---|
| 17 | if (maxPort > 65535) {
|
|---|
| 18 | maxPort = 65535;
|
|---|
| 19 | }
|
|---|
| 20 | debug('detect free port between [%s, %s)', port, maxPort);
|
|---|
| 21 | if (typeof callback === 'function') {
|
|---|
| 22 | return tryListen(host, port, maxPort, callback);
|
|---|
| 23 | }
|
|---|
| 24 | // promise
|
|---|
| 25 | return new Promise((resolve, reject) => {
|
|---|
| 26 | tryListen(host, port, maxPort, (error, realPort) => {
|
|---|
| 27 | if (error) {
|
|---|
| 28 | reject(error);
|
|---|
| 29 | } else {
|
|---|
| 30 | resolve(realPort);
|
|---|
| 31 | }
|
|---|
| 32 | });
|
|---|
| 33 | });
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | function tryListen(host, port, maxPort, callback) {
|
|---|
| 37 | function handleError() {
|
|---|
| 38 | port++;
|
|---|
| 39 | if (port >= maxPort) {
|
|---|
| 40 | debug(
|
|---|
| 41 | 'port: %s >= maxPort: %s, give up and use random port',
|
|---|
| 42 | port,
|
|---|
| 43 | maxPort
|
|---|
| 44 | );
|
|---|
| 45 | port = 0;
|
|---|
| 46 | maxPort = 0;
|
|---|
| 47 | }
|
|---|
| 48 | tryListen(host, port, maxPort, callback);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | // 1. check specified host (or null)
|
|---|
| 52 | listen(port, host, (err, realPort) => {
|
|---|
| 53 | // ignore random listening
|
|---|
| 54 | if (port === 0) {
|
|---|
| 55 | return callback(err, realPort);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if (err) {
|
|---|
| 59 | return handleError(err);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | // 2. check default host
|
|---|
| 63 | listen(port, null, err => {
|
|---|
| 64 | if (err) {
|
|---|
| 65 | return handleError(err);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | // 3. check localhost
|
|---|
| 69 | listen(port, 'localhost', err => {
|
|---|
| 70 | if (err) {
|
|---|
| 71 | return handleError(err);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | // 4. check current ip
|
|---|
| 75 | let ip;
|
|---|
| 76 | try {
|
|---|
| 77 | ip = address.ip();
|
|---|
| 78 | } catch (err) {
|
|---|
| 79 | // Skip the `ip` check if `address.ip()` fails
|
|---|
| 80 | return callback(null, realPort);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | listen(port, ip, (err, realPort) => {
|
|---|
| 84 | if (err) {
|
|---|
| 85 | return handleError(err);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | callback(null, realPort);
|
|---|
| 89 | });
|
|---|
| 90 | });
|
|---|
| 91 | });
|
|---|
| 92 | });
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | function listen(port, hostname, callback) {
|
|---|
| 96 | const server = new net.Server();
|
|---|
| 97 |
|
|---|
| 98 | server.on('error', err => {
|
|---|
| 99 | debug('listen %s:%s error: %s', hostname, port, err);
|
|---|
| 100 | server.close();
|
|---|
| 101 | if (err.code === 'ENOTFOUND') {
|
|---|
| 102 | debug('ignore dns ENOTFOUND error, get free %s:%s', hostname, port);
|
|---|
| 103 | return callback(null, port);
|
|---|
| 104 | }
|
|---|
| 105 | return callback(err);
|
|---|
| 106 | });
|
|---|
| 107 |
|
|---|
| 108 | server.listen(port, hostname, () => {
|
|---|
| 109 | port = server.address().port;
|
|---|
| 110 | server.close();
|
|---|
| 111 | debug('get free %s:%s', hostname, port);
|
|---|
| 112 | return callback(null, port);
|
|---|
| 113 | });
|
|---|
| 114 | }
|
|---|