Skip to content

Commit 8f3ccc5

Browse files
committed
Merge branch 'nrafter-filter-update'
2 parents a5cadcc + bdc7ede commit 8f3ccc5

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,14 @@ var Server = require('bittorrent-tracker').Server
111111
var server = new Server({
112112
udp: true, // enable udp server? [default=true]
113113
http: true, // enable http server? [default=true]
114-
filter: function (infoHash) {
114+
filter: function (infoHash, params) {
115115
// black/whitelist for disallowing/allowing torrents [default=allow all]
116116
// this example only allows this one torrent
117117
return infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa'
118+
119+
// you can also block by peer id (whitelisting torrent clients) or by
120+
// secret key, as you get full access to the original http GET
121+
// request parameters in `params`
118122
})
119123
})
120124

examples/express-embed/server.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@ var Server = require('../..').Server
44
var express = require('express')
55
var app = express()
66

7+
// https://wiki.theory.org/BitTorrentSpecification#peer_id
8+
var whitelist = {
9+
UT: true // uTorrent
10+
}
11+
712
var server = new Server({
813
http: false, // we do our own
9-
udp: false // not interested
14+
udp: false, // not interested
15+
filter: function (params) {
16+
// black/whitelist for disallowing/allowing specific clients [default=allow all]
17+
// this example only allows the uTorrent client
18+
var client = params.peer_id[1] + params.peer_id[2]
19+
return whitelist[client]
20+
}
1021
})
1122

1223
var onHttpRequest = server.onHttpRequest.bind(server)

server.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ Server.prototype.close = function (cb) {
109109
}
110110
}
111111

112-
Server.prototype.getSwarm = function (infoHash) {
112+
Server.prototype.getSwarm = function (infoHash, params) {
113113
var self = this
114+
if (!params) params = {}
114115
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
115-
if (self._filter && !self._filter(infoHash)) return null
116+
if (self._filter && !self._filter(infoHash, params)) return null
116117
var swarm = self.torrents[infoHash]
117118
if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, this)
118119
return swarm
@@ -207,7 +208,7 @@ Server.prototype._onRequest = function (params, cb) {
207208

208209
Server.prototype._onAnnounce = function (params, cb) {
209210
var self = this
210-
var swarm = self.getSwarm(params.info_hash)
211+
var swarm = self.getSwarm(params.info_hash, params)
211212
if (swarm === null) return cb(new Error('disallowed info_hash'))
212213
if (!params.event || params.event === 'empty') params.event = 'update'
213214
swarm.announce(params, function (err, response) {

0 commit comments

Comments
 (0)