mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2024-12-14 11:26:25 +00:00
Merge branch 's1dd-master'
This commit is contained in:
commit
098ec7041c
@ -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) {
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user