From 65e1ff3e9abb2ef705907e0791c501b5a08c57d9 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Sat, 18 Feb 2017 14:24:44 +0100 Subject: [PATCH 01/17] Add upnp nat traversal --- index.js | 31 ++++++++++++++++++-- lib/nat-traversal.js | 69 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++ 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 lib/nat-traversal.js diff --git a/index.js b/index.js index c0662887..ef5076e9 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,7 @@ var randombytes = require('randombytes') var speedometer = require('speedometer') var zeroFill = require('zero-fill') +var NatTraversal = require('./lib/nat-traversal') // browser exclude var TCPPool = require('./lib/tcp-pool') // browser exclude var Torrent = require('./lib/torrent') @@ -106,6 +107,10 @@ function WebTorrent (opts) { if (global.WRTC && !self.tracker.wrtc) self.tracker.wrtc = global.WRTC } + if (typeof NatTraversal === 'function') { + self._natTraversal = new NatTraversal() + } + if (typeof TCPPool === 'function') { self._tcpPool = new TCPPool(self) } else { @@ -128,7 +133,12 @@ function WebTorrent (opts) { self.dht.once('listening', function () { var address = self.dht.address() - if (address) self.dhtPort = address.port + if (address) { + self.dhtPort = address.port + if (self._natTraversal) { + self._natTraversal.portMapping(self.dhtPort) + } + } }) // Ignore warning when there are > 10 torrents in the client @@ -425,6 +435,18 @@ WebTorrent.prototype._destroy = function (err, cb) { }) } + if (self._natTraversal) { + tasks.push(function (cb) { + if (self.dhtPort) { + self._natTraversal.portUnMapping(self.dhtPort) + } + if (self.torrentPort) { + self._natTraversal.portUnMapping(self.torrentPort) + } + self._natTraversal.destroy(cb) + }) + } + parallel(tasks, cb) if (err) self.emit('error', err) @@ -441,7 +463,12 @@ WebTorrent.prototype._onListening = function () { // Sometimes server.address() returns `null` in Docker. // WebTorrent issue: https://github.com/feross/bittorrent-swarm/pull/18 var address = this._tcpPool.server.address() - if (address) this.torrentPort = address.port + if (address) { + this.torrentPort = address.port + if (this._natTraversal) { + this._natTraversal.portMapping(this.torrentPort) + } + } } this.emit('listening') diff --git a/lib/nat-traversal.js b/lib/nat-traversal.js new file mode 100644 index 00000000..c0478dd0 --- /dev/null +++ b/lib/nat-traversal.js @@ -0,0 +1,69 @@ + +module.exports = NatTraversal + +var debug = require('debug')('webtorrent:nat-traversal') +var natUpnp = require('nat-upnp') // browser exclude + +function NatTraversal () { + var self = this + + debug('Create UPnP client') + self._upnpClient = natUpnp.createClient() + self.ttl = 60 * 30 + self.timeout = self.ttl - 60 + self.intervals = {} +} + +NatTraversal.prototype.portMapping = function (port, cb) { + var self = this + + debug('Mapping port %d on router using UPnP', port) + self._upnpClient.portMapping({ + public: port, + private: port, + description: 'WebTorrent', + ttl: self.ttl + }, function (err) { + if (err) { + return typeof cb === 'function' && cb(err) + } + self.intervals[port] = setInterval(NatTraversal.prototype.portMapping.bind(self, port), self.timeout) + debug('Port %d mapped on router using UPnP', port) + if (typeof cb === 'function') cb() + }) +} + +NatTraversal.prototype.portUnMapping = function (port, cb) { + var self = this + if (!self.intervals[port]) return typeof cb === 'function' && cb(new Error('Port not mapped')) + + debug('Unmapping port %d on router using UPnP', port) + clearInterval(self.intervals[port]) + delete self.intervals[port] + + self._upnpClient.portUnmapping({ + public: port + }, function (err) { + if (err) { + return typeof cb === 'function' && cb(err) + } + debug('Port %d unmapped on router using UPnP', port) + if (typeof cb === 'function') cb() + }) +} + +NatTraversal.prototype.destroy = function (cb) { + var self = this + + // Clear all intervals + Object.keys(self.intervals).forEach(function (port) { + self.portUnMapping(port) + }) + + // Waiting next tick to prevent breaking some sockets + process.nextTick(function () { + self._upnpClient.close() + debug('UPnP client closed') + cb() + }) +} diff --git a/package.json b/package.json index 320014be..6a21be88 100644 --- a/package.json +++ b/package.json @@ -8,11 +8,13 @@ "url": "https://webtorrent.io" }, "browser": { + "./lib/nat-traversal.js": false, "./lib/server.js": false, "./lib/tcp-pool.js": false, "bittorrent-dht/client": false, "fs-chunk-store": "memory-chunk-store", "load-ip-set": false, + "nat-upnp": false, "net": false, "os": false, "ut_pex": false @@ -41,6 +43,7 @@ "memory-chunk-store": "^1.2.0", "mime": "^1.3.4", "multistream": "^2.0.5", + "nat-upnp": "^1.0.4", "package-json-versionify": "^1.0.2", "parse-torrent": "^5.8.0", "pump": "^1.0.1", From a2bd6977677e94754f2aff4ae6c301bc65db43c5 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Sat, 18 Feb 2017 21:16:42 +0100 Subject: [PATCH 02/17] Adds NAT-PMP port mapping --- index.js | 11 +--- lib/nat-traversal.js | 145 ++++++++++++++++++++++++++++++++++--------- package.json | 4 ++ 3 files changed, 121 insertions(+), 39 deletions(-) diff --git a/index.js b/index.js index ef5076e9..6784727e 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,6 @@ var randombytes = require('randombytes') var speedometer = require('speedometer') var zeroFill = require('zero-fill') -var NatTraversal = require('./lib/nat-traversal') // browser exclude var TCPPool = require('./lib/tcp-pool') // browser exclude var Torrent = require('./lib/torrent') @@ -107,9 +106,7 @@ function WebTorrent (opts) { if (global.WRTC && !self.tracker.wrtc) self.tracker.wrtc = global.WRTC } - if (typeof NatTraversal === 'function') { - self._natTraversal = new NatTraversal() - } + self._natTraversal = require('./lib/nat-traversal') // browser exclude if (typeof TCPPool === 'function') { self._tcpPool = new TCPPool(self) @@ -437,12 +434,6 @@ WebTorrent.prototype._destroy = function (err, cb) { if (self._natTraversal) { tasks.push(function (cb) { - if (self.dhtPort) { - self._natTraversal.portUnMapping(self.dhtPort) - } - if (self.torrentPort) { - self._natTraversal.portUnMapping(self.torrentPort) - } self._natTraversal.destroy(cb) }) } diff --git a/lib/nat-traversal.js b/lib/nat-traversal.js index c0478dd0..1e9393b1 100644 --- a/lib/nat-traversal.js +++ b/lib/nat-traversal.js @@ -1,69 +1,156 @@ - -module.exports = NatTraversal - +var arrayRemove = require('unordered-array-remove') var debug = require('debug')('webtorrent:nat-traversal') var natUpnp = require('nat-upnp') // browser exclude +var natpmp = require('nat-pmp') // browser exclude +var network = require('network') // browser exclude + +// Use single instance +module.exports = new NatTraversal() function NatTraversal () { var self = this + self._destroyed = false + + // The RECOMMENDED Port Mapping Lifetime is 7200 seconds (two hours). + self.ttl = 7200 + // Refresh the mapping 10 minutes before the end of its lifetime + self.timeout = (self.ttl - 600) * 1000 + self._openedPorts = [] + self._intervalsUpnp = {} + self._intervalsPmp = {} + + self._upnpPortMapping = function (port, cb) { + var self = this - debug('Create UPnP client') + debug('Mapping port %d on router using UPnP', port) + self._upnpClient.portMapping({ + public: port, + private: port, + description: 'WebTorrent', + ttl: self.ttl + }, function (err) { + if (self._destroyed) return typeof cb === 'function' && cb() + if (err) { + return typeof cb === 'function' && cb(err) + } + self._intervalsUpnp[port] = setInterval(self._pmpPortMapping.bind(self, port), self.timeout) + debug('Port %d mapped on router using UPnP', port) + if (typeof cb === 'function') cb() + }) + } + + self._pmpPortMapping = function (port, cb) { + var self = this + + debug('Mapping port %d on router using NAT-PMP', port) + self._pmpClient.portMapping({ + private: port, + public: port, + ttl: self.ttl, + type: 'tcp' + }, function (err/*, info*/) { + if (self._destroyed) return typeof cb === 'function' && cb() + if (err) { + debug('Error mapping port %d using NAT-PMP', port, err) + return typeof cb === 'function' && cb(err) + } + self._intervalsPmp[port] = setInterval(self._pmpPortMapping.bind(self, port), self.timeout) + debug('Port %d mapped on router using NAT-PMP', port) + if (typeof cb === 'function') cb() + }) + } + + debug('UPnP client creation') self._upnpClient = natUpnp.createClient() - self.ttl = 60 * 30 - self.timeout = self.ttl - 60 - self.intervals = {} + + // Lookup gateway IP + debug('Lookup gateway IP') + network.get_gateway_ip(function (err, ip) { + if (self._destroyed) return + if (err) { + return debug('Could not find gateway IP for NAT-PMP', err) + } + debug('NAT-PMP client creation', ip) + self._pmpClient = natpmp.connect(ip) + self._openedPorts.forEach(function (port) { + self._pmpPortMapping(port) + }) + }) } NatTraversal.prototype.portMapping = function (port, cb) { var self = this + if (self._destroyed) return typeof cd === 'function' && cb() - debug('Mapping port %d on router using UPnP', port) - self._upnpClient.portMapping({ - public: port, - private: port, - description: 'WebTorrent', - ttl: self.ttl - }, function (err) { + self._openedPorts.push(port) + + // Try UPnP first + self._upnpPortMapping(port, function (err) { + if (self._destroyed) return typeof cb === 'function' && cb() if (err) { - return typeof cb === 'function' && cb(err) + debug('UPnP port mapping failed on %d', port, err.message) + } + + // Then NAT-PMP + if (self._pmpClient) { + self._pmpPortMapping(port, cb) + } else if (typeof cb === 'function') { + cb() } - self.intervals[port] = setInterval(NatTraversal.prototype.portMapping.bind(self, port), self.timeout) - debug('Port %d mapped on router using UPnP', port) - if (typeof cb === 'function') cb() }) } NatTraversal.prototype.portUnMapping = function (port, cb) { var self = this - if (!self.intervals[port]) return typeof cb === 'function' && cb(new Error('Port not mapped')) + if (self._destroyed) return typeof cd === 'function' && cb() + arrayRemove(self._openedPorts, self._openedPorts.indexOf(port)) - debug('Unmapping port %d on router using UPnP', port) - clearInterval(self.intervals[port]) - delete self.intervals[port] + // Clear intervals + if (self._intervalsUpnp[port]) { + clearInterval(self._intervalsUpnp[port]) + delete self._intervalsUpnp[port] + } + if (self._intervalsPmp[port]) { + clearInterval(self._intervalsPmp[port]) + delete self._intervalsPmp[port] + } + debug('Unmapping port %d on router using UPnP', port) self._upnpClient.portUnmapping({ public: port }, function (err) { - if (err) { - return typeof cb === 'function' && cb(err) + if (!err) debug('Port %d unmapped on router using UPnP', port) + if (self._pmpClient) { + debug('Unmapping port %d on router using NAT-PMP', port) + self._pmpClient.portUnmapping({ + private: port, + public: port + }, cb) + } else { + if (typeof cb === 'function') cb() } - debug('Port %d unmapped on router using UPnP', port) - if (typeof cb === 'function') cb() }) } NatTraversal.prototype.destroy = function (cb) { var self = this + if (self._destroyed) return cb() - // Clear all intervals - Object.keys(self.intervals).forEach(function (port) { + // Unmap all ports + self._openedPorts.forEach(function (port) { self.portUnMapping(port) }) + self._destroyed = true + + if (self._pmpClient) { + debug('Close pmp client') + self._pmpClient.close() + } // Waiting next tick to prevent breaking some sockets process.nextTick(function () { + debug('Close UPnP client') self._upnpClient.close() - debug('UPnP client closed') cb() }) } diff --git a/package.json b/package.json index 6a21be88..6cc82f56 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,10 @@ "bittorrent-dht/client": false, "fs-chunk-store": "memory-chunk-store", "load-ip-set": false, + "nat-pmp": false, "nat-upnp": false, "net": false, + "network": false, "os": false, "ut_pex": false }, @@ -43,7 +45,9 @@ "memory-chunk-store": "^1.2.0", "mime": "^1.3.4", "multistream": "^2.0.5", + "nat-pmp": "^1.0.0", "nat-upnp": "^1.0.4", + "network": "^0.3.2", "package-json-versionify": "^1.0.2", "parse-torrent": "^5.8.0", "pump": "^1.0.1", From 1ce6c9c56df31a8b96e4a776d3244e9532284569 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Sat, 18 Feb 2017 21:29:54 +0100 Subject: [PATCH 03/17] standard fix --- lib/nat-traversal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nat-traversal.js b/lib/nat-traversal.js index 1e9393b1..e63d4040 100644 --- a/lib/nat-traversal.js +++ b/lib/nat-traversal.js @@ -48,7 +48,7 @@ function NatTraversal () { public: port, ttl: self.ttl, type: 'tcp' - }, function (err/*, info*/) { + }, function (err/* , info */) { if (self._destroyed) return typeof cb === 'function' && cb() if (err) { debug('Error mapping port %d using NAT-PMP', port, err) From 2e4385a56fe9b991d12dee0de16a2b2e1306fe3f Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Wed, 22 Feb 2017 15:05:19 +0100 Subject: [PATCH 04/17] Testing patch for the tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6cc82f56..80fb22da 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "memory-chunk-store": "^1.2.0", "mime": "^1.3.4", "multistream": "^2.0.5", - "nat-pmp": "^1.0.0", + "nat-pmp": "github:yciabaud/node-nat-pmp#patch-1", "nat-upnp": "^1.0.4", "network": "^0.3.2", "package-json-versionify": "^1.0.2", From 3bc4205af956ad44fad87de8ca72e85ad7541eb6 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Wed, 22 Feb 2017 15:13:39 +0100 Subject: [PATCH 05/17] Add NAt support for HTTP server --- lib/server.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/server.js b/lib/server.js index 911da59c..b957989c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -10,6 +10,7 @@ var url = require('url') function Server (torrent, opts) { var server = http.createServer(opts) + var natTraversal = require('./nat-traversal') var sockets = [] var pendingReady = [] @@ -17,6 +18,7 @@ function Server (torrent, opts) { server.on('connection', onConnection) server.on('request', onRequest) + server.on('listening', onListening) var _close = server.close server.close = function (cb) { @@ -36,6 +38,10 @@ function Server (torrent, opts) { socket.destroy() }) + if (server.address()) { + natTraversal.portUnMapping(server.address().port) + } + // Only call `server.close` if user has not called it already if (closed) process.nextTick(cb) else server.close(cb) @@ -134,5 +140,9 @@ function Server (torrent, opts) { } } + function onListening () { + natTraversal.portMapping(server.address().port) + } + return server } From cf0559078bbed214b492564f0f277017fd0d0780 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Wed, 22 Feb 2017 17:01:29 +0100 Subject: [PATCH 06/17] Fix browser issues and revert nat-pmp patch --- index.js | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 6784727e..838cb912 100644 --- a/index.js +++ b/index.js @@ -132,7 +132,7 @@ function WebTorrent (opts) { var address = self.dht.address() if (address) { self.dhtPort = address.port - if (self._natTraversal) { + if (self._natTraversal.portMapping) { self._natTraversal.portMapping(self.dhtPort) } } @@ -432,7 +432,7 @@ WebTorrent.prototype._destroy = function (err, cb) { }) } - if (self._natTraversal) { + if (self._natTraversal.destroy) { tasks.push(function (cb) { self._natTraversal.destroy(cb) }) @@ -456,7 +456,7 @@ WebTorrent.prototype._onListening = function () { var address = this._tcpPool.server.address() if (address) { this.torrentPort = address.port - if (this._natTraversal) { + if (this._natTraversal.portMapping) { this._natTraversal.portMapping(this.torrentPort) } } diff --git a/package.json b/package.json index 80fb22da..6cc82f56 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "memory-chunk-store": "^1.2.0", "mime": "^1.3.4", "multistream": "^2.0.5", - "nat-pmp": "github:yciabaud/node-nat-pmp#patch-1", + "nat-pmp": "^1.0.0", "nat-upnp": "^1.0.4", "network": "^0.3.2", "package-json-versionify": "^1.0.2", From 4ba6f598f05e789d245fac77ba49a49925e474fb Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Wed, 22 Feb 2017 17:43:03 +0100 Subject: [PATCH 07/17] restore patch --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6cc82f56..80fb22da 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "memory-chunk-store": "^1.2.0", "mime": "^1.3.4", "multistream": "^2.0.5", - "nat-pmp": "^1.0.0", + "nat-pmp": "github:yciabaud/node-nat-pmp#patch-1", "nat-upnp": "^1.0.4", "network": "^0.3.2", "package-json-versionify": "^1.0.2", From c6b00b3cf2979788f38077ae6b5b5131f5084da2 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Sun, 17 Jun 2018 15:24:02 +0300 Subject: [PATCH 08/17] allow UDP for DHT port --- index.js | 4 ++-- lib/nat-traversal.js | 41 +++++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index af7579c5..401e6d67 100644 --- a/index.js +++ b/index.js @@ -137,7 +137,7 @@ function WebTorrent (opts) { if (address) { self.dhtPort = address.port if (self._natTraversal.portMapping) { - self._natTraversal.portMapping(self.dhtPort) + self._natTraversal.portMapping(self.dhtPort, 'udp') } } }) @@ -464,7 +464,7 @@ WebTorrent.prototype._onListening = function () { if (address) { this.torrentPort = address.port if (this._natTraversal.portMapping) { - this._natTraversal.portMapping(this.torrentPort) + this._natTraversal.portMapping(this.torrentPort, 'tcp') } } } diff --git a/lib/nat-traversal.js b/lib/nat-traversal.js index e63d4040..34426d57 100644 --- a/lib/nat-traversal.js +++ b/lib/nat-traversal.js @@ -19,43 +19,44 @@ function NatTraversal () { self._intervalsUpnp = {} self._intervalsPmp = {} - self._upnpPortMapping = function (port, cb) { + self._upnpPortMapping = function (port, protocol, cb) { var self = this - debug('Mapping port %d on router using UPnP', port) + debug('Mapping port %d for protocol %s on router using UPnP', port, udp) self._upnpClient.portMapping({ public: port, private: port, description: 'WebTorrent', + protocol: protocol, ttl: self.ttl }, function (err) { if (self._destroyed) return typeof cb === 'function' && cb() if (err) { return typeof cb === 'function' && cb(err) } - self._intervalsUpnp[port] = setInterval(self._pmpPortMapping.bind(self, port), self.timeout) - debug('Port %d mapped on router using UPnP', port) + self._intervalsUpnp[port] = setInterval(self._pmpPortMapping.bind(self, port, protocol), self.timeout) + debug('Port %d for protocol %s mapped on router using UPnP', port, protocol) if (typeof cb === 'function') cb() }) } - self._pmpPortMapping = function (port, cb) { + self._pmpPortMapping = function (port, protocol, cb) { var self = this - debug('Mapping port %d on router using NAT-PMP', port) + debug('Mapping port %d for protocol %s on router using NAT-PMP', port, protocol) self._pmpClient.portMapping({ private: port, public: port, ttl: self.ttl, - type: 'tcp' + type: protocol }, function (err/* , info */) { if (self._destroyed) return typeof cb === 'function' && cb() if (err) { debug('Error mapping port %d using NAT-PMP', port, err) return typeof cb === 'function' && cb(err) } - self._intervalsPmp[port] = setInterval(self._pmpPortMapping.bind(self, port), self.timeout) - debug('Port %d mapped on router using NAT-PMP', port) + self._intervalsPmp[port] = setInterval(self._pmpPortMapping.bind(self, port, protocol), self.timeout) + debug('Port %d for protocol %s mapped on router using NAT-PMP', port, protocol) if (typeof cb === 'function') cb() }) } @@ -72,20 +73,24 @@ function NatTraversal () { } debug('NAT-PMP client creation', ip) self._pmpClient = natpmp.connect(ip) - self._openedPorts.forEach(function (port) { - self._pmpPortMapping(port) + self._openedPorts.forEach(function (obj) { + self._pmpPortMapping(obj.port, obj.protocol) }) }) } -NatTraversal.prototype.portMapping = function (port, cb) { +NatTraversal.prototype.portMapping = function (port, protocol, cb) { var self = this if (self._destroyed) return typeof cd === 'function' && cb() + if (typeof protocol === 'function') { + cb = protocol + protocol = 'tcp' + } - self._openedPorts.push(port) + self._openedPorts.push({port: port, protocol: protocol}) // Try UPnP first - self._upnpPortMapping(port, function (err) { + self._upnpPortMapping(port, protocol, function (err) { if (self._destroyed) return typeof cb === 'function' && cb() if (err) { debug('UPnP port mapping failed on %d', port, err.message) @@ -93,7 +98,7 @@ NatTraversal.prototype.portMapping = function (port, cb) { // Then NAT-PMP if (self._pmpClient) { - self._pmpPortMapping(port, cb) + self._pmpPortMapping(port, protocol, cb) } else if (typeof cb === 'function') { cb() } @@ -103,7 +108,7 @@ NatTraversal.prototype.portMapping = function (port, cb) { NatTraversal.prototype.portUnMapping = function (port, cb) { var self = this if (self._destroyed) return typeof cd === 'function' && cb() - arrayRemove(self._openedPorts, self._openedPorts.indexOf(port)) + arrayRemove(self._openedPorts, self._openedPorts.findIndex(o => o.port === port)) // Clear intervals if (self._intervalsUpnp[port]) { @@ -137,8 +142,8 @@ NatTraversal.prototype.destroy = function (cb) { if (self._destroyed) return cb() // Unmap all ports - self._openedPorts.forEach(function (port) { - self.portUnMapping(port) + self._openedPorts.forEach(function (obj) { + self.portUnMapping(obj.port) }) self._destroyed = true From 1a7b11ed648a69b307387601cd7bb52876a103f2 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Sun, 17 Jun 2018 15:35:29 +0300 Subject: [PATCH 09/17] fix unrecognized variable --- lib/nat-traversal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nat-traversal.js b/lib/nat-traversal.js index 34426d57..05ceb877 100644 --- a/lib/nat-traversal.js +++ b/lib/nat-traversal.js @@ -22,7 +22,7 @@ function NatTraversal () { self._upnpPortMapping = function (port, protocol, cb) { var self = this - debug('Mapping port %d for protocol %s on router using UPnP', port, udp) + debug('Mapping port %d for protocol %s on router using UPnP', port, protocol) self._upnpClient.portMapping({ public: port, private: port, From a1d230be59a473876a1d5a100b44b23c37d2eac8 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Sun, 1 Jul 2018 12:30:37 +0300 Subject: [PATCH 11/17] typo (cd -> cb) --- lib/nat-traversal.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/nat-traversal.js b/lib/nat-traversal.js index 05ceb877..5efa32c7 100644 --- a/lib/nat-traversal.js +++ b/lib/nat-traversal.js @@ -81,7 +81,7 @@ function NatTraversal () { NatTraversal.prototype.portMapping = function (port, protocol, cb) { var self = this - if (self._destroyed) return typeof cd === 'function' && cb() + if (self._destroyed) return typeof c === 'function' && cb() if (typeof protocol === 'function') { cb = protocol protocol = 'tcp' @@ -107,7 +107,7 @@ NatTraversal.prototype.portMapping = function (port, protocol, cb) { NatTraversal.prototype.portUnMapping = function (port, cb) { var self = this - if (self._destroyed) return typeof cd === 'function' && cb() + if (self._destroyed) return typeof cb === 'function' && cb() arrayRemove(self._openedPorts, self._openedPorts.findIndex(o => o.port === port)) // Clear intervals From ff97eb89058b415f55ba1aa494ea2ca5507ed656 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Thu, 6 Sep 2018 13:51:05 +0300 Subject: [PATCH 12/17] fix to this._debug --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index a21a130c..db37844b 100644 --- a/index.js +++ b/index.js @@ -421,7 +421,7 @@ class WebTorrent extends EventEmitter { this.emit('listening') } - _debug () { + this._debug () { const args = [].slice.call(arguments) args[0] = `[${this._debugId}] ${args[0]}` debug(...args) From 6850f5e4949fdb76b5353adac79d98d9bb4777b1 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Thu, 6 Sep 2018 14:02:21 +0300 Subject: [PATCH 13/17] fix unterminated bracket --- index.js | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index db37844b..6fdc5706 100644 --- a/index.js +++ b/index.js @@ -405,23 +405,18 @@ class WebTorrent extends EventEmitter { if (this._tcpPool) { // Sometimes server.address() returns `null` in Docker. - const address = this._tcpPool.server.address() - if (address) this.torrentPort = address.port - } - - if (this._tcpPool) { - // Sometimes server.address() returns `null` in Docker. - var address = this._tcpPool.server.address() - if (address) { - this.torrentPort = address.port - if (this._natTraversal.portMapping) { - this._natTraversal.portMapping(this.torrentPort, 'tcp') + var address = this._tcpPool.server.address() + if (address) { + this.torrentPort = address.port + if (this._natTraversal.portMapping) { + this._natTraversal.portMapping(this.torrentPort, 'tcp') + } } + this.emit('listening') } - this.emit('listening') } - this._debug () { + _debug () { const args = [].slice.call(arguments) args[0] = `[${this._debugId}] ${args[0]}` debug(...args) From d181b2be35495d06519e88dc1e7c744066fb850a Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Thu, 6 Sep 2018 14:27:14 +0300 Subject: [PATCH 14/17] fix callback to arrow function --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 6fdc5706..e568c7bf 100644 --- a/index.js +++ b/index.js @@ -385,7 +385,7 @@ class WebTorrent extends EventEmitter { } if (this._natTraversal.destroy) { - tasks.push(function (cb) { + tasks.push(cb => { this._natTraversal.destroy(cb) }) } From d872923b84e8fa15f732e94884f77cd44209e78c Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Thu, 6 Sep 2018 14:53:57 +0300 Subject: [PATCH 15/17] change callback to arrow function --- index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index e568c7bf..1805cf1b 100644 --- a/index.js +++ b/index.js @@ -117,15 +117,15 @@ class WebTorrent extends EventEmitter { this._destroy(err) }) - this.dht.once('listening', function () { - var address = this.dht.address() - if (address) { - this.dhtPort = address.port - if (self._natTraversal.portMapping) { - this._natTraversal.portMapping(this.dhtPort, 'udp') + this.dht.once('listening', () => { + const address = this.dht.address() + if (address) { + this.dhtPort = address.port + if (this._natTraversal.portMapping) { + this._natTraversal.portMapping(this.dhtPort, 'udp') + } } - } - }) + }) // Ignore warning when there are > 10 torrents in the client this.dht.setMaxListeners(0) From 53e0ddcb2e74be67f90c867a163b14f99f4518fe Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Thu, 6 Sep 2018 15:03:55 +0300 Subject: [PATCH 16/17] fix: re-add `natTraversal` in server.js --- lib/server.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/server.js b/lib/server.js index 61584814..5b3622b7 100644 --- a/lib/server.js +++ b/lib/server.js @@ -7,6 +7,7 @@ const url = require('url') function Server (torrent, opts = {}) { const server = http.createServer() + const natTraversal = require('./nat-traversal') if (!opts.origin) opts.origin = '*' // allow all origins by default const sockets = [] From f2b8bfd2584f2b1dd7c1c08cabc2f657d5b9fe2d Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Thu, 6 Sep 2018 15:20:34 +0300 Subject: [PATCH 17/17] standard fixes --- index.js | 18 +++++++++--------- lib/nat-traversal.js | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 1805cf1b..c5c70280 100644 --- a/index.js +++ b/index.js @@ -95,15 +95,15 @@ class WebTorrent extends EventEmitter { } } - this._natTraversal = require('./lib/nat-traversal') // browser exclude + this._natTraversal = require('./lib/nat-traversal') // browser exclude - if (typeof TCPPool === 'function') { - this._tcpPool = new TCPPool(this) - } else { - process.nextTick(function () { - this._onListening() - }) - } + if (typeof TCPPool === 'function') { + this._tcpPool = new TCPPool(this) + } else { + process.nextTick(function () { + this._onListening() + }) + } // stats this._downloadSpeed = speedometer() @@ -391,7 +391,7 @@ class WebTorrent extends EventEmitter { } parallel(tasks, cb) - + if (err) this.emit('error', err) this.torrents = [] diff --git a/lib/nat-traversal.js b/lib/nat-traversal.js index 5efa32c7..dbfcc8b0 100644 --- a/lib/nat-traversal.js +++ b/lib/nat-traversal.js @@ -87,7 +87,7 @@ NatTraversal.prototype.portMapping = function (port, protocol, cb) { protocol = 'tcp' } - self._openedPorts.push({port: port, protocol: protocol}) + self._openedPorts.push({ port: port, protocol: protocol }) // Try UPnP first self._upnpPortMapping(port, protocol, function (err) {