From 5efef2b709256c83fc504808daddc1ef4b99e0f4 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 22 Sep 2016 13:31:31 -0600 Subject: [PATCH] tunnel-packer as own module --- index.js | 15 ++++++++------- package.json | 34 ++++++++++++++++++++++++++++++++++ test.js | 17 +++++++++++++++-- 3 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 package.json diff --git a/index.js b/index.js index 9c856ef..4dab907 100644 --- a/index.js +++ b/index.js @@ -157,14 +157,15 @@ module.exports.create = function (opts) { 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); + var header = Buffer.from( + /*servername + ',' +*/address.family + ',' + address.address + ',' + address.port + ',' + 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); - buf.write(meta[1], 1); - buf.write(header, 2); - buf.write(data, 2 + header.length); + meta.copy(buf, 0, 0, meta.byteLength); + header.copy(buf, 2, 0, header.byteLength); + data.copy(buf, 2 + header.byteLength, 0, data.byteLength); return buf; }; diff --git a/package.json b/package.json new file mode 100644 index 0000000..3108bda --- /dev/null +++ b/package.json @@ -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 (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" +} diff --git a/test.js b/test.js index 357756e..d0bb430 100644 --- a/test.js +++ b/test.js @@ -3,7 +3,12 @@ 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 address = { + family: 'IPv4' +, address: '127.0.1.1' +, port: 443 +}; +var header = address.family + ',' + address.address + ',' + address.port + ',' + hello.byteLength; var buf = Buffer.concat([ Buffer.from([ 255 - version, header.length ]) , Buffer.from(header) @@ -12,7 +17,8 @@ var buf = Buffer.concat([ var services = { 'ssh': 22, 'http': 4080, 'https': 8443 }; var clients = {}; var count = 0; -var machine = require('./').create({ +var packer = require('./'); +var machine = packer.create({ onMessage: function (opts) { var id = opts.family + ',' + opts.address + ',' + opts.port; var service = 'https'; @@ -39,7 +45,14 @@ var machine = require('./').create({ 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('');