2016-09-22 17:06:34 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var sni = require('sni');
|
|
|
|
var machine = require('./machine.js');
|
|
|
|
var hello = require('fs').readFileSync('./sni.hello.bin');
|
|
|
|
var version = 1;
|
2016-09-22 18:01:57 +00:00
|
|
|
var header = 'IPv4,127.0.1.1,443,' + hello.byteLength;
|
2016-09-22 17:06:34 +00:00
|
|
|
var buf = Buffer.concat([
|
|
|
|
Buffer.from([ 255 - version, header.length ])
|
|
|
|
, Buffer.from(header)
|
|
|
|
, hello
|
|
|
|
]);
|
|
|
|
var services = { 'ssh': 22, 'http': 4080, 'https': 8443 };
|
2016-09-22 18:01:57 +00:00
|
|
|
var clients = {};
|
2016-09-22 17:06:34 +00:00
|
|
|
|
|
|
|
machine.onMessage = function (opts) {
|
|
|
|
var id = opts.family + ',' + opts.address + ',' + opts.port;
|
|
|
|
var service = 'https';
|
|
|
|
var port = services[service];
|
|
|
|
var servername = sni(opts.data);
|
|
|
|
|
|
|
|
console.log('');
|
|
|
|
console.log('[onMessage]');
|
2016-09-22 18:01:57 +00:00
|
|
|
if (!opts.data.equals(hello)) {
|
|
|
|
throw new Error("'data' packet is not equal to original 'hello' packet");
|
2016-09-22 17:06:34 +00:00
|
|
|
}
|
2016-09-22 18:01:57 +00:00
|
|
|
console.log('all ', opts.data.byteLength, 'bytes are equal');
|
|
|
|
console.log('src:', opts.family, opts.address + ':' + opts.port);
|
|
|
|
console.log('dst:', 'IPv4 127.0.0.1:' + port);
|
2016-09-22 17:06:34 +00:00
|
|
|
|
2016-09-22 18:01:57 +00:00
|
|
|
if (!clients[id]) {
|
|
|
|
clients[id] = true;
|
|
|
|
if (!servername) {
|
|
|
|
throw new Error("no servername found for '" + id + "'");
|
|
|
|
}
|
|
|
|
console.log("servername: '" + servername + "'");
|
|
|
|
}
|
2016-09-22 17:06:34 +00:00
|
|
|
};
|
|
|
|
|
2016-09-22 18:01:57 +00:00
|
|
|
|
2016-09-22 17:06:34 +00:00
|
|
|
// full message in one go
|
2016-09-22 18:01:57 +00:00
|
|
|
// 223 = 2 + 22 + 199
|
2016-09-22 17:06:34 +00:00
|
|
|
console.log('');
|
2016-09-22 18:01:57 +00:00
|
|
|
console.log('[WHOLE BUFFER]', 2, header.length, hello.length, buf.byteLength);
|
|
|
|
clients = {};
|
|
|
|
machine.fns.addChunk(buf);
|
|
|
|
|
2016-09-22 17:06:34 +00:00
|
|
|
|
|
|
|
// messages one byte at a time
|
2016-09-22 18:01:57 +00:00
|
|
|
console.log('');
|
|
|
|
console.log('[BYTE-BY-BYTE BUFFER]', 1);
|
|
|
|
clients = {};
|
2016-09-22 17:06:34 +00:00
|
|
|
buf.forEach(function (byte) {
|
|
|
|
machine.fns.addChunk(Buffer.from([ byte ]));
|
|
|
|
});
|
2016-09-22 18:01:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
// split messages in overlapping thirds
|
|
|
|
// 0-2 (2)
|
|
|
|
// 2-24 (22)
|
|
|
|
// 24-223 (199)
|
|
|
|
// 223-225 (2)
|
|
|
|
// 225-247 (22)
|
|
|
|
// 247-446 (199)
|
|
|
|
buf = Buffer.concat([ buf, buf ]);
|
|
|
|
console.log('');
|
|
|
|
console.log('[OVERLAPPING BUFFERS]', buf.length);
|
|
|
|
clients = {};
|
|
|
|
[ buf.slice(0, 7) // version + header
|
|
|
|
, buf.slice(7, 14) // header
|
|
|
|
, buf.slice(14, 21) // header
|
|
|
|
, buf.slice(21, 28) // header + body
|
|
|
|
, buf.slice(28, 217) // body
|
|
|
|
, buf.slice(217, 224) // body + version
|
|
|
|
, buf.slice(224, 238) // version + header
|
|
|
|
, buf.slice(238, buf.byteLength) // header + body
|
|
|
|
].forEach(function (buf) {
|
|
|
|
machine.fns.addChunk(Buffer.from(buf));
|
|
|
|
});
|