diff --git a/client.js b/client.js index f1ff3040..6a628c0f 100644 --- a/client.js +++ b/client.js @@ -53,6 +53,9 @@ function Client (peerId, port, torrent, opts) { self._rtcConfig = opts.rtcConfig self._wrtc = opts.wrtc + self._userAgent = opts.userAgent + self._key = opts.key + self._statsForAnnounce = opts.statsForAnnounce debug('new client %s', self._infoHashHex) @@ -262,11 +265,15 @@ Client.prototype._defaultAnnounceOpts = function (opts) { if (opts.numwant == null) opts.numwant = common.DEFAULT_ANNOUNCE_PEERS - if (opts.uploaded == null) opts.uploaded = 0 - if (opts.downloaded == null) opts.downloaded = 0 + var providedStats = self._statsForAnnounce ? self._statsForAnnounce() : {} + + if (opts.uploaded == null) { opts.uploaded = providedStats.uploaded || 0 } + if (opts.downloaded == null) { opts.downloaded = providedStats.downloaded || 0 } + if (self._key) { opts.key = self._key } if (opts.left == null && self.torrentLength != null) { opts.left = self.torrentLength - opts.downloaded } + return opts } diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index b82691f0..d59386fd 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -41,15 +41,20 @@ HTTPTracker.prototype.announce = function (opts) { if (self.destroyed) return var params = { - numwant: opts.numwant, + info_hash: self.client._infoHashBinary, + peer_id: self.client._peerIdBinary, + port: self.client._port, uploaded: opts.uploaded, downloaded: opts.downloaded, - event: opts.event, + left: opts.left, + numwant: opts.numwant, + key: opts.key, compact: (opts.compact == null) ? 1 : opts.compact, - info_hash: self.client._infoHashBinary, - peer_id: self.client._peerIdBinary, - port: self.client._port + event: opts.event } + + if (!params.key) { delete params.key } + if (self._trackerId) params.trackerid = self._trackerId self._request(self.announceUrl, params, self._onAnnounceResponse.bind(self)) @@ -86,10 +91,15 @@ HTTPTracker.prototype.destroy = function (cb) { HTTPTracker.prototype._request = function (requestUrl, params, cb) { var self = this - var u = requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') + + var headers = {} + var url = requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') + common.querystringStringify(params) - get.concat(u, function (err, data, res) { + if (self.client._userAgent) { + headers['User-Agent'] = self.client._userAgent + } + + get.concat({ url: url, headers: headers }, function (err, data, res) { if (self.destroyed) return if (err) return self.client.emit('warning', err) if (res.statusCode !== 200) {