From 9938c949eee9c69c6774767e885e40f0a73898d9 Mon Sep 17 00:00:00 2001 From: Ivan Borzenkov Date: Tue, 17 Aug 2021 21:16:50 +0300 Subject: feat: Add PE/MSE support (#1820) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * reimplement #1384 * add option for secure - performance issue * fixes after code review * fix error * use const in hex * use stored hash * Update lib/peer.js Co-authored-by: Diego Rodríguez Baquero * fix const Co-authored-by: Diego Rodríguez Baquero --- index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'index.js') diff --git a/index.js b/index.js index cbca4d3..8866665 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,7 @@ const path = require('path') const Peer = require('simple-peer') const queueMicrotask = require('queue-microtask') const randombytes = require('randombytes') +const sha1 = require('simple-sha1') const speedometer = require('speedometer') const { ThrottleGroup } = require('speed-limiter') @@ -80,6 +81,10 @@ class WebTorrent extends EventEmitter { this._downloadLimit = Math.max((typeof opts.downloadLimit === 'number') ? opts.downloadLimit : -1, -1) this._uploadLimit = Math.max((typeof opts.uploadLimit === 'number') ? opts.uploadLimit : -1, -1) + if (opts.secure === true) { + require('./lib/peer').enableSecure() + } + this._debug( 'new webtorrent (peerId %s, nodeId %s, port %s)', this.peerId, this.nodeId, this.torrentPort @@ -446,6 +451,19 @@ class WebTorrent extends EventEmitter { args[0] = `[${this._debugId}] ${args[0]}` debug(...args) } + + _getByHash (infoHashHash) { + for (const torrent of this.torrents) { + if (!torrent.infoHashHash) { + torrent.infoHashHash = sha1.sync(Buffer.from('72657132' /* 'req2' */ + torrent.infoHash, 'hex')) + } + if (infoHashHash === torrent.infoHashHash) { + return torrent + } + } + + return null + } } WebTorrent.WEBRTC_SUPPORT = Peer.WEBRTC_SUPPORT -- cgit v1.2.3