From 4c16d3d61021cb9d8352a8f93463ffd6afcf7ad9 Mon Sep 17 00:00:00 2001 From: Trym Skaar Date: Sat, 24 Oct 2015 18:53:50 +0200 Subject: [PATCH 1/4] Allow setting user agent for http tracker client --- client.js | 1 + lib/client/http-tracker.js | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/client.js b/client.js index f1ff3040..44bf0822 100644 --- a/client.js +++ b/client.js @@ -53,6 +53,7 @@ function Client (peerId, port, torrent, opts) { self._rtcConfig = opts.rtcConfig self._wrtc = opts.wrtc + self._userAgent = opts.userAgent debug('new client %s', self._infoHashHex) diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index b82691f0..2af85d0f 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -86,10 +86,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) { From 60387dd00002bd0fc248a02093c3ef96f28fab47 Mon Sep 17 00:00:00 2001 From: Trym Skaar Date: Sat, 24 Oct 2015 18:55:12 +0200 Subject: [PATCH 2/4] Include left, key params in http announce --- lib/client/http-tracker.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index 2af85d0f..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)) From eb854e8f89732d24c7883ce5b22ca13b9d46a3af Mon Sep 17 00:00:00 2001 From: Trym Skaar Date: Sat, 24 Oct 2015 20:40:01 +0200 Subject: [PATCH 3/4] Ability to provide callback function to provide stats for announce --- client.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client.js b/client.js index 44bf0822..4c99712c 100644 --- a/client.js +++ b/client.js @@ -54,6 +54,7 @@ function Client (peerId, port, torrent, opts) { self._rtcConfig = opts.rtcConfig self._wrtc = opts.wrtc self._userAgent = opts.userAgent + self._statsForAnnounce = opts.statsForAnnounce debug('new client %s', self._infoHashHex) @@ -263,8 +264,10 @@ 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 (opts.left == null && self.torrentLength != null) { opts.left = self.torrentLength - opts.downloaded From f8ea07ab9f5cdfd108eef84c87c5a7bac4706b2a Mon Sep 17 00:00:00 2001 From: Trym Skaar Date: Mon, 26 Oct 2015 03:41:40 +0100 Subject: [PATCH 4/4] Include key param in announce --- client.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/client.js b/client.js index 4c99712c..6a628c0f 100644 --- a/client.js +++ b/client.js @@ -54,6 +54,7 @@ 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) @@ -266,11 +267,13 @@ Client.prototype._defaultAnnounceOpts = function (opts) { 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 (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 }