|
3 | 3 | exports.Client = Client |
4 | 4 | exports.Server = Server |
5 | 5 |
|
6 | | -var bn = require('bn.js') |
| 6 | +var BN = require('bn.js') |
7 | 7 | var bncode = require('bncode') |
8 | 8 | var compact2string = require('compact2string') |
9 | 9 | var dgram = require('dgram') |
@@ -71,7 +71,7 @@ Tracker.prototype.complete = function (opts) { |
71 | 71 | var self = this |
72 | 72 | opts = opts || {} |
73 | 73 | opts.event = 'completed' |
74 | | - opts.downloaded = self._torrentLength |
| 74 | + opts.downloaded = opts.downloaded || self.torrentLength || 0 |
75 | 75 | self._request(opts) |
76 | 76 | } |
77 | 77 |
|
@@ -126,13 +126,16 @@ Tracker.prototype._request = function (opts) { |
126 | 126 | info_hash: bytewiseEncodeURIComponent(self.client._infoHash), |
127 | 127 | peer_id: bytewiseEncodeURIComponent(self.client._peerId), |
128 | 128 | port: self.client._port, |
129 | | - left: self.client._torrentLength - (opts.downloaded || 0), |
130 | 129 | compact: 1, |
131 | 130 | numwant: self.client._numWant, |
132 | 131 | uploaded: 0, // default, user should provide real value |
133 | 132 | downloaded: 0 // default, user should provide real value |
134 | 133 | }, opts) |
135 | 134 |
|
| 135 | + if (self.client.torrentLength !== undefined) { |
| 136 | + opts.left = self.client.torrentLength - (opts.downloaded || 0) |
| 137 | + } |
| 138 | + |
136 | 139 | if (self._trackerId) { |
137 | 140 | opts.trackerid = self._trackerId |
138 | 141 | } |
@@ -267,7 +270,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) { |
267 | 270 | self.client._infoHash, |
268 | 271 | self.client._peerId, |
269 | 272 | toUInt64(opts.downloaded || 0), |
270 | | - toUInt64(opts.left || 0), |
| 273 | + opts.left ? toUInt64(opts.left) : new Buffer('FFFFFFFFFFFFFFFF', 'hex'), |
271 | 274 | toUInt64(opts.uploaded || 0), |
272 | 275 | toUInt32(EVENTS[opts.event] || 0), |
273 | 276 | toUInt32(0), // ip address (optional) |
@@ -390,13 +393,17 @@ function Client (peerId, port, torrent, opts) { |
390 | 393 | self._infoHash = Buffer.isBuffer(torrent.infoHash) |
391 | 394 | ? torrent.infoHash |
392 | 395 | : new Buffer(torrent.infoHash, 'hex') |
393 | | - self._torrentLength = torrent.length |
| 396 | + self.torrentLength = torrent.length |
394 | 397 | self._announce = torrent.announce |
395 | 398 |
|
396 | 399 | // optional |
397 | 400 | self._numWant = self._opts.numWant || 80 |
398 | 401 | self._intervalMs = self._opts.interval || (30 * 60 * 1000) // default: 30 minutes |
399 | 402 |
|
| 403 | + if (typeof torrent.announce === 'string') { |
| 404 | + // magnet-uri returns a string if the magnet uri only contains one 'tr' parameter |
| 405 | + torrent.announce = [torrent.announce] |
| 406 | + } |
400 | 407 | self._trackers = torrent.announce.map(function (announceUrl) { |
401 | 408 | return new Tracker(self, announceUrl, self._opts) |
402 | 409 | }) |
@@ -700,7 +707,7 @@ function toUInt32 (n) { |
700 | 707 |
|
701 | 708 | function toUInt64 (n) { |
702 | 709 | if (n > MAX_UINT || typeof n === 'string') { |
703 | | - var bytes = bn(n).toArray() |
| 710 | + var bytes = new BN(n).toArray() |
704 | 711 | while (bytes.length < 8) { |
705 | 712 | bytes.unshift(0) |
706 | 713 | } |
|
0 commit comments