Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function error (message) {
debug('sent error %s', message)
res.end(bencode.encode({
'failure reason': message
}))
// even though it's an error for the client, it's just a warning for the server.
// don't crash the server because a client sent bad data :)
self.emit('warning', new Error(message))
}
}
ensure(torrent.info.name, 'info.name')
ensure(torrent.info['piece length'], 'info[\'piece length\']')
ensure(torrent.info.pieces, 'info.pieces')
if (torrent.info.files) {
torrent.info.files.forEach(function (file) {
ensure(typeof file.length === 'number', 'info.files[0].length')
ensure(file.path, 'info.files[0].path')
})
} else {
ensure(typeof torrent.info.length === 'number', 'info.length')
}
var result = {}
result.info = torrent.info
result.infoBuffer = bencode.encode(torrent.info)
result.infoHash = sha1.sync(result.infoBuffer)
result.name = torrent.info.name.toString()
result.private = !!torrent.info.private
if (torrent['creation date']) result.created = new Date(torrent['creation date'] * 1000)
if (Buffer.isBuffer(torrent.comment)) result.comment = torrent.comment.toString()
// announce/announce-list may be missing if metadata fetched via ut_metadata extension
var announce = torrent['announce-list']
if (!announce) {
if (torrent.announce) {
announce = [[torrent.announce]]
} else {
announce = []
ensure(torrent.info['name.utf-8'] || torrent.info.name, 'info.name')
ensure(torrent.info['piece length'], 'info[\'piece length\']')
ensure(torrent.info.pieces, 'info.pieces')
if (torrent.info.files) {
torrent.info.files.forEach(file => {
ensure(typeof file.length === 'number', 'info.files[0].length')
ensure(file['path.utf-8'] || file.path, 'info.files[0].path')
})
} else {
ensure(typeof torrent.info.length === 'number', 'info.length')
}
const result = {
info: torrent.info,
infoBuffer: bencode.encode(torrent.info),
name: (torrent.info['name.utf-8'] || torrent.info.name).toString(),
announce: []
}
result.infoHash = sha1.sync(result.infoBuffer)
result.infoHashBuffer = Buffer.from(result.infoHash, 'hex')
if (torrent.info.private !== undefined) result.private = !!torrent.info.private
if (torrent['creation date']) result.created = new Date(torrent['creation date'] * 1000)
if (torrent['created by']) result.createdBy = torrent['created by'].toString()
if (Buffer.isBuffer(torrent.comment)) result.comment = torrent.comment.toString()
// announce and announce-list will be missing if metadata fetched via ut_metadata
if (Array.isArray(torrent['announce-list']) && torrent['announce-list'].length > 0) {
_onDone(metadata){
try {
let info = bencode.decode(metadata).info;
if (info) {
metadata = bencode.encode(info);
}
}
catch (err) {
return;
}
let infohash = crypto.createHash('sha1').update(metadata).digest('hex');
if (this._infohash.toString('hex') !== infohash ) {
return false;
}
this.emit('metadata', {info: bencode.decode(metadata)}, this._infohash);
}
var token = a.token
if (!token) return
if (!this._validateToken(host, token)) {
return this._rpc.error(peer, query, [203, 'cannot `put` with bad token'])
}
if (v.length > 1000) {
return this._rpc.error(peer, query, [205, 'data payload too large'])
}
var isMutable = !!(a.k || a.sig)
if (isMutable && !a.k && !a.sig) return
var key = isMutable
? this._hash(a.salt ? Buffer.concat([a.k, a.salt]) : a.k)
: this._hash(bencode.encode(v))
var keyHex = key.toString('hex')
this.emit('put', key, v)
if (isMutable) {
if (!this._verify) return this._rpc.error(peer, query, [400, 'verification not supported'])
if (!this._verify(a.sig, encodeSigData(a), a.k)) return
var prev = this._values.get(keyHex)
if (prev && typeof a.cas === 'number' && prev.seq !== a.cas) {
return this._rpc.error(peer, query, [301, 'CAS mismatch, re-read and try again'])
}
if (prev && typeof prev.seq === 'number' && !(a.seq > prev.seq)) {
return this._rpc.error(peer, query, [302, 'sequence number less than current'])
}
this._values.set(keyHex, {v: v, k: a.k, salt: a.salt, sig: a.sig, seq: a.seq, id: id})
} else {
Wire.prototype._onDone = function(metadata) {
try {
var info = bencode.decode(metadata).info;
if (info) {
metadata = bencode.encode(info);
}
} catch (err) {
this._fail();
return;
}
var infohash = crypto
.createHash('sha1')
.update(metadata)
.digest('hex');
if (this._infohash.toString('hex') != infohash) {
this._fail();
return false;
}
this.emit('metadata', { info: bencode.decode(metadata) }, this._infohash);
};
_requestPiece(piece){
let msg = Buffer.concat([
new Buffer([BT_MSG_ID]),
new Buffer([this._ut_metadata]),
bencode.encode({msg_type: 0, piece: piece})
]);
this._sendMessage(msg);
}
_send (dict, trailer) {
let buf = bencode.encode(dict)
if (Buffer.isBuffer(trailer)) {
buf = Buffer.concat([buf, trailer])
}
this._wire.extended('ut_metadata', buf)
}
sendKRPC(msg, rinfo) {
var buf = bencode.encode(msg);
this.socket.send(buf, 0, buf.length, rinfo.port, rinfo.address);
}