diff --git a/index.js b/index.js index 1e80cbd..1688ccd 100644 --- a/index.js +++ b/index.js @@ -37,8 +37,15 @@ module.exports = function (metadata) { } ut_metadata.prototype.onExtendedHandshake = function (handshake) { + if (!handshake.m.ut_metadata) { + return this.emit('warning', new Error('Peer does not support ut_metadata')) + } + if (!handshake.metadata_size) { + return this.emit('warning', new Error('Peer does not have metadata')) + } + this._metadataSize = handshake.metadata_size - if (this._metadataSize && this._fetching) { + if (this._fetching) { this._requestPieces() } } @@ -51,7 +58,7 @@ module.exports = function (metadata) { dict = bncode.decode(str.substring(0, trailerIndex)) trailer = buf.slice(trailerIndex) } catch (err) { - this.emit('warning', new Error('Could not decode ut_metadata message')) + // drop invalid messages return } diff --git a/test/fetch.js b/test/fetch.js index 278cf83..8965f24 100644 --- a/test/fetch.js +++ b/test/fetch.js @@ -48,7 +48,7 @@ test('fetch()', function (t) { }) test('fetch() from peer without metadata', function (t) { - t.plan(1) + t.plan(2) var wire1 = new Protocol() var wire2 = new Protocol() @@ -69,6 +69,10 @@ test('fetch() from peer without metadata', function (t) { // extended handshake, so he doesn't have metadata } + wire2.ext('ut_metadata').on('warning', function (err) { + t.pass('got warning about peer missing metadata') + }) + wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.handshake(parsedTorrent.infoHash, id2) })