Skip to content

Commit e55896e

Browse files
committed
Add https support
1 parent dc533f1 commit e55896e

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

client.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var EventEmitter = require('events').EventEmitter
1111
var extend = require('extend.js')
1212
var hat = require('hat')
1313
var http = require('http')
14+
var https = require('https')
1415
var inherits = require('inherits')
1516
var once = require('once')
1617
var url = require('url')
@@ -52,7 +53,8 @@ function Client (peerId, port, torrent, opts) {
5253
if (typeof torrent.announce === 'string') torrent.announce = [ torrent.announce ]
5354
self._trackers = (torrent.announce || [])
5455
.filter(function (announceUrl) {
55-
return announceUrl.indexOf('udp://') === 0 || announceUrl.indexOf('http://') === 0
56+
return announceUrl.indexOf('udp://') === 0 || announceUrl.indexOf('http://') === 0 ||
57+
announceUrl.indexOf('https://') === 0
5658
})
5759
.map(function (announceUrl) {
5860
return new Tracker(self, announceUrl, self._opts)
@@ -152,7 +154,8 @@ function Tracker (client, announceUrl, opts) {
152154

153155
if (self._announceUrl.indexOf('udp://') === 0) {
154156
self._requestImpl = self._requestUdp
155-
} else if (self._announceUrl.indexOf('http://') === 0) {
157+
} else if (self._announceUrl.indexOf('http://') === 0 ||
158+
self._announceUrl.indexOf('https://') === 0) {
156159
self._requestImpl = self._requestHttp
157160
}
158161
}
@@ -265,9 +268,10 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) {
265268
}
266269
}
267270

271+
var protocol = url.parse(self._announceUrl).protocol
268272
var fullUrl = requestUrl + '?' + common.querystringStringify(opts)
269273

270-
var req = http.get(fullUrl, function (res) {
274+
var req = (protocol === 'http:' ? http : https).get(fullUrl, function (res) {
271275
if (res.statusCode !== 200) {
272276
res.resume() // consume the whole stream
273277
self.client.emit('warning', new Error('Invalid response code ' + res.statusCode + ' from tracker ' + requestUrl))

0 commit comments

Comments
 (0)