Merge branch 's1dd-master'

This commit is contained in:
Feross Aboukhadijeh 2015-01-29 13:27:00 -08:00
commit 098ec7041c
3 changed files with 70 additions and 1 deletions

View File

@ -107,7 +107,12 @@ var Server = require('bittorrent-tracker').Server
var server = new Server({ var server = new Server({
udp: true, // enable udp server? [default=true] 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) { server.on('error', function (err) {

View File

@ -66,6 +66,8 @@ function Server (opts) {
self._udpSocket.on('listening', onListening) self._udpSocket.on('listening', onListening)
} }
if (typeof opts.filter === 'function') self._filter = opts.filter
var num = !!self._httpServer + !!self._udpSocket var num = !!self._httpServer + !!self._udpSocket
function onListening () { function onListening () {
num -= 1 num -= 1
@ -120,6 +122,7 @@ Server.prototype.close = function (cb) {
Server.prototype.getSwarm = function (infoHash) { Server.prototype.getSwarm = function (infoHash) {
var self = this var self = this
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex') if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
if (self._filter && self._filter(infoHash)) return null
var swarm = self.torrents[infoHash] var swarm = self.torrents[infoHash]
if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, this) if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, this)
return swarm return swarm
@ -201,6 +204,7 @@ Server.prototype._onRequest = function (params, cb) {
Server.prototype._onAnnounce = function (params, cb) { Server.prototype._onAnnounce = function (params, cb) {
var self = this var self = this
var swarm = self.getSwarm(params.info_hash) var swarm = self.getSwarm(params.info_hash)
if (swarm === null) return cb(new Error('invalid hash'))
swarm.announce(params, function (err, response) { swarm.announce(params, function (err, response) {
if (response) { if (response) {
if (!response.action) response.action = common.ACTIONS.ANNOUNCE if (!response.action) response.action = common.ACTIONS.ANNOUNCE

View File

@ -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()
})
})
})
})
})