From 65e1ff3e9abb2ef705907e0791c501b5a08c57d9 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Sat, 18 Feb 2017 14:24:44 +0100 Subject: [PATCH 01/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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/15] 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 f643c8336f2f6daf85747d5ac51fa574fac17660 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Fri, 20 Jul 2018 14:57:35 +0300 Subject: [PATCH 12/15] pass discoveryIntervalMs for torrent-discovery --- lib/torrent.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/torrent.js b/lib/torrent.js index a7253f66..5c33de5d 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -72,6 +72,7 @@ function Torrent (torrentId, client, opts) { this.announce = opts.announce this.urlList = opts.urlList + this.discoveryIntervalMs = opts.discoveryIntervalMs this.path = opts.path this.skipVerify = !!opts.skipVerify @@ -339,7 +340,8 @@ Torrent.prototype._onListening = function () { dht: !self.private && self.client.dht, tracker: trackerOpts, port: self.client.torrentPort, - userAgent: USER_AGENT + userAgent: USER_AGENT, + discoveryIntervalMs: self.discoveryIntervalMs }) self.discovery.on('error', onError) From 7907c27e12b1d48bcb5ddf4c4cbe6b19fc682274 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Fri, 20 Jul 2018 15:00:28 +0300 Subject: [PATCH 13/15] discoveryIntervalMs -> intervalMs --- lib/torrent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/torrent.js b/lib/torrent.js index 5c33de5d..b22800b3 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -341,7 +341,7 @@ Torrent.prototype._onListening = function () { tracker: trackerOpts, port: self.client.torrentPort, userAgent: USER_AGENT, - discoveryIntervalMs: self.discoveryIntervalMs + intervalMs: self.discoveryIntervalMs }) self.discovery.on('error', onError) From 1e471d9cea7a0a79d35956e784c387f594f850a4 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Fri, 20 Jul 2018 16:34:49 +0300 Subject: [PATCH 14/15] use oleiba@bittorrent-dht (set ROTATE_INTERVAL) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 912a3c48..08688a39 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "dependencies": { "addr-to-ip-port": "^1.4.2", "bitfield": "^2.0.0", - "bittorrent-dht": "^8.0.0", + "bittorrent-dht": "git+https://github.com/oleiba/bittorrent-dht.git", "bittorrent-protocol": "^2.1.5", "chunk-store-stream": "^2.0.2", "create-torrent": "^3.24.5", From abc427fa54c51aafd0bf8c02ce409a85e2bfe848 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Thu, 26 Jul 2018 13:10:11 +0300 Subject: [PATCH 15/15] disable natTraversal if 'opts.enableNatTraversal == false' --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index 02b3b013..cda34041 100644 --- a/index.js +++ b/index.js @@ -111,6 +111,9 @@ function WebTorrent (opts) { } self._natTraversal = require('./lib/nat-traversal') // browser exclude + if (opts.enableNatTraversal === false) { + self._natTraversal.portMapping = null + } if (typeof TCPPool === 'function') { self._tcpPool = new TCPPool(self)