Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
setMetadata (metadata) {
if (this._metadataComplete) return true
debug('set metadata')
// if full torrent dictionary was passed in, pull out just `info` key
try {
const info = bencode.decode(metadata).info
if (info) {
metadata = bencode.encode(info)
}
} catch (err) {}
// check hash
if (this._infoHash && this._infoHash !== sha1.sync(metadata)) {
return false
}
this.cancel()
this.metadata = metadata
this._metadataComplete = true
this._metadataSize = this.metadata.length
this._wire.extendedHandshake.metadata_size = this._metadataSize
function onMessage(bugout, identifier, wire, message) {
// hash to reference incoming message
var hash = toHex(nacl.hash(message).slice(16));
var t = now();
debug("raw message", identifier, message.length, hash);
if (!bugout.seen[hash]) {
var unpacked = bencode.decode(message);
// if this is an encrypted packet first try to decrypt it
if (unpacked.e && unpacked.n && unpacked.ek) {
var ek = unpacked.ek.toString();
debug("message encrypted by", ek, unpacked);
var decrypted = nacl.box.open(unpacked.e, unpacked.n, bs58.decode(ek), bugout.keyPairEncrypt.secretKey);
if (decrypted) {
unpacked = bencode.decode(decrypted);
} else {
unpacked = null;
}
}
// if there's no data decryption failed
if (unpacked && unpacked.p) {
debug("unpacked message", unpacked);
var packet = bencode.decode(unpacked.p);
var pk = packet.pk.toString();
onMessage (buf) {
let dict
let trailer
try {
const str = buf.toString()
const trailerIndex = str.indexOf('ee') + 2
dict = bencode.decode(str.substring(0, trailerIndex))
trailer = buf.slice(trailerIndex)
} catch (err) {
// drop invalid messages
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)
DHT.prototype._onData = function (data, rinfo) {
var self = this
var addr = rinfo.address + ':' + rinfo.port
try {
var message = bencode.decode(data)
if (!message) throw new Error('message is empty')
} catch (err) {
//console.log('bad message from ' + addr + ' ' + err.message)
//modif
spies.call(self,('receiving bad message from know spy '+addr),addr);
return
}
debug('got message from ' + addr + ' ' + JSON.stringify(message))
if (message.y) {
var type = message.y.toString()
}
//modif
spies.call(self,'receiving message from know spy '+addr+' message type '+type,addr);
Wire.prototype._onDone = function(metadata) {
try {
var info = bencode.decode(metadata).info;
if (info) {
metadata = bencode.encode(info);
}
}
catch (err) {
return;
}
var 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);
};
const onMessage = safe((message, rinfo) => {
const msg = bencode.decode(message);
const type = msg.y && Buffer.isBuffer(msg.y) ? msg.y.toString() : msg.y;
const query = msg.q && Buffer.isBuffer(msg.q) ? msg.q.toString() : msg.q;
if (type === 'r' && msg.r.nodes) {
onFindNodeResponse(msg.r.nodes);
} else if (type === 'q' && query === 'get_peers') {
onGetPeersRequest(msg, rinfo);
} else if (type === 'q' && query === 'announce_peer') {
onAnnouncePeerRequest(msg, rinfo);
}
});
parse(data, address) {
try {
const message = bencode.decode(data)
if (message.y.toString() == 'r' && message.r.nodes) {
this.onFoundNodes(message.r.nodes)
} else if (message.y.toString() == 'q') {
switch(message.q.toString()) {
case 'get_peers':
this.onGetPeersRequest(message, address)
break
case 'announce_peer':
this.onAnnouncePeerRequest(message, address)
break
case 'find_node':
this.onFindNodeRequest(message, address)
case 'ping':
this.onPingRequest(message, address)
break
}
export const processMessage = async ({ id, message, rinfo }, socket, knex) => {
try {
const decoded = bencode.decode(message);
const type = decoded.y ? decoded.y.toString('utf8') : undefined;
await addNode({ decoded, id, rinfo }, socket, knex);
if (type === 'q') {
await query({ decoded, id, message, rinfo }, socket, knex);
} else if (type === 'r') {
await response({ decoded, id, message, rinfo }, socket, knex);
} else if (type === 'e') {
await log(`Received an error message : ${decoded.e[0]} : ${decoded.e[1].toString('utf8')}`);
} else {
await handleUndefinedType({ decoded, id, message, rinfo, type }, socket, knex);
}
} catch (error) {
log(error, true);
}
onPiece(piece) {
const str = piece.toString();
const trailerIndex = str.indexOf('ee') + 2;
const dict = bencode.decode(str.substring(0, trailerIndex));
const trailer = piece.slice(trailerIndex);
if (dict.msg_type === 1 && trailer.length > PIECE_LENGTH) {
trailer.copy(this.metadata, dict.piece * PIECE_LENGTH);
this.bitfield.set(dict.piece);
this.checkDone();
}
}
Wire.prototype._onExtended = function(ext, buf) {
if (ext === 0) {
try {
this._onExtHandshake(bencode.decode(buf));
}
catch (err) {
this._fail();
}
}
else {
this._onPiece(buf);
}
};