2014-06-12 01:56:20 +01:00
|
|
|
var bencode = require('bencode')
|
2014-07-02 23:23:48 +01:00
|
|
|
var BitField = require('bitfield')
|
2014-03-23 03:34:50 +00:00
|
|
|
var EventEmitter = require('events').EventEmitter
|
|
|
|
var inherits = require('inherits')
|
2014-09-15 17:10:50 +01:00
|
|
|
var sha1 = require('git-sha1')
|
2014-03-23 03:34:50 +00:00
|
|
|
|
2014-08-17 11:06:45 +01:00
|
|
|
var MAX_METADATA_SIZE = 10000000 // 10MB
|
2014-04-20 02:26:19 +01:00
|
|
|
var BITFIELD_GROW = 1000
|
2014-03-23 03:34:50 +00:00
|
|
|
var PIECE_LENGTH = 16 * 1024
|
|
|
|
|
|
|
|
module.exports = function (metadata) {
|
|
|
|
|
|
|
|
inherits(ut_metadata, EventEmitter)
|
|
|
|
|
|
|
|
function ut_metadata (wire) {
|
|
|
|
EventEmitter.call(this)
|
|
|
|
|
|
|
|
this._wire = wire
|
|
|
|
|
|
|
|
this._metadataComplete = false
|
|
|
|
this._metadataSize = null
|
|
|
|
this._remainingRejects = null // how many reject messages to tolerate before quitting
|
|
|
|
this._fetching = false
|
2014-04-20 02:12:44 +01:00
|
|
|
|
2014-04-20 02:26:19 +01:00
|
|
|
// The largest .torrent file that I know of is ~1-2MB, which is ~100 pieces.
|
2014-08-17 11:06:45 +01:00
|
|
|
// Therefore, cap the bitfield to 10x that (1000 pieces) so a malicious peer can't
|
|
|
|
// make it grow to fill all memory.
|
2014-04-20 02:12:44 +01:00
|
|
|
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
|
2014-03-23 03:34:50 +00:00
|
|
|
|
2014-03-28 06:36:04 +00:00
|
|
|
if (Buffer.isBuffer(metadata)) {
|
2014-06-04 07:11:04 +01:00
|
|
|
this.setMetadata(metadata)
|
2014-03-23 03:34:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 06:17:08 +00:00
|
|
|
// Name of the bittorrent-protocol extension
|
2014-06-04 07:43:05 +01:00
|
|
|
ut_metadata.prototype.name = 'ut_metadata'
|
|
|
|
|
2014-03-23 05:15:57 +00:00
|
|
|
ut_metadata.prototype.onHandshake = function (infoHash, peerId, extensions) {
|
|
|
|
this._infoHash = infoHash
|
2014-09-15 17:10:50 +01:00
|
|
|
this._infoHashHex = infoHash.toString('hex')
|
2014-03-23 03:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype.onExtendedHandshake = function (handshake) {
|
2014-05-15 11:31:42 +01:00
|
|
|
if (!handshake.m || !handshake.m.ut_metadata) {
|
2014-03-23 04:10:23 +00:00
|
|
|
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'))
|
|
|
|
}
|
|
|
|
|
2014-08-17 11:06:45 +01:00
|
|
|
if (handshake.metadata_size > MAX_METADATA_SIZE) {
|
|
|
|
return this.emit('warning', new Error('Peer gave maliciously large metadata size'))
|
|
|
|
}
|
|
|
|
|
2014-03-23 03:34:50 +00:00
|
|
|
this._metadataSize = handshake.metadata_size
|
2014-03-23 05:15:57 +00:00
|
|
|
this._numPieces = Math.ceil(this._metadataSize / PIECE_LENGTH)
|
|
|
|
this._remainingRejects = this._numPieces * 2
|
|
|
|
|
2014-03-23 04:10:23 +00:00
|
|
|
if (this._fetching) {
|
2014-03-23 03:34:50 +00:00
|
|
|
this._requestPieces()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype.onMessage = function (buf) {
|
|
|
|
var dict, trailer
|
|
|
|
try {
|
|
|
|
var str = buf.toString()
|
|
|
|
var trailerIndex = str.indexOf('ee') + 2
|
2014-06-12 01:56:20 +01:00
|
|
|
dict = bencode.decode(str.substring(0, trailerIndex))
|
2014-03-23 03:34:50 +00:00
|
|
|
trailer = buf.slice(trailerIndex)
|
|
|
|
} catch (err) {
|
2014-03-23 04:10:23 +00:00
|
|
|
// drop invalid messages
|
2014-03-23 03:34:50 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (dict.msg_type) {
|
|
|
|
case 0:
|
|
|
|
// ut_metadata request (from peer)
|
|
|
|
// example: { 'msg_type': 0, 'piece': 0 }
|
|
|
|
this._onRequest(dict.piece)
|
|
|
|
break
|
|
|
|
case 1:
|
|
|
|
// ut_metadata data (in response to our request)
|
|
|
|
// example: { 'msg_type': 1, 'piece': 0, 'total_size': 3425 }
|
|
|
|
this._onData(dict.piece, trailer, dict.total_size)
|
|
|
|
break
|
|
|
|
case 2:
|
|
|
|
// ut_metadata reject (peer doesn't have piece we requested)
|
|
|
|
// { 'msg_type': 2, 'piece': 0 }
|
|
|
|
this._onReject(dict.piece)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 05:17:25 +01:00
|
|
|
/**
|
|
|
|
* Ask the peer to send metadata.
|
|
|
|
* @public
|
|
|
|
*/
|
2014-03-23 03:34:50 +00:00
|
|
|
ut_metadata.prototype.fetch = function () {
|
|
|
|
if (this._metadataComplete) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this._fetching = true
|
|
|
|
if (this._metadataSize) {
|
|
|
|
this._requestPieces()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 05:17:25 +01:00
|
|
|
/**
|
|
|
|
* Stop asking the peer to send metadata.
|
|
|
|
* @public
|
|
|
|
*/
|
2014-03-23 03:34:50 +00:00
|
|
|
ut_metadata.prototype.cancel = function () {
|
|
|
|
this._fetching = false
|
|
|
|
}
|
|
|
|
|
2014-06-04 07:11:04 +01:00
|
|
|
ut_metadata.prototype.setMetadata = function (metadata) {
|
|
|
|
if (this._metadataComplete) return true
|
|
|
|
|
|
|
|
// if full torrent dictionary was passed in, pull out just `info` key
|
|
|
|
try {
|
2014-06-12 01:56:20 +01:00
|
|
|
var info = bencode.decode(metadata).info
|
2014-06-04 07:11:04 +01:00
|
|
|
if (info) {
|
2014-06-12 01:56:20 +01:00
|
|
|
metadata = bencode.encode(info)
|
2014-06-04 07:11:04 +01:00
|
|
|
}
|
|
|
|
} catch (err) {}
|
|
|
|
|
|
|
|
// check hash
|
2014-09-15 17:10:50 +01:00
|
|
|
if (this._infoHashHex && this._infoHashHex !== sha1(metadata)) {
|
2014-06-04 07:11:04 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cancel()
|
|
|
|
|
|
|
|
this.metadata = metadata
|
2014-06-04 05:17:25 +01:00
|
|
|
this._metadataComplete = true
|
|
|
|
this._metadataSize = this.metadata.length
|
|
|
|
this._wire.extendedHandshake.metadata_size = this._metadataSize
|
2014-06-04 07:11:04 +01:00
|
|
|
|
2014-06-12 01:56:20 +01:00
|
|
|
this.emit('metadata', bencode.encode({ info: bencode.decode(this.metadata) }))
|
2014-06-04 07:11:04 +01:00
|
|
|
|
|
|
|
return true
|
2014-06-04 05:17:25 +01:00
|
|
|
}
|
|
|
|
|
2014-03-23 03:34:50 +00:00
|
|
|
ut_metadata.prototype._send = function (dict, trailer) {
|
2014-06-12 01:56:20 +01:00
|
|
|
var buf = bencode.encode(dict)
|
2014-03-23 03:34:50 +00:00
|
|
|
if (Buffer.isBuffer(trailer)) {
|
|
|
|
buf = Buffer.concat([buf, trailer])
|
|
|
|
}
|
|
|
|
this._wire.extended('ut_metadata', buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype._request = function (piece) {
|
2014-03-23 05:36:12 +00:00
|
|
|
this._send({ msg_type: 0, piece: piece })
|
2014-03-23 03:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype._data = function (piece, buf, totalSize) {
|
2014-03-23 05:36:12 +00:00
|
|
|
var msg = { msg_type: 1, piece: piece }
|
2014-03-23 03:34:50 +00:00
|
|
|
if (typeof totalSize === 'number') {
|
|
|
|
msg.total_size = totalSize
|
|
|
|
}
|
|
|
|
this._send(msg, buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype._reject = function (piece) {
|
2014-03-23 05:36:12 +00:00
|
|
|
this._send({ msg_type: 2, piece: piece })
|
2014-03-23 03:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype._onRequest = function (piece) {
|
2014-04-23 14:38:50 +01:00
|
|
|
if (!this._metadataComplete) {
|
2014-06-04 05:17:16 +01:00
|
|
|
this._reject(piece)
|
2014-04-23 14:38:50 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var start = piece * PIECE_LENGTH
|
|
|
|
var end = start + PIECE_LENGTH
|
|
|
|
if (end > this._metadataSize) {
|
|
|
|
end = this._metadataSize
|
2014-03-23 03:34:50 +00:00
|
|
|
}
|
2014-04-23 14:38:50 +01:00
|
|
|
var buf = this.metadata.slice(start, end)
|
|
|
|
this._data(piece, buf, this._metadataSize)
|
2014-03-23 03:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype._onData = function (piece, buf, totalSize) {
|
|
|
|
if (buf.length > PIECE_LENGTH) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buf.copy(this.metadata, piece * PIECE_LENGTH)
|
|
|
|
this._bitfield.set(piece)
|
|
|
|
this._checkDone()
|
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype._onReject = function (piece) {
|
|
|
|
if (this._remainingRejects > 0 && this._fetching) {
|
|
|
|
// If we haven't been rejected too much, then try to request the piece again
|
|
|
|
this._request(piece)
|
|
|
|
this._remainingRejects -= 1
|
|
|
|
} else {
|
|
|
|
this.emit('warning', new Error('Peer sent "reject" too much'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype._requestPieces = function () {
|
|
|
|
this.metadata = new Buffer(this._metadataSize)
|
|
|
|
|
|
|
|
for (var piece = 0; piece < this._numPieces; piece++) {
|
|
|
|
this._request(piece)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ut_metadata.prototype._checkDone = function () {
|
|
|
|
var done = true
|
|
|
|
for (var piece = 0; piece < this._numPieces; piece++) {
|
|
|
|
if (!this._bitfield.get(piece)) {
|
|
|
|
done = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!done) return
|
|
|
|
|
2014-06-04 07:11:04 +01:00
|
|
|
// attempt to set metadata -- may fail sha1 check
|
|
|
|
var success = this.setMetadata(this.metadata)
|
2014-05-07 20:09:39 +01:00
|
|
|
|
2014-06-04 07:11:04 +01:00
|
|
|
if (!success) {
|
2014-03-23 05:15:57 +00:00
|
|
|
this._failedMetadata()
|
|
|
|
}
|
|
|
|
}
|
2014-03-23 03:34:50 +00:00
|
|
|
|
2014-03-23 05:15:57 +00:00
|
|
|
ut_metadata.prototype._failedMetadata = function () {
|
2014-04-20 02:12:44 +01:00
|
|
|
// reset bitfield & try again
|
|
|
|
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
|
2014-03-23 05:15:57 +00:00
|
|
|
this._remainingRejects -= this._numPieces
|
|
|
|
if (this._remainingRejects > 0) {
|
|
|
|
this._requestPieces()
|
|
|
|
} else {
|
|
|
|
this.emit('warning', new Error('Peer sent invalid metadata'))
|
|
|
|
}
|
2014-03-23 03:34:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ut_metadata
|
|
|
|
}
|