proxy-packer.js/test/parse.js

118 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2016-09-22 19:11:54 +00:00
'use strict';
var sni = require('sni');
2018-05-31 04:48:32 +00:00
var hello = require('fs').readFileSync(__dirname + '/sni.hello.bin');
2016-09-22 19:11:54 +00:00
var version = 1;
2016-09-22 19:31:31 +00:00
var address = {
family: 'IPv4'
, address: '127.0.1.1'
2018-05-31 04:48:32 +00:00
, port: 4321
, service: 'foo-https'
, serviceport: 443
, name: 'foo-pokemap.hellabit.com'
2016-09-22 19:31:31 +00:00
};
2016-09-30 06:35:29 +00:00
var header = address.family + ',' + address.address + ',' + address.port + ',' + hello.byteLength
2018-05-31 04:48:32 +00:00
+ ',' + (address.service || '') + ',' + (address.serviceport || '') + ',' + (address.name || '')
2016-09-30 06:35:29 +00:00
;
2016-09-22 19:11:54 +00:00
var buf = Buffer.concat([
Buffer.from([ 255 - version, header.length ])
, Buffer.from(header)
, hello
]);
var services = { 'ssh': 22, 'http': 4080, 'https': 8443 };
var clients = {};
var count = 0;
2018-05-31 04:48:32 +00:00
var packer = require('../');
2016-09-22 19:31:31 +00:00
var machine = packer.create({
2018-05-31 04:48:32 +00:00
onmessage: function (tun) {
var id = tun.family + ',' + tun.address + ',' + tun.port;
2016-09-22 19:11:54 +00:00
var service = 'https';
var port = services[service];
2018-05-31 04:48:32 +00:00
var servername = sni(tun.data);
2016-09-22 19:11:54 +00:00
console.log('');
console.log('[onMessage]');
2018-05-31 04:48:32 +00:00
if (!tun.data.equals(hello)) {
2016-09-22 19:11:54 +00:00
throw new Error("'data' packet is not equal to original 'hello' packet");
}
2018-05-31 04:48:32 +00:00
console.log('all', tun.data.byteLength, 'bytes are equal');
console.log('src:', tun.family, tun.address + ':' + tun.port + ':' + tun.serviceport);
2016-09-22 19:11:54 +00:00
console.log('dst:', 'IPv4 127.0.0.1:' + port);
if (!clients[id]) {
clients[id] = true;
if (!servername) {
throw new Error("no servername found for '" + id + "'");
}
2018-05-31 04:48:32 +00:00
console.log("servername: '" + servername + "'", tun.name);
2016-09-22 19:11:54 +00:00
}
count += 1;
}
2016-09-30 06:35:29 +00:00
, onerror: function () {
throw new Error("Did not expect onerror");
}
, onend: function () {
throw new Error("Did not expect onend");
}
2016-09-22 19:11:54 +00:00
});
2016-09-22 19:31:31 +00:00
var packed = packer.pack(address, hello);
2016-09-22 19:11:54 +00:00
2016-09-22 19:31:31 +00:00
if (!packed.equals(buf)) {
console.error(buf.toString('hex') === packed.toString('hex'));
console.error(packed.toString('hex'), packed.byteLength);
console.error(buf.toString('hex'), buf.byteLength);
throw new Error("packer did not pack as expected");
}
2016-09-22 19:11:54 +00:00
console.log('');
// full message in one go
// 223 = 2 + 22 + 199
console.log('[WHOLE BUFFER]', 2, header.length, hello.length, buf.byteLength);
clients = {};
machine.fns.addChunk(buf);
console.log('');
// messages one byte at a time
console.log('[BYTE-BY-BYTE BUFFER]', 1);
clients = {};
buf.forEach(function (byte) {
machine.fns.addChunk(Buffer.from([ byte ]));
});
console.log('');
// 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('[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));
});
console.log('');
process.on('exit', function () {
if (count !== 4) {
throw new Error("should have delivered 4 messages, not", count);
}
console.log('TESTS PASS');
console.log('');
});