tunnel-packer as own module

This commit is contained in:
AJ ONeal 2016-09-22 13:31:31 -06:00
父節點 d2926777a6
當前提交 5efef2b709
共有 3 個文件被更改,包括 57 次插入9 次删除

查看文件

@ -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;
};

34
package.json Normal 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
查看文件

@ -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('');