From de19b61c522762f5d4ec6e67160ccf68085f64ef Mon Sep 17 00:00:00 2001 From: Nick Rafter Date: Thu, 19 Feb 2015 12:01:46 -0500 Subject: [PATCH 1/2] Exposed full http request to filter function for more configurable filtering --- README.md | 7 +++++-- examples/express-embed/server.js | 13 ++++++++++++- server.js | 8 ++++---- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d668a53..cded742 100644 --- a/README.md +++ b/README.md @@ -111,10 +111,13 @@ var Server = require('bittorrent-tracker').Server var server = new Server({ udp: true, // enable udp server? [default=true] http: true, // enable http server? [default=true] - filter: function (infoHash) { + filter: function (params) { // black/whitelist for disallowing/allowing torrents [default=allow all] // this example only allows this one torrent - return infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa' + return params.info_hash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa' + + // you can also block by peer id (whitelisting torrent clients) or by + // secret key, as you get full access to the original http request }) }) diff --git a/examples/express-embed/server.js b/examples/express-embed/server.js index 294f013..496ad21 100755 --- a/examples/express-embed/server.js +++ b/examples/express-embed/server.js @@ -4,9 +4,20 @@ var Server = require('../..').Server var express = require('express') var app = express() +// https://wiki.theory.org/BitTorrentSpecification#peer_id +var whitelist = { + UT: true // uTorrent +} + var server = new Server({ http: false, // we do our own - udp: false // not interested + udp: false, // not interested + filter: function (params) { + // black/whitelist for disallowing/allowing specific clients [default=allow all] + // this example only allows the uTorrent client + var client = params.peer_id[1] + params.peer_id[2] + return whitelist[client] + } }) var onHttpRequest = server.onHttpRequest.bind(server) diff --git a/server.js b/server.js index 1d31427..186024f 100644 --- a/server.js +++ b/server.js @@ -109,10 +109,10 @@ Server.prototype.close = function (cb) { } } -Server.prototype.getSwarm = function (infoHash) { +Server.prototype.getSwarm = function (infoHash, params) { var self = this - if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex') - if (self._filter && !self._filter(infoHash)) return null + if (Buffer.isBuffer(infoHash)) infoHash = params.info_hash = infoHash.toString('hex') + if (self._filter && !self._filter(params)) return null var swarm = self.torrents[infoHash] if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, this) return swarm @@ -207,7 +207,7 @@ Server.prototype._onRequest = function (params, cb) { Server.prototype._onAnnounce = function (params, cb) { var self = this - var swarm = self.getSwarm(params.info_hash) + var swarm = self.getSwarm(params.info_hash, params) if (swarm === null) return cb(new Error('disallowed info_hash')) if (!params.event || params.event === 'empty') params.event = 'update' swarm.announce(params, function (err, response) { From bdc7edea70b4c00a20e4b16baa1430b0e1648d2a Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 19 Feb 2015 12:56:09 -0800 Subject: [PATCH 2/2] preserve backwards compatibility --- README.md | 9 +++++---- server.js | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index cded742..1fd8a7a 100644 --- a/README.md +++ b/README.md @@ -111,13 +111,14 @@ var Server = require('bittorrent-tracker').Server var server = new Server({ udp: true, // enable udp server? [default=true] http: true, // enable http server? [default=true] - filter: function (params) { + filter: function (infoHash, params) { // black/whitelist for disallowing/allowing torrents [default=allow all] // this example only allows this one torrent - return params.info_hash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa' - + return infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa' + // you can also block by peer id (whitelisting torrent clients) or by - // secret key, as you get full access to the original http request + // secret key, as you get full access to the original http GET + // request parameters in `params` }) }) diff --git a/server.js b/server.js index 186024f..d50dc06 100644 --- a/server.js +++ b/server.js @@ -111,8 +111,9 @@ Server.prototype.close = function (cb) { Server.prototype.getSwarm = function (infoHash, params) { var self = this - if (Buffer.isBuffer(infoHash)) infoHash = params.info_hash = infoHash.toString('hex') - if (self._filter && !self._filter(params)) return null + if (!params) params = {} + if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex') + if (self._filter && !self._filter(infoHash, params)) return null var swarm = self.torrents[infoHash] if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, this) return swarm