diff --git a/index.js b/index.js new file mode 100644 index 0000000..9c856ef --- /dev/null +++ b/index.js @@ -0,0 +1,170 @@ +'use strict'; + +module.exports.create = function (opts) { + + var machine = { onMessage: opts.onMessage }; + 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 = {}; + + machine.fns.version = function (chunk) { + //console.log(''); + //console.log('[version]'); + if ((255 - machine._version) !== chunk[machine.chunkIndex]) { + console.error("not v" + machine._version + " (or data is corrupt)"); + // no idea how to fix this yet + } + machine.chunkIndex += 1; + + return true; + }; + + + machine.headerLen = 0; + machine.fns.headerLength = function (chunk) { + //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); + } + + // 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; + } + + if (machine.bufIndex > 0) { + // 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.onMessage({ + family: machine.family + , address: machine.address + , port: machine.port + , data: machine.buf.slice(0, machine.bufIndex) + }); + + machine.chunkIndex += partLen; // === chunk.length + machine.buf = null; // reset to null + machine.bufIndex = 0; // reset to 0 + + 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.pack = function (address, data) { + var version = 1; + var header = /*servername + ',' +*/address.family + ',' + address.address + ',' + address.port + ',' + data.byteLength; + var meta = [ 255 - version, header.length ]; + var buf = Buffer.alloc(meta.length + header.length + data.byteLength); + + buf.write(meta[0], 0); + buf.write(meta[1], 1); + buf.write(header, 2); + buf.write(data, 2 + header.length); + + return buf; +}; diff --git a/sni.hello.bin b/sni.hello.bin new file mode 100644 index 0000000..85b0344 Binary files /dev/null and b/sni.hello.bin differ diff --git a/test.js b/test.js new file mode 100644 index 0000000..357756e --- /dev/null +++ b/test.js @@ -0,0 +1,93 @@ +'use strict'; + +var sni = require('sni'); +var hello = require('fs').readFileSync('./sni.hello.bin'); +var version = 1; +var header = 'IPv4,127.0.1.1,443,' + hello.byteLength; +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; +var machine = require('./').create({ + 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]'); + if (!opts.data.equals(hello)) { + throw new Error("'data' packet is not equal to original 'hello' packet"); + } + 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); + + if (!clients[id]) { + clients[id] = true; + if (!servername) { + throw new Error("no servername found for '" + id + "'"); + } + console.log("servername: '" + servername + "'"); + } + + count += 1; + } +}); + + + +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(''); +});