small refactor

This commit is contained in:
AJ ONeal 2018-06-01 01:36:29 -06:00
parent 8c1c8d8006
commit c54f121a85
1 changed files with 40 additions and 13 deletions

View File

@ -53,6 +53,7 @@ module.exports.print = function (config) {
}; };
module.exports.assign = function (state, tun, cb) { module.exports.assign = function (state, tun, cb) {
console.log('first message from', tun);
var net = state.net || require('net'); var net = state.net || require('net');
var handlers = {}; 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) { function terminateTls(tun, cb) {
var socketPair = require('socket-pair'); var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) { var conn = socketPair.create(function (err, other) {
@ -152,6 +190,7 @@ module.exports.assign = function (state, tun, cb) {
if (!state.greenlock) { if (!state.greenlock) {
state.greenlock = require('greenlock').create(state.greenlockConfig); state.greenlock = require('greenlock').create(state.greenlockConfig);
} }
if (!state.terminatorServer) { if (!state.terminatorServer) {
state.terminatorServer = require('tls').createServer(state.greenlock.tlsOptions, function (tlsSocket) { state.terminatorServer = require('tls').createServer(state.greenlock.tlsOptions, function (tlsSocket) {
var Packer = require('proxy-packer'); var Packer = require('proxy-packer');
@ -166,19 +205,7 @@ module.exports.assign = function (state, tun, cb) {
handlers.https(tlsSocket); handlers.https(tlsSocket);
return; return;
} }
if (parseInt(conf.handler, 10)) { invokeHandler(conf, tlsSocket, tun, id);
// 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);
}
}); });
} }