Skip to content

Commit e83328b

Browse files
committed
fix bug where client mangles tracker port
The client was converting the binary (compact) response to a utf8 string which would modify the data in some cases. Keeping it as a buffer the whole time solves the issue. Fixes webtorrent#19.
1 parent 67be622 commit e83328b

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports.Server = Server
44
var BN = require('bn.js')
55
var bncode = require('bncode')
66
var compact2string = require('compact2string')
7+
var concat = require('concat-stream')
78
var dgram = require('dgram')
89
var EventEmitter = require('events').EventEmitter
910
var extend = require('extend.js')
@@ -146,18 +147,14 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) {
146147
var fullUrl = requestUrl + '?' + querystring.stringify(opts)
147148

148149
var req = http.get(fullUrl, function (res) {
149-
var data = ''
150150
if (res.statusCode !== 200) {
151151
res.resume() // consume the whole stream
152152
self.client.emit('error', new Error('Invalid response code ' + res.statusCode + ' from tracker ' + requestUrl))
153153
return
154154
}
155-
res.on('data', function (chunk) {
156-
data += chunk
157-
})
158-
res.on('end', function () {
155+
res.pipe(concat(function (data) {
159156
self._handleResponse(requestUrl, data)
160-
})
157+
}))
161158
})
162159

163160
req.on('error', function (err) {
@@ -640,7 +637,6 @@ Server.prototype._onHttpRequest = function (req, res) {
640637
if (warning) {
641638
response['warning message'] = warning
642639
}
643-
644640
res.end(bncode.encode(response))
645641

646642
} else if (s[0] === '/scrape') { // unofficial scrape message
@@ -698,6 +694,7 @@ Server.prototype._getPeersCompact = function (swarm) {
698694
var peer = swarm.peers[peerId]
699695
return peer.ip + ':' + peer.port
700696
})
697+
701698
return string2compact(addrs)
702699
}
703700

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"bn.js": "^0.7.1",
1515
"bncode": "^0.5.3",
1616
"compact2string": "^1.2.0",
17+
"concat-stream": "^1.4.5",
1718
"extend.js": "0.0.1",
1819
"hat": "0.0.3",
1920
"inherits": "^2.0.1",

0 commit comments

Comments
 (0)