telebit-relay.js/wstunneld.js

504 lines
16 KiB
JavaScript
Raw Normal View History

2016-09-27 21:56:06 +00:00
'use strict';
var sni = require('sni');
var url = require('url');
var PromiseA = require('bluebird');
2016-09-27 21:56:06 +00:00
var jwt = require('jsonwebtoken');
2016-09-28 05:26:16 +00:00
var packer = require('tunnel-packer');
function timeoutPromise(duration) {
return new PromiseA(function (resolve) {
setTimeout(resolve, duration);
});
}
2017-04-05 02:31:58 +00:00
var Devices = {};
Devices.add = function (store, servername, newDevice) {
2017-05-26 22:33:27 +00:00
var devices = store[servername] || [];
devices.push(newDevice);
store[servername] = devices;
2017-04-05 02:31:58 +00:00
};
Devices.remove = function (store, servername, device) {
2017-05-26 22:33:27 +00:00
var devices = store[servername] || [];
var index = devices.indexOf(device);
if (index < 0) {
console.warn('attempted to remove non-present device', device.deviceId, 'from', servername);
return null;
}
return devices.splice(index, 1)[0];
2017-04-05 02:31:58 +00:00
};
Devices.list = function (store, servername) {
if (store[servername] && store[servername].length) {
2017-05-26 22:33:27 +00:00
return store[servername];
}
// There wasn't an exact match so check any of the wildcard domains, sorted longest
// first so the one with the biggest natural match with be found first.
var deviceList = [];
Object.keys(store).filter(function (pattern) {
return pattern[0] === '*' && store[pattern].length;
2017-05-26 22:33:27 +00:00
}).sort(function (a, b) {
return b.length - a.length;
}).some(function (pattern) {
var subPiece = pattern.slice(1);
if (subPiece === servername.slice(-subPiece.length)) {
console.log('"'+servername+'" matches "'+pattern+'"');
deviceList = store[pattern];
return true;
}
});
return deviceList;
2017-04-05 02:31:58 +00:00
};
Devices.exist = function (store, servername) {
2017-05-26 22:33:27 +00:00
return !!(Devices.list(store, servername).length);
2017-04-05 02:31:58 +00:00
};
Devices.next = function (store, servername) {
var devices = Devices.list(store, servername);
var device;
2017-04-05 02:31:58 +00:00
if (devices._index >= devices.length) {
devices._index = 0;
}
device = devices[devices._index || 0];
2017-04-05 02:31:58 +00:00
devices._index = (devices._index || 0) + 1;
return device;
2017-04-05 02:31:58 +00:00
};
module.exports.store = { Devices: Devices };
2016-10-13 00:16:14 +00:00
module.exports.create = function (copts) {
2017-04-05 02:31:58 +00:00
var deviceLists = {};
var activityTimeout = copts.activityTimeout || 2*60*1000;
var pongTimeout = copts.pongTimeout || 10*1000;
2016-09-27 21:56:06 +00:00
2016-10-01 05:52:37 +00:00
function onWsConnection(ws) {
var socketId = packer.socketToId(ws.upgradeReq.socket);
var remotes = {};
2017-04-05 02:31:58 +00:00
function logName() {
var result = Object.keys(remotes).map(function (jwtoken) {
return remotes[jwtoken].deviceId;
}).join(';');
2016-09-30 19:15:58 +00:00
return result || socketId;
2016-09-27 21:56:06 +00:00
}
function closeBrowserConn(cid) {
var remote;
Object.keys(remotes).some(function (jwtoken) {
if (remotes[jwtoken].clients[cid]) {
remote = remotes[jwtoken];
return true;
}
});
if (!remote) {
return;
}
remote.closing[cid] = true;
PromiseA.resolve()
.then(function () {
remote.clients[cid].end();
return timeoutPromise(500);
})
.then(function () {
if (remote.clients[cid]) {
console.warn(cid, 'browser connection still present after calling `end`');
remote.clients[cid].destroy();
return timeoutPromise(500);
}
})
.then(function () {
if (remote.clients[cid]) {
console.error(cid, 'browser connection still present after calling `destroy`');
delete remote.clients[cid];
}
})
.catch(function (err) {
console.warn('failed to close browser connection', cid, err);
})
.then(function () {
delete remote.closing[cid];
})
;
}
function addToken(jwtoken) {
if (remotes[jwtoken]) {
// return { message: "token sent multiple times", code: "E_TOKEN_REPEAT" };
return null;
}
var token;
try {
token = jwt.verify(jwtoken, copts.secret);
} catch (e) {
token = null;
}
if (!token) {
return { message: "invalid access token", code: "E_INVALID_TOKEN" };
}
if (!Array.isArray(token.domains)) {
if ('string' === typeof token.name) {
token.domains = [ token.name ];
}
}
2017-05-26 22:33:27 +00:00
if (!Array.isArray(token.domains) || !token.domains.length) {
return { message: "invalid server name", code: "E_INVALID_NAME" };
}
if (token.domains.some(function (name) { return typeof name !== 'string'; })) {
return { message: "invalid server name", code: "E_INVALID_NAME" };
}
// Add the custom properties we need to manage this remote, then add it to all the relevant
// domains and the list of all this websocket's remotes.
token.deviceId = (token.device && (token.device.id || token.device.hostname)) || token.domains.join(',');
token.ws = ws;
token.clients = {};
token.closing = {};
token.domains.forEach(function (domainname) {
console.log('domainname', domainname);
Devices.add(deviceLists, domainname, token);
});
remotes[jwtoken] = token;
console.log("added token '" + token.deviceId + "' to websocket", socketId);
return null;
}
function removeToken(jwtoken) {
var remote = remotes[jwtoken];
if (!remote) {
return { message: 'specified token not present', code: 'E_INVALID_TOKEN'};
}
// Prevent any more browser connections being sent to this remote, and any existing
// connections from trying to send more data across the connection.
remote.domains.forEach(function (domainname) {
Devices.remove(deviceLists, domainname, remote);
});
remote.ws = null;
// Close all of the existing browser connections associated with this websocket connection.
Object.keys(remote.clients).forEach(function (cid) {
closeBrowserConn(cid);
});
delete remotes[jwtoken];
console.log("removed token '" + remote.deviceId + "' from websocket", socketId);
return null;
}
var firstToken;
var authn = (ws.upgradeReq.headers.authorization||'').split(/\s+/);
if (authn[0] && 'basic' === authn[0].toLowerCase()) {
try {
authn = new Buffer(authn[1], 'base64').toString('ascii').split(':');
firstToken = authn[1];
} catch (err) { }
}
if (!firstToken) {
firstToken = url.parse(ws.upgradeReq.url, true).query.access_token;
}
if (firstToken) {
var err = addToken(firstToken);
if (err) {
ws.send(packer.pack(null, [0, err], 'control'));
ws.close();
return;
}
}
var commandHandlers = {
add_token: addToken
, delete_token: function (token) {
if (token !== '*') {
return removeToken(token);
}
var err;
Object.keys(remotes).some(function (jwtoken) {
err = removeToken(jwtoken);
return err;
});
return err;
}
};
var packerHandlers = {
oncontrol: function (opts) {
var cmd, err;
try {
cmd = JSON.parse(opts.data.toString());
} catch (err) {}
if (!Array.isArray(cmd) || typeof cmd[0] !== 'number') {
var msg = 'received bad command "' + opts.data.toString() + '"';
console.warn(msg, 'from websocket', socketId);
ws.send(packer.pack(null, [0, {message: msg, code: 'E_BAD_COMMAND'}], 'control'));
return;
}
if (cmd[0] < 0) {
// We only ever send one command and we send it once, so we just hard coded the ID as 1.
if (cmd[0] === -1) {
if (cmd[1]) {
console.log('received error response to hello from', socketId, cmd[1]);
}
}
else {
console.warn('received response to unknown command', cmd, 'from', socketId);
}
return;
}
if (cmd[0] === 0) {
console.warn('received dis-associated error from', socketId, cmd[1]);
return;
}
if (commandHandlers[cmd[1]]) {
err = commandHandlers[cmd[1]].apply(null, cmd.slice(2));
}
else {
err = { message: 'unknown command "'+cmd[1]+'"', code: 'E_UNKNOWN_COMMAND' };
}
ws.send(packer.pack(null, [-cmd[0], err], 'control'));
}
, onmessage: function (opts) {
2016-10-01 05:52:37 +00:00
var cid = packer.addrToId(opts);
console.log("remote '" + logName() + "' has data for '" + cid + "'", opts.data.byteLength);
2016-09-30 06:46:02 +00:00
var browserConn;
Object.keys(remotes).some(function (jwtoken) {
if (remotes[jwtoken].clients[cid]) {
browserConn = remotes[jwtoken].clients[cid];
return true;
}
});
2016-09-30 06:46:02 +00:00
if (browserConn) {
browserConn.write(opts.data);
}
else {
ws.send(packer.pack(opts, null, 'error'));
2016-09-30 06:46:02 +00:00
}
}
, onend: function (opts) {
2016-10-01 05:52:37 +00:00
var cid = packer.addrToId(opts);
2016-09-30 06:46:02 +00:00
console.log('[TunnelEnd]', cid);
closeBrowserConn(cid);
2016-09-30 06:46:02 +00:00
}
, onerror: function (opts) {
2016-10-01 05:52:37 +00:00
var cid = packer.addrToId(opts);
2016-09-30 06:46:02 +00:00
console.log('[TunnelError]', cid);
closeBrowserConn(cid);
2016-09-30 06:46:02 +00:00
}
};
var unpacker = packer.create(packerHandlers);
2017-04-05 02:31:58 +00:00
var lastActivity = Date.now();
var timeoutId;
function refreshTimeout() {
lastActivity = Date.now();
}
function checkTimeout() {
// Determine how long the connection has been "silent", ie no activity.
var silent = Date.now() - lastActivity;
// If we have had activity within the last activityTimeout then all we need to do is
// call this function again at the soonest time when the connection could be timed out.
if (silent < activityTimeout) {
timeoutId = setTimeout(checkTimeout, activityTimeout-silent);
}
// Otherwise we check to see if the pong has also timed out, and if not we send a ping
// and call this function again when the pong will have timed out.
else if (silent < activityTimeout + pongTimeout) {
console.log('pinging', logName());
try {
ws.ping();
} catch (err) {
console.warn('failed to ping home cloud', logName());
}
timeoutId = setTimeout(checkTimeout, pongTimeout);
}
// Last case means the ping we sent before didn't get a response soon enough, so we
// need to close the websocket connection.
else {
console.log('home cloud', logName(), 'connection timed out');
ws.close(1013, 'connection timeout');
}
}
timeoutId = setTimeout(checkTimeout, activityTimeout);
// Note that our websocket library automatically handles pong responses on ping requests
// before it even emits the event.
ws.on('ping', refreshTimeout);
ws.on('pong', refreshTimeout);
ws.on('message', function forwardMessage(chunk) {
refreshTimeout();
2016-09-30 02:20:38 +00:00
console.log('message from home cloud to tunneler to browser', chunk.byteLength);
2016-09-30 07:11:24 +00:00
//console.log(chunk.toString());
unpacker.fns.addChunk(chunk);
});
2017-04-05 02:31:58 +00:00
function hangup() {
clearTimeout(timeoutId);
console.log('home cloud', logName(), 'connection closing');
Object.keys(remotes).forEach(function (jwtoken) {
removeToken(jwtoken);
});
ws.terminate();
2017-04-05 02:31:58 +00:00
}
2016-09-28 05:26:16 +00:00
2017-04-05 02:31:58 +00:00
ws.on('close', hangup);
ws.on('error', hangup);
// We only ever send one command and we send it once, so we just hard code the ID as 1
ws.send(packer.pack(null, [1, 'hello', [unpacker._version], Object.keys(commandHandlers)], 'control'));
}
2016-09-27 21:56:06 +00:00
function pipeWs(servername, service, browserConn, remote) {
console.log('[pipeWs] servername:', servername, 'service:', service);
var browserAddr = packer.socketToAddr(browserConn);
browserAddr.service = service;
var cid = packer.addrToId(browserAddr);
console.log('[pipeWs] browser is', cid, 'home-cloud is', packer.socketToId(remote.ws.upgradeReq.socket));
function sendWs(data, serviceOverride) {
if (remote.ws && !remote.closing[cid]) {
2016-10-12 23:11:11 +00:00
try {
remote.ws.send(packer.pack(browserAddr, data, serviceOverride), { binary: true });
} catch (err) {
console.warn('[pipeWs] error sending websocket message', err);
2016-10-12 23:11:11 +00:00
}
}
}
remote.clients[cid] = browserConn;
browserConn.on('data', function (chunk) {
console.log('[pipeWs] data from browser to tunneler', chunk.byteLength);
sendWs(chunk);
2016-09-30 02:20:38 +00:00
});
browserConn.on('error', function (err) {
console.warn('[pipeWs] browser connection error', err);
2016-09-30 02:20:38 +00:00
});
browserConn.on('close', function (hadErr) {
console.log('[pipeWs] browser connection closing');
sendWs(null, hadErr ? 'error': 'end');
2016-09-30 02:20:38 +00:00
delete remote.clients[cid];
});
}
2016-10-01 05:52:37 +00:00
function onTcpConnection(browser) {
2016-09-27 21:56:06 +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;
2016-09-30 02:20:38 +00:00
browser.once('data', function (firstChunk) {
2016-09-27 21:56:06 +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)
process.nextTick(function () {
2016-09-30 02:20:38 +00:00
browser.unshift(firstChunk);
2016-09-27 21:56:06 +00:00
});
var service = 'tcp';
var servername;
var str;
var m;
function tryTls() {
2017-04-05 02:31:58 +00:00
if (-1 !== copts.servernames.indexOf(servername)) {
2017-04-05 08:13:03 +00:00
console.log("Lock and load, admin interface time!");
2017-04-05 02:31:58 +00:00
copts.httpsTunnel(servername, browser);
return;
}
2017-04-05 08:13:03 +00:00
if (!servername) {
console.log("No SNI was given, so there's nothing we can do here");
copts.httpsInvalid(servername, browser);
return;
}
var nextDevice = Devices.next(deviceLists, servername);
2017-04-05 08:13:03 +00:00
if (!nextDevice) {
console.log("No devices match the given servername");
2017-04-05 02:31:58 +00:00
copts.httpsInvalid(servername, browser);
2016-09-27 21:56:06 +00:00
return;
}
2017-04-05 02:31:58 +00:00
console.log("pipeWs(servername, service, socket, deviceLists['" + servername + "'])");
pipeWs(servername, service, browser, nextDevice);
2016-09-27 21:56:06 +00:00
}
// https://github.com/mscdex/httpolyglot/issues/3#issuecomment-173680155
if (22 === firstChunk[0]) {
// TLS
service = 'https';
servername = (sni(firstChunk)||'').toLowerCase();
2016-09-30 02:20:38 +00:00
console.log("tls hello servername:", servername);
2016-09-27 21:56:06 +00:00
tryTls();
return;
}
if (firstChunk[0] > 32 && firstChunk[0] < 127) {
str = firstChunk.toString();
2016-09-30 02:20:38 +00:00
m = str.match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im);
servername = (m && m[1].toLowerCase() || '').split(':')[0];
console.log('servername', servername);
2016-09-27 21:56:06 +00:00
if (/HTTP\//i.test(str)) {
service = 'http';
// TODO disallow http entirely
// /^\/\.well-known\/acme-challenge\//.test(str)
2017-04-05 02:31:58 +00:00
if (/well-known/.test(str)) {
2016-09-27 21:56:06 +00:00
// HTTP
2017-04-05 02:31:58 +00:00
if (Devices.exist(deviceLists, servername)) {
pipeWs(servername, service, browser, Devices.next(deviceLists, servername));
2016-09-28 05:26:16 +00:00
return;
}
2017-04-05 08:13:03 +00:00
copts.handleHttp(servername, browser);
2016-09-27 21:56:06 +00:00
}
else {
// redirect to https
2017-04-05 02:31:58 +00:00
copts.handleInsecureHttp(servername, browser);
2016-09-27 21:56:06 +00:00
}
return;
}
}
console.error("Got unexpected connection", str);
2016-09-30 02:20:38 +00:00
browser.write(JSON.stringify({ error: {
2016-09-27 21:56:06 +00:00
message: "not sure what you were trying to do there..."
, code: 'E_INVALID_PROTOCOL' }
}));
2016-09-30 02:20:38 +00:00
browser.end();
2016-09-27 21:56:06 +00:00
});
2016-09-30 07:11:24 +00:00
browser.on('error', function (err) {
console.error('[error] tcp socket raw TODO forward and close');
console.error(err);
});
2016-10-01 05:52:37 +00:00
}
return {
tcp: onTcpConnection
, ws: onWsConnection
, isClientDomain: Devices.exist.bind(null, deviceLists)
};
2016-10-01 05:52:37 +00:00
};