From 7a36b1af8aa6ad7fc767975202cf841eae680863 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 30 Jun 2018 13:20:00 -0600 Subject: [PATCH] bugfix: show success, not just error --- lib/handlers/local-app-error.js | 19 +++++++++++++++++++ lib/sorting-hat.js | 30 ++++++++++++++++++------------ 2 files changed, 37 insertions(+), 12 deletions(-) create mode 100644 lib/handlers/local-app-error.js diff --git a/lib/handlers/local-app-error.js b/lib/handlers/local-app-error.js new file mode 100644 index 0000000..5c60911 --- /dev/null +++ b/lib/handlers/local-app-error.js @@ -0,0 +1,19 @@ +'use strict'; + +module.exports = function (opts) { + console.log("Could not connect"); + var socket = opts.socket; + var handler = opts.handler; + var http = require('http'); + var server = http.createServer(function (req, res) { + console.log('responding to thing'); + res.statusCode = 500; + res.setHeader('Content-Type', 'text/html'); + res.end("" + + "Couldn't Connect" + + "Could not connect to localhost:" + handler + "" + + ""); + }); + //server.emit('connection', socket); + socket.end("Could not connect to localhost:" + handler); +}; diff --git a/lib/sorting-hat.js b/lib/sorting-hat.js index 12827bb..184d850 100644 --- a/lib/sorting-hat.js +++ b/lib/sorting-hat.js @@ -113,16 +113,19 @@ module.exports.assign = function (state, tun, cb) { , remoteAddress: tun.address , remotePort: tun.port }; - var conn = net.createConnection(netOpts, function () { + var conn = net.createConnection(netOpts); + conn.once('connect', function () { // this will happen before 'data' or 'readable' is triggered // We use the data from the netOpts object so that the createConnection function has // the oppurtunity of removing/changing it if it wants/needs to handle it differently. cb(null, conn); - cb = function () {}; // for error events - }); - conn.on('error', function (err) { - cb(err); + conn.removeListener('error', onError); }); + function onError(err) { + if (cb) { cb(err); } + } + conn.on('error', onError); + return conn; } function redirectHttp(cb) { @@ -237,18 +240,21 @@ module.exports.assign = function (state, tun, cb) { function invokeHandler(conf, tlsSocket, tun, id) { if (parseInt(conf.handler, 10)) { // TODO http-proxy with proper headers and ws support - getNetConn(conf.handler, function (err, conn) { + //tlsSocket.pause(); + var conn = getNetConn(conf.handler, function (err/*, conn*/) { if (err) { - // TODO direct to site with error page - console.error("probably couldn't connect to handler"); - tlsSocket.write("Couldn't connect to localhost:" + conf.handler); - tlsSocket.end(); + console.log("[DEBUG] need to handle error"); + require('./handlers/local-app-error.js')({ handler: conf.handler, socket: tlsSocket }); return; } console.info("Port-Forwarding '" + (tun.name || tun.serviceport) + "' to '" + conf.handler + "'"); - conn.pipe(tlsSocket); - tlsSocket.pipe(conn); + //conn.pipe(tlsSocket); + //tlsSocket.pipe(conn); + //tlsSocket.resume(); }); + conn.pipe(tlsSocket); + tlsSocket.pipe(conn); + tlsSocket.resume(); return; } var handle = tun.name || tun.port;