portprober.js 5.9 KB

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