Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NAT traversal techniques #1419

Closed
wants to merge 18 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

allow UDP for DHT port

  • Loading branch information
oleiba committed Jun 17, 2018
commit c6b00b3cf2979788f38077ae6b5b5131f5084da2
@@ -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')
}
}
}
@@ -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,28 +73,32 @@ 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)
}

// 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

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.