# net/portprober.js

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 |
20 | var exec = require('child_process').exec,
21 | fs = require('fs'),
22 | net = require('net');
23 |
24 | var promise = require('../index').promise;
25 |
26 |
27 | /**
28 | * The IANA suggested ephemeral port range.
29 | * @type {{min: number, max: number}}
30 | * @const
31 | * @see http://en.wikipedia.org/wiki/Ephemeral_ports
32 | */
33 | var DEFAULT_IANA_RANGE = {min: 49152, max: 65535};
34 |
35 |
36 | /**
37 | * The epheremal port range for the current system. Lazily computed on first
38 | * access.
39 | * @type {webdriver.promise.Promise.<{min: number, max: number}>}
40 | */
41 | var systemRange = null;
42 |
43 |
44 | /**
45 | * Computes the ephemeral port range for the current system. This is based on
46 | * http://stackoverflow.com/a/924337.
47 | * @return {webdriver.promise.Promise.<{min: number, max: number}>} A promise
48 | * that will resolve to the ephemeral port range of the current system.
49 | */
50 | function findSystemPortRange() {
51 | if (systemRange) {
52 | return systemRange;
53 | }
54 | var range = process.platform === 'win32' ?
55 | findWindowsPortRange() : findUnixPortRange();
56 | return systemRange = range.thenCatch(function() {
57 | return DEFAULT_IANA_RANGE;
58 | });
59 | }
60 |
61 |
62 | /**
63 | * Executes a command and returns its output if it succeeds.
64 | * @param {string} cmd The command to execute.
65 | * @return {!webdriver.promise.Promise.<string>} A promise that will resolve
66 | * with the command's stdout data.
67 | */
68 | function execute(cmd) {
69 | var result = promise.defer();
70 | exec(cmd, function(err, stdout) {
71 | if (err) {
72 | result.reject(err);
73 | } else {
74 | result.fulfill(stdout);
75 | }
76 | });
77 | return result.promise;
78 | }
79 |
80 |
81 | /**
82 | * Computes the ephemeral port range for a Unix-like system.
83 | * @return {!webdriver.promise.Promise.<{min: number, max: number}>} A promise
84 | * that will resolve with the ephemeral port range on the current system.
85 | */
86 | function findUnixPortRange() {
87 | var cmd;
88 | if (process.platform === 'sunos') {
89 | cmd =
90 | '/usr/sbin/ndd /dev/tcp tcp_smallest_anon_port tcp_largest_anon_port';
91 | } else if (fs.existsSync('/proc/sys/net/ipv4/ip_local_port_range')) {
92 | // Linux
93 | cmd = 'cat /proc/sys/net/ipv4/ip_local_port_range';
94 | } else {
95 | cmd = 'sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last' +
96 | ' | sed -e "s/.*:\\s*//"';
97 | }
98 |
99 | return execute(cmd).then(function(stdout) {
100 | if (!stdout || !stdout.length) return DEFAULT_IANA_RANGE;
101 | var range = stdout.trim().split(/\s+/).map(Number);
102 | if (range.some(isNaN)) return DEFAULT_IANA_RANGE;
103 | return {min: range[0], max: range[1]};
104 | });
105 | }
106 |
107 |
108 | /**
109 | * Computes the ephemeral port range for a Windows system.
110 | * @return {!webdriver.promise.Promise.<{min: number, max: number}>} A promise
111 | * that will resolve with the ephemeral port range on the current system.
112 | */
113 | function findWindowsPortRange() {
114 | var deferredRange = promise.defer();
115 | // First, check if we're running on XP. If this initial command fails,
116 | // we just fallback on the default IANA range.
117 | return execute('cmd.exe /c ver').then(function(stdout) {
118 | if (/Windows XP/.test(stdout)) {
119 | // TODO: Try to read these values from the registry.
120 | return {min: 1025, max: 5000};
121 | } else {
122 | return execute('netsh int ipv4 show dynamicport tcp').
123 | then(function(stdout) {
124 | /* > netsh int ipv4 show dynamicport tcp
125 | Protocol tcp Dynamic Port Range
126 | ---------------------------------
127 | Start Port : 49152
128 | Number of Ports : 16384
129 | */
130 | var range = stdout.split(/\n/).filter(function(line) {
131 | return /.*:\s*\d+/.test(line);
132 | }).map(function(line) {
133 | return Number(line.split(/:\s*/)[1]);
134 | });
135 |
136 | return {
137 | min: range[0],
138 | max: range[0] + range[1]
139 | };
140 | });
141 | }
142 | });
143 | }
144 |
145 |
146 | /**
147 | * Tests if a port is free.
148 | * @param {number} port The port to test.
149 | * @param {string=} opt_host The bound host to test the {@code port} against.
150 | * Defaults to {@code INADDR_ANY}.
151 | * @return {!webdriver.promise.Promise.<boolean>} A promise that will resolve
152 | * with whether the port is free.
153 | */
154 | function isFree(port, opt_host) {
155 | var result = promise.defer(function() {
156 | server.cancel();
157 | });
158 |
159 | var server = net.createServer().on('error', function(e) {
160 | if (e.code === 'EADDRINUSE') {
161 | result.fulfill(false);
162 | } else {
163 | result.reject(e);
164 | }
165 | });
166 |
167 | server.listen(port, opt_host, function() {
168 | server.close(function() {
169 | result.fulfill(true);
170 | });
171 | });
172 |
173 | return result.promise;
174 | }
175 |
176 |
177 | /**
178 | * @param {string=} opt_host The bound host to test the {@code port} against.
179 | * Defaults to {@code INADDR_ANY}.
180 | * @return {!webdriver.promise.Promise.<number>} A promise that will resolve
181 | * to a free port. If a port cannot be found, the promise will be
182 | * rejected.
183 | */
184 | function findFreePort(opt_host) {
185 | return findSystemPortRange().then(function(range) {
186 | var attempts = 0;
187 | var deferredPort = promise.defer();
188 | findPort();
189 | return deferredPort.promise;
190 |
191 | function findPort() {
192 | attempts += 1;
193 | if (attempts > 10) {
194 | deferredPort.reject(Error('Unable to find a free port'));
195 | }
196 |
197 | var port = Math.floor(
198 | Math.random() * (range.max - range.min) + range.min);
199 | isFree(port, opt_host).then(function(isFree) {
200 | if (isFree) {
201 | deferredPort.fulfill(port);
202 | } else {
203 | findPort();
204 | }
205 | });
206 | }
207 | });
208 | }
209 |
210 |
211 | // PUBLIC API
212 |
213 |
214 | exports.findFreePort = findFreePort;
215 | exports.isFree = isFree;