mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-19 04:31:36 +00:00
BREAKING: opts.filter function is async; add createSwarm
- The expected signature for opts.filter is now: function myFilterFn (infoHash, params, cb) { cb(infoHash === 'blah) } This allows interfacing with a database and fixes #80. Also, swarm.getSwarm() is no longer responsible for creating a Swarm instance, only returning an instance if there is already one. Creating a swarm happens in swarm.createSwarm(). This change only affects users who were override swarm.getSwarm().
This commit is contained in:
parent
37f95b4b54
commit
bf5b9f289a
66
server.js
66
server.js
@ -175,19 +175,19 @@ Server.prototype.close = function (cb) {
|
|||||||
else cb(null)
|
else cb(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
Server.prototype.getSwarm = function (infoHash, params) {
|
Server.prototype.createSwarm = function (infoHash) {
|
||||||
var self = this
|
var self = this
|
||||||
if (!params) params = {}
|
|
||||||
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
|
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
|
||||||
|
var swarm = self.torrents[infoHash] = new Swarm(infoHash, self)
|
||||||
if (self._filter && !self._filter(infoHash, params)) return null
|
|
||||||
|
|
||||||
var swarm = self.torrents[infoHash]
|
|
||||||
if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, self)
|
|
||||||
|
|
||||||
return swarm
|
return swarm
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Server.prototype.getSwarm = function (infoHash) {
|
||||||
|
var self = this
|
||||||
|
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
|
||||||
|
return self.torrents[infoHash]
|
||||||
|
}
|
||||||
|
|
||||||
Server.prototype.onHttpRequest = function (req, res, opts) {
|
Server.prototype.onHttpRequest = function (req, res, opts) {
|
||||||
var self = this
|
var self = this
|
||||||
if (!opts) opts = {}
|
if (!opts) opts = {}
|
||||||
@ -334,7 +334,10 @@ Server.prototype._onWebSocketRequest = function (socket, params) {
|
|||||||
if (params.answer) {
|
if (params.answer) {
|
||||||
debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id)
|
debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id)
|
||||||
|
|
||||||
var swarm = self.getSwarm(params.info_hash, params)
|
var swarm = self.getSwarm(params.info_hash)
|
||||||
|
if (!swarm) {
|
||||||
|
return self.emit('warning', new Error('no swarm with that `info_hash`'))
|
||||||
|
}
|
||||||
var toPeer = swarm.peers[params.to_peer_id]
|
var toPeer = swarm.peers[params.to_peer_id]
|
||||||
if (!toPeer) {
|
if (!toPeer) {
|
||||||
return self.emit('warning', new Error('no peer with that `to_peer_id`'))
|
return self.emit('warning', new Error('no peer with that `to_peer_id`'))
|
||||||
@ -370,8 +373,28 @@ 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, params)
|
|
||||||
if (swarm === null) return cb(new Error('disallowed info_hash'))
|
var swarm = self.getSwarm(params.info_hash)
|
||||||
|
if (swarm) announce()
|
||||||
|
else createSwarm()
|
||||||
|
|
||||||
|
function createSwarm () {
|
||||||
|
if (self._filter) {
|
||||||
|
self._filter(params.info_hash, params, function (allowed) {
|
||||||
|
if (allowed) {
|
||||||
|
swarm = self.createSwarm(params.info_hash)
|
||||||
|
announce()
|
||||||
|
} else {
|
||||||
|
cb(new Error('disallowed info_hash'))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
swarm = self.createSwarm(params.info_hash)
|
||||||
|
announce()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function announce () {
|
||||||
if (!params.event || params.event === 'empty') params.event = 'update'
|
if (!params.event || params.event === 'empty') params.event = 'update'
|
||||||
swarm.announce(params, function (err, response) {
|
swarm.announce(params, function (err, response) {
|
||||||
if (err) return cb(err)
|
if (err) return cb(err)
|
||||||
@ -409,6 +432,8 @@ Server.prototype._onAnnounce = function (params, cb) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Server.prototype._onScrape = function (params, cb) {
|
Server.prototype._onScrape = function (params, cb) {
|
||||||
var self = this
|
var self = this
|
||||||
|
|
||||||
@ -419,15 +444,20 @@ Server.prototype._onScrape = function (params, cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
series(params.info_hash.map(function (infoHash) {
|
series(params.info_hash.map(function (infoHash) {
|
||||||
var swarm = self.getSwarm(infoHash)
|
|
||||||
return function (cb) {
|
return function (cb) {
|
||||||
|
var swarm = self.getSwarm(infoHash)
|
||||||
|
if (swarm) {
|
||||||
swarm.scrape(params, function (err, scrapeInfo) {
|
swarm.scrape(params, function (err, scrapeInfo) {
|
||||||
cb(err, scrapeInfo && {
|
if (err) return cb(err)
|
||||||
|
cb(null, {
|
||||||
infoHash: infoHash,
|
infoHash: infoHash,
|
||||||
complete: scrapeInfo.complete || 0,
|
complete: (scrapeInfo && scrapeInfo.complete) || 0,
|
||||||
incomplete: scrapeInfo.incomplete || 0
|
incomplete: (scrapeInfo && scrapeInfo.incomplete) || 0
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
cb(null, { infoHash: infoHash, complete: 0, incomplete: 0 })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}), function (err, results) {
|
}), function (err, results) {
|
||||||
if (err) return cb(err)
|
if (err) return cb(err)
|
||||||
@ -440,9 +470,9 @@ Server.prototype._onScrape = function (params, cb) {
|
|||||||
|
|
||||||
results.forEach(function (result) {
|
results.forEach(function (result) {
|
||||||
response.files[common.hexToBinary(result.infoHash)] = {
|
response.files[common.hexToBinary(result.infoHash)] = {
|
||||||
complete: result.complete,
|
complete: result.complete || 0,
|
||||||
incomplete: result.incomplete,
|
incomplete: result.incomplete || 0,
|
||||||
downloaded: result.complete // TODO: this only provides a lower-bound
|
downloaded: result.complete || 0 // TODO: this only provides a lower-bound
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -15,8 +15,10 @@ var peerId = new Buffer('01234567890123456789')
|
|||||||
function testFilterOption (t, serverType) {
|
function testFilterOption (t, serverType) {
|
||||||
t.plan(6)
|
t.plan(6)
|
||||||
var opts = serverType === 'http' ? { udp: false } : { http: false }
|
var opts = serverType === 'http' ? { udp: false } : { http: false }
|
||||||
opts.filter = function (infoHash) {
|
opts.filter = function (infoHash, params, cb) {
|
||||||
return infoHash !== parsedBitlove.infoHash
|
process.nextTick(function () {
|
||||||
|
cb(infoHash !== parsedBitlove.infoHash)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
var server = new Server(opts)
|
var server = new Server(opts)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user