Skip to content

Commit ee7783b

Browse files
committed
Add a httpAgent options to http and websocket client trackers.
1 parent 8135ca3 commit ee7783b

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

client.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ inherits(Client, EventEmitter)
3131
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
3232
* @param {number} opts.rtcConfig RTCPeerConnection configuration object
3333
* @param {number} opts.wrtc custom webrtc impl (useful in node.js)
34+
* @param {object} opts.httpAgent HTTP agent impl (used to proxy http requests in node.js)
3435
*/
3536
function Client (opts) {
3637
var self = this
@@ -62,6 +63,7 @@ function Client (opts) {
6263

6364
self._rtcConfig = opts.rtcConfig
6465
self._wrtc = opts.wrtc
66+
self._httpAgent = opts.httpAgent
6567
self._getAnnounceOpts = opts.getAnnounceOpts
6668

6769
debug('new client %s', self.infoHash)

lib/client/http-tracker.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ HTTPTracker.prototype.destroy = function (cb) {
8787

8888
HTTPTracker.prototype._request = function (requestUrl, params, cb) {
8989
var self = this
90-
var u = requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') +
91-
common.querystringStringify(params)
90+
var u = {
91+
url: requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') + common.querystringStringify(params),
92+
agent: self.client._httpAgent
93+
}
9294

9395
get.concat(u, function (err, res, data) {
9496
if (self.destroyed) return

lib/client/websocket-tracker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ WebSocketTracker.prototype._openSocket = function () {
167167
if (self.socket) {
168168
socketPool[self.announceUrl].consumers += 1
169169
} else {
170-
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
170+
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl, { agent: self.client._httpAgent })
171171
self.socket.consumers = 1
172172
self.socket.on('connect', self._onSocketConnectBound)
173173
}

test/client.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var Buffer = require('safe-buffer').Buffer
22
var Client = require('../')
33
var common = require('./common')
44
var fixtures = require('webtorrent-fixtures')
5+
var http = require('http')
56
var test = require('tape')
67

78
var peerId1 = Buffer.from('01234567890123456789')
@@ -380,3 +381,54 @@ test('http: client announce with numwant', function (t) {
380381
test('udp: client announce with numwant', function (t) {
381382
testClientAnnounceWithNumWant(t, 'udp')
382383
})
384+
385+
function testClientStartHttpAgent (t, serverType) {
386+
t.plan(5)
387+
388+
common.createServer(t, serverType, function (server, announceUrl) {
389+
var agent = new http.Agent()
390+
var agentUsed = false
391+
agent.createSocket = function (req, options) {
392+
agentUsed = true
393+
return http.Agent.prototype.createSocket.call(this, req, options)
394+
}
395+
var client = new Client({
396+
infoHash: fixtures.leaves.parsedTorrent.infoHash,
397+
announce: announceUrl,
398+
peerId: peerId1,
399+
port: port,
400+
wrtc: {},
401+
httpAgent: agent
402+
})
403+
404+
if (serverType === 'ws') common.mockWebsocketTracker(client)
405+
client.on('error', function (err) { t.error(err) })
406+
client.on('warning', function (err) { t.error(err) })
407+
408+
client.once('update', function (data) {
409+
t.equal(data.announce, announceUrl)
410+
t.equal(typeof data.complete, 'number')
411+
t.equal(typeof data.incomplete, 'number')
412+
413+
t.ok(agentUsed)
414+
415+
client.stop()
416+
417+
client.once('update', function () {
418+
t.pass('got response to stop')
419+
server.close()
420+
client.destroy()
421+
})
422+
})
423+
424+
client.start()
425+
})
426+
}
427+
428+
test('http: client.start(httpAgent)', function (t) {
429+
testClientStartHttpAgent(t, 'http')
430+
})
431+
432+
test('ws: client.start(httpAgent)', function (t) {
433+
testClientStartHttpAgent(t, 'ws')
434+
})

0 commit comments

Comments
 (0)