Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add a httpAgent options to http and websocket client trackers.
  • Loading branch information
yciabaud committed Aug 25, 2016
commit ee7783bb57dfbfac9e707b03131d5d10fb2a513d
2 changes: 2 additions & 0 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ inherits(Client, EventEmitter)
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
* @param {number} opts.rtcConfig RTCPeerConnection configuration object
* @param {number} opts.wrtc custom webrtc impl (useful in node.js)
* @param {object} opts.httpAgent HTTP agent impl (used to proxy http requests in node.js)
*/
function Client (opts) {
var self = this
Expand Down Expand Up @@ -62,6 +63,7 @@ function Client (opts) {

self._rtcConfig = opts.rtcConfig
self._wrtc = opts.wrtc
self._httpAgent = opts.httpAgent
self._getAnnounceOpts = opts.getAnnounceOpts

debug('new client %s', self.infoHash)
Expand Down
6 changes: 4 additions & 2 deletions lib/client/http-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ HTTPTracker.prototype.destroy = function (cb) {

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

get.concat(u, function (err, res, data) {
if (self.destroyed) return
Expand Down
2 changes: 1 addition & 1 deletion lib/client/websocket-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ WebSocketTracker.prototype._openSocket = function () {
if (self.socket) {
socketPool[self.announceUrl].consumers += 1
} else {
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl)
self.socket = socketPool[self.announceUrl] = new Socket(self.announceUrl, { agent: self.client._httpAgent })
self.socket.consumers = 1
self.socket.on('connect', self._onSocketConnectBound)
}
Expand Down
52 changes: 52 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Buffer = require('safe-buffer').Buffer
var Client = require('../')
var common = require('./common')
var fixtures = require('webtorrent-fixtures')
var http = require('http')
var test = require('tape')

var peerId1 = Buffer.from('01234567890123456789')
Expand Down Expand Up @@ -380,3 +381,54 @@ test('http: client announce with numwant', function (t) {
test('udp: client announce with numwant', function (t) {
testClientAnnounceWithNumWant(t, 'udp')
})

function testClientStartHttpAgent (t, serverType) {
t.plan(5)

common.createServer(t, serverType, function (server, announceUrl) {
var agent = new http.Agent()
var agentUsed = false
agent.createSocket = function (req, options) {
agentUsed = true
return http.Agent.prototype.createSocket.call(this, req, options)
}
var client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl,
peerId: peerId1,
port: port,
wrtc: {},
httpAgent: agent
})

if (serverType === 'ws') common.mockWebsocketTracker(client)
client.on('error', function (err) { t.error(err) })
client.on('warning', function (err) { t.error(err) })

client.once('update', function (data) {
t.equal(data.announce, announceUrl)
t.equal(typeof data.complete, 'number')
t.equal(typeof data.incomplete, 'number')

t.ok(agentUsed)

client.stop()

client.once('update', function () {
t.pass('got response to stop')
server.close()
client.destroy()
})
})

client.start()
})
}

test('http: client.start(httpAgent)', function (t) {
testClientStartHttpAgent(t, 'http')
})

test('ws: client.start(httpAgent)', function (t) {
testClientStartHttpAgent(t, 'ws')
})