diff options
author | Alex <alxmorais8@msn.com> | 2020-03-15 00:11:40 +0300 |
---|---|---|
committer | Alex <alxmorais8@msn.com> | 2020-03-15 00:11:40 +0300 |
commit | 1feb4b6f9aad2793c662c9fb55993edfa83f8079 (patch) | |
tree | 7b385ded17be43145e580a23daff51fd65ced17f | |
parent | 31f5166d9ee807d181ab547fcc58421ea85efdf5 (diff) |
Add tests for private option
-rw-r--r-- | docs/api.md | 2 | ||||
-rw-r--r-- | lib/torrent.js | 11 | ||||
-rw-r--r-- | test/node/download-private-dht.js | 104 |
3 files changed, 115 insertions, 2 deletions
diff --git a/docs/api.md b/docs/api.md index 5fdac4a..062343b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -89,7 +89,7 @@ If `opts` is specified, then the default options (shown below) will be overridde getAnnounceOpts: Function, // Custom callback to allow sending extra parameters to the tracker maxWebConns: Number, // Max number of simultaneous connections per web seed [default=4] path: String, // Folder to download files to (default=`/tmp/webtorrent/`) - private: Boolean, // If true, client will not share the hash with the DHT or with other peers using peer exchange (PEX) + private: Boolean, // If true, client will not share the hash with the DHT nor with PEX (default is the privacy of the parsed torrent) store: Function // Custom chunk store (must follow [abstract-chunk-store](https://www.npmjs.com/package/abstract-chunk-store) API) } ``` diff --git a/lib/torrent.js b/lib/torrent.js index 9375d99..765678c 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -69,11 +69,12 @@ class Torrent extends EventEmitter { this.urlList = opts.urlList this.path = opts.path - this.private = !!opts.private this.skipVerify = !!opts.skipVerify this._store = opts.store || FSChunkStore this._getAnnounceOpts = opts.getAnnounceOpts + if (typeof opts.private === 'boolean') this.private = opts.private + this.strategy = opts.strategy || 'sequential' this.maxWebConns = opts.maxWebConns || 4 @@ -253,6 +254,14 @@ class Torrent extends EventEmitter { _processParsedTorrent (parsedTorrent) { this._debugId = parsedTorrent.infoHash.toString('hex').substring(0, 7) + if (typeof this.private !== 'undefined') { + // `private` option overrides default, only if it's defined + parsedTorrent.private = this.private + } else { + // use default value + this.private = !!parsedTorrent.private + } + if (this.announce) { // Allow specifying trackers via `opts` parameter parsedTorrent.announce = parsedTorrent.announce.concat(this.announce) diff --git a/test/node/download-private-dht.js b/test/node/download-private-dht.js index 35f90d3..0bc43c8 100644 --- a/test/node/download-private-dht.js +++ b/test/node/download-private-dht.js @@ -101,3 +101,107 @@ test('public torrent should use DHT', function (t) { }) }) }) + +test('public torrent with forced private option should not use DHT', function (t) { + t.plan(4) + + var dhtServer = new DHT({ bootstrap: false }) + + dhtServer.on('error', function (err) { t.fail(err) }) + dhtServer.on('warning', function (err) { t.fail(err) }) + + var client + + series([ + function (cb) { + dhtServer.listen(cb) + }, + + function (cb) { + client = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } + }) + + client.on('error', function (err) { t.fail(err) }) + client.on('warning', function (err) { t.fail(err) }) + + var torrent = client.add(fixtures.leaves.parsedTorrent, { + private: true, + store: MemoryChunkStore + }) + + torrent.on('dhtAnnounce', function () { + t.fail('client announced to dht') + }) + + client.on('torrent', function () { + if (!torrent.discovery.dht) { + t.pass('dht is disabled for this torrent') + cb(null) + } + }) + } + ], function (err) { + t.error(err) + + dhtServer.destroy(function (err) { + t.error(err, 'dht server destroyed') + }) + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) + }) +}) + +test('private torrent with forced public option should use DHT', function (t) { + t.plan(4) + + var dhtServer = new DHT({ bootstrap: false }) + + dhtServer.on('error', function (err) { t.fail(err) }) + dhtServer.on('warning', function (err) { t.fail(err) }) + + var client + + series([ + function (cb) { + dhtServer.listen(cb) + }, + + function (cb) { + client = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + dhtServer.address().port } + }) + + client.on('error', function (err) { t.fail(err) }) + client.on('warning', function (err) { t.fail(err) }) + + var torrent = client.add(fixtures.bunny.parsedTorrent, { + private: false, + store: MemoryChunkStore + }) + + torrent.on('dhtAnnounce', function () { + t.pass('client announced to dht') + cb(null) + }) + + client.on('torrent', function () { + if (!torrent.client.dht) { + t.fail('dht server is null') + } + }) + } + ], function (err) { + t.error(err) + + dhtServer.destroy(function (err) { + t.error(err, 'dht server destroyed') + }) + client.destroy(function (err) { + t.error(err, 'client destroyed') + }) + }) +}) |