diff --git a/README.md b/README.md index d668a53..1fd8a7a 100644 --- a/README.md +++ b/README.md @@ -111,10 +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 (infoHash) { + filter: function (infoHash, params) { // black/whitelist for disallowing/allowing torrents [default=allow all] // this example only allows this one torrent 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 GET + // request parameters in `params` }) }) 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..d50dc06 100644 --- a/server.js +++ b/server.js @@ -109,10 +109,11 @@ Server.prototype.close = function (cb) { } } -Server.prototype.getSwarm = function (infoHash) { +Server.prototype.getSwarm = function (infoHash, params) { var self = this + if (!params) params = {} if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex') - if (self._filter && !self._filter(infoHash)) return null + 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 @@ -207,7 +208,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) {