diff --git a/lib/torrent.js b/lib/torrent.js index 9d4ecdf5..0275d70c 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -196,7 +196,9 @@ Torrent.prototype._onParsedTorrent = function (parsedTorrent) { // create swarm self.swarm = new Swarm(self.infoHash, self.client.peerId, { - handshake: { dht: !!self.client.dht } + handshake: { + dht: self.private ? false : !!self.client.dht + } }) self.swarm.on('error', self._onError.bind(self)) self.swarm.on('wire', self._onWire.bind(self)) @@ -263,7 +265,9 @@ Torrent.prototype._onSwarmListening = function () { // begin discovering peers via the DHT and tracker servers self.discovery = new Discovery({ announce: self.announce, - dht: self.client.dht, + dht: self.private + ? false + : self.client.dht, tracker: self.client.tracker, peerId: self.client.peerId, port: self.client.torrentPort, @@ -587,8 +591,8 @@ Torrent.prototype._onWire = function (wire, addr) { wire.ut_metadata.fetch() } - // use ut_pex extension - if (typeof ut_pex === 'function') { + // use ut_pex extension if the torrent is not flagged as private + if (typeof ut_pex === 'function' && !self.private) { wire.use(ut_pex()) // wire.ut_pex.start() // TODO two-way communication diff --git a/test/download-private-dht.js b/test/download-private-dht.js new file mode 100644 index 00000000..14419265 --- /dev/null +++ b/test/download-private-dht.js @@ -0,0 +1,118 @@ +var auto = require('run-auto') +var DHT = require('bittorrent-dht/server') +var fs = require('fs') +var parseTorrent = require('parse-torrent') +var test = require('tape') +var WebTorrent = require('../') + +var bunnyTorrent = fs.readFileSync(__dirname + '/torrents/big-buck-bunny-private.torrent') +var bunnyParsed = parseTorrent(bunnyTorrent) + +var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent') +var leavesParsed = parseTorrent(leavesTorrent) + +// remove trackers from .torrent file +bunnyParsed.announce = [] +leavesParsed.announce = [] + +test('private torrent should not use DHT', function (t) { + t.plan(3) + + var dhtServer = new DHT({ bootstrap: false }) + + dhtServer.on('error', function (err) { t.fail(err) }) + dhtServer.on('warning', function (err) { t.fail(err) }) + + auto({ + dhtPort: function (cb) { + dhtServer.listen(function () { + var port = dhtServer.address().port + cb(null, port) + }) + }, + + client: ['dhtPort', function (cb, r) { + var client = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + }) + client.on('error', function (err) { t.fail(err) }) + client.on('warning', function (err) { t.fail(err) }) + + var torrent = client.add(bunnyParsed) + + torrent.on('dhtAnnounce', function () { + t.fail('client announced to dht') + }) + + client.on('torrent', function () { + if (!torrent.discovery.dht && !torrent.swarm.handshakeOpts.dht) { + t.pass('dht is disabled for this torrent') + cb(null, client) + } + }) + + }] + + }, function (err, r) { + if (err) throw err + + dhtServer.destroy(function () { + t.pass('dht server destroyed') + }) + r.client.destroy(function () { + t.pass('client destroyed') + }) + }) +}) + +test('public torrent should use DHT', function (t) { + t.plan(3) + + var dhtServer = new DHT({ bootstrap: false }) + + dhtServer.on('error', function (err) { t.fail(err) }) + dhtServer.on('warning', function (err) { t.fail(err) }) + + auto({ + dhtPort: function (cb) { + dhtServer.listen(function () { + var port = dhtServer.address().port + cb(null, port) + }) + }, + + client: ['dhtPort', function (cb, r) { + var client = new WebTorrent({ + tracker: false, + dht: { bootstrap: '127.0.0.1:' + r.dhtPort } + }) + client.on('error', function (err) { t.fail(err) }) + client.on('warning', function (err) { t.fail(err) }) + + var torrent = client.add(leavesParsed) + + torrent.on('dhtAnnounce', function () { + t.pass('client announced to dht') + cb(null, client) + }) + + client.on('torrent', function () { + if (!torrent.client.dht) { + t.fail('dht server is null') + } + }) + + }] + + }, function (err, r) { + if (err) throw err + + dhtServer.destroy(function () { + t.pass('dht server destroyed') + }) + r.client.destroy(function () { + t.pass('client destroyed') + }) + }) +}) diff --git a/test/torrents/big-buck-bunny-private.torrent b/test/torrents/big-buck-bunny-private.torrent new file mode 100644 index 00000000..25018e7d Binary files /dev/null and b/test/torrents/big-buck-bunny-private.torrent differ