diff options
author | Julen Garcia Leunda <hicom150@gmail.com> | 2020-09-30 22:39:09 +0300 |
---|---|---|
committer | Julen Garcia Leunda <hicom150@gmail.com> | 2020-10-02 16:37:33 +0300 |
commit | 692b3d113c0a08a17ee53bca7e16180a94b4fbfe (patch) | |
tree | 07ba6df67701f9397d46a0ddaf26f7f5b2c880a1 /lib | |
parent | 7aee819796c540df0b247fec1853098f9a591d4c (diff) |
Add uTP support (BEP29)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/conn-pool.js (renamed from lib/tcp-pool.js) | 79 | ||||
-rw-r--r-- | lib/peer.js | 40 | ||||
-rw-r--r-- | lib/torrent.js | 34 |
3 files changed, 118 insertions, 35 deletions
diff --git a/lib/tcp-pool.js b/lib/conn-pool.js index 1094c4f..ddc0672 100644 --- a/lib/tcp-pool.js +++ b/lib/conn-pool.js @@ -1,30 +1,30 @@ const arrayRemove = require('unordered-array-remove') -const debug = require('debug')('webtorrent:tcp-pool') +const debug = require('debug')('webtorrent:conn-pool') const net = require('net') // browser exclude +const utp = require('utp-native') // browser exclude const Peer = require('./peer') /** - * TCPPool + * Connection Pool * - * A "TCP pool" allows multiple swarms to listen on the same TCP port and determines + * A connection pool allows multiple swarms to listen on the same TCP/UDP port and determines * which swarm incoming connections are intended for by inspecting the bittorrent * handshake that the remote peer sends. * * @param {number} port */ -class TCPPool { +class ConnPool { constructor (client) { - debug('create tcp pool (port %s)', client.torrentPort) + debug('create pool (port %s)', client.torrentPort) - this.server = net.createServer() this._client = client // Temporarily store incoming connections so they can be destroyed if the server is // closed before the connection is passed off to a Torrent. this._pendingConns = [] - this._onConnectionBound = conn => { + this._onConnectionBound = (conn) => { this._onConnection(conn) } @@ -32,42 +32,77 @@ class TCPPool { this._client._onListening() } - this._onError = err => { + this._onTCPError = (err) => { this._client._destroy(err) } - this.server.on('connection', this._onConnectionBound) - this.server.on('listening', this._onListening) - this.server.on('error', this._onError) + this._onUTPError = () => { + this._client.utp = false + } - this.server.listen(client.torrentPort) + // Setup TCP + this.tcpServer = net.createServer() + this.tcpServer.on('connection', this._onConnectionBound) + this.tcpServer.on('error', this._onTCPError) + + // Start TCP + this.tcpServer.listen(client.torrentPort, () => { + debug('creating tcpServer in port %s', this.tcpServer.address().port) + if (this._client.utp) { + // Setup uTP + this.utpServer = utp.createServer() + this.utpServer.on('connection', this._onConnectionBound) + this.utpServer.on('listening', this._onListening) + this.utpServer.on('error', this._onUTPError) + + // Start uTP + debug('creating utpServer in port %s', this.tcpServer.address().port) + this.utpServer.listen(this.tcpServer.address().port) + } else { + this._onListening() + } + }) } /** - * Destroy this TCP pool. + * Destroy this Conn pool. * @param {function} cb */ destroy (cb) { - debug('destroy tcp pool') + debug('destroy conn pool') - this.server.removeListener('connection', this._onConnectionBound) - this.server.removeListener('listening', this._onListening) - this.server.removeListener('error', this._onError) + if (this.utpServer) { + this.utpServer.removeListener('connection', this._onConnectionBound) + this.utpServer.removeListener('listening', this._onListening) + this.utpServer.removeListener('error', this._onUTPError) + } + + this.tcpServer.removeListener('connection', this._onConnectionBound) + this.tcpServer.removeListener('error', this._onTCPError) // Destroy all open connection objects so server can close gracefully without waiting // for connection timeout or remote peer to disconnect. - this._pendingConns.forEach(conn => { + this._pendingConns.forEach((conn) => { conn.on('error', noop) conn.destroy() }) + if (this.utpServer) { + try { + this.utpServer.close(cb) + } catch (err) { + if (cb) process.nextTick(cb) + } + } + try { - this.server.close(cb) + this.tcpServer.close(cb) } catch (err) { if (cb) process.nextTick(cb) } - this.server = null + this.tcpServer = null + this.utpServer = null this._client = null this._pendingConns = null } @@ -92,7 +127,7 @@ class TCPPool { self._pendingConns.push(conn) conn.once('close', cleanupPending) - const peer = Peer.createTCPIncomingPeer(conn) + const peer = this.utpServer ? Peer.createUTPIncomingPeer(conn) : Peer.createTCPIncomingPeer(conn) const wire = peer.wire wire.once('handshake', onHandshake) @@ -125,4 +160,4 @@ class TCPPool { function noop () {} -module.exports = TCPPool +module.exports = ConnPool diff --git a/lib/peer.js b/lib/peer.js index 0ddf535..b0fea6d 100644 --- a/lib/peer.js +++ b/lib/peer.js @@ -5,6 +5,7 @@ const Wire = require('bittorrent-protocol') const WebConn = require('./webconn') const CONNECT_TIMEOUT_TCP = 5000 +const CONNECT_TIMEOUT_UTP = 5000 const CONNECT_TIMEOUT_WEBRTC = 25000 const HANDSHAKE_TIMEOUT = 25000 @@ -46,6 +47,22 @@ exports.createTCPIncomingPeer = conn => { } /** + * Incoming uTP peers start out connected, because the remote peer connected to the + * listening port of the uTP server. Until the remote peer sends a handshake, we don't + * know what swarm the connection is intended for. + */ +exports.createUTPIncomingPeer = conn => { + const addr = `${conn.remoteAddress}:${conn.remotePort}` + const peer = new Peer(addr, 'utpIncoming') + peer.conn = conn + peer.addr = addr + + peer.onConnect() + + return peer +} + +/** * Outgoing TCP peers start out with just an IP address. At some point (when there is an * available connection), the client can attempt to connect to the address. */ @@ -58,6 +75,18 @@ exports.createTCPOutgoingPeer = (addr, swarm) => { } /** + * Outgoing uTP peers start out with just an IP address. At some point (when there is an + * available connection), the client can attempt to connect to the address. + */ +exports.createUTPOutgoingPeer = (addr, swarm) => { + const peer = new Peer(addr, 'utpOutgoing') + peer.addr = addr + peer.swarm = swarm + + return peer +} + +/** * Peer that represents a Web Seed (BEP17 / BEP19). */ exports.createWebSeedPeer = (url, swarm) => { @@ -193,9 +222,16 @@ class Peer { startConnectTimeout () { clearTimeout(this.connectTimeout) + + const connectTimeoutValues = { + webrtc: CONNECT_TIMEOUT_WEBRTC, + tcpOutgoing: CONNECT_TIMEOUT_TCP, + utpOutgoing: CONNECT_TIMEOUT_UTP + } + this.connectTimeout = setTimeout(() => { this.destroy(new Error('connect timeout')) - }, this.type === 'webrtc' ? CONNECT_TIMEOUT_WEBRTC : CONNECT_TIMEOUT_TCP) + }, connectTimeoutValues[this.type]) if (this.connectTimeout.unref) this.connectTimeout.unref() } @@ -212,7 +248,7 @@ class Peer { this.destroyed = true this.connected = false - debug('destroy %s (error: %s)', this.id, err && (err.message || err)) + debug('destroy %s %s (error: %s)', this.type, this.id, err && (err.message || err)) clearTimeout(this.connectTimeout) clearTimeout(this.handshakeTimeout) diff --git a/lib/torrent.js b/lib/torrent.js index caa6687..56326fd 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -24,6 +24,7 @@ const sha1 = require('simple-sha1') const speedometer = require('speedometer') const utMetadata = require('ut_metadata') const utPex = require('ut_pex') // browser exclude +const utp = require('utp-native') // browser exclude const parseRange = require('parse-numeric-range') const File = require('./file') @@ -750,7 +751,7 @@ class Torrent extends EventEmitter { } } - const wasAdded = !!this._addPeer(peer) + const wasAdded = !!this._addPeer(peer, this.client.utp ? 'utp' : 'tcp') if (wasAdded) { this.emit('peer', peer) } else { @@ -759,7 +760,7 @@ class Torrent extends EventEmitter { return wasAdded } - _addPeer (peer) { + _addPeer (peer, type) { if (this.destroyed) { if (typeof peer !== 'string') peer.destroy() return null @@ -787,7 +788,7 @@ class Torrent extends EventEmitter { let newPeer if (typeof peer === 'string') { // `peer` is an addr ("ip:port" string) - newPeer = Peer.createTCPOutgoingPeer(peer, this) + newPeer = type === 'utp' ? Peer.createUTPOutgoingPeer(peer, this) : Peer.createTCPOutgoingPeer(peer, this) } else { // `peer` is a WebRTC connection (simple-peer) newPeer = Peer.createWebRTCPeer(peer, this) @@ -1680,7 +1681,7 @@ class Torrent extends EventEmitter { const peer = this._queue.shift() if (!peer) return // queue could be empty - this._debug('tcp connect attempt to %s', peer.addr) + this._debug('%s connect attempt to %s', peer.type, peer.addr) const parts = addrToIPPort(peer.addr) const opts = { @@ -1688,7 +1689,13 @@ class Torrent extends EventEmitter { port: parts[1] } - const conn = peer.conn = net.connect(opts) + if (peer.type === 'utpOutgoing') { + peer.conn = utp.connect(opts.port, opts.host) + } else { + peer.conn = net.connect(opts) + } + + const conn = peer.conn conn.once('connect', () => { peer.onConnect() }) conn.once('error', err => { peer.destroy(err) }) @@ -1701,11 +1708,16 @@ class Torrent extends EventEmitter { // TODO: If torrent is done, do not try to reconnect after a timeout if (peer.retries >= RECONNECT_WAIT.length) { - this._debug( - 'conn %s closed: will not re-add (max %s attempts)', - peer.addr, RECONNECT_WAIT.length - ) - return + if (this.client.utp) { + const newPeer = this._addPeer(peer.addr, 'tcp') + if (newPeer) newPeer.retries = 0 + } else { + this._debug( + 'conn %s closed: will not re-add (max %s attempts)', + peer.addr, RECONNECT_WAIT.length + ) + return + } } const ms = RECONNECT_WAIT[peer.retries] @@ -1715,7 +1727,7 @@ class Torrent extends EventEmitter { ) const reconnectTimeout = setTimeout(() => { - const newPeer = this._addPeer(peer.addr) + const newPeer = this._addPeer(peer.addr, this.client.utp ? 'utp' : 'tcp') if (newPeer) newPeer.retries = peer.retries + 1 }, ms) if (reconnectTimeout.unref) reconnectTimeout.unref() |