2018-04-25 17:41:20 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-31 05:24:43 +00:00
|
|
|
var Packer = require('proxy-packer');
|
2018-04-25 17:41:20 +00:00
|
|
|
var sni = require('sni');
|
|
|
|
|
2018-05-31 06:12:37 +00:00
|
|
|
function pipeWs(servername, service, conn, remote, serviceport) {
|
2018-04-25 17:41:20 +00:00
|
|
|
console.log('[pipeWs] servername:', servername, 'service:', service);
|
|
|
|
|
2018-05-31 05:24:43 +00:00
|
|
|
var browserAddr = Packer.socketToAddr(conn);
|
2018-04-25 17:41:20 +00:00
|
|
|
browserAddr.service = service;
|
2018-05-31 06:12:37 +00:00
|
|
|
browserAddr.serviceport = serviceport;
|
|
|
|
browserAddr.name = servername;
|
2018-05-31 05:24:43 +00:00
|
|
|
var cid = Packer.addrToId(browserAddr);
|
2018-04-25 17:41:20 +00:00
|
|
|
conn.tunnelCid = cid;
|
2018-05-31 05:24:43 +00:00
|
|
|
console.log('[pipeWs] browser is', cid, 'home-cloud is', Packer.socketToId(remote.upgradeReq.socket));
|
2018-04-25 17:41:20 +00:00
|
|
|
|
|
|
|
function sendWs(data, serviceOverride) {
|
|
|
|
if (remote.ws && (!conn.tunnelClosing || serviceOverride)) {
|
|
|
|
try {
|
2018-05-31 05:24:43 +00:00
|
|
|
remote.ws.send(Packer.pack(browserAddr, data, serviceOverride), { binary: true });
|
2018-04-25 17:41:20 +00:00
|
|
|
// If we can't send data over the websocket as fast as this connection can send it to us
|
|
|
|
// (or there are a lot of connections trying to send over the same websocket) then we
|
|
|
|
// need to pause the connection for a little. We pause all connections if any are paused
|
|
|
|
// to make things more fair so a connection doesn't get stuck waiting for everyone else
|
|
|
|
// to finish because it got caught on the boundary. Also if serviceOverride is set it
|
|
|
|
// means the connection is over, so no need to pause it.
|
|
|
|
if (!serviceOverride && (remote.pausedConns.length || remote.ws.bufferedAmount > 1024*1024)) {
|
|
|
|
// console.log('pausing', cid, 'to allow web socket to catch up');
|
|
|
|
conn.pause();
|
|
|
|
remote.pausedConns.push(conn);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('[pipeWs] error sending websocket message', err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.clients[cid] = conn;
|
|
|
|
conn.on('data', function (chunk) {
|
|
|
|
console.log('[pipeWs] data from browser to tunneler', chunk.byteLength);
|
|
|
|
sendWs(chunk);
|
|
|
|
});
|
|
|
|
conn.on('error', function (err) {
|
|
|
|
console.warn('[pipeWs] browser connection error', err);
|
|
|
|
});
|
|
|
|
conn.on('close', function (hadErr) {
|
|
|
|
console.log('[pipeWs] browser connection closing');
|
|
|
|
sendWs(null, hadErr ? 'error': 'end');
|
|
|
|
delete remote.clients[cid];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.createTcpConnectionHandler = function (copts) {
|
|
|
|
var Devices = copts.Devices;
|
|
|
|
|
2018-05-31 06:12:37 +00:00
|
|
|
return function onTcpConnection(conn, serviceport) {
|
2018-05-31 06:18:02 +00:00
|
|
|
serviceport = serviceport || conn.localPort;
|
2018-04-25 17:41:20 +00:00
|
|
|
// this works when I put it here, but I don't know if it's tls yet here
|
|
|
|
// httpsServer.emit('connection', socket);
|
|
|
|
//tls3000.emit('connection', socket);
|
|
|
|
|
|
|
|
//var tlsSocket = new tls.TLSSocket(socket, { secureContext: tls.createSecureContext(tlsOpts) });
|
|
|
|
//tlsSocket.on('data', function (chunk) {
|
|
|
|
// console.log('dummy', chunk.byteLength);
|
|
|
|
//});
|
|
|
|
|
|
|
|
//return;
|
|
|
|
conn.once('data', function (firstChunk) {
|
2018-05-26 21:21:03 +00:00
|
|
|
conn.pause();
|
|
|
|
conn.unshift(firstChunk);
|
|
|
|
|
2018-04-25 17:41:20 +00:00
|
|
|
// BUG XXX: this assumes that the packet won't be chunked smaller
|
|
|
|
// than the 'hello' or the point of the 'Host' header.
|
|
|
|
// This is fairly reasonable, but there are edge cases where
|
|
|
|
// it does not hold (such as manual debugging with telnet)
|
|
|
|
// and so it should be fixed at some point in the future
|
|
|
|
|
|
|
|
// defer after return (instead of being in many places)
|
2018-05-26 21:21:03 +00:00
|
|
|
function deferData(fn) {
|
|
|
|
if (fn) {
|
|
|
|
copts[fn](servername, conn)
|
|
|
|
}
|
|
|
|
process.nextTick(function () {
|
|
|
|
conn.resume();
|
|
|
|
});
|
|
|
|
}
|
2018-04-25 17:41:20 +00:00
|
|
|
|
|
|
|
var service = 'tcp';
|
|
|
|
var servername;
|
|
|
|
var str;
|
|
|
|
var m;
|
|
|
|
|
|
|
|
function tryTls() {
|
2018-05-24 19:19:50 +00:00
|
|
|
var vhost;
|
|
|
|
|
|
|
|
console.log("");
|
|
|
|
|
2018-05-23 11:12:39 +00:00
|
|
|
if (!copts.servernames.length) {
|
|
|
|
console.log("https => admin => setup => (needs bogus tls certs to start?)");
|
2018-05-26 21:21:03 +00:00
|
|
|
deferData('httpsSetupServer');
|
2018-05-23 11:12:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:41:20 +00:00
|
|
|
if (-1 !== copts.servernames.indexOf(servername)) {
|
|
|
|
console.log("Lock and load, admin interface time!");
|
2018-05-26 21:21:03 +00:00
|
|
|
deferData('httpsTunnel');
|
2018-04-25 17:41:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-24 19:19:50 +00:00
|
|
|
if (copts.config.nowww && /^www\./i.test(servername)) {
|
|
|
|
console.log("TODO: use www bare redirect");
|
|
|
|
}
|
|
|
|
|
|
|
|
function run() {
|
|
|
|
if (!servername) {
|
|
|
|
console.log("No SNI was given, so there's nothing we can do here");
|
2018-05-26 21:21:03 +00:00
|
|
|
deferData('httpsInvalid');
|
2018-05-24 19:19:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var nextDevice = Devices.next(copts.deviceLists, servername);
|
|
|
|
if (!nextDevice) {
|
|
|
|
console.log("No devices match the given servername");
|
2018-05-26 21:21:03 +00:00
|
|
|
deferData('httpsInvalid');
|
2018-05-24 19:19:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("pipeWs(servername, service, socket, deviceLists['" + servername + "'])");
|
2018-05-26 21:21:03 +00:00
|
|
|
deferData();
|
2018-05-31 06:12:37 +00:00
|
|
|
pipeWs(servername, service, conn, nextDevice, serviceport);
|
2018-04-25 17:41:20 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 19:19:50 +00:00
|
|
|
if (copts.config.vhost) {
|
|
|
|
console.log("VHOST path", copts.config.vhost);
|
|
|
|
vhost = copts.config.vhost.replace(/:hostname/, (servername||''));
|
|
|
|
console.log("VHOST name", vhost);
|
|
|
|
//copts.httpsVhost(servername, conn);
|
|
|
|
//return;
|
|
|
|
require('fs').readdir(vhost, function (err, nodes) {
|
|
|
|
console.log("VHOST error?", err);
|
|
|
|
if (err) { run(); return; }
|
2018-05-26 21:21:03 +00:00
|
|
|
if (nodes) { deferData('httpsVhost'); }
|
2018-05-24 19:19:50 +00:00
|
|
|
});
|
2018-04-25 17:41:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-24 19:19:50 +00:00
|
|
|
run();
|
2018-04-25 17:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/mscdex/httpolyglot/issues/3#issuecomment-173680155
|
|
|
|
if (22 === firstChunk[0]) {
|
|
|
|
// TLS
|
|
|
|
service = 'https';
|
|
|
|
servername = (sni(firstChunk)||'').toLowerCase();
|
|
|
|
console.log("tls hello servername:", servername);
|
|
|
|
tryTls();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstChunk[0] > 32 && firstChunk[0] < 127) {
|
|
|
|
str = firstChunk.toString();
|
|
|
|
m = str.match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im);
|
|
|
|
servername = (m && m[1].toLowerCase() || '').split(':')[0];
|
|
|
|
console.log('servername', servername);
|
2018-05-23 11:12:39 +00:00
|
|
|
|
2018-04-25 17:41:20 +00:00
|
|
|
if (/HTTP\//i.test(str)) {
|
2018-05-23 11:12:39 +00:00
|
|
|
if (!copts.servernames.length) {
|
|
|
|
console.log('copts.httpSetupServer', copts.httpSetupServer);
|
2018-05-26 21:21:03 +00:00
|
|
|
deferData();
|
2018-05-23 11:12:39 +00:00
|
|
|
copts.httpSetupServer.emit('connection', conn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:41:20 +00:00
|
|
|
service = 'http';
|
2018-05-23 11:12:39 +00:00
|
|
|
// TODO make https redirect configurable
|
2018-04-25 17:41:20 +00:00
|
|
|
// /^\/\.well-known\/acme-challenge\//.test(str)
|
|
|
|
if (/well-known/.test(str)) {
|
|
|
|
// HTTP
|
|
|
|
if (Devices.exist(copts.deviceLists, servername)) {
|
2018-05-26 21:21:03 +00:00
|
|
|
deferData();
|
2018-05-31 06:12:37 +00:00
|
|
|
pipeWs(servername, service, conn, Devices.next(copts.deviceLists, servername), serviceport);
|
2018-04-25 17:41:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-26 21:21:03 +00:00
|
|
|
deferData('handleHttp');
|
|
|
|
return;
|
2018-04-25 17:41:20 +00:00
|
|
|
}
|
2018-05-26 21:21:03 +00:00
|
|
|
|
|
|
|
// redirect to https
|
|
|
|
deferData('handleInsecureHttp');
|
2018-04-25 17:41:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.error("Got unexpected connection", str);
|
|
|
|
conn.write(JSON.stringify({ error: {
|
|
|
|
message: "not sure what you were trying to do there..."
|
|
|
|
, code: 'E_INVALID_PROTOCOL' }
|
|
|
|
}));
|
|
|
|
conn.end();
|
|
|
|
});
|
|
|
|
conn.on('error', function (err) {
|
|
|
|
console.error('[error] tcp socket raw TODO forward and close');
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|