1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | "use strict";
|
9 |
|
10 | var fs = require('fs'),
|
11 | os = require('os'),
|
12 | net = require('net'),
|
13 | path = require('path'),
|
14 | _async = require('async'),
|
15 | debug = require('debug'),
|
16 | mkdirp = require('mkdirp').mkdirp;
|
17 |
|
18 | var debugTestPort = debug('portfinder:testPort'),
|
19 | debugGetPort = debug('portfinder:getPort'),
|
20 | debugDefaultHosts = debug('portfinder:defaultHosts');
|
21 |
|
22 | var internals = {};
|
23 |
|
24 | internals.testPort = function(options, callback) {
|
25 | if (!callback) {
|
26 | callback = options;
|
27 | options = {};
|
28 | }
|
29 |
|
30 | options.server = options.server || net.createServer(function () {
|
31 |
|
32 |
|
33 |
|
34 | });
|
35 |
|
36 | debugTestPort("entered testPort(): trying", options.host, "port", options.port);
|
37 |
|
38 | function onListen () {
|
39 | debugTestPort("done w/ testPort(): OK", options.host, "port", options.port);
|
40 |
|
41 | options.server.removeListener('error', onError);
|
42 | options.server.close();
|
43 | callback(null, options.port);
|
44 | }
|
45 |
|
46 | function onError (err) {
|
47 | debugTestPort("done w/ testPort(): failed", options.host, "w/ port", options.port, "with error", err.code);
|
48 |
|
49 | options.server.removeListener('listening', onListen);
|
50 |
|
51 | if (!(err.code == 'EADDRINUSE' || err.code == 'EACCES')) {
|
52 | return callback(err);
|
53 | }
|
54 |
|
55 | var nextPort = exports.nextPort(options.port);
|
56 |
|
57 | if (nextPort > exports.highestPort) {
|
58 | return callback(new Error('No open ports available'));
|
59 | }
|
60 |
|
61 | internals.testPort({
|
62 | port: nextPort,
|
63 | host: options.host,
|
64 | server: options.server
|
65 | }, callback);
|
66 | }
|
67 |
|
68 | options.server.once('error', onError);
|
69 | options.server.once('listening', onListen);
|
70 |
|
71 | if (options.host) {
|
72 | options.server.listen(options.port, options.host);
|
73 | } else {
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 | options.server.listen(options.port);
|
80 | }
|
81 | };
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | exports.basePort = 8000;
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | exports.setBasePort = function (port) {
|
94 | exports.basePort = port;
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | exports.highestPort = 65535;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | exports.setHighestPort = function (port) {
|
108 | exports.highestPort = port;
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | exports.basePath = '/tmp/portfinder'
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | exports.getPort = function (options, callback) {
|
124 | if (!callback) {
|
125 | callback = options;
|
126 | options = {};
|
127 |
|
128 | }
|
129 |
|
130 | options.port = Number(options.port) || Number(exports.basePort);
|
131 | options.host = options.host || null;
|
132 | options.stopPort = Number(options.stopPort) || Number(exports.highestPort);
|
133 |
|
134 | if(!options.startPort) {
|
135 | options.startPort = Number(options.port);
|
136 | if(options.startPort < 0) {
|
137 | throw Error('Provided options.startPort(' + options.startPort + ') is less than 0, which are cannot be bound.');
|
138 | }
|
139 | if(options.stopPort < options.startPort) {
|
140 | throw Error('Provided options.stopPort(' + options.stopPort + 'is less than options.startPort (' + options.startPort + ')');
|
141 | }
|
142 | }
|
143 |
|
144 | if (options.host) {
|
145 | if (exports._defaultHosts.indexOf(options.host) !== -1) {
|
146 | exports._defaultHosts.push(options.host)
|
147 | }
|
148 | }
|
149 |
|
150 | var openPorts = [], currentHost;
|
151 | return _async.eachSeries(exports._defaultHosts, function(host, next) {
|
152 | debugGetPort("in eachSeries() iteration callback: host is", host);
|
153 |
|
154 | return internals.testPort({ host: host, port: options.port }, function(err, port) {
|
155 | if (err) {
|
156 | debugGetPort("in eachSeries() iteration callback testPort() callback", "with an err:", err.code);
|
157 | currentHost = host;
|
158 | return next(err);
|
159 | } else {
|
160 | debugGetPort("in eachSeries() iteration callback testPort() callback",
|
161 | "with a success for port", port);
|
162 | openPorts.push(port);
|
163 | return next();
|
164 | }
|
165 | });
|
166 | }, function(err) {
|
167 |
|
168 | if (err) {
|
169 | debugGetPort("in eachSeries() result callback: err is", err);
|
170 |
|
171 |
|
172 | if (err.code === 'EADDRNOTAVAIL' || err.code === 'EINVAL') {
|
173 | if (options.host === currentHost) {
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | var msg = 'Provided host ' + options.host + ' could NOT be bound. Please provide a different host address or hostname';
|
180 | return callback(Error(msg));
|
181 | } else {
|
182 | var idx = exports._defaultHosts.indexOf(currentHost);
|
183 | exports._defaultHosts.splice(idx, 1);
|
184 | return exports.getPort(options, callback);
|
185 | }
|
186 | } else {
|
187 |
|
188 | return callback(err);
|
189 | }
|
190 | }
|
191 |
|
192 |
|
193 | openPorts.sort(function(a, b) {
|
194 | return a - b;
|
195 | });
|
196 |
|
197 | debugGetPort("in eachSeries() result callback: openPorts is", openPorts);
|
198 |
|
199 | if (openPorts[0] === openPorts[openPorts.length-1]) {
|
200 |
|
201 | if(openPorts[0] <= options.stopPort) {
|
202 | return callback(null, openPorts[0]);
|
203 | }
|
204 | else {
|
205 | var msg = 'No open ports found in between '+ options.startPort + ' and ' + options.stopPort;
|
206 | return callback(Error(msg));
|
207 | }
|
208 | } else {
|
209 |
|
210 | return exports.getPort({ port: openPorts.pop(), host: options.host, startPort: options.startPort, stopPort: options.stopPort }, callback);
|
211 | }
|
212 |
|
213 | });
|
214 | };
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | exports.getPortPromise = function (options) {
|
222 | if (typeof Promise !== 'function') {
|
223 | throw Error('Native promise support is not available in this version of node.' +
|
224 | 'Please install a polyfill and assign Promise to global.Promise before calling this method');
|
225 | }
|
226 | if (!options) {
|
227 | options = {};
|
228 | }
|
229 | return new Promise(function(resolve, reject) {
|
230 | exports.getPort(options, function(err, port) {
|
231 | if (err) {
|
232 | return reject(err);
|
233 | }
|
234 | resolve(port);
|
235 | });
|
236 | });
|
237 | }
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | exports.getPorts = function (count, options, callback) {
|
247 | if (!callback) {
|
248 | callback = options;
|
249 | options = {};
|
250 | }
|
251 |
|
252 | var lastPort = null;
|
253 | _async.timesSeries(count, function(index, asyncCallback) {
|
254 | if (lastPort) {
|
255 | options.port = exports.nextPort(lastPort);
|
256 | }
|
257 |
|
258 | exports.getPort(options, function (err, port) {
|
259 | if (err) {
|
260 | asyncCallback(err);
|
261 | } else {
|
262 | lastPort = port;
|
263 | asyncCallback(null, port);
|
264 | }
|
265 | });
|
266 | }, callback);
|
267 | };
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | exports.getSocket = function (options, callback) {
|
277 | if (!callback) {
|
278 | callback = options;
|
279 | options = {};
|
280 | }
|
281 |
|
282 | options.mod = options.mod || parseInt(755, 8);
|
283 | options.path = options.path || exports.basePath + '.sock';
|
284 |
|
285 |
|
286 |
|
287 |
|
288 | function testSocket () {
|
289 | fs.stat(options.path, function (err) {
|
290 |
|
291 |
|
292 |
|
293 |
|
294 | if (err) {
|
295 | if (err.code == 'ENOENT') {
|
296 | callback(null, options.path);
|
297 | }
|
298 | else {
|
299 | callback(err);
|
300 | }
|
301 | }
|
302 | else {
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | options.path = exports.nextSocket(options.path);
|
308 | exports.getSocket(options, callback);
|
309 | }
|
310 | });
|
311 | }
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | function createAndTestSocket (dir) {
|
318 | mkdirp(dir, options.mod, function (err) {
|
319 | if (err) {
|
320 | return callback(err);
|
321 | }
|
322 |
|
323 | options.exists = true;
|
324 | testSocket();
|
325 | });
|
326 | }
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | function checkAndTestSocket () {
|
335 | var dir = path.dirname(options.path);
|
336 |
|
337 | fs.stat(dir, function (err, stats) {
|
338 | if (err || !stats.isDirectory()) {
|
339 | return createAndTestSocket(dir);
|
340 | }
|
341 |
|
342 | options.exists = true;
|
343 | testSocket();
|
344 | });
|
345 | }
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 | return options.exists
|
353 | ? testSocket()
|
354 | : checkAndTestSocket();
|
355 | };
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 | exports.nextPort = function (port) {
|
364 | return port + 1;
|
365 | };
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 | exports.nextSocket = function (socketPath) {
|
374 | var dir = path.dirname(socketPath),
|
375 | name = path.basename(socketPath, '.sock'),
|
376 | match = name.match(/^([a-zA-z]+)(\d*)$/i),
|
377 | index = parseInt(match[2]),
|
378 | base = match[1];
|
379 | if (isNaN(index)) {
|
380 | index = 0;
|
381 | }
|
382 |
|
383 | index += 1;
|
384 | return path.join(dir, base + index + '.sock');
|
385 | };
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 | exports._defaultHosts = (function() {
|
464 | var interfaces = {};
|
465 | try{
|
466 | interfaces = os.networkInterfaces();
|
467 | }
|
468 | catch(e) {
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 | if (e.syscall === 'uv_interface_addresses') {
|
479 |
|
480 |
|
481 | } else {
|
482 | throw e;
|
483 | }
|
484 | }
|
485 |
|
486 | var interfaceNames = Object.keys(interfaces),
|
487 | hiddenButImportantHost = '0.0.0.0',
|
488 | results = [hiddenButImportantHost];
|
489 | for (var i = 0; i < interfaceNames.length; i++) {
|
490 | var _interface = interfaces[interfaceNames[i]];
|
491 | for (var j = 0; j < _interface.length; j++) {
|
492 | var curr = _interface[j];
|
493 | results.push(curr.address);
|
494 | }
|
495 | }
|
496 |
|
497 |
|
498 | results.push(null);
|
499 |
|
500 | debugDefaultHosts("exports._defaultHosts is: %o", results);
|
501 |
|
502 | return results;
|
503 | }());
|