diff --git a/bin/stunnel.js b/bin/stunnel.js index 5fa50a3..832b20e 100755 --- a/bin/stunnel.js +++ b/bin/stunnel.js @@ -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); }()); diff --git a/wsclient.js b/wsclient.js index b74ea54..d02a550 100644 --- a/wsclient.js +++ b/wsclient.js @@ -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) { - //console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes"); - //console.log(JSON.stringify(chunk.toString())); - wstunneler.send(pack(opts, chunk), { binary: true }); + // '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);