Skip to content

Commit c3abef7

Browse files
committed
tracker should not modify opts object, it's passed to all trackers
1 parent 53c973b commit c3abef7

File tree

8 files changed

+86
-75
lines changed

8 files changed

+86
-75
lines changed

client.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ inherits(Client, EventEmitter)
2323
* @param {Number} port torrent client listening port
2424
* @param {Object} torrent parsed torrent
2525
* @param {Object} opts options object
26-
* @param {Number} opts.numWant number of peers to request
26+
* @param {Number} opts.numwant number of peers to request
2727
* @param {Number} opts.interval announce interval (in ms)
2828
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
2929
* @param {Number} opts.wrtc custom webrtc implementation
@@ -56,7 +56,7 @@ function Client (peerId, port, torrent, opts) {
5656
self._wrtc = opts.wrtc
5757

5858
// optional
59-
self._numWant = opts.numWant || common.DEFAULT_ANNOUNCE_PEERS
59+
self._numwant = opts.numwant || common.DEFAULT_ANNOUNCE_PEERS
6060
self._intervalMs = opts.interval || common.DEFAULT_ANNOUNCE_INTERVAL
6161

6262
debug('new client %s', self._infoHashHex)
@@ -160,6 +160,7 @@ Client.prototype.start = function (opts) {
160160
* @param {Object} opts
161161
* @param {number=} opts.uploaded
162162
* @param {number=} opts.downloaded
163+
* @param {number=} opts.numwant
163164
* @param {number=} opts.left (if not set, calculated automatically)
164165
*/
165166
Client.prototype.stop = function (opts) {
@@ -175,6 +176,7 @@ Client.prototype.stop = function (opts) {
175176
* @param {Object} opts
176177
* @param {number=} opts.uploaded
177178
* @param {number=} opts.downloaded
179+
* @param {number=} opts.numwant
178180
* @param {number=} opts.left (if not set, calculated automatically)
179181
*/
180182
Client.prototype.complete = function (opts) {
@@ -194,6 +196,7 @@ Client.prototype.complete = function (opts) {
194196
* @param {Object} opts
195197
* @param {number=} opts.uploaded
196198
* @param {number=} opts.downloaded
199+
* @param {number=} opts.numwant
197200
* @param {number=} opts.left (if not set, calculated automatically)
198201
*/
199202
Client.prototype.update = function (opts) {
@@ -207,22 +210,21 @@ Client.prototype.update = function (opts) {
207210
Client.prototype._announce = function (opts) {
208211
var self = this
209212
self._trackers.forEach(function (tracker) {
213+
// tracker should not modify `opts` object, it's passed to all trackers
210214
tracker.announce(opts)
211215
})
212216
}
213217

214218
/**
215219
* Send a scrape request to the trackers.
216220
* @param {Object} opts
217-
* @param {number=} opts.uploaded
218-
* @param {number=} opts.downloaded
219-
* @param {number=} opts.left (if not set, calculated automatically)
220221
*/
221222
Client.prototype.scrape = function (opts) {
222223
var self = this
223224
debug('send `scrape`')
224225
if (!opts) opts = {}
225226
self._trackers.forEach(function (tracker) {
227+
// tracker should not modify `opts` object, it's passed to all trackers
226228
tracker.scrape(opts)
227229
})
228230
}
@@ -258,7 +260,7 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
258260
var self = this
259261
if (!opts) opts = {}
260262

261-
if (opts.numWant == null) opts.numWant = self._numWant
263+
if (opts.numwant == null) opts.numwant = self._numwant
262264

263265
if (opts.uploaded == null) opts.uploaded = 0
264266
if (opts.downloaded == null) opts.downloaded = 0

lib/http-tracker.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,20 @@ function HTTPTracker (client, announceUrl, opts) {
4545
HTTPTracker.prototype.announce = function (opts) {
4646
var self = this
4747
if (self.destroyed) return
48-
if (self._trackerId) opts.trackerid = self._trackerId
4948

50-
if (opts.compact == null) opts.compact = 1
51-
if (opts.numwant == null) opts.numwant = self.client._numWant // spec says 'numwant'
52-
53-
opts.info_hash = self.client._infoHashBinary
54-
opts.peer_id = self.client._peerIdBinary
55-
opts.port = self.client._port
49+
var params = {
50+
numwant: opts.numwant,
51+
uploaded: opts.uploaded,
52+
downloaded: opts.downloaded,
53+
event: opts.event,
54+
compact: (opts.compact == null) ? 1 : opts.compact,
55+
info_hash: self.client._infoHashBinary,
56+
peer_id: self.client._peerIdBinary,
57+
port: self.client._port
58+
}
59+
if (self._trackerId) params.trackerid = self._trackerId
5660

57-
self._request(self._announceUrl, opts, self._onAnnounceResponse.bind(self))
61+
self._request(self._announceUrl, params, self._onAnnounceResponse.bind(self))
5862
}
5963

6064
HTTPTracker.prototype.scrape = function (opts) {
@@ -66,13 +70,15 @@ HTTPTracker.prototype.scrape = function (opts) {
6670
return
6771
}
6872

69-
opts.info_hash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
70-
? opts.infoHash.map(function (infoHash) { return infoHash.toString('binary') })
73+
var infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
74+
? opts.infoHash.map(function (infoHash) {
75+
return infoHash.toString('binary')
76+
})
7177
: (opts.infoHash || self.client._infoHash).toString('binary')
72-
73-
if (opts.infoHash) delete opts.infoHash
74-
75-
self._request(self._scrapeUrl, opts, self._onScrapeResponse.bind(self))
78+
var params = {
79+
info_hash: infoHashes
80+
}
81+
self._request(self._scrapeUrl, params, self._onScrapeResponse.bind(self))
7682
}
7783

7884
// TODO: Improve this interface
@@ -95,12 +101,11 @@ HTTPTracker.prototype.destroy = function (cb) {
95101
cb(null)
96102
}
97103

98-
HTTPTracker.prototype._request = function (requestUrl, opts, cb) {
104+
HTTPTracker.prototype._request = function (requestUrl, params, cb) {
99105
var self = this
100106

101107
var u = requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') +
102-
common.querystringStringify(opts)
103-
108+
common.querystringStringify(params)
104109
get.concat(u, function (err, data, res) {
105110
if (self.destroyed) return
106111
if (err) return self.client.emit('warning', err)

lib/parse_udp.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ function parseUdpRequest (msg, rinfo) {
1313
transactionId: msg.readUInt32BE(12)
1414
}
1515

16-
// TODO: randomize
1716
if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) {
1817
throw new Error('received packet with invalid connection id')
1918
}

lib/parse_websocket.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function parseWebSocketRequest (socket, params) {
3030
Number(params.offers && params.offers.length) || 0, // no default - explicit only
3131
common.MAX_ANNOUNCE_PEERS
3232
)
33+
params.compact = -1 // return full peer objects (used for websocket responses)
3334

3435
return params
3536
}

lib/swarm.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ Swarm.prototype.announce = function (params, cb) {
1515
var self = this
1616
var peer = self.peers[params.addr || params.peer_id]
1717

18-
// Dispatch announce event
19-
var fn = '_onAnnounce_' + params.event
20-
if (self[fn]) {
21-
self[fn](params, peer) // process event
22-
23-
var peerType = params.compact === undefined ? 'webrtc' : 'addr'
24-
cb(null, {
25-
complete: self.complete,
26-
incomplete: self.incomplete,
27-
peers: self._getPeers(params.numwant, peerType)
28-
})
18+
if (params.event === 'started') {
19+
self._onAnnounceStarted(params, peer)
20+
} else if (params.event === 'stopped') {
21+
self._onAnnounceStopped(params, peer)
22+
} else if (params.event === 'completed') {
23+
self._onAnnounceCompleted(params, peer)
24+
} else if (params.event === 'update') {
25+
self._onAnnounceUpdate(params, peer)
2926
} else {
3027
cb(new Error('invalid event'))
28+
return
3129
}
30+
console.log('FEROSS', params.compact)
31+
cb(null, {
32+
complete: self.complete,
33+
incomplete: self.incomplete,
34+
peers: self._getPeers(params.numwant, !!params.socket)
35+
})
3236
}
3337

3438
Swarm.prototype.scrape = function (params, cb) {
@@ -38,7 +42,7 @@ Swarm.prototype.scrape = function (params, cb) {
3842
})
3943
}
4044

41-
Swarm.prototype._onAnnounce_started = function (params, peer) {
45+
Swarm.prototype._onAnnounceStarted = function (params, peer) {
4246
if (peer) {
4347
debug('unexpected `started` event from peer that is already in swarm')
4448
return this._onAnnounce_update(params, peer) // treat as an update
@@ -48,14 +52,14 @@ Swarm.prototype._onAnnounce_started = function (params, peer) {
4852
else this.incomplete += 1
4953
peer = this.peers[params.addr || params.peer_id] = {
5054
complete: params.left === 0,
51-
ip: params.ip, // only http+udp
5255
peerId: params.peer_id, // as hex
53-
port: params.port, // only http+udp
56+
ip: params.ip, // only http, udp
57+
port: params.port, // only http, udp
5458
socket: params.socket // only websocket
5559
}
5660
}
5761

58-
Swarm.prototype._onAnnounce_stopped = function (params, peer) {
62+
Swarm.prototype._onAnnounceStopped = function (params, peer) {
5963
if (!peer) {
6064
debug('unexpected `stopped` event from peer that is not in swarm')
6165
return // do nothing
@@ -66,7 +70,7 @@ Swarm.prototype._onAnnounce_stopped = function (params, peer) {
6670
this.peers[params.addr || params.peer_id] = null
6771
}
6872

69-
Swarm.prototype._onAnnounce_completed = function (params, peer) {
73+
Swarm.prototype._onAnnounceCompleted = function (params, peer) {
7074
if (!peer) {
7175
debug('unexpected `completed` event from peer that is not in swarm')
7276
return this._onAnnounce_started(params, peer) // treat as a start
@@ -81,7 +85,7 @@ Swarm.prototype._onAnnounce_completed = function (params, peer) {
8185
peer.complete = true
8286
}
8387

84-
Swarm.prototype._onAnnounce_update = function (params, peer) {
88+
Swarm.prototype._onAnnounceUpdate = function (params, peer) {
8589
if (!peer) {
8690
debug('unexpected `update` event from peer that is not in swarm')
8791
return this._onAnnounce_started(params, peer) // treat as a start
@@ -94,16 +98,14 @@ Swarm.prototype._onAnnounce_update = function (params, peer) {
9498
}
9599
}
96100

97-
Swarm.prototype._getPeers = function (numWant, peerType) {
101+
Swarm.prototype._getPeers = function (numwant, isWebRTC) {
98102
var peers = []
99103
var ite = randomIterate(Object.keys(this.peers))
100-
while (true) {
101-
var peerId = ite()
102-
if (peers.length >= numWant || peerId == null) return peers
104+
var peerId
105+
while ((peerId = ite()) && peers.length < numwant) {
103106
var peer = this.peers[peerId]
104-
if (peer &&
105-
((peerType === 'webrtc' && peer.socket) || (peerType === 'addr' && peer.ip))) {
106-
peers.push(peer)
107-
}
107+
if (!peer) continue
108+
if ((isWebRTC && peer.socket) || (!isWebRTC && peer.ip)) peers.push(peer)
108109
}
110+
return peers
109111
}

lib/udp-tracker.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ UDPTracker.prototype._request = function (opts) {
208208
}
209209

210210
function announce (connectionId, opts) {
211-
opts = opts || {}
212211
transactionId = genTransactionId()
213212

214213
send(Buffer.concat([
@@ -217,14 +216,14 @@ UDPTracker.prototype._request = function (opts) {
217216
transactionId,
218217
self.client._infoHash,
219218
self.client._peerId,
220-
toUInt64(opts.downloaded || 0),
219+
toUInt64(opts.downloaded),
221220
opts.left != null ? toUInt64(opts.left) : new Buffer('FFFFFFFFFFFFFFFF', 'hex'),
222-
toUInt64(opts.uploaded || 0),
223-
common.toUInt32(common.EVENTS[opts.event] || 0),
221+
toUInt64(opts.uploaded),
222+
common.toUInt32(common.EVENTS[opts.event]),
224223
common.toUInt32(0), // ip address (optional)
225224
common.toUInt32(0), // key (optional)
226-
common.toUInt32(opts.numWant || common.DEFAULT_ANNOUNCE_PEERS),
227-
toUInt16(self.client._port || 0)
225+
common.toUInt32(opts.numwant),
226+
toUInt16(self.client._port)
228227
]))
229228
}
230229

lib/websocket-tracker.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,23 @@ WebSocketTracker.prototype.announce = function (opts) {
5151
return self._socket.once('connect', self.announce.bind(self, opts))
5252
}
5353

54-
opts.info_hash = self.client._infoHashBinary
55-
opts.peer_id = self.client._peerIdBinary
56-
57-
// Limit number of offers (temporarily)
54+
// TODO: Limit number of offers (temporarily)
5855
// TODO: remove this when we cleanup old RTCPeerConnections cleanly
59-
if (opts.numWant > 10) opts.numWant = 10
60-
61-
self._generateOffers(opts.numWant, function (offers) {
62-
opts.offers = offers
63-
64-
if (self._trackerId) {
65-
opts.trackerid = self._trackerId
56+
var numwant = Math.min(opts.numwant, 10)
57+
58+
self._generateOffers(numwant, function (offers) {
59+
var params = {
60+
numwant: numwant,
61+
uploaded: opts.uploaded || 0,
62+
downloaded: opts.downloaded,
63+
event: opts.event,
64+
info_hash: self.client._infoHashBinary,
65+
peer_id: self.client._peerIdBinary,
66+
offers: offers
6667
}
67-
self._send(opts)
68+
if (self._trackerId) params.trackerid = self._trackerId
69+
70+
self._send(params)
6871
})
6972
}
7073

@@ -252,13 +255,13 @@ WebSocketTracker.prototype._send = function (params) {
252255
self._socket.send(message)
253256
}
254257

255-
WebSocketTracker.prototype._generateOffers = function (numWant, cb) {
258+
WebSocketTracker.prototype._generateOffers = function (numwant, cb) {
256259
var self = this
257260
var offers = []
258-
debug('generating %s offers', numWant)
261+
debug('generating %s offers', numwant)
259262

260263
// TODO: cleanup dead peers and peers that never get a return offer, from self._peers
261-
for (var i = 0; i < numWant; ++i) {
264+
for (var i = 0; i < numwant; ++i) {
262265
generateOffer()
263266
}
264267

@@ -280,8 +283,8 @@ WebSocketTracker.prototype._generateOffers = function (numWant, cb) {
280283
}
281284

282285
function checkDone () {
283-
if (offers.length === numWant) {
284-
debug('generated %s offers', numWant)
286+
if (offers.length === numwant) {
287+
debug('generated %s offers', numwant)
285288
cb(offers)
286289
}
287290
}

test/client.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function testClientAnnounceWithNumWant (t, serverType) {
190190
})
191191
client2.start()
192192
client2.once('update', function () {
193-
var client3 = new Client(peerId3, port + 2, parsedTorrent, { numWant: 1 })
193+
var client3 = new Client(peerId3, port + 2, parsedTorrent, { numwant: 1 })
194194
client3.on('error', function (err) {
195195
t.error(err)
196196
})
@@ -228,10 +228,10 @@ function testClientAnnounceWithNumWant (t, serverType) {
228228
})
229229
}
230230

231-
test('http: client announce with numWant', function (t) {
231+
test('http: client announce with numwant', function (t) {
232232
testClientAnnounceWithNumWant(t, 'http')
233233
})
234234

235-
test('udp: client announce with numWant', function (t) {
235+
test('udp: client announce with numwant', function (t) {
236236
testClientAnnounceWithNumWant(t, 'udp')
237237
})

0 commit comments

Comments
 (0)