From c54f121a8514e09d72434c3d78a9a2e747e74e1c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 1 Jun 2018 01:36:29 -0600 Subject: [PATCH] small refactor --- lib/sorting-hat.js | 53 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/lib/sorting-hat.js b/lib/sorting-hat.js index 7eb971a..f98acc3 100644 --- a/lib/sorting-hat.js +++ b/lib/sorting-hat.js @@ -53,6 +53,7 @@ module.exports.print = function (config) { }; module.exports.assign = function (state, tun, cb) { + console.log('first message from', tun); var net = state.net || require('net'); var handlers = {}; @@ -140,6 +141,43 @@ 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 + var conn = getNetConn(conf.handler); + console.info("Port-Forwarding '" + (tun.name || tun.serviceport) + "' to '" + conf.handler + "'"); + conn.pipe(tlsSocket); + tlsSocket.pipe(conn); + return; + } + var handle = tun.name || tun.port; + var handler; + var path = require('path'); + var homedir = require('os').homedir(); + var localshare = path.join(homedir, '.local/share/telebit/apps'); + + if (/^~/.test(conf.handler)) { + conf.handler = require('path').join(require('os').homedir(), conf.handler.replace(/^~(\/?)/, '')); + } + + try { + handler = require(conf.handler); + console.info("Handling '" + handle + ":" + id + "' with '" + conf.handler + "'"); + handler(tlsSocket, tun, id); + } catch(e1) { + try { + handler = require(path.join(localshare, conf.handler)); + console.info("Handling '" + handle + ":" + id + "' with '" + conf.handler + "'"); + handler(tlsSocket, tun, id); + } catch(e2) { + console.error("Failed to load '" + conf.handler + "':", e1.message); + console.error("Failed to load '" + path.join(localshare, conf.handler) + "':", e2.message); + console.warn("Using default handler for '" + handle + ":" + id + "'"); + handlers.https(tlsSocket, tun, id); + } + } + } + function terminateTls(tun, cb) { var socketPair = require('socket-pair'); var conn = socketPair.create(function (err, other) { @@ -152,6 +190,7 @@ module.exports.assign = function (state, tun, cb) { if (!state.greenlock) { state.greenlock = require('greenlock').create(state.greenlockConfig); } + if (!state.terminatorServer) { state.terminatorServer = require('tls').createServer(state.greenlock.tlsOptions, function (tlsSocket) { var Packer = require('proxy-packer'); @@ -166,19 +205,7 @@ module.exports.assign = function (state, tun, cb) { handlers.https(tlsSocket); return; } - if (parseInt(conf.handler, 10)) { - // TODO http-proxy with proper headers and ws support - var conn = getNetConn(conf.handler); - conn.pipe(tlsSocket); - tlsSocket.pipe(conn); - } - var handler; - try { - handler = require(conf.handler); - handler(tlsSocket, addr, id); - } catch(e) { - handlers.https(tlsSocket, addr, id); - } + invokeHandler(conf, tlsSocket, tun, id); }); }