From e55896e2687ffbfac0c52ab46ca96f54ea32bc8e Mon Sep 17 00:00:00 2001 From: Anthony MOI Date: Thu, 22 Jan 2015 13:29:50 +0100 Subject: [PATCH 1/2] Add https support --- client.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/client.js b/client.js index 27703ee..c275ab8 100644 --- a/client.js +++ b/client.js @@ -11,6 +11,7 @@ var EventEmitter = require('events').EventEmitter var extend = require('extend.js') var hat = require('hat') var http = require('http') +var https = require('https') var inherits = require('inherits') var once = require('once') var url = require('url') @@ -52,7 +53,8 @@ function Client (peerId, port, torrent, opts) { if (typeof torrent.announce === 'string') torrent.announce = [ torrent.announce ] self._trackers = (torrent.announce || []) .filter(function (announceUrl) { - return announceUrl.indexOf('udp://') === 0 || announceUrl.indexOf('http://') === 0 + return announceUrl.indexOf('udp://') === 0 || announceUrl.indexOf('http://') === 0 || + announceUrl.indexOf('https://') === 0 }) .map(function (announceUrl) { return new Tracker(self, announceUrl, self._opts) @@ -152,7 +154,8 @@ function Tracker (client, announceUrl, opts) { if (self._announceUrl.indexOf('udp://') === 0) { self._requestImpl = self._requestUdp - } else if (self._announceUrl.indexOf('http://') === 0) { + } else if (self._announceUrl.indexOf('http://') === 0 || + self._announceUrl.indexOf('https://') === 0) { self._requestImpl = self._requestHttp } } @@ -265,9 +268,10 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) { } } + var protocol = url.parse(self._announceUrl).protocol var fullUrl = requestUrl + '?' + common.querystringStringify(opts) - var req = http.get(fullUrl, function (res) { + var req = (protocol === 'http:' ? http : https).get(fullUrl, function (res) { if (res.statusCode !== 200) { res.resume() // consume the whole stream self.client.emit('warning', new Error('Invalid response code ' + res.statusCode + ' from tracker ' + requestUrl)) From aee4211b7d13787e4dbaaed52603b91a9e03c598 Mon Sep 17 00:00:00 2001 From: Anthony MOI Date: Thu, 22 Jan 2015 14:00:24 +0100 Subject: [PATCH 2/2] use url to parse announceUrl --- client.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/client.js b/client.js index c275ab8..2b49bfd 100644 --- a/client.js +++ b/client.js @@ -53,8 +53,8 @@ function Client (peerId, port, torrent, opts) { if (typeof torrent.announce === 'string') torrent.announce = [ torrent.announce ] self._trackers = (torrent.announce || []) .filter(function (announceUrl) { - return announceUrl.indexOf('udp://') === 0 || announceUrl.indexOf('http://') === 0 || - announceUrl.indexOf('https://') === 0 + var protocol = url.parse(announceUrl).protocol + return protocol === 'udp:' || protocol === 'http:' || protocol === 'https:' }) .map(function (announceUrl) { return new Tracker(self, announceUrl, self._opts) @@ -152,10 +152,10 @@ function Tracker (client, announceUrl, opts) { self._intervalMs = self.client._intervalMs // use client interval initially self._interval = null - if (self._announceUrl.indexOf('udp://') === 0) { + var protocol = url.parse(self._announceUrl).protocol + if (protocol === 'udp:') { self._requestImpl = self._requestUdp - } else if (self._announceUrl.indexOf('http://') === 0 || - self._announceUrl.indexOf('https://') === 0) { + } else if (protocol === 'http:' || protocol === 'https:') { self._requestImpl = self._requestHttp } } @@ -271,7 +271,7 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) { var protocol = url.parse(self._announceUrl).protocol var fullUrl = requestUrl + '?' + common.querystringStringify(opts) - var req = (protocol === 'http:' ? http : https).get(fullUrl, function (res) { + var req = (protocol === 'https:' ? https : http).get(fullUrl, function (res) { if (res.statusCode !== 200) { res.resume() // consume the whole stream self.client.emit('warning', new Error('Invalid response code ' + res.statusCode + ' from tracker ' + requestUrl))