mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2024-12-14 03:16:24 +00:00
Merge pull request #1 from feross/feross/request-handler-changes
Replace the request handler option by exposing the Swarm
This commit is contained in:
commit
a77873a765
@ -190,11 +190,11 @@ HTTPTracker.prototype._onAnnounceResponse = function (data) {
|
||||
self._trackerId = trackerId
|
||||
}
|
||||
|
||||
self.client.emit('update', {
|
||||
var response = Object.assign({}, data, {
|
||||
announce: self.announceUrl,
|
||||
complete: data.complete,
|
||||
incomplete: data.incomplete
|
||||
infoHash: common.binaryToHex(data.info_hash)
|
||||
})
|
||||
self.client.emit('update', response)
|
||||
|
||||
var addrs
|
||||
if (Buffer.isBuffer(data.peers)) {
|
||||
@ -248,15 +248,12 @@ HTTPTracker.prototype._onScrapeResponse = function (data) {
|
||||
}
|
||||
|
||||
keys.forEach(function (infoHash) {
|
||||
var response = data[infoHash]
|
||||
// TODO: optionally handle data.flags.min_request_interval
|
||||
// (separate from announce interval)
|
||||
self.client.emit('scrape', {
|
||||
var response = Object.assign(data[infoHash], {
|
||||
announce: self.announceUrl,
|
||||
infoHash: common.binaryToHex(infoHash),
|
||||
complete: response.complete,
|
||||
incomplete: response.incomplete,
|
||||
downloaded: response.downloaded
|
||||
infoHash: common.binaryToHex(infoHash)
|
||||
})
|
||||
self.client.emit('scrape', response)
|
||||
})
|
||||
}
|
||||
|
@ -266,8 +266,11 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) {
|
||||
}
|
||||
|
||||
if (data.complete != null) {
|
||||
data.announce = self.announceUrl
|
||||
self.client.emit('update', data)
|
||||
var response = Object.assign({}, data, {
|
||||
announce: self.announceUrl,
|
||||
infoHash: common.binaryToHex(data.info_hash)
|
||||
})
|
||||
self.client.emit('update', response)
|
||||
}
|
||||
|
||||
var peer
|
||||
@ -323,16 +326,13 @@ WebSocketTracker.prototype._onScrapeResponse = function (data) {
|
||||
}
|
||||
|
||||
keys.forEach(function (infoHash) {
|
||||
var response = data[infoHash]
|
||||
// TODO: optionally handle data.flags.min_request_interval
|
||||
// (separate from announce interval)
|
||||
self.client.emit('scrape', {
|
||||
var response = Object.assign(data[infoHash], {
|
||||
announce: self.announceUrl,
|
||||
infoHash: common.binaryToHex(infoHash),
|
||||
complete: response.complete,
|
||||
incomplete: response.incomplete,
|
||||
downloaded: response.downloaded
|
||||
infoHash: common.binaryToHex(infoHash)
|
||||
})
|
||||
self.client.emit('scrape', response)
|
||||
})
|
||||
}
|
||||
|
||||
|
19
server.js
19
server.js
@ -35,7 +35,6 @@ inherits(Server, EventEmitter)
|
||||
* @param {boolean} opts.ws start a websocket server? (default: true)
|
||||
* @param {boolean} opts.stats enable web-based statistics? (default: true)
|
||||
* @param {function} opts.filter black/whitelist fn for disallowing/allowing torrents
|
||||
* @param {function} opts.requestHandler functions to handle params / response
|
||||
*/
|
||||
function Server (opts) {
|
||||
var self = this
|
||||
@ -65,18 +64,6 @@ function Server (opts) {
|
||||
self.udp6 = null
|
||||
self.ws = null
|
||||
|
||||
self._reqHandler = opts.requestHandler || {}
|
||||
if (!self._reqHandler.getParams) {
|
||||
self._reqHandler.getParams = function (params) {
|
||||
return params
|
||||
}
|
||||
}
|
||||
if (!self._reqHandler.getResponse) {
|
||||
self._reqHandler.getResponse = function (params, cb) {
|
||||
return cb
|
||||
}
|
||||
}
|
||||
|
||||
// start an http tracker unless the user explictly says no
|
||||
if (opts.http !== false) {
|
||||
self.http = http.createServer()
|
||||
@ -293,6 +280,8 @@ function Server (opts) {
|
||||
}
|
||||
}
|
||||
|
||||
Server.Swarm = Swarm
|
||||
|
||||
Server.prototype._onError = function (err) {
|
||||
var self = this
|
||||
self.emit('error', err)
|
||||
@ -365,7 +354,7 @@ Server.prototype.createSwarm = function (infoHash, cb) {
|
||||
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
|
||||
|
||||
process.nextTick(function () {
|
||||
var swarm = self.torrents[infoHash] = new Swarm(infoHash, self)
|
||||
var swarm = self.torrents[infoHash] = new Server.Swarm(infoHash, self)
|
||||
cb(null, swarm)
|
||||
})
|
||||
}
|
||||
@ -649,8 +638,6 @@ Server.prototype._onWebSocketError = function (socket, err) {
|
||||
|
||||
Server.prototype._onRequest = function (params, cb) {
|
||||
var self = this
|
||||
params = self._reqHandler.getParams(params)
|
||||
cb = self._reqHandler.getResponse(params, cb)
|
||||
if (params && params.action === common.ACTIONS.CONNECT) {
|
||||
cb(null, { action: common.ACTIONS.CONNECT })
|
||||
} else if (params && params.action === common.ACTIONS.ANNOUNCE) {
|
||||
|
@ -3,26 +3,33 @@ var Client = require('../')
|
||||
var common = require('./common')
|
||||
var fixtures = require('webtorrent-fixtures')
|
||||
var test = require('tape')
|
||||
var Server = require('../server')
|
||||
|
||||
var peerId = Buffer.from('01234567890123456789')
|
||||
|
||||
function testRequestHandler (t, serverType) {
|
||||
t.plan(4)
|
||||
t.plan(5)
|
||||
|
||||
var opts = { serverType: serverType } // this is test-suite-only option
|
||||
opts.requestHandler = {
|
||||
getParams: function (params) {
|
||||
params.extra = 123
|
||||
return params
|
||||
},
|
||||
getResponse: function (params, cb) {
|
||||
return function (err, response) {
|
||||
response.complete = params.extra * 2
|
||||
cb(err, response)
|
||||
}
|
||||
|
||||
class Swarm extends Server.Swarm {
|
||||
announce (params, cb) {
|
||||
super.announce(params, function (err, response) {
|
||||
if (err) return cb(response)
|
||||
response.complete = 246
|
||||
response.extraData = 'hi'
|
||||
cb(null, response)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Use a custom Swarm implementation for this test only
|
||||
var OldSwarm = Server.Swarm
|
||||
Server.Swarm = Swarm
|
||||
t.on('end', function () {
|
||||
Server.Swarm = OldSwarm
|
||||
})
|
||||
|
||||
common.createServer(t, opts, function (server, announceUrl) {
|
||||
var client1 = new Client({
|
||||
infoHash: fixtures.alice.parsedTorrent.infoHash,
|
||||
@ -41,6 +48,7 @@ function testRequestHandler (t, serverType) {
|
||||
|
||||
client1.once('update', function (data) {
|
||||
t.equal(data.complete, 246)
|
||||
t.equal(data.extraData.toString(), 'hi')
|
||||
|
||||
client1.destroy(function () {
|
||||
t.pass('client1 destroyed')
|
||||
@ -59,10 +67,8 @@ test('http: request handler option intercepts announce requests and responses',
|
||||
testRequestHandler(t, 'http')
|
||||
})
|
||||
|
||||
test('udp: request handler option intercepts announce requests and responses', function (t) {
|
||||
testRequestHandler(t, 'udp')
|
||||
})
|
||||
|
||||
test('ws: request handler option intercepts announce requests and responses', function (t) {
|
||||
testRequestHandler(t, 'ws')
|
||||
})
|
||||
|
||||
// NOTE: it's not possible to include extra data in a UDP response, because it's compact and accepts only params that are in the spec!
|
||||
|
Loading…
Reference in New Issue
Block a user