telebit-relay.js/handlers.js

115 lines
4.2 KiB
JavaScript
Raw Permalink Normal View History

2017-04-05 02:31:58 +00:00
'use strict';
var http = require('http');
var tls = require('tls');
var packerStream = require('tunnel-packer').Stream;
var redirectHttps = require('redirect-https')();
2017-04-05 02:31:58 +00:00
module.exports.create = function (program) {
2017-04-05 08:13:03 +00:00
var tunnelAdminTlsOpts = {};
2017-04-05 08:18:35 +00:00
// Probably a reverse proxy on an internal network (or ACME challenge)
function notFound(req, res) {
2017-04-05 02:31:58 +00:00
console.log('req.socket.encrypted', req.socket.encrypted);
2017-04-05 08:13:03 +00:00
res.statusCode = 404;
res.end("File not found.\n");
2017-04-05 08:18:35 +00:00
}
program.httpServer = http.createServer(
program.greenlock && program.greenlock.middleware(notFound)
|| notFound
);
2017-04-05 08:13:03 +00:00
program.handleHttp = function (servername, socket) {
console.log("handleHttp('" + servername + "', socket)");
socket.__my_servername = servername;
program.httpServer.emit('connection', socket);
};
2017-04-05 08:13:03 +00:00
// Probably something that needs to be redirected to https
2017-04-05 08:18:35 +00:00
function redirectHttpsAndClose(req, res) {
res.setHeader('Connection', 'close');
redirectHttps(req, res);
2017-04-05 08:18:35 +00:00
}
program.httpInsecureServer = http.createServer(
program.greenlock && program.greenlock.middleware(redirectHttpsAndClose)
|| redirectHttpsAndClose
);
2017-04-05 08:13:03 +00:00
program.handleInsecureHttp = function (servername, socket) {
console.log("handleInsecureHttp('" + servername + "', socket)");
socket.__my_servername = servername;
program.httpInsecureServer.emit('connection', socket);
};
//
// SNI is not recogonized / cannot be handled
//
2017-04-05 02:31:58 +00:00
program.httpInvalidSniServer = http.createServer(function (req, res) {
res.end("You're doing strange things that make me feel uncomfortable. Please don't touch me there any more.");
});
program.tlsInvalidSniServer = tls.createServer(program.tlsOptions, function (tlsSocket) {
console.log('tls connection');
// things get a little messed up here
program.httpInvalidSniServer.emit('connection', tlsSocket);
});
program.httpsInvalid = function (servername, socket) {
// none of these methods work:
// httpsServer.emit('connection', socket); // this didn't work
// tlsServer.emit('connection', socket); // this didn't work either
//console.log('chunkLen', firstChunk.byteLength);
var myDuplex = packerStream.create(socket);
console.log('httpsInvalid servername', servername);
program.tlsInvalidSniServer.emit('connection', myDuplex);
socket.on('data', function (chunk) {
console.log('[' + Date.now() + '] socket data', chunk.byteLength);
myDuplex.push(chunk);
});
socket.on('error', function (err) {
console.error('[error] httpsInvalid TODO close');
console.error(err);
});
};
2017-04-05 08:13:03 +00:00
//
// To ADMIN / CONTROL PANEL of the Tunnel Server Itself
//
program.httpTunnelServer = http.createServer(function (req, res) {
console.log('req.socket.encrypted', req.socket.encrypted);
res.end('Hello, World!');
});
Object.keys(program.tlsOptions).forEach(function (key) {
tunnelAdminTlsOpts[key] = program.tlsOptions[key];
});
tunnelAdminTlsOpts.SNICallback = (program.greenlock && program.greenlock.httpsOptions && function (servername, cb) {
console.log("time to handle '" + servername + "'");
program.greenlock.httpsOptions.SNICallback(servername, cb);
}) || tunnelAdminTlsOpts.SNICallback;
program.tlsTunnelServer = tls.createServer(tunnelAdminTlsOpts, function (tlsSocket) {
console.log('tls connection');
// things get a little messed up here
(program.httpTunnelServer || program.httpServer).emit('connection', tlsSocket);
});
2017-04-05 02:31:58 +00:00
program.httpsTunnel = function (servername, socket) {
// none of these methods work:
// httpsServer.emit('connection', socket); // this didn't work
// tlsServer.emit('connection', socket); // this didn't work either
//console.log('chunkLen', firstChunk.byteLength);
var myDuplex = packerStream.create(socket);
2017-04-05 08:13:03 +00:00
console.log('httpsTunnel (Admin) servername', servername);
2017-04-05 02:31:58 +00:00
program.tlsTunnelServer.emit('connection', myDuplex);
socket.on('data', function (chunk) {
console.log('[' + Date.now() + '] socket data', chunk.byteLength);
myDuplex.push(chunk);
});
socket.on('error', function (err) {
2017-04-05 08:13:03 +00:00
console.error('[error] httpsTunnel (Admin) TODO close');
2017-04-05 02:31:58 +00:00
console.error(err);
});
};
};