From 55079798c5833546580bcd1876935160a787a4eb Mon Sep 17 00:00:00 2001 From: Sidd Sridharan Date: Thu, 29 Jan 2015 12:24:17 -0800 Subject: [PATCH] white/blacklist implemented (#44) + docs/tests updated --- README.md | 7 +++++- server.js | 4 ++++ test/scrape.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a0f0ce6..8ba0ed4 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,12 @@ var Server = require('bittorrent-tracker').Server var server = new Server({ udp: true, // enable udp server? [default=true] - http: true // enable http server? [default=true] + http: true, // enable http server? [default=true] + filter: function (hash) { + // specify white/blacklist for disallowing/allowing torrents + return hash !== 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa' + }) + }) server.on('error', function (err) { diff --git a/server.js b/server.js index bcacf8b..392fc99 100644 --- a/server.js +++ b/server.js @@ -66,6 +66,8 @@ function Server (opts) { self._udpSocket.on('listening', onListening) } + if (typeof opts.filter === 'function') self._filter = opts.filter + var num = !!self._httpServer + !!self._udpSocket function onListening () { num -= 1 @@ -120,6 +122,7 @@ Server.prototype.close = function (cb) { Server.prototype.getSwarm = function (infoHash) { var self = this if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex') + if (self._filter && self._filter(infoHash)) return null var swarm = self.torrents[infoHash] if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, this) return swarm @@ -201,6 +204,7 @@ Server.prototype._onRequest = function (params, cb) { Server.prototype._onAnnounce = function (params, cb) { var self = this var swarm = self.getSwarm(params.info_hash) + if (swarm === null) return cb(new Error('invalid hash')) swarm.announce(params, function (err, response) { if (response) { if (!response.action) response.action = common.ACTIONS.ANNOUNCE diff --git a/test/scrape.js b/test/scrape.js index 73d38f3..abf9599 100644 --- a/test/scrape.js +++ b/test/scrape.js @@ -169,3 +169,63 @@ test('server: all info_hash scrape', function (t) { }) }) }) + +test('http nonwhitelisted torrent does not appear in scrape', function (t) { + var server = new Server({ + filter: function (hash) { + return hash !== parsedBitlove + }, + udp: false + }) + + server.on('error', function (err) { + t.error(err) + }) + + portfinder.getPort(function (err, port) { + t.error(err) + server.listen(port) + var announceUrl = 'http://127.0.0.1:' + port + '/announce' + var scrapeUrl = 'http://127.0.0.1:' + port + '/scrape' + + parsedBitlove.announce = [ announceUrl ] + + server.once('listening', function () { + var client = new Client(peerId, port, parsedBitlove) + + client.start() + + client.on('error', function (err) { + t.error(err) + }) + + server.once('warning', function (err) { + if (err) { + get.concat(scrapeUrl, function (err, data, res) { + if (err) throw err + + t.equal(res.statusCode, 200) + data = bencode.decode(data) + t.ok(data.files) + + t.notOk(data.files[binaryBitlove]) + + client.stop() + server.close(function () { + t.end() + }) + }) + } + }) + + server.once('start', function (err) { + if (err) throw err + client.stop() + server.close(function () { + t.fail('server should have thrown an error; filter condition was probably successful') + t.end() + }) + }) + }) + }) +})