From cb85e1226023b66755c48a6f448c6785baf29d79 Mon Sep 17 00:00:00 2001 From: Krizz Date: Wed, 14 Oct 2015 19:19:14 -0400 Subject: [PATCH 1/2] Choose random port if port not specified --- bin/cmd.js | 25 ++++++++++++++++--------- package.json | 1 + 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 1e393009..31669376 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -14,6 +14,7 @@ var path = require('path') var prettyBytes = require('pretty-bytes') var WebTorrent = require('../') var zeroFill = require('zero-fill') +var grop = require('grop') process.title = 'WebTorrent' @@ -67,10 +68,7 @@ var argv = minimist(process.argv.slice(2), { 'subtitles', 'on-done', 'on-exit' - ], - default: { - port: 8000 - } + ] }) if (process.env.DEBUG || argv.stdout) { @@ -310,10 +308,19 @@ function runDownload (torrentId) { // Start http server server = torrent.createServer() - server.listen(argv.port, function () { - if (torrent.ready) onReady() - else torrent.once('ready', onReady) + var port; + + // Find available port + grop(function (availablePort) { + // if port is specified the use that + port = argv.port || availablePort; + server.listen(port, function () { + if (torrent.ready) onReady() + else torrent.once('ready', onReady) + }) }) + + server.once('connection', function () { serving = true }) @@ -366,8 +373,8 @@ function runDownload (torrentId) { function onSelection (index) { href = (argv.airplay || argv.chromecast || argv.xbmc) - ? 'http://' + networkAddress() + ':' + argv.port + '/' + index - : 'http://localhost:' + argv.port + '/' + index + ? 'http://' + networkAddress() + ':' + port + '/' + index + : 'http://localhost:' + port + '/' + index if (playerName) torrent.files[index].select() if (argv.stdout) torrent.files[index].createReadStream().pipe(process.stdout) diff --git a/package.json b/package.json index 25f4e3dd..cfa08ee5 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "end-of-stream": "^1.0.0", "executable": "^1.1.0", "fs-chunk-store": "^1.3.4", + "grop": "^0.1.0", "hat": "0.0.3", "immediate-chunk-store": "^1.0.7", "inherits": "^2.0.1", From b7f5a40c66b73c7835743dfa0039549de0d5f4de Mon Sep 17 00:00:00 2001 From: Krizz Date: Wed, 14 Oct 2015 19:19:14 -0400 Subject: [PATCH 2/2] Choose random port if port not specified --- bin/cmd.js | 24 +++++++++++++++--------- package.json | 1 + 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 1e393009..0909af28 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -14,6 +14,7 @@ var path = require('path') var prettyBytes = require('pretty-bytes') var WebTorrent = require('../') var zeroFill = require('zero-fill') +var grop = require('grop') process.title = 'WebTorrent' @@ -67,10 +68,7 @@ var argv = minimist(process.argv.slice(2), { 'subtitles', 'on-done', 'on-exit' - ], - default: { - port: 8000 - } + ] }) if (process.env.DEBUG || argv.stdout) { @@ -310,10 +308,18 @@ function runDownload (torrentId) { // Start http server server = torrent.createServer() - server.listen(argv.port, function () { - if (torrent.ready) onReady() - else torrent.once('ready', onReady) + + // Find available port + grop(function (availablePort) { + // if port is specified the use that + var port = argv.port || availablePort; + server.listen(port, function () { + if (torrent.ready) onReady() + else torrent.once('ready', onReady) + }) }) + + server.once('connection', function () { serving = true }) @@ -366,8 +372,8 @@ function runDownload (torrentId) { function onSelection (index) { href = (argv.airplay || argv.chromecast || argv.xbmc) - ? 'http://' + networkAddress() + ':' + argv.port + '/' + index - : 'http://localhost:' + argv.port + '/' + index + ? 'http://' + networkAddress() + ':' + server.address().port + '/' + index + : 'http://localhost:' + server.address().port + '/' + index if (playerName) torrent.files[index].select() if (argv.stdout) torrent.files[index].createReadStream().pipe(process.stdout) diff --git a/package.json b/package.json index 25f4e3dd..cfa08ee5 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "end-of-stream": "^1.0.0", "executable": "^1.1.0", "fs-chunk-store": "^1.3.4", + "grop": "^0.1.0", "hat": "0.0.3", "immediate-chunk-store": "^1.0.7", "inherits": "^2.0.1",