small refactor
This commit is contained in:
parent
7a08220838
commit
3a6df4db63
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue