telebit.js/lib/sorting-hat.js

577 lines
19 KiB
JavaScript
Raw Permalink Normal View History

2018-06-08 06:46:07 +00:00
'use strict';
2018-07-01 06:53:26 +00:00
2018-06-08 06:46:07 +00:00
var os = require('os');
var path = require('path');
2018-07-01 06:53:26 +00:00
var fs = require('fs');
2018-06-08 06:46:07 +00:00
2018-05-31 10:10:47 +00:00
module.exports.print = function (config) {
var services = { https: {}, http: {}, tcp: {} };
// Note: the remote needs to know:
// what servernames to forward
// what ports to forward
// what udp ports to forward
// redirect http to https automatically
// redirect www to nowww automatically
if (config.http) {
Object.keys(config.http).forEach(function (hostname) {
if ('*' === hostname) {
config.servernames.forEach(function (servername) {
services.https[servername] = config.http[hostname];
services.http[servername] = 'redirect-https';
});
return;
}
services.https[hostname] = config.http[hostname];
services.http[hostname] = 'redirect-https';
});
}
/*
Object.keys(config.localPorts).forEach(function (port) {
var proto = config.localPorts[port];
if (!proto) { return; }
if ('http' === proto) {
config.servernames.forEach(function (servername) {
services.http[servername] = port;
});
return;
}
if ('https' === proto) {
config.servernames.forEach(function (servername) {
services.https[servername] = port;
});
return;
}
if (true === proto) { proto = 'tcp'; }
if ('tcp' !== proto) { throw new Error("unsupported protocol '" + proto + "'"); }
//services[proxy.protocol]['*'] = proxy.port;
//services[proxy.protocol][proxy.hostname] = proxy.port;
services[proto]['*'] = port;
});
*/
Object.keys(services).forEach(function (protocol) {
var subServices = services[protocol];
Object.keys(subServices).forEach(function (hostname) {
console.info('[local proxy]', protocol + '://' + hostname + ' => ' + subServices[hostname]);
});
});
console.info('');
};
2018-05-31 06:19:53 +00:00
module.exports.assign = function (state, tun, cb) {
2018-09-14 05:42:04 +00:00
//console.log('first message from', tun);
2018-05-31 06:19:53 +00:00
var net = state.net || require('net');
2018-06-01 08:45:38 +00:00
function trySsh(tun, cb) {
// https://security.stackexchange.com/questions/43231/plausibly-deniable-ssh-does-it-make-sense?rq=1
// https://tools.ietf.org/html/rfc4253#section-4.2
2018-06-09 18:57:00 +00:00
var sshPort;
if (-1 !== ['true', 'enable', 'auto', 'on'].indexOf(state.config.sshAuto)) {
sshPort = 22;
} else {
sshPort = parseInt(state.config.sshAuto, 10);
}
if (!sshPort || 'SSH-2.0-' !== tun.data.slice(0, 8).toString()) {
2018-06-01 08:45:38 +00:00
cb(null, false);
return;
}
getNetConn(sshPort, cb);
2018-06-01 08:45:38 +00:00
}
2018-05-31 10:10:47 +00:00
var handlers = {};
handlers.http = function (socket) {
if (!state.greenlock) {
state.greenlock = require('greenlock').create(state.greenlockConfig);
}
if (!state.httpRedirectServer) {
state.redirectHttps = require('redirect-https')();
state.httpRedirectServer = require('http').createServer(state.greenlock.middleware(state.redirectHttps));
}
state.httpRedirectServer.emit('connection', socket);
process.nextTick(function () { socket.resume(); });
2018-05-31 10:10:47 +00:00
};
handlers.https = function (tlsSocket) {
2018-06-08 06:46:07 +00:00
console.log('Encrypted', tlsSocket.encrypted, tlsSocket.remoteAddress, tlsSocket.remotePort);
2018-05-31 10:10:47 +00:00
if (!state.defaultHttpServer) {
2018-06-08 06:46:07 +00:00
state._finalHandler = require('finalhandler');
state._serveStatic = require('serve-static');
state._defaultServe = state._serveStatic(path.join(__dirname, 'html'));
2018-05-31 10:10:47 +00:00
state.defaultHttpServer = require('http').createServer(function (req, res) {
2018-07-01 06:53:26 +00:00
// TODO serve api
2018-06-08 06:46:07 +00:00
state._defaultServe(req, res, state._finalHandler(req, res));
2018-05-31 10:10:47 +00:00
});
}
state.defaultHttpServer.emit('connection', tlsSocket);
process.nextTick(function () { tlsSocket.resume(); });
2018-05-31 10:10:47 +00:00
};
function getNetConn(port, cb) {
var netOpts = {
port: port
, host: '127.0.0.1'
, servername: tun.name
, name: tun.name
, serviceport: tun.serviceport
, data: tun.data
, remoteFamily: tun.family
, remoteAddress: tun.address
, remotePort: tun.port
};
var conn = net.createConnection(netOpts, 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);
});
}
2018-05-31 10:10:47 +00:00
function redirectHttp(cb) {
var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) {
2018-05-31 10:10:47 +00:00
if (err) { cb(err); return; }
handlers.http(other);
cb(null, conn);
});
//if (tun.data) { conn.write(tun.data); }
return conn;
}
2018-07-01 07:18:10 +00:00
function errorTcp(conf, cb) {
var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) {
if (err) { cb(err); return; }
cb(null, conn);
other.write("\n" +
[ "[Telebit Error Server]"
, "Could not load '" + conf.handler + "' as a module, file, or directory."
].join("\n") + "\n\n");
other.end();
});
//if (tun.data) { conn.write(tun.data); }
return conn;
}
2018-07-28 04:30:58 +00:00
function fileDirTcp(opts, cb) {
2018-07-01 07:18:10 +00:00
var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) {
if (err) { cb(err); return; }
2018-07-28 04:30:58 +00:00
if (opts.stat.isFile()) {
fs.createReadStream(opts.config.handler).pipe(other);
2018-07-01 07:18:10 +00:00
} else {
2018-07-28 04:30:58 +00:00
fs.readdir(opts.config.handler, function (err, nodes) {
2018-07-01 07:18:10 +00:00
other.write('\n' + nodes.join('\n') + '\n\n');
other.end();
});
}
cb(null, conn);
});
//if (tun.data) { conn.write(tun.data); }
return conn;
}
2018-06-01 09:50:27 +00:00
function echoTcp(cb) {
var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) {
if (err) { cb(err); return; }
other.on('data', function (chunk) {
other.write(chunk);
});
other.on('end', function () {
other.end();
});
cb(null, conn);
2018-06-08 08:50:00 +00:00
other.write("\n" +
[ "[Telebit Echo Server] v1.0"
, "To configure tcp run the following:"
, "\ttelebit tcp <port number or module name>"
, "\tex: telebit tcp 5050"
, "\tex: telebit tcp /path/to/module"
, "\tex: telebit tcp none"
].join("\n") + "\n\n");
2018-06-01 09:50:27 +00:00
});
//if (tun.data) { conn.write(tun.data); }
return conn;
}
function defineProps(other, tun) {
Object.defineProperty(other, 'remoteFamily', {
enumerable: false,
configurable: true,
get: function() {
return tun.family;
}
});
Object.defineProperty(other, 'remoteAddress', {
enumerable: false,
configurable: true,
get: function() {
return tun.address;
}
});
Object.defineProperty(other, 'remotePort', {
enumerable: false,
configurable: true,
get: function() {
return parseInt(tun.port);
}
});
Object.defineProperty(other, 'localPort', {
enumerable: false,
configurable: true,
get: function() {
return parseInt(tun.serviceport);
}
});
}
2018-06-08 08:50:00 +00:00
function invokeTcpHandler(conf, socket, tun, id, cb) {
var conn;
if (parseInt(conf.handler, 10)) {
getNetConn(conf.handler, cb);
2018-06-08 08:50:00 +00:00
return conn;
}
var handle = tun.port;
var handler;
var handlerpath = conf.handler;
var homedir = os.homedir();
var localshare = path.join(homedir, '.local/share/telebit/apps');
if (/^~/.test(handlerpath)) {
handlerpath = path.join(homedir, handlerpath.replace(/^~(\/?)/, ''));
}
try {
handler = require(handlerpath);
console.info("Handling '" + handle + ":" + id + "' with '" + handlerpath + "'");
} catch(e1) {
try {
handler = require(path.join(localshare, handlerpath));
console.info("Handling '" + handle + ":" + id + "' with '" + handlerpath + "'");
} catch(e2) {
console.error("Failed to require('" + handlerpath + "'):", e1.message);
console.error("Failed to require('" + path.join(localshare, handlerpath) + "'):", e2.message);
2018-07-01 06:53:26 +00:00
console.warn("Trying static and index handlers for '" + handle + ":" + id + "'");
2018-07-01 07:18:10 +00:00
handler = null;
// fallthru
2018-06-08 08:50:00 +00:00
}
}
2018-07-01 07:18:10 +00:00
if (handler) {
var socketPair = require('socket-pair');
conn = socketPair.create(function (err, other) {
handler(other, tun, id);
cb(null, conn);
});
return conn;
}
fs.access(conf.handler, fs.constants.R_OK, function (err1) {
fs.stat(conf.handler, function (err2, stat) {
2018-07-01 07:35:45 +00:00
if ((err1 || err2) || !(stat.isFile() || stat.isDirectory())) {
2018-07-01 07:18:10 +00:00
errorTcp(conf, cb);
return;
}
2018-07-28 04:30:58 +00:00
fileDirTcp({ config: conf, stat: stat }, cb);
2018-07-01 07:18:10 +00:00
});
2018-06-08 08:50:00 +00:00
});
}
2018-06-09 18:57:00 +00:00
var handlerservers = {};
2018-06-01 07:36:29 +00:00
function invokeHandler(conf, tlsSocket, tun, id) {
2018-06-01 07:36:49 +00:00
if (parseInt(conf.handler, 10)) {
// TODO http-proxy with proper headers and ws support
getNetConn(conf.handler, function (err, conn) {
process.nextTick(function () { tlsSocket.resume(); });
if (err) {
2018-06-30 19:20:00 +00:00
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);
});
2018-06-01 07:36:49 +00:00
return;
}
var handle = tun.name || tun.port;
var handler;
2018-06-08 06:46:07 +00:00
var handlerpath = conf.handler;
var homedir = os.homedir();
2018-06-01 07:36:49 +00:00
var localshare = path.join(homedir, '.local/share/telebit/apps');
2018-06-09 18:57:00 +00:00
var http = require('http');
// 1. No modification handlerpath may be an aboslute path
// 2. it may be relative to a user home directory
2018-06-23 05:52:47 +00:00
// 3. it may be relative to a user ~/local/share
2018-06-09 18:57:00 +00:00
tlsSocket._tun = tun;
tlsSocket._id = id;
if (handlerservers[conf.handler]) {
handlerservers[conf.handler].emit('connection', tlsSocket);
process.nextTick(function () { tlsSocket.resume(); });
2018-06-09 18:57:00 +00:00
return;
}
2018-06-01 07:36:29 +00:00
2018-06-08 06:46:07 +00:00
if (/^~/.test(handlerpath)) {
2018-06-23 05:52:47 +00:00
// TODO have the telebit remote tell which user is running
2018-06-08 06:46:07 +00:00
handlerpath = path.join(homedir, handlerpath.replace(/^~(\/?)/, ''));
2018-06-01 07:36:49 +00:00
}
2018-06-01 07:36:29 +00:00
2018-06-01 07:36:49 +00:00
try {
2018-06-08 06:46:07 +00:00
handler = require(handlerpath);
2018-09-14 05:42:04 +00:00
console.info("Trying to handle '" + handle + ":" + id + "' with '" + handlerpath + "'");
2018-06-01 07:36:49 +00:00
} catch(e1) {
try {
2018-06-08 06:46:07 +00:00
handler = require(path.join(localshare, handlerpath));
2018-09-14 05:42:04 +00:00
console.info("Skip. (couldn't require('" + handlerpath + "'):", e1.message + ")");
console.info("Trying to handle '" + handle + ":" + id + "' with '" + handlerpath + "'");
2018-06-01 07:36:49 +00:00
} catch(e2) {
2018-09-14 05:42:04 +00:00
console.info("Skip. (couldn't require('" + path.join(localshare, handlerpath) + "'):", e2.message + ")");
console.info("Last chance! (using static and index handlers for '" + handle + ":" + id + "')");
2018-06-09 18:57:00 +00:00
handler = null;
// fallthru
2018-06-01 07:36:49 +00:00
}
}
2018-06-09 18:57:00 +00:00
if (handler) {
handlerservers[conf.handler] = http.createServer(handler);
handlerservers[conf.handler].emit('connection', tlsSocket);
process.nextTick(function () { tlsSocket.resume(); });
2018-06-09 18:57:00 +00:00
return;
}
2018-07-01 06:53:26 +00:00
fs.access(conf.handler, fs.constants.R_OK, function (err1) {
fs.stat(conf.handler, function (err2, stat) {
if (err1 || err2) {
// TODO handle errors
handlers.https(tlsSocket, tun, id);
return;
}
var isFile = stat.isFile();
state._finalHandler = require('finalhandler');
state._serveStatic = require('serve-static');
state._serveIndex = require('serve-index');
var serveIndex;
var serveStatic;
var dlStatic;
2018-07-01 06:53:26 +00:00
if (isFile) {
serveStatic = state._serveStatic(path.dirname(conf.handler), { dotfiles: 'allow', index: [ 'index.html' ] });
dlStatic = state._serveStatic(path.dirname(conf.handler), { acceptRanges: false, dotfiles: 'allow', index: [ 'index.html' ] });
2018-07-01 06:53:26 +00:00
serveIndex = function (req, res, next) { next(); };
isFile = path.basename(conf.handler);
} else {
serveStatic = state._serveStatic(conf.handler, { dotfiles: 'allow', index: [ 'index.html' ] });
dlStatic = state._serveStatic(conf.handler, { acceptRanges: false, dotfiles: 'allow', index: [ 'index.html' ] });
serveIndex = state._serveIndex(conf.handler, {
hidden: true, icons: true
, template: require('serve-tpl-attachment')({ privatefiles: 'ignore' })
});
2018-07-01 06:53:26 +00:00
}
handler = function (req, res) {
var qIndex = req.url.indexOf('?');
var fIndex;
var fname;
if (-1 === qIndex) {
qIndex = req.url.length;
}
req.querystring = req.url.substr(qIndex);
req.url = req.url.substr(0, qIndex);
req.query = require('querystring').parse(req.querystring.substr(1));
2018-07-01 06:53:26 +00:00
if (isFile) {
req.url = '/' + isFile;
}
//console.log('[req.query]', req.url, req.query);
if (req.query.download) {
fIndex = req.url.lastIndexOf('/');
fname = req.url.substr(fIndex + 1);
res.setHeader('Content-Disposition', 'attachment; filename="'+decodeURIComponent(fname)+'"');
res.setHeader('Content-Type', 'application/octet-stream');
dlStatic(req, res, function () {
serveIndex(req, res, state._finalHandler(req, res));
});
} else {
serveStatic(req, res, function () {
serveIndex(req, res, state._finalHandler(req, res));
});
}
2018-07-01 06:53:26 +00:00
};
handlerservers[conf.handler] = http.createServer(handler);
handlerservers[conf.handler].emit('connection', tlsSocket);
process.nextTick(function () { tlsSocket.resume(); });
});
});
2018-06-01 07:36:29 +00:00
}
function terminateTls(tun, cb) {
2018-05-31 10:10:47 +00:00
var socketPair = require('socket-pair');
var conn = socketPair.create(function (err, other) {
2018-05-31 10:10:47 +00:00
if (err) { cb(err); return; }
//console.log('[hit tcp connection]', other.remoteFamily, other.remoteAddress, other.remotePort, other.localPort);
defineProps(other, tun);
//console.log('[hit tcp connection]', other.remoteFamily, other.remoteAddress, other.remotePort, other.localPort);
2018-05-31 10:10:47 +00:00
if (!state.greenlock) {
state.greenlock = require('greenlock').create(state.greenlockConfig);
}
2018-06-01 07:36:29 +00:00
2018-05-31 10:10:47 +00:00
if (!state.terminatorServer) {
state.terminatorServer = require('tls').createServer(state.greenlock.tlsOptions, function (tlsSocket) {
var Packer = require('proxy-packer');
var addr = Packer.socketToAddr(tlsSocket);
var id = Packer.addrToId(addr);
defineProps(tlsSocket, addr);
//console.log('[hit tls server]', tlsSocket.remoteFamily, tlsSocket.remoteAddress, tlsSocket.remotePort, tlsSocket.localPort);
//console.log(addr);
2018-06-08 06:46:07 +00:00
var conf = state.servernames[tlsSocket.servername];
2018-06-01 08:45:38 +00:00
tlsSocket.once('data', function (firstChunk) {
tlsSocket.pause();
//tlsSocket.unshift(firstChunk);
tlsSocket._handle.onread(firstChunk.length, firstChunk);
trySsh({ data: firstChunk }, function (err, conn) {
if (conn) {
conn.pipe(tlsSocket);
tlsSocket.pipe(conn);
return;
}
2018-06-08 06:46:07 +00:00
if (!conf || !conf.handler || 'none' === conf.handler) {
2018-06-01 08:45:38 +00:00
console.log('https default handler');
handlers.https(tlsSocket);
return;
}
2018-09-14 05:42:04 +00:00
//console.log('https invokeHandler');
2018-06-01 08:45:38 +00:00
invokeHandler(conf, tlsSocket, tun, id);
});
});
2018-05-31 10:10:47 +00:00
});
}
//console.log('[hit tcp connection]', other.remoteFamily, other.remoteAddress, other.remotePort, other.localPort);
2018-05-31 10:10:47 +00:00
state.terminatorServer.emit('connection', other);
cb(null, conn);
});
//if (tun.data) { conn.write(tun.data); }
return conn;
}
2018-05-31 20:48:12 +00:00
var handled;
if (!tun.name && !tun.serviceport) {
console.log('tun:\n',tun);
//console.warn(tun.data.toString());
cb(new Error("No routing information for ':tun_id'. Missing both 'name' and 'serviceport'."));
return;
}
if ('http' === tun.service || 'https' === tun.service) {
if (!tun.name) {
cb(new Error("No routing information for ':tun_id'. Service '" + tun.service + "' is missing 'name'."));
return;
}
}
if ('http' === tun.service) {
// TODO match *.example.com
2018-06-08 06:46:07 +00:00
handled = Object.keys(state.servernames).some(function (sn) {
2018-05-31 20:48:12 +00:00
if (sn !== tun.name) { return; }
console.log('Found config match for PLAIN', tun.name);
2018-06-08 06:46:07 +00:00
if (!state.servernames[sn]) { return; }
2018-05-31 20:48:12 +00:00
2018-06-08 06:46:07 +00:00
if (false === state.servernames[sn].terminate) {
2018-05-31 20:48:12 +00:00
cb(new Error("insecure http not supported yet"));
return true;
}
console.log('Redirecting HTPTP for', tun.name);
redirectHttp(cb);
return true;
});
if (!handled) {
redirectHttp(cb);
}
return;
}
2018-05-31 10:10:47 +00:00
if ('https' === tun.service) {
// TODO match *.example.com
2018-06-08 06:46:07 +00:00
handled = Object.keys(state.servernames).some(function (sn) {
2018-05-31 10:10:47 +00:00
if (sn !== tun.name) { return; }
console.log('Found config match for TLS', tun.name);
2018-06-08 06:46:07 +00:00
if (!state.servernames[sn]) { return; }
2018-05-31 10:10:47 +00:00
2018-06-08 06:46:07 +00:00
if (false === state.servernames[sn].terminate) {
2018-05-31 10:10:47 +00:00
cb(new Error("insecure http not supported yet"));
return true;
}
console.log('Terminating TLS for', tun.name);
terminateTls(tun, cb);
2018-05-31 10:10:47 +00:00
return true;
});
if (!handled) {
terminateTls(tun, cb);
2018-05-31 10:10:47 +00:00
}
2018-05-31 06:19:53 +00:00
return;
}
2018-06-01 08:10:27 +00:00
if ('tcp' === tun.service) {
2018-06-01 08:45:38 +00:00
trySsh(tun, function (err, conn) {
if (conn) { cb(null, conn); return; }
2018-06-01 09:50:27 +00:00
// TODO add TCP handlers
2018-06-08 06:46:07 +00:00
var conf = state.ports[tun.serviceport];
if (!conf || !conf.handler || 'none' === conf.handler) {
2018-06-08 08:50:00 +00:00
console.log('Using echo server for tcp');
2018-06-08 06:46:07 +00:00
echoTcp(cb);
2018-06-08 08:50:00 +00:00
return;
2018-06-08 06:46:07 +00:00
}
var Packer = require('proxy-packer');
//var addr = Packer.socketToAddr(conn);
var id = Packer.addrToId(tun);
2018-06-08 08:50:00 +00:00
invokeTcpHandler(conf, conn, tun, id, cb);
2018-06-01 08:45:38 +00:00
});
2018-06-01 09:50:27 +00:00
return;
2018-06-01 08:10:27 +00:00
}
console.warn("Unknown service '" + tun.service + "'");
/*
2018-05-31 10:10:47 +00:00
var portList = state.services[service];
var port;
2018-05-31 06:19:53 +00:00
port = portList[tun.name];
if (!port) {
// Check for any wildcard domains, sorted longest to shortest so the one with the
// biggest natural match will be found first.
Object.keys(portList).filter(function (pattern) {
return pattern[0] === '*' && pattern.length > 1;
}).sort(function (a, b) {
return b.length - a.length;
}).some(function (pattern) {
var subPiece = pattern.slice(1);
if (subPiece === tun.name.slice(-subPiece.length)) {
port = portList[pattern];
return true;
}
});
}
if (!port) {
port = portList['*'];
}
*/
2018-05-31 06:19:53 +00:00
};