tunnel-packer as own module

This commit is contained in:
AJ ONeal 2016-09-22 13:31:31 -06:00
parent d2926777a6
commit 5efef2b709
3 changed files with 57 additions and 9 deletions

View File

@ -157,14 +157,15 @@ module.exports.create = function (opts) {
module.exports.pack = function (address, data) { module.exports.pack = function (address, data) {
var version = 1; var version = 1;
var header = /*servername + ',' +*/address.family + ',' + address.address + ',' + address.port + ',' + data.byteLength; var header = Buffer.from(
var meta = [ 255 - version, header.length ]; /*servername + ',' +*/address.family + ',' + address.address + ',' + address.port + ',' + data.byteLength
var buf = Buffer.alloc(meta.length + header.length + data.byteLength); );
var meta = Buffer.from([ 255 - version, header.length ]);
var buf = Buffer.alloc(meta.byteLength + header.byteLength + data.byteLength);
buf.write(meta[0], 0); meta.copy(buf, 0, 0, meta.byteLength);
buf.write(meta[1], 1); header.copy(buf, 2, 0, header.byteLength);
buf.write(header, 2); data.copy(buf, 2 + header.byteLength, 0, data.byteLength);
buf.write(data, 2 + header.length);
return buf; return buf;
}; };

34
package.json Normal file
View File

@ -0,0 +1,34 @@
{
"name": "tunnel-packer",
"version": "1.0.0",
"description": "A strategy for packing and unpacking tunneled network messages (or any stream)",
"main": "index.js",
"scripts": {
"test": "node test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Daplie/tunnel-packer.git"
},
"keywords": [
"tunnel",
"tcp",
"sni",
"https",
"ssl",
"http",
"proxy",
"pack",
"unpack",
"message",
"msg",
"packer",
"unpacker"
],
"author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
"license": "(MIT OR Apache-2.0)",
"bugs": {
"url": "https://github.com/Daplie/tunnel-packer/issues"
},
"homepage": "https://github.com/Daplie/tunnel-packer#readme"
}

17
test.js
View File

@ -3,7 +3,12 @@
var sni = require('sni'); var sni = require('sni');
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 address = {
family: 'IPv4'
, address: '127.0.1.1'
, port: 443
};
var header = address.family + ',' + address.address + ',' + address.port + ',' + hello.byteLength;
var buf = Buffer.concat([ var buf = Buffer.concat([
Buffer.from([ 255 - version, header.length ]) Buffer.from([ 255 - version, header.length ])
, Buffer.from(header) , Buffer.from(header)
@ -12,7 +17,8 @@ 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; var count = 0;
var machine = require('./').create({ var packer = require('./');
var machine = packer.create({
onMessage: function (opts) { 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';
@ -39,7 +45,14 @@ var machine = require('./').create({
count += 1; count += 1;
} }
}); });
var packed = packer.pack(address, hello);
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");
}
console.log(''); console.log('');