From 191a62ad3d89fbbc1ab701b2873157d6d1edddfc Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 27 Sep 2016 12:39:53 -0400 Subject: [PATCH] muxing... --- mux-tcp.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ serve.js | 13 ++++++++ 2 files changed, 101 insertions(+) create mode 100644 mux-tcp.js diff --git a/mux-tcp.js b/mux-tcp.js new file mode 100644 index 0000000..8555f01 --- /dev/null +++ b/mux-tcp.js @@ -0,0 +1,88 @@ +'use strict'; + +var net = require('net'); +var tls = require('tls'); +var http = require('http'); +var tlsOpts = require('localhost.daplie.com-certificates').merge({}); +var Duplex = require('stream').Duplex; +var httpServer = http.createServer(function (req, res) { + res.end('Hello, World!'); +}); +var tcp3000 = net.createServer(); + +tcp3000.listen(3000, function () { + console.log('listening on 3000'); +}); + + +var tls3000 = tls.createServer(tlsOpts, function (tlsSocket) { + httpServer.emit('connection', tlsSocket); + /* + tlsSocket.on('data', function (chunk) { + console.log('chunk', chunk.byteLength); + }); + */ +}); + + +tcp3000.on('connection', function (socket) { + // this works when I put it here, but I don't know if it's tls yet here + // httpsServer.emit('connection', socket); + //tls3000.emit('connection', socket); + + //var tlsSocket = new tls.TLSSocket(socket, { secureContext: tls.createSecureContext(tlsOpts) }); + //tlsSocket.on('data', function (chunk) { + // console.log('dummy', chunk.byteLength); + //}); + + //return; + socket.once('data', function (chunk) { + if (/http\/1/i.test(chunk.toString())) { + + console.log("looks like http, continue"); + + // this works as expected + httpServer.emit('connection', socket); + + } else { + + console.log("doesn't look like http, try tls"); + + // none of these methods work: + // httpsServer.emit('connection', socket); // this didn't work + // tlsServer.emit('connection', socket); // this didn't work either + var myDuplex = new Duplex(); + myDuplex._write = function (chunk, encoding, cb) { + console.log('_write', chunk.byteLength); + socket.write(chunk, encoding); + cb(); + }; + myDuplex._read = function (size) { + console.log('_read'); + var x = socket.read(size); + if (x) { + console.log('_read', size); + this.push(x); + } + }; + socket.on('data', function (chunk) { + console.log('socket data', chunk.byteLength); + myDuplex.push(chunk); + }); + + tls3000.emit('connection', myDuplex); + //var tlsSocket = new tls.TLSSocket(myDuplex, { secureContext: tls.createSecureContext(tlsOpts) }); + /* + var tlsSocket = new tls.TLSSocket(socket, { secureContext: tls.createSecureContext(tlsOpts) }); + tlsSocket.on('data', function (chunk) { + console.log('tls chunk', chunk.byteLength); + }); + */ + + + } + + socket.unshift(chunk); + }); + +}); diff --git a/serve.js b/serve.js index c90a0d1..07818a5 100644 --- a/serve.js +++ b/serve.js @@ -189,4 +189,17 @@ function socketToId(socket) { server5443.listen(5443, function () { console.log('listening on 5443'); }); + + + var http80 = require('http').createServer(); + http80.on('request', function (req, res) { + res.end('Happy Day!'); + }); + + var server80 = net.createServer(function (client) { + http80.emit('connection', client); + }); + server80.listen(80, function () { + console.log('listening on 80'); + }); //});