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

Respect the "private" flag in .torrent files #402

Merged
merged 1 commit into from Sep 2, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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
@@ -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')
})
})
})
Binary file not shown.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.