Skip to content

Commit de19b61

Browse files
author
Nick Rafter
committed
Exposed full http request to filter function for more configurable filtering
1 parent a5cadcc commit de19b61

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,13 @@ 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 (params) {
115115
// black/whitelist for disallowing/allowing torrents [default=allow all]
116116
// this example only allows this one torrent
117-
return infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa'
117+
return params.info_hash === '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 request
118121
})
119122
})
120123

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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ 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 (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
115-
if (self._filter && !self._filter(infoHash)) return null
114+
if (Buffer.isBuffer(infoHash)) infoHash = params.info_hash = infoHash.toString('hex')
115+
if (self._filter && !self._filter(params)) return null
116116
var swarm = self.torrents[infoHash]
117117
if (!swarm) swarm = self.torrents[infoHash] = new Swarm(infoHash, this)
118118
return swarm
@@ -207,7 +207,7 @@ Server.prototype._onRequest = function (params, cb) {
207207

208208
Server.prototype._onAnnounce = function (params, cb) {
209209
var self = this
210-
var swarm = self.getSwarm(params.info_hash)
210+
var swarm = self.getSwarm(params.info_hash, params)
211211
if (swarm === null) return cb(new Error('disallowed info_hash'))
212212
if (!params.event || params.event === 'empty') params.event = 'update'
213213
swarm.announce(params, function (err, response) {

0 commit comments

Comments
 (0)