2024-08-23 14:13:13 +03:00

198 lines
4.1 KiB
JavaScript

var BufferUtils = require('./util/bufferutils');
var Message = function(code, payload) {
this.code = code;
this.payload = payload;
};
'use strict';
const Buffer = require('buffer').Buffer;
const torrentParser = require('./torrent-parser');
const util = require('./util');
module.exports.buildHandshake = torrent => {
const buf = Buffer.alloc(68);
// pstrlen
buf.writeUInt8(19, 0);
// pstr
buf.write('BitTorrent protocol', 1);
// reserved
buf.writeUInt32BE(0, 20);
buf.writeUInt32BE(0, 24);
// info hash
torrentParser.infoHash(torrent).copy(buf, 28);
// peer id
util.genId().copy(buf, 48);
return buf;
};
module.exports.buildKeepAlive = () => Buffer.alloc(4);
module.exports.buildChoke = () => {
const buf = Buffer.alloc(5);
// length
buf.writeUInt32BE(1, 0);
// id
buf.writeUInt8(0, 4);
return buf;
};
module.exports.buildUnchoke = () => {
const buf = Buffer.alloc(5);
// length
buf.writeUInt32BE(1, 0);
// id
buf.writeUInt8(1, 4);
return buf;
};
module.exports.buildInterested = () => {
const buf = Buffer.alloc(5);
// length
buf.writeUInt32BE(1, 0);
// id
buf.writeUInt8(2, 4);
return buf;
};
module.exports.buildUninterested = () => {
const buf = Buffer.alloc(5);
// length
buf.writeUInt32BE(1, 0);
// id
buf.writeUInt8(3, 4);
return buf;
};
module.exports.buildHave = payload => {
const buf = Buffer.alloc(9);
// length
buf.writeUInt32BE(5, 0);
// id
buf.writeUInt8(4, 4);
// piece index
buf.writeUInt32BE(payload, 5);
return buf;
};
module.exports.buildBitfield = bitfield => {
const buf = Buffer.alloc(bitfield.length + 1 + 4);
// length
buf.writeUInt32BE(payload.length + 1, 0);
// id
buf.writeUInt8(5, 4);
// bitfield
bitfield.copy(buf, 5);
return buf;
};
module.exports.buildRequest = payload => {
const buf = Buffer.alloc(17);
// length
buf.writeUInt32BE(13, 0);
// id
buf.writeUInt8(6, 4);
// piece index
buf.writeUInt32BE(payload.index, 5);
// begin
buf.writeUInt32BE(payload.begin, 9);
// length
buf.writeUInt32BE(payload.length, 13);
return buf;
};
module.exports.buildPiece = payload => {
const buf = Buffer.alloc(payload.block.length + 13);
// length
buf.writeUInt32BE(payload.block.length + 9, 0);
// id
buf.writeUInt8(7, 4);
// piece index
buf.writeUInt32BE(payload.index, 5);
// begin
buf.writeUInt32BE(payload.begin, 9);
// block
payload.block.copy(buf, 13);
return buf;
};
module.exports.buildCancel = payload => {
const buf = Buffer.alloc(17);
// length
buf.writeUInt32BE(13, 0);
// id
buf.writeUInt8(8, 4);
// piece index
buf.writeUInt32BE(payload.index, 5);
// begin
buf.writeUInt32BE(payload.begin, 9);
// length
buf.writeUInt32BE(payload.length, 13);
return buf;
};
module.exports.buildPort = payload => {
const buf = Buffer.alloc(7);
// length
buf.writeUInt32BE(3, 0);
// id
buf.writeUInt8(9, 4);
// listen-port
buf.writeUInt16BE(payload, 5);
return buf;
};
module.exports.parse = msg => {
const id = msg.length > 4 ? msg.readInt8(4) : null;
let payload = msg.length > 5 ? msg.slice(5) : null;
if (id === 6 || id === 7 || id === 8) {
const rest = payload.slice(8);
payload = {
index: payload.readInt32BE(0),
begin: payload.readInt32BE(4)
};
payload[id === 7 ? 'block' : 'length'] = rest;
}
return {
size : msg.readInt32BE(0),
id : id,
payload : payload
}
};
Message.prototype.writeTo = function(stream) {
if (this.code === Message.KEEPALIVE) {
stream.write(BufferUtils.fromInt(0));
} else {
var length = 1 + (this.payload ? this.payload.length : 0);
stream.write(BufferUtils.fromInt(length));
var code = Buffer.alloc(1);
code[0] = this.code;
stream.write(code);
if (this.payload) {
stream.write(this.payload);
}
}
};
Message.KEEPALIVE = -1;
Message.CHOKE = 0;
Message.UNCHOKE = 1;
Message.INTERESTED = 2;
Message.UNINTERESTED = 3;
Message.HAVE = 4;
Message.BITFIELD = 5;
Message.REQUEST = 6;
Message.PIECE = 7;
Message.CANCEL = 8;
Message.PORT = 9;
Message.EXTENDED = 20;
Message.EXTENDED_HANDSHAKE = 'nt_handshake';
Message.EXTENDED_METADATA = 'ut_metadata';
module.exports = Message;