Skip to content

Commit 6e0dea5

Browse files
author
Rich Jones
committed
Initial stab at solving webtorrent#181 - see notes in PR
1 parent 524eb13 commit 6e0dea5

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ var optionalOpts = {
7070
uploaded: 0,
7171
downloaded: 0,
7272
left: 0,
73+
ip: 'custom.ip.address.or.hostname',
7374
customParam: 'blah' // custom parameters supported
7475
}
7576
}
@@ -252,6 +253,7 @@ $ bittorrent-tracker --help
252253
253254
Options:
254255
-p, --port [number] change the port [default: 8000]
256+
--trust-ip trust 'ip' parameter in GET requests
255257
--trust-proxy trust 'x-forwarded-for' header from reverse proxy
256258
--interval client announce interval (ms) [default: 600000]
257259
--http enable http server

bin/cmd.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var argv = minimist(process.argv.slice(2), {
1616
'http',
1717
'quiet',
1818
'silent',
19+
'trust-ip',
1920
'trust-proxy',
2021
'udp',
2122
'version',
@@ -53,6 +54,7 @@ if (argv.help) {
5354
--http-hostname [string] change the http server hostname [default: '::']
5455
--udp-hostname [string] change the udp hostname [default: '0.0.0.0']
5556
--udp6-hostname [string] change the udp6 hostname [default: '::']
57+
--trust-ip trust 'ip' parameter in GET requests
5658
--trust-proxy trust 'x-forwarded-for' header from reverse proxy
5759
--interval client announce interval (ms) [default: 600000]
5860
--http enable http server
@@ -82,6 +84,7 @@ var server = new Server({
8284
http: argv.http,
8385
interval: argv.interval,
8486
stats: argv.stats,
87+
trustIp: argv['trust-ip'],
8588
trustProxy: argv['trust-proxy'],
8689
udp: argv.udp,
8790
ws: argv.ws

lib/server/parse-http.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ function parseHttpRequest (req, opts) {
3131
common.MAX_ANNOUNCE_PEERS
3232
)
3333

34-
params.ip = opts.trustProxy
35-
? req.headers['x-forwarded-for'] || req.connection.remoteAddress
36-
: req.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4
34+
params.ip = opts.trustIp
35+
? params.ip || opts.trustProxy
36+
? req.headers['x-forwarded-for'] || req.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force IPv4
37+
: req.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '')
38+
: req.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '')
3739
params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port
3840

3941
params.headers = req.headers

server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ inherits(Server, EventEmitter)
2929
*
3030
* @param {Object} opts options object
3131
* @param {Number} opts.interval tell clients to announce on this interval (ms)
32+
* @param {Number} opts.trustIp trust 'ip' parameter in GET requests
3233
* @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy
3334
* @param {boolean} opts.http start an http server? (default: true)
3435
* @param {boolean} opts.udp start a udp server? (default: true)
@@ -49,6 +50,7 @@ function Server (opts) {
4950
: 10 * 60 * 1000 // 10 min
5051

5152
self._trustProxy = !!opts.trustProxy
53+
self._trustIp = !!opts.trustIp
5254
if (typeof opts.filter === 'function') self._filter = opts.filter
5355

5456
self.peersCacheLength = opts.peersCacheLength
@@ -366,6 +368,7 @@ Server.prototype.onHttpRequest = function (req, res, opts) {
366368
var self = this
367369
if (!opts) opts = {}
368370
opts.trustProxy = opts.trustProxy || self._trustProxy
371+
opts.trustIp = opts.trustIp || self._trustIp
369372

370373
var params
371374
try {
@@ -445,6 +448,7 @@ Server.prototype.onWebSocketConnection = function (socket, opts) {
445448
var self = this
446449
if (!opts) opts = {}
447450
opts.trustProxy = opts.trustProxy || self._trustProxy
451+
opts.trustIp = opts.trustIp || self._trustIp
448452

449453
socket.peerId = null // as hex
450454
socket.infoHashes = [] // swarms that this socket is participating in

test/client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ function testClientGetAnnounceOpts (t, serverType) {
255255
port: port,
256256
getAnnounceOpts: function () {
257257
return {
258-
testParam: 'this is a test'
258+
testParam: 'this is a test',
259+
ip: 'test.bittorrenttest.xyz'
259260
}
260261
},
261262
wrtc: {}

0 commit comments

Comments
 (0)