-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.js
152 lines (108 loc) · 2.75 KB
/
messages.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var Types =
exports.Types = {
HELLO: 0x00,
CIPHER: 0x01,
READY: 0x02,
DATA: 0x04,
END: 0x07
},
VERSION =
exports.VERSION = 0x00,
HEADER_SIZE = 1,
FLAGS_REPLY = 0x01;
exports.read = function(buf) {
var header = buf.readUInt8(0),
msg = {
type: header >>> 5,
flags: header & 0x1F,
content: buf.slice(1)
};
return msg;
};
exports.writeHello = function(data) {
var buf = new Buffer(
HEADER_SIZE + 2 + (data.ciphers.length * 2)
);
writeHeader(buf, Types.HELLO);
buf.writeUInt8(VERSION, HEADER_SIZE);
var pos = HEADER_SIZE + 1;
buf.writeUInt8(data.ciphers.length, pos++);
for(var i in data.ciphers) {
buf.writeUInt16BE(data.ciphers[i], pos);
pos += 2;
}
return buf;
};
exports.parseHello = function(msg) {
if(msg.content.length < 2)
throw new Error('Buffer is too short');
msg.version = msg.content.readUInt8(0);
var ciphersLength = msg.content.readUInt8(1);
if(!ciphersLength)
throw new Error('Ciphers not found');
if(msg.content.length < 2 + ciphersLength * 2)
throw new Error('Buffer is too short');
msg.ciphers = [];
msg.ciphersMap = {};
for(var pos = 2; msg.ciphers.length < ciphersLength; pos += 2) {
var cipher = msg.content.readUInt16BE(pos);
msg.ciphers.push(cipher);
}
return msg;
};
exports.writeCipher = function(data) {
var flags = data.reply ? FLAGS_REPLY : 0,
buf = new Buffer(
HEADER_SIZE + 5 + data.cipherData.length
);
writeHeader(buf, Types.CIPHER, flags);
buf.writeUInt8(data.version, HEADER_SIZE);
buf.writeUInt16BE(data.cipher, HEADER_SIZE + 1);
buf.writeUInt16BE(data.cipherData.length, HEADER_SIZE + 3);
data.cipherData.copy(buf, HEADER_SIZE + 5);
return buf;
};
exports.parseCipher = function(msg) {
if(msg.content.length < 5)
throw new Error('Buffer is too short');
msg.reply = (msg.flags & FLAGS_REPLY) === FLAGS_REPLY;
msg.version = msg.content.readUInt8(0);
msg.cipher = msg.content.readUInt16BE(1);
var dataLength = msg.content.readUInt16BE(3),
end = 5 + dataLength;
if(msg.content.length < end)
throw new Error('Buffer is too short');
msg.cipherData = msg.content.slice(5, end);
return msg;
};
exports.writeReady = function(content) {
var buf = new Buffer(
HEADER_SIZE + content.length
);
writeHeader(buf, Types.READY);
content.copy(buf, HEADER_SIZE);
return buf;
};
exports.writeData = function(content) {
var buf = new Buffer(
HEADER_SIZE + content.length
);
writeHeader(buf, Types.DATA);
content.copy(buf, HEADER_SIZE);
return buf;
};
exports.writeEnd = function() {
var buf = new Buffer(
HEADER_SIZE
);
writeHeader(buf, Types.END);
return buf;
};
/*** local helpers ***/
function writeHeader(buf, type, flags) {
var header = (type << 5);
if(flags)
header |= flags;
buf.writeUInt8(header, 0);
return buf;
};