UNPKG

6.01 kBJavaScriptView Raw
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18'use strict';
19
20var exec = require('child_process').exec,
21 fs = require('fs'),
22 net = require('net');
23
24
25/**
26 * The IANA suggested ephemeral port range.
27 * @type {{min: number, max: number}}
28 * @const
29 * @see http://en.wikipedia.org/wiki/Ephemeral_ports
30 */
31const DEFAULT_IANA_RANGE = {min: 49152, max: 65535};
32
33
34/**
35 * The epheremal port range for the current system. Lazily computed on first
36 * access.
37 * @type {Promise.<{min: number, max: number}>}
38 */
39var systemRange = null;
40
41
42/**
43 * Computes the ephemeral port range for the current system. This is based on
44 * http://stackoverflow.com/a/924337.
45 * @return {!Promise<{min: number, max: number}>} A promise that will resolve to
46 * the ephemeral port range of the current system.
47 */
48function findSystemPortRange() {
49 if (systemRange) {
50 return systemRange;
51 }
52 var range = process.platform === 'win32' ?
53 findWindowsPortRange() : findUnixPortRange();
54 return systemRange = range.catch(function() {
55 return DEFAULT_IANA_RANGE;
56 });
57}
58
59
60/**
61 * Executes a command and returns its output if it succeeds.
62 * @param {string} cmd The command to execute.
63 * @return {!Promise<string>} A promise that will resolve with the command's
64 * stdout data.
65 */
66function execute(cmd) {
67 return new Promise((resolve, reject) => {
68 exec(cmd, function(err, stdout) {
69 if (err) {
70 reject(err);
71 } else {
72 resolve(stdout);
73 }
74 });
75 });
76}
77
78
79/**
80 * Computes the ephemeral port range for a Unix-like system.
81 * @return {!Promise<{min: number, max: number}>} A promise that will resolve
82 * with the ephemeral port range on the current system.
83 */
84function findUnixPortRange() {
85 var cmd;
86 if (process.platform === 'sunos') {
87 cmd =
88 '/usr/sbin/ndd /dev/tcp tcp_smallest_anon_port tcp_largest_anon_port';
89 } else if (fs.existsSync('/proc/sys/net/ipv4/ip_local_port_range')) {
90 // Linux
91 cmd = 'cat /proc/sys/net/ipv4/ip_local_port_range';
92 } else {
93 cmd = 'sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last' +
94 ' | sed -e "s/.*:\\s*//"';
95 }
96
97 return execute(cmd).then(function(stdout) {
98 if (!stdout || !stdout.length) return DEFAULT_IANA_RANGE;
99 var range = stdout.trim().split(/\s+/).map(Number);
100 if (range.some(isNaN)) return DEFAULT_IANA_RANGE;
101 return {min: range[0], max: range[1]};
102 });
103}
104
105
106/**
107 * Computes the ephemeral port range for a Windows system.
108 * @return {!Promise<{min: number, max: number}>} A promise that will resolve
109 * with the ephemeral port range on the current system.
110 */
111function findWindowsPortRange() {
112 // First, check if we're running on XP. If this initial command fails,
113 // we just fallback on the default IANA range.
114 return execute('cmd.exe /c ver').then(function(stdout) {
115 if (/Windows XP/.test(stdout)) {
116 // TODO: Try to read these values from the registry.
117 return {min: 1025, max: 5000};
118 } else {
119 return execute('netsh int ipv4 show dynamicport tcp').
120 then(function(stdout) {
121 /* > netsh int ipv4 show dynamicport tcp
122 Protocol tcp Dynamic Port Range
123 ---------------------------------
124 Start Port : 49152
125 Number of Ports : 16384
126 */
127 var range = stdout.split(/\n/).filter(function(line) {
128 return /.*:\s*\d+/.test(line);
129 }).map(function(line) {
130 return Number(line.split(/:\s*/)[1]);
131 });
132
133 return {
134 min: range[0],
135 max: range[0] + range[1]
136 };
137 });
138 }
139 });
140}
141
142
143/**
144 * Tests if a port is free.
145 * @param {number} port The port to test.
146 * @param {string=} opt_host The bound host to test the {@code port} against.
147 * Defaults to {@code INADDR_ANY}.
148 * @return {!Promise<boolean>} A promise that will resolve with whether the port
149 * is free.
150 */
151function isFree(port, opt_host) {
152 return new Promise((resolve, reject) => {
153 let server = net.createServer().on('error', function(e) {
154 if (e.code === 'EADDRINUSE') {
155 resolve(false);
156 } else {
157 reject(e);
158 }
159 });
160
161 server.listen(port, opt_host, function() {
162 server.close(() => resolve(true));
163 });
164 });
165}
166
167
168/**
169 * @param {string=} opt_host The bound host to test the {@code port} against.
170 * Defaults to {@code INADDR_ANY}.
171 * @return {!Promise<number>} A promise that will resolve to a free port. If a
172 * port cannot be found, the promise will be rejected.
173 */
174function findFreePort(opt_host) {
175 return findSystemPortRange().then(function(range) {
176 var attempts = 0;
177 return new Promise((resolve, reject) => {
178 findPort();
179
180 function findPort() {
181 attempts += 1;
182 if (attempts > 10) {
183 reject(Error('Unable to find a free port'));
184 }
185
186 var port = Math.floor(
187 Math.random() * (range.max - range.min) + range.min);
188 isFree(port, opt_host).then(function(isFree) {
189 if (isFree) {
190 resolve(port);
191 } else {
192 findPort();
193 }
194 });
195 }
196 });
197 });
198}
199
200
201// PUBLIC API
202
203
204exports.findFreePort = findFreePort;
205exports.isFree = isFree;