This commit is contained in:
AJ ONeal 2016-09-22 12:17:45 -06:00
parent 269605e1ba
commit 88be06c872
3 changed files with 163 additions and 151 deletions

View File

@ -34,7 +34,7 @@ var tunneler = net.connect({ port: 5443 , host: 'pokemap.hellabit.com' }, functi
// a place to store data // a place to store data
// file management // file management
// Synergy Teamwork Paradigm = Jabberwocky // Synergy Teamwork Paradigm = Jabberwocky
var machine = require('./machine.js'); var machine = require('./machine.js').create();
machine.onMessage = function (opts) { machine.onMessage = function (opts) {
var id = opts.family + ',' + opts.address + ',' + opts.port; var id = opts.family + ',' + opts.address + ',' + opts.port;
var service = 'https'; var service = 'https';

View File

@ -1,167 +1,166 @@
'use strict'; 'use strict';
var machine = {}; module.exports.create = function () {
machine._version = 1;
machine.state = 0;
machine.states = { 0: 'version', 1: 'headerLength', 2: 'header', 3: 'data'/*, 4: 'error'*/ };
machine.states_length = Object.keys(machine.states).length;
machine.chunkIndex = 0;
machine.fns = {};
function debug(chunk, i, len) { var machine = {};
i = i || 0; machine._version = 1;
len = len || chunk.length - i; machine.state = 0;
console.log(chunk.slice(i, len)[0]); machine.states = { 0: 'version', 1: 'headerLength', 2: 'header', 3: 'data'/*, 4: 'error'*/ };
console.log(chunk); machine.states_length = Object.keys(machine.states).length;
console.log('state:', machine.states[machine.state]); machine.chunkIndex = 0;
console.log('statei:', machine.state); machine.fns = {};
console.log('index:', machine.chunkIndex);
}
machine.fns.version = function (chunk) { function debug(chunk, i, len) {
//console.log(''); i = i || 0;
//console.log('[version]'); len = len || chunk.length - i;
if ((255 - machine._version) !== chunk[machine.chunkIndex]) { console.log(chunk.slice(i, len)[0]);
console.error("not v" + machine._version + " (or data is corrupt)"); console.log(chunk);
// no idea how to fix this yet console.log('state:', machine.states[machine.state]);
console.log('statei:', machine.state);
console.log('index:', machine.chunkIndex);
} }
machine.chunkIndex += 1;
return true; machine.fns.version = function (chunk) {
}; //console.log('');
//console.log('[version]');
if ((255 - machine._version) !== chunk[machine.chunkIndex]) {
machine.headerLen = 0; console.error("not v" + machine._version + " (or data is corrupt)");
machine.fns.headerLength = function (chunk) { // no idea how to fix this yet
//console.log('');
//console.log('[headerLength]');
machine.headerLen = chunk[machine.chunkIndex];
machine.chunkIndex += 1;
return true;
};
machine.buf = null;
machine.bufIndex = 0;
//var buf = Buffer.alloc(4096);
machine.fns.header = function (chunk) {
//console.log('');
//console.log('[header]');
var curSize = machine.bufIndex + (chunk.length - machine.chunkIndex);
var partLen = 0;
var str = '';
var part;
if (curSize < machine.headerLen) {
// I still don't have the whole header,
// so just create a large enough buffer,
// write these bits, and wait for the next chunk.
if (!machine.buf) {
machine.buf = Buffer.alloc(machine.headerLen);
//console.log('[1a] machine.headerLen:', machine.headerLen);
} }
machine.chunkIndex += 1;
// partLen should be no more than the available size
partLen = Math.min(machine.headerLen - machine.bufIndex, chunk.length - machine.chunkIndex);
part = chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen);
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex, machine.chunkIndex + partLen);
machine.chunkIndex += partLen; // this MUST be chunk.length
machine.bufIndex += partLen;
return false;
}
else {
// it's now ready to discover the whole header
if (machine.buf) {
str += machine.buf.slice(0, machine.bufIndex).toString();
}
partLen = machine.headerLen - str.length;
part = chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen);
str += part.toString();
machine.chunkIndex += partLen;
machine.buf = null; // back to null
machine.bufIndex = 0; // back to 0
machine._headers = str.split(/,/g);
machine.family = machine._headers[0];
machine.address = machine._headers[1];
machine.port = machine._headers[2];
machine.bodyLen = parseInt(machine._headers[3], 10) || -1;
return true; return true;
} };
};
machine.fns.data = function (chunk) {
//console.log('');
//console.log('[data]');
var curSize = machine.bufIndex + (chunk.length - machine.chunkIndex);
//console.log('curSize:', curSize);
//console.log('bodyLen:', machine.bodyLen, typeof machine.bodyLen);
var partLen = 0;
partLen = Math.min(machine.bodyLen - machine.bufIndex, chunk.length - machine.chunkIndex); machine.headerLen = 0;
machine.fns.headerLength = function (chunk) {
//console.log('');
//console.log('[headerLength]');
machine.headerLen = chunk[machine.chunkIndex];
machine.chunkIndex += 1;
if (curSize < machine.bodyLen) { return true;
//console.log('curSize < bodyLen'); };
// I still don't have the whole header,
// so just create a large enough buffer, machine.buf = null;
// write these bits, and wait for the next chunk. machine.bufIndex = 0;
if (!machine.buf) { //var buf = Buffer.alloc(4096);
machine.buf = Buffer.alloc(machine.bodyLen); machine.fns.header = function (chunk) {
//console.log('');
//console.log('[header]');
var curSize = machine.bufIndex + (chunk.length - machine.chunkIndex);
var partLen = 0;
var str = '';
var part;
if (curSize < machine.headerLen) {
// I still don't have the whole header,
// so just create a large enough buffer,
// write these bits, and wait for the next chunk.
if (!machine.buf) {
machine.buf = Buffer.alloc(machine.headerLen);
}
// partLen should be no more than the available size
partLen = Math.min(machine.headerLen - machine.bufIndex, chunk.length - machine.chunkIndex);
part = chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen);
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex, machine.chunkIndex + partLen);
machine.chunkIndex += partLen; // this MUST be chunk.length
machine.bufIndex += partLen;
return false;
}
else {
// it's now ready to discover the whole header
if (machine.buf) {
str += machine.buf.slice(0, machine.bufIndex).toString();
}
partLen = machine.headerLen - str.length;
part = chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen);
str += part.toString();
machine.chunkIndex += partLen;
machine.buf = null; // back to null
machine.bufIndex = 0; // back to 0
machine._headers = str.split(/,/g);
machine.family = machine._headers[0];
machine.address = machine._headers[1];
machine.port = machine._headers[2];
machine.bodyLen = parseInt(machine._headers[3], 10) || -1;
return true;
}
};
machine.fns.data = function (chunk) {
//console.log('');
//console.log('[data]');
var curSize = machine.bufIndex + (chunk.length - machine.chunkIndex);
//console.log('curSize:', curSize);
//console.log('bodyLen:', machine.bodyLen, typeof machine.bodyLen);
var partLen = 0;
partLen = Math.min(machine.bodyLen - machine.bufIndex, chunk.length - machine.chunkIndex);
if (curSize < machine.bodyLen) {
//console.log('curSize < bodyLen');
// I still don't have the whole header,
// so just create a large enough buffer,
// write these bits, and wait for the next chunk.
if (!machine.buf) {
machine.buf = Buffer.alloc(machine.bodyLen);
}
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex, machine.chunkIndex + partLen);
machine.chunkIndex += partLen; // this MUST be chunk.length
machine.bufIndex += partLen;
return false;
} }
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex, machine.chunkIndex + partLen); if (machine.bufIndex > 0) {
machine.chunkIndex += partLen; // this MUST be chunk.length // the completing remainder of the body is in the current slice
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex, machine.chunkIndex + partLen);
}
else {
// the whole body is in the current slice
machine.buf = chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen);
}
machine.bufIndex += partLen; machine.bufIndex += partLen;
return false; machine.onMessage({
} family: machine.family
, address: machine.address
, port: machine.port
, data: machine.buf.slice(0, machine.bufIndex)
});
if (machine.buf) { machine.chunkIndex += partLen; // === chunk.length
// the completing remainder of the body is in the current slice machine.buf = null; // reset to null
chunk.copy(machine.buf, machine.bufIndex, machine.chunkIndex, machine.chunkIndex + partLen); machine.bufIndex = 0; // reset to 0
}
else {
// the whole body is in the current slice
machine.buf = chunk.slice(machine.chunkIndex, machine.chunkIndex + partLen);
}
machine.onMessage({ return true;
family: machine.family };
, address: machine.address machine.fns.addChunk = function (chunk) {
, port: machine.port //console.log('');
, data: machine.buf //console.log('[addChunk]');
}); machine.chunkIndex = 0;
while (machine.chunkIndex < chunk.length) {
//console.log('chunkIndex:', machine.chunkIndex, 'state:', machine.state);
machine.chunkIndex += partLen; // === chunk.length if (true === machine.fns[machine.states[machine.state]](chunk)) {
machine.buf = null; // reset to null machine.state += 1;
machine.bufIndex = 0; // reset to 0 machine.state %= machine.states_length;
}
return true;
};
machine.fns.addChunk = function (chunk) {
//console.log('');
//console.log('[addChunk]');
machine.chunkIndex = 0;
while (machine.chunkIndex < chunk.length) {
//console.log('chunkIndex:', machine.chunkIndex, 'state:', machine.state);
if (true === machine.fns[machine.states[machine.state]](chunk)) {
machine.state += 1;
machine.state %= machine.states_length;
} }
} };
return machine;
}; };
module.exports = machine;
process.on('uncaughtException', function (err) {
console.error(err);
debug(Buffer.from([0]));
});

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
var sni = require('sni'); var sni = require('sni');
var machine = require('./machine.js'); var machine = require('./machine.js').create();
var hello = require('fs').readFileSync('./sni.hello.bin'); var hello = require('fs').readFileSync('./sni.hello.bin');
var version = 1; var version = 1;
var header = 'IPv4,127.0.1.1,443,' + hello.byteLength; var header = 'IPv4,127.0.1.1,443,' + hello.byteLength;
@ -12,6 +12,7 @@ var buf = Buffer.concat([
]); ]);
var services = { 'ssh': 22, 'http': 4080, 'https': 8443 }; var services = { 'ssh': 22, 'http': 4080, 'https': 8443 };
var clients = {}; var clients = {};
var count = 0;
machine.onMessage = function (opts) { machine.onMessage = function (opts) {
var id = opts.family + ',' + opts.address + ',' + opts.port; var id = opts.family + ',' + opts.address + ',' + opts.port;
@ -24,7 +25,7 @@ machine.onMessage = function (opts) {
if (!opts.data.equals(hello)) { if (!opts.data.equals(hello)) {
throw new Error("'data' packet is not equal to original 'hello' packet"); throw new Error("'data' packet is not equal to original 'hello' packet");
} }
console.log('all ', opts.data.byteLength, 'bytes are equal'); console.log('all', opts.data.byteLength, 'bytes are equal');
console.log('src:', opts.family, opts.address + ':' + opts.port); console.log('src:', opts.family, opts.address + ':' + opts.port);
console.log('dst:', 'IPv4 127.0.0.1:' + port); console.log('dst:', 'IPv4 127.0.0.1:' + port);
@ -35,24 +36,28 @@ machine.onMessage = function (opts) {
} }
console.log("servername: '" + servername + "'"); console.log("servername: '" + servername + "'");
} }
count += 1;
}; };
console.log('');
// full message in one go // full message in one go
// 223 = 2 + 22 + 199 // 223 = 2 + 22 + 199
console.log('');
console.log('[WHOLE BUFFER]', 2, header.length, hello.length, buf.byteLength); console.log('[WHOLE BUFFER]', 2, header.length, hello.length, buf.byteLength);
clients = {}; clients = {};
machine.fns.addChunk(buf); machine.fns.addChunk(buf);
console.log('');
// messages one byte at a time // messages one byte at a time
console.log('');
console.log('[BYTE-BY-BYTE BUFFER]', 1); console.log('[BYTE-BY-BYTE BUFFER]', 1);
clients = {}; clients = {};
buf.forEach(function (byte) { buf.forEach(function (byte) {
machine.fns.addChunk(Buffer.from([ byte ])); machine.fns.addChunk(Buffer.from([ byte ]));
}); });
console.log('');
// split messages in overlapping thirds // split messages in overlapping thirds
@ -63,7 +68,6 @@ buf.forEach(function (byte) {
// 225-247 (22) // 225-247 (22)
// 247-446 (199) // 247-446 (199)
buf = Buffer.concat([ buf, buf ]); buf = Buffer.concat([ buf, buf ]);
console.log('');
console.log('[OVERLAPPING BUFFERS]', buf.length); console.log('[OVERLAPPING BUFFERS]', buf.length);
clients = {}; clients = {};
[ buf.slice(0, 7) // version + header [ buf.slice(0, 7) // version + header
@ -77,3 +81,12 @@ clients = {};
].forEach(function (buf) { ].forEach(function (buf) {
machine.fns.addChunk(Buffer.from(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('');
});