fixed buggy metadata vs metadata.info

This commit is contained in:
fisch0920 2014-05-09 07:11:14 -04:00
parent 1fde6fefbc
commit bc3d1ea668
3 changed files with 17 additions and 5 deletions

View File

@ -31,7 +31,17 @@ module.exports = function (metadata) {
this._bitfield = new BitField(0, { grow: BITFIELD_GROW }) this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
if (Buffer.isBuffer(metadata)) { if (Buffer.isBuffer(metadata)) {
this._gotMetadata(metadata) var info = null
try {
// if buffer fails to decode or there is no info key, then metadata is corrupt
info = bncode.encode(bncode.decode(metadata).info)
} catch (err) {
// TODO: throw or disregard invalid metadata?
//throw new Error('ut_metadata constructed with invalid metadata')
}
if (info)
this._gotMetadata(info)
} }
} }
@ -185,7 +195,7 @@ module.exports = function (metadata) {
// check hash // check hash
if (sha1(this.metadata) === this._infoHash.toString('hex')) { if (sha1(this.metadata) === this._infoHash.toString('hex')) {
this._gotMetadata(bncode.encode({ info: bncode.decode(this.metadata) })) this._gotMetadata(this.metadata)
} else { } else {
this._failedMetadata() this._failedMetadata()
} }
@ -197,7 +207,7 @@ module.exports = function (metadata) {
this._metadataComplete = true this._metadataComplete = true
this._metadataSize = this.metadata.length this._metadataSize = this.metadata.length
this._wire.extendedHandshake.metadata_size = this._metadataSize this._wire.extendedHandshake.metadata_size = this._metadataSize
this.emit('metadata', this.metadata) this.emit('metadata', bncode.encode({ info: bncode.decode(this.metadata) }))
} }
ut_metadata.prototype._failedMetadata = function () { ut_metadata.prototype._failedMetadata = function () {

View File

@ -1,6 +1,7 @@
var fs = require('fs') var fs = require('fs')
var Protocol = require('bittorrent-protocol') var Protocol = require('bittorrent-protocol')
var ut_metadata = require('../') var ut_metadata = require('../')
var bncode = require('bncode')
var test = require('tape') var test = require('tape')
// Used in multiple tests // Used in multiple tests
@ -28,6 +29,6 @@ test('wire.use(ut_metadata(metadata))', function (t) {
t.ok(wire.ut_metadata) t.ok(wire.ut_metadata)
t.ok(wire.ut_metadata.fetch) t.ok(wire.ut_metadata.fetch)
t.ok(wire.ut_metadata.cancel) t.ok(wire.ut_metadata.cancel)
t.equal(wire.ut_metadata.metadata, metadata) t.equal(wire.ut_metadata.metadata.toString('hex'), bncode.encode(bncode.decode(metadata).info).toString('hex'))
t.end() t.end()
}) })

View File

@ -2,6 +2,7 @@ var fs = require('fs')
var parseTorrent = require('parse-torrent') var parseTorrent = require('parse-torrent')
var Protocol = require('bittorrent-protocol') var Protocol = require('bittorrent-protocol')
var ut_metadata = require('../') var ut_metadata = require('../')
var bncode = require('bncode')
var test = require('tape') var test = require('tape')
// Used in multiple tests // Used in multiple tests
@ -24,7 +25,7 @@ test('fetch()', function (t) {
wire2.ut_metadata.on('metadata', function (_metadata) { wire2.ut_metadata.on('metadata', function (_metadata) {
// got metadata! // got metadata!
t.deepEqual(_metadata, metadata) t.equal(_metadata.toString('hex'), bncode.encode({ info: bncode.decode(metadata).info }).toString('hex'))
}) })
wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.on('handshake', function (infoHash, peerId, extensions) {