allow overwriting net

This commit is contained in:
AJ ONeal 2016-10-05 23:02:28 -06:00
parent b54da005b1
commit 495dc57fb0
2 changed files with 57 additions and 6 deletions

View File

@ -86,6 +86,42 @@ program.locals.forEach(function (proxy) {
});
program.token = program.token || jwt.sign(tokenData, program.secret || 'shhhhh');
program.net = {
createConnection: function (info, cb) {
/*
var Dup = {
write: function (chunk, encoding, cb) {
//console.log('_write', chunk.byteLength);
this.__my_socket.write(chunk, encoding);
cb();
}
, read: function (size) {
//console.log('_read');
var x = this.__my_socket.read(size);
if (x) {
console.log('_read', size);
this.push(x);
}
}
};
var myDuplex = new (require('streams').Duplex);
myDuplex._write = Dup.write;
myDuplex._read = Dup.read;
myDuplex.remoteFamily = socket.remoteFamily;
myDuplex.remoteAddress = socket.remoteAddress;
myDuplex.remotePort = socket.remotePort;
myDuplex.localFamily = socket.localFamily;
myDuplex.localAddress = socket.localAddress;
myDuplex.localPort = socket.localPort;
*/
// info = { servername, port, host, remoteAddress: { family, address, port } }
var net = require('net');
// socket = { write, push, end, events: [ 'readable', 'data', 'error', 'end' ] };
var socket = net.createConnection({ port: info.port, host: info.host }, cb);
return socket;
}
};
stunnel.connect(program);
}());

View File

@ -1,7 +1,6 @@
(function () {
'use strict';
var net = require('net');
var WebSocket = require('ws');
var sni = require('sni');
var pack = require('tunnel-packer').pack;
@ -47,6 +46,7 @@ function run(copts) {
// Synergy Teamwork Paradigm = Jabberwocky
var handlers = {
onmessage: function (opts) {
var net = copts.net || require('net');
var cid = addrToId(opts);
var service = opts.service;
var port = services[service];
@ -83,14 +83,29 @@ function run(copts) {
console.info("[connect] new client '" + cid + "' for '" + servername + "' (" + (handlers._numClients() + 1) + " clients)");
localclients[cid] = net.createConnection({ port: port, host: '127.0.0.1' }, function () {
localclients[cid] = net.createConnection({
servername: servername
, port: port
, host: '127.0.0.1'
, remoteAddress: {
family: opts.family
, address: opts.address
, port: opts.port
}
}, function () {
//console.log("[=>] first packet from tunneler to '" + cid + "' as '" + opts.service + "'", opts.data.byteLength);
localclients[cid].write(opts.data);
});
localclients[cid].on('data', function (chunk) {
// 'data'
localclients[cid].on('readable', function (size) {
var chunk;
do {
chunk = localclients[cid].read(size);
//console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
//console.log(JSON.stringify(chunk.toString()));
wstunneler.send(pack(opts, chunk), { binary: true });
} while (chunk);
});
localclients[cid].on('error', function (err) {
handlers._onLocalError(cid, opts, err);