Skip to content

Commit

Permalink
fix bug where client mangles tracker port
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
feross committed May 19, 2014
1 parent 67be622 commit e83328b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
11 changes: 4 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports.Server = Server
var BN = require('bn.js')
var bncode = require('bncode')
var compact2string = require('compact2string')
var concat = require('concat-stream')
var dgram = require('dgram')
var EventEmitter = require('events').EventEmitter
var extend = require('extend.js')
Expand Down Expand Up @@ -146,18 +147,14 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) {
var fullUrl = requestUrl + '?' + querystring.stringify(opts)

var req = http.get(fullUrl, function (res) {
var data = ''
if (res.statusCode !== 200) {
res.resume() // consume the whole stream
self.client.emit('error', new Error('Invalid response code ' + res.statusCode + ' from tracker ' + requestUrl))
return
}
res.on('data', function (chunk) {
data += chunk
})
res.on('end', function () {
res.pipe(concat(function (data) {
self._handleResponse(requestUrl, data)
})
}))
})

req.on('error', function (err) {
Expand Down Expand Up @@ -640,7 +637,6 @@ Server.prototype._onHttpRequest = function (req, res) {
if (warning) {
response['warning message'] = warning
}

res.end(bncode.encode(response))

} else if (s[0] === '/scrape') { // unofficial scrape message
Expand Down Expand Up @@ -698,6 +694,7 @@ Server.prototype._getPeersCompact = function (swarm) {
var peer = swarm.peers[peerId]
return peer.ip + ':' + peer.port
})

return string2compact(addrs)
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"bn.js": "^0.7.1",
"bncode": "^0.5.3",
"compact2string": "^1.2.0",
"concat-stream": "^1.4.5",
"extend.js": "0.0.1",
"hat": "0.0.3",
"inherits": "^2.0.1",
Expand Down

0 comments on commit e83328b

Please sign in to comment.