From 146d4d46dd10588a116a192335637cb243c1f07b Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Fri, 24 Feb 2017 16:18:32 +0100 Subject: [PATCH 1/8] Extension point on requests and responses --- server.js | 31 +++++++++++++----- test/request-handler.js | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 test/request-handler.js diff --git a/server.js b/server.js index 3723516..abb2d2d 100644 --- a/server.js +++ b/server.js @@ -27,14 +27,15 @@ inherits(Server, EventEmitter) * metrics from clients that help the tracker keep overall statistics about the torrent. * Responses include a peer list that helps the client participate in the torrent. * - * @param {Object} opts options object - * @param {Number} opts.interval tell clients to announce on this interval (ms) - * @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy - * @param {boolean} opts.http start an http server? (default: true) - * @param {boolean} opts.udp start a udp server? (default: true) - * @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 {Object} opts options object + * @param {Number} opts.interval tell clients to announce on this interval (ms) + * @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy + * @param {boolean} opts.http start an http server? (default: true) + * @param {boolean} opts.udp start a udp server? (default: true) + * @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 @@ -64,6 +65,18 @@ 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() @@ -636,6 +649,8 @@ 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) { diff --git a/test/request-handler.js b/test/request-handler.js new file mode 100644 index 0000000..f186230 --- /dev/null +++ b/test/request-handler.js @@ -0,0 +1,70 @@ +var Buffer = require('safe-buffer').Buffer +var Client = require('../') +var common = require('./common') +var fixtures = require('webtorrent-fixtures') +var test = require('tape') + +var peerId = Buffer.from('01234567890123456789') + +function testRequestHandler (t, serverType) { + t.plan(4) + + 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) + } + } + } + + common.createServer(t, opts, function (server, announceUrl) { + var client1 = new Client({ + infoHash: fixtures.alice.parsedTorrent.infoHash, + announce: announceUrl, + peerId: peerId, + port: 6881, + wrtc: {} + }) + + client1.on('error', function (err) { t.error(err) }) + if (serverType === 'ws') common.mockWebsocketTracker(client1) + + server.once('start', function () { + t.pass('got start message from client1') + }) + + client1.once('update', function (data) { + console.log(data) + + t.equal(data.complete, 246) + + client1.destroy(function () { + t.pass('client1 destroyed') + }) + + server.close(function () { + t.pass('server destroyed') + }) + }) + + client1.start() + }) +} + +test('http: request handler option intercepts announce requests and responses', function (t) { + 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') +}) From 1efb832dd7e390487470e75ae1acdc41949e805a Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Fri, 24 Feb 2017 18:08:00 +0100 Subject: [PATCH 2/8] Remove debug log --- test/request-handler.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/request-handler.js b/test/request-handler.js index f186230..978ea7e 100644 --- a/test/request-handler.js +++ b/test/request-handler.js @@ -40,8 +40,6 @@ function testRequestHandler (t, serverType) { }) client1.once('update', function (data) { - console.log(data) - t.equal(data.complete, 246) client1.destroy(function () { From 8482c3af0ae87dc89e44f4ab1161e0b692e215f2 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Fri, 24 Feb 2017 18:08:44 +0100 Subject: [PATCH 3/8] Add extra data on ws annouce event --- lib/client/websocket-tracker.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index 894408e..c37ea7f 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -266,11 +266,8 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) { } if (data.complete != null) { - self.client.emit('update', { - announce: self.announceUrl, - complete: data.complete, - incomplete: data.incomplete - }) + data.announce = self.announceUrl + self.client.emit('update', data) } var peer From f65983e466c0ef7279141639c072bbf6a8c22b22 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 10 Mar 2017 13:38:04 -0800 Subject: [PATCH 4/8] do not filter out extra keys from 'update' events --- lib/client/http-tracker.js | 15 ++++++--------- lib/client/websocket-tracker.js | 16 ++++++++-------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index 629ae16..2cffc2a 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -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) }) } diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index c37ea7f..058bc28 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -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) }) } From ee9ebc506c0349eba962ad46b2769ab7019d74ca Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 10 Mar 2017 13:38:27 -0800 Subject: [PATCH 5/8] remove requestHandler option --- server.js | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/server.js b/server.js index abb2d2d..3723516 100644 --- a/server.js +++ b/server.js @@ -27,15 +27,14 @@ inherits(Server, EventEmitter) * metrics from clients that help the tracker keep overall statistics about the torrent. * Responses include a peer list that helps the client participate in the torrent. * - * @param {Object} opts options object - * @param {Number} opts.interval tell clients to announce on this interval (ms) - * @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy - * @param {boolean} opts.http start an http server? (default: true) - * @param {boolean} opts.udp start a udp server? (default: true) - * @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 + * @param {Object} opts options object + * @param {Number} opts.interval tell clients to announce on this interval (ms) + * @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy + * @param {boolean} opts.http start an http server? (default: true) + * @param {boolean} opts.udp start a udp server? (default: true) + * @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 */ 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() @@ -649,8 +636,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) { From 992cd77fc75f44dd8829393211756abbaba6debe Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 10 Mar 2017 13:38:43 -0800 Subject: [PATCH 6/8] expose Swarm object for easy overriding --- server.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 3723516..419073b 100644 --- a/server.js +++ b/server.js @@ -280,6 +280,8 @@ function Server (opts) { } } +Server.Swarm = Swarm + Server.prototype._onError = function (err) { var self = this self.emit('error', err) @@ -352,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) }) } From 51b57f66cfd5e0e788a934d3d346102ecd52e60d Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 10 Mar 2017 13:39:02 -0800 Subject: [PATCH 7/8] test: remove UDP request handler test -- not possible --- test/request-handler.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/request-handler.js b/test/request-handler.js index 978ea7e..7c8fe80 100644 --- a/test/request-handler.js +++ b/test/request-handler.js @@ -59,10 +59,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! From 0c683dfe64bbef267709069f89d0fd9beef27700 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 10 Mar 2017 13:39:12 -0800 Subject: [PATCH 8/8] test: change request handler test to new approach --- test/request-handler.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/test/request-handler.js b/test/request-handler.js index 7c8fe80..e04e665 100644 --- a/test/request-handler.js +++ b/test/request-handler.js @@ -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')