-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
240 lines (219 loc) · 6.34 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var crypto = require('crypto');
var chacha20 = require('chacha20');
// encode a packet
exports.encode = function(head, body)
{
// support different arg types
if(head === null) head = false; // grrrr
if(typeof head == 'number') head = new Buffer(String.fromCharCode(json));
if(typeof head == 'object')
{
// accept a packet as the first arg
if(Buffer.isBuffer(head.body) && body === undefined)
{
body = head.body;
head = head.head || head.json;
}
// serialize raw json
if(!Buffer.isBuffer(head))
{
head = new Buffer(JSON.stringify(head));
// require real json object
if(head.length < 7) head = false;
}
}
head = head || new Buffer(0);
if(typeof body == 'string') body = new Buffer(body, 'binary');
body = body || new Buffer(0);
var len = new Buffer(2);
len.writeInt16BE(head.length, 0);
return Buffer.concat([len, head, body]);
}
// packet decoding, add values to a buffer return
exports.decode =function(bin)
{
if(!bin) return undefined;
var buf = (typeof bin == 'string') ? new Buffer(bin, 'binary') : bin;
if(bin.length < 2) return undefined;
// read and validate the json length
var len = buf.readUInt16BE(0);
if(len > (buf.length - 2)) return undefined;
buf.head = buf.slice(2, len+2);
buf.body = buf.slice(len + 2);
// parse out the json
buf.json = {};
if(len >= 7)
{
try {
buf.json = JSON.parse(buf.head.toString('utf8'));
} catch(E) {
return undefined;
}
}
return buf;
}
// convenience to create a valid packet object
exports.packet = function(head, body)
{
return exports.decode(exports.encode(head, body));
}
exports.isPacket = function(packet)
{
if(!Buffer.isBuffer(packet)) return false;
if(packet.length < 2) return false;
if(typeof packet.json != 'object') return false;
if(!Buffer.isBuffer(packet.head)) return false;
if(!Buffer.isBuffer(packet.body)) return false;
return true;
}
// read a bytestream for a packet, decode the header and pass body through
var Transform = require('stream').Transform;
exports.stream = function(cbHead){
var stream = new Transform();
var buf = new Buffer(0);
stream._transform = function(data,enc,cbTransform)
{
// no buffer means pass everything through
if(!buf)
{
stream.push(data);
return cbTransform();
}
// gather until full header
buf = Buffer.concat([buf,data]);
var packet = exports.decode(buf);
if(!packet) return cbTransform();
buf = false; // pass through all future data
// give to the app
cbHead(packet, function(err){
if(err) return cbTransform(err);
stream.push(packet.body);
cbTransform();
});
}
return stream;
}
// chunking stream
var Duplex = require('stream').Duplex;
exports.chunking = function(args, cbPacket){
if(!args) args = {};
if(!cbPacket) cbPacket = function(err, packet){ };
// chunks can have space for 1 to 255 bytes
if(!args.size || args.size > 256) args.size = 256;
var space = args.size - 1;
if(space < 1) space = 1; // minimum
var blocked = false;
if(args.blocking) args.ack = true; // blocking requires acks
var stream = new Duplex({allowHalfOpen:false});
var queue = [];
// incoming chunked data coming from another stream
var chunks = new Buffer(0);
var data = new Buffer(0);
stream._write = function(data2,enc,cbWrite)
{
// trigger an error when http is detected, but otherwise continue
if(data.length == 0 && data2.slice(0,5).toString() == 'GET /')
{
cbPacket("HTTP detected",data2);
}
data = Buffer.concat([data,data2]);
while(data.length)
{
var len = data.readUInt8(0);
// packet done or ack
if(len === 0)
{
blocked = false;
if(chunks.length)
{
var packet = exports.decode(chunks);
chunks = new Buffer(0);
if(packet) cbPacket(false, packet);
}
data = data.slice(1);
continue;
}
// not a full chunk yet, wait for more
if(data.length < (len+1)) break;
// full chunk, buffer it up
blocked = false;
chunks = Buffer.concat([chunks,data.slice(1,len+1)]);
data = data.slice(len+1);
// ensure a response when enabled
if(args.ack)
{
if(!queue.length) queue.push(new Buffer("\0"));
}
}
stream.send(); // always try sending more data
cbWrite();
}
// accept packets to be chunked
stream.send = function(packet)
{
// break packet into chunks and add to queue
while(packet)
{
var len = new Buffer(1);
var chunk = packet.slice(0,space);
packet = packet.slice(chunk.length);
len.writeUInt8(chunk.length,0);
// check if we can include the packet terminating zero
var zero = new Buffer(0);
if(packet.length == 0 && chunk.length <= space)
{
zero = new Buffer("\0");
packet = false;
}
queue.push(Buffer.concat([len,chunk,zero]));
}
// pull next chunk off the queue
if(queue.length && !blocked)
{
var chunk = queue.shift();
if(args.blocking && chunk.length > 1) blocked = true;
if(stream.push(chunk)) stream.send(); // let the loop figure itself out
}
}
// try sending more chunks
stream._read = function(size)
{
stream.send();
}
return stream;
}
function keyize(key)
{
if(!key) key = "telehash";
if(Buffer.isBuffer(key) && key.length == 32) return key;
return crypto.createHash('sha256').update(key).digest();
}
exports.cloak = function(packet, key, rounds)
{
if(!(key = keyize(key)) || !Buffer.isBuffer(packet)) return undefined;
if(!rounds) rounds = 1;
// get a non-zero start
while(1)
{
var nonce = crypto.randomBytes(8);
if(nonce[0] == 0) continue;
break;
}
var cloaked = Buffer.concat([nonce, chacha20.encrypt(key, nonce, packet)]);
rounds--;
return (rounds) ? exports.cloak(cloaked, key, rounds) : cloaked;
}
exports.decloak = function(cloaked, key, rounds)
{
if(!(key = keyize(key)) || !Buffer.isBuffer(cloaked) || cloaked.length < 2) return undefined;
if(!rounds) rounds = 0;
if(cloaked[0] == 0)
{
var packet = exports.decode(cloaked);
if(packet) packet.cloaked = rounds;
return packet;
}
if(cloaked.length < 10) return undefined; // must have cloak and a minimum packet
rounds++;
return exports.decloak(chacha20.decrypt(key, cloaked.slice(0,8), cloaked.slice(8)), key, rounds);
}