-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgBuilder.js
More file actions
116 lines (93 loc) · 2.62 KB
/
Copy pathmsgBuilder.js
File metadata and controls
116 lines (93 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const {createInfoHash,getPeerId} = require('./utils');
let message;
module.exports.handshake = () => {
if(!message){
let pstr = 'BitTorrent protocol';
let buff = Buffer.allocUnsafe(49+pstr.length);
let infoHash = createInfoHash();
let peerId = getPeerId();
buff.writeUInt8(pstr.length,0);
buff.write(pstr,1,pstr.length);
buff.writeUInt32BE(0,pstr.length+1);
buff.writeUInt32BE(0,pstr.length+5);
infoHash.copy(buff,pstr.length+9);
peerId.copy(buff,pstr.length+9+infoHash.length);
message = buff;
return message;
}return message;
}
module.exports.keepAlive = () => {
return Buffer.alloc(4);
}
module.exports.choke = () => {
let buff = Buffer.alloc(5);
buff.writeUInt32BE(1,0);
buff.writeUInt8(0,4);
return buff;
}
module.exports.unchoke = () => {
let buff = Buffer.alloc(5);
buff.writeUInt32BE(1,0);
buff.writeUInt8(1,4);
return buff;
}
module.exports.intrested = () => {
let buff = Buffer.alloc(5);
buff.writeUInt32BE(1,0);
buff.writeUInt8(2,4);
return buff;
}
module.exports.notIntrested = () => {
let buff = Buffer.alloc(5);
buff.writeUInt32BE(1,0);
buff.writeUInt8(3,4);
return buff;
}
module.exports.have = (pieceIndex) => {
let buff = Buffer.alloc(9);
buff.writeUInt32BE(5,0);
buff.writeUInt8(4,4);
buff.writeUInt32BE(pieceIndex,5);
return buff;
}
module.exports.bitField = (bitfield) => {
let buff = Buffer.alloc(5+bitfield.length);
buff.writeUInt32BE(1+bitfield.length,0);
buff.writeUInt8(5,4);
bitField.copy(buff,5);
return buff;
}
module.exports.request = (index,begin,length) => {
let buff = Buffer.alloc(17);
buff.writeUInt32BE(13,0);
buff.writeUInt8(6,4);
buff.writeUInt32BE(index,5);
buff.writeUInt32BE(begin,9);
buff.writeUInt32BE(length,13);
return buff;
}
module.exports.piece = (index,begin,block) => {
let buff = Buffer.alloc(13+block.length);
buff.writeUInt32BE(9+block.length,0);
buff.writeUInt8(7,4);
buff.writeUInt32BE(index,5);
buff.writeUInt32BE(begin,9);
block.copy(buff,13);
return buff;
}
module.exports.cancel = (index,begin,length) => {
let buff = Buffer.from(17);
buff.writeUInt32BE(13,0);
buff.writeUInt8(9,4);
buff.writeUInt32BE(index,5);
buff.writeUInt32BE(begin,9);
buff.writeUInt32BE(length,13);
return buff;
}
module.exports.port = (listenPort) => {
let buff = Buffer.alloc(7);
buff.writeUInt32BE(3,0);
buff.writeUInt8(9,4);
buff.writeUInt16BE(listenPort,5);
return buff;
}