From 8be76e1eb2abe639e62537cbc4f13aab15aa43a5 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 19 Jun 2018 23:43:28 +0000 Subject: [PATCH] major refactor (only briefly tested) --- lib/pipe-ws.js | 23 +- lib/relay.js | 1012 +++++++++++++++++++++++---------------------- lib/unwrap-tls.js | 22 +- 3 files changed, 541 insertions(+), 516 deletions(-) diff --git a/lib/pipe-ws.js b/lib/pipe-ws.js index 089bd89..9efe374 100644 --- a/lib/pipe-ws.js +++ b/lib/pipe-ws.js @@ -2,42 +2,45 @@ var Packer = require('proxy-packer'); -module.exports = function pipeWs(servername, service, conn, remote, serviceport) { +module.exports = function pipeWs(servername, service, srv, conn, serviceport) { var browserAddr = Packer.socketToAddr(conn); var cid = Packer.addrToId(browserAddr); browserAddr.service = service; browserAddr.serviceport = serviceport; browserAddr.name = servername; conn.tunnelCid = cid; - var rid = Packer.socketToId(remote.upgradeReq.socket); + var rid = Packer.socketToId(srv.upgradeReq.socket); //if (state.debug) { console.log('[pipeWs] client', cid, '=> remote', rid, 'for', servername, 'via', service); } function sendWs(data, serviceOverride) { - if (remote.ws && (!conn.tunnelClosing || serviceOverride)) { + if (srv.ws && (!conn.tunnelClosing || serviceOverride)) { try { - remote.ws.send(Packer.pack(browserAddr, data, serviceOverride), { binary: true }); + srv.ws.send(Packer.pack(browserAddr, data, serviceOverride), { binary: true }); // 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)) { + if (!serviceOverride && (srv.pausedConns.length || srv.ws.bufferedAmount > 1024*1024)) { // console.log('pausing', cid, 'to allow web socket to catch up'); conn.pause(); - remote.pausedConns.push(conn); + srv.pausedConns.push(conn); } } catch (err) { - console.warn('[pipeWs] remote', rid, ' => client', cid, 'error sending websocket message', err); + console.warn('[pipeWs] srv', rid, ' => client', cid, 'error sending websocket message', err); } } } - remote.clients[cid] = conn; + srv.clients[cid] = conn; + conn.servername = servername; + conn.serviceport = serviceport; + conn.service = service; conn.on('data', function (chunk) { - //if (state.debug) { console.log('[pipeWs] client', cid, ' => remote', rid, chunk.byteLength, 'bytes'); } + //if (state.debug) { console.log('[pipeWs] client', cid, ' => srv', rid, chunk.byteLength, 'bytes'); } sendWs(chunk); }); @@ -48,7 +51,7 @@ module.exports = function pipeWs(servername, service, conn, remote, serviceport) conn.on('close', function (hadErr) { //if (state.debug) { console.log('[pipeWs] client', cid, 'closing'); } sendWs(null, hadErr ? 'error': 'end'); - delete remote.clients[cid]; + delete srv.clients[cid]; }); }; diff --git a/lib/relay.js b/lib/relay.js index 3760e5f..95de678 100644 --- a/lib/relay.js +++ b/lib/relay.js @@ -2,9 +2,9 @@ var url = require('url'); var PromiseA = require('bluebird'); -var jwt = require('jsonwebtoken'); +var sni = require('sni'); var Packer = require('proxy-packer'); -var portServers = {}; +var PortServers = {}; function timeoutPromise(duration) { return new PromiseA(function (resolve) { @@ -15,34 +15,364 @@ function timeoutPromise(duration) { var Devices = require('./device-tracker'); var pipeWs = require('./pipe-ws.js'); -module.exports.store = { Devices: Devices }; -module.exports.create = function (state) { - state.deviceLists = {}; - //var deviceLists = {}; - var activityTimeout = state.activityTimeout || 2*60*1000; - var pongTimeout = state.pongTimeout || 10*1000; - state.Devices = Devices; - var onTcpConnection = require('./unwrap-tls').createTcpConnectionHandler(state); +var Server = { + _initCommandHandlers: function (state, srv) { + var commandHandlers = { + add_token: function addToken(newAuth) { + return Server.addToken(state, srv, newAuth); + } + , delete_token: function (token) { + return state.Promise.resolve(function () { + var err; - // TODO Use a Single TCP Handler - // Issues: - // * dynamic ports are dedicated to a device or cluster - // * servernames could come in on ports that belong to a different device - // * servernames could come in that belong to no device - // * this could lead to an attack / security vulnerability with ACME certificates - // Solutions - // * Restrict dynamic ports to a particular device - // * Restrict the use of servernames - function onDynTcpConn(conn) { - var serviceport = this.address().port; + if (token !== '*') { + err = Server.removeToken(state, srv, token); + if (err) { return state.Promise.reject(err); } + } + + Object.keys(srv.grants).some(function (jwtoken) { + err = Server.removeToken(state, srv, jwtoken); + return err; + }); + if (err) { return state.Promise.reject(err); } + + return null; + }); + } + }; + commandHandlers.auth = commandHandlers.add_token; + commandHandlers.authn = commandHandlers.add_token; + commandHandlers.authz = commandHandlers.add_token; + srv._commandHandlers = commandHandlers; + } +, _initPackerHandlers: function (state, srv) { + var packerHandlers = { + oncontrol: function (tun) { + var cmd; + try { + cmd = JSON.parse(tun.data.toString()); + } catch (e) {} + if (!Array.isArray(cmd) || typeof cmd[0] !== 'number') { + var msg = 'received bad command "' + tun.data.toString() + '"'; + console.warn(msg, 'from websocket', srv.socketId); + Server.sendTunnelMsg(srv, 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.warn('received error response to hello from', srv.socketId, cmd[1]); + } + } + else { + console.warn('received response to unknown command', cmd, 'from', srv.socketId); + } + return; + } + + if (cmd[0] === 0) { + console.warn('received dis-associated error from', srv.socketId, cmd[1]); + return; + } + + function onSuccess() { + Server.sendTunnelMsg(srv, null, [-cmd[0], null], 'control'); + } + function onError(err) { + Server.sendTunnelMsg(srv, null, [-cmd[0], err], 'control'); + } + + if (!srv._commandHandlers[cmd[1]]) { + onError({ message: 'unknown command "'+cmd[1]+'"', code: 'E_UNKNOWN_COMMAND' }); + return; + } + + console.log('command:', cmd[1], cmd.slice(2)); + return srv._commandHandlers[cmd[1]].apply(null, cmd.slice(2)).then(onSuccess, onError); + } + + , onmessage: function (tun) { + var cid = Packer.addrToId(tun); + if (state.debug) { console.log("remote '" + Server.logName(state, srv) + "' has data for '" + cid + "'", tun.data.byteLength); } + + var browserConn = Server.getBrowserConn(state, srv, cid); + if (!browserConn) { + Server.sendTunnelMsg(srv, tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error'); + return; + } + + browserConn.write(tun.data); + // tunnelRead is how many bytes we've read from the tunnel, and written to the browser. + browserConn.tunnelRead = (browserConn.tunnelRead || 0) + tun.data.byteLength; + // If we have more than 1MB buffered data we need to tell the other side to slow down. + // Once we've finished sending what we have we can tell the other side to keep going. + // If we've already sent the 'pause' message though don't send it again, because we're + // probably just dealing with data queued before our message got to them. + if (!browserConn.remotePaused && browserConn.bufferSize > 1024*1024) { + Server.sendTunnelMsg(srv, tun, browserConn.tunnelRead, 'pause'); + browserConn.remotePaused = true; + + browserConn.once('drain', function () { + Server.sendTunnelMsg(srv, tun, browserConn.tunnelRead, 'resume'); + browserConn.remotePaused = false; + }); + } + } + + , onpause: function (tun) { + var cid = Packer.addrToId(tun); + console.log('[TunnelPause]', cid); + var browserConn = Server.getBrowserConn(state, srv, cid); + if (browserConn) { + browserConn.manualPause = true; + browserConn.pause(); + } else { + Server.sendTunnelMsg(srv, tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error'); + } + } + + , onresume: function (tun) { + var cid = Packer.addrToId(tun); + console.log('[TunnelResume]', cid); + var browserConn = Server.getBrowserConn(state, srv, cid); + if (browserConn) { + browserConn.manualPause = false; + browserConn.resume(); + } else { + Server.sendTunnelMsg(srv, tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error'); + } + } + + , onend: function (tun) { + var cid = Packer.addrToId(tun); + console.log('[TunnelEnd]', cid); + Server.closeBrowserConn(state, srv, cid); + } + , onerror: function (tun) { + var cid = Packer.addrToId(tun); + console.warn('[TunnelError]', cid, tun.message); + Server.closeBrowserConn(state, srv, cid); + } + }; + srv._packerHandlers = packerHandlers; + srv.unpacker = Packer.create(srv._packerHandlers); + } +, _initSocketHandlers: function (state, srv) { + function refreshTimeout() { + srv.lastActivity = Date.now(); + } + + function checkTimeout() { + // Determine how long the connection has been "silent", ie no activity. + var silent = Date.now() - srv.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 < state.activityTimeout) { + srv.timeoutId = setTimeout(checkTimeout, state.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 < state.activityTimeout + state.pongTimeout) { + if (state.debug) { console.log('pinging', Server.logName(state, srv)); } + try { + srv.ws.ping(); + } catch (err) { + console.warn('failed to ping home cloud', Server.logName(state, srv)); + } + srv.timeoutId = setTimeout(checkTimeout, state.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.warn('home cloud', Server.logName(state, srv), 'connection timed out'); + srv.ws.close(1013, 'connection timeout'); + } + } + + function forwardMessage(chunk) { + refreshTimeout(); + if (state.debug) { console.log('[ws] device => client : demultiplexing message ', chunk.byteLength, 'bytes'); } + //console.log(chunk.toString()); + srv.unpacker.fns.addChunk(chunk); + } + + function hangup() { + clearTimeout(srv.timeoutId); + console.log('[ws] device hangup', Server.logName(state, srv), 'connection closing'); + Object.keys(srv.grants).forEach(function (jwtoken) { + Server.removeToken(state, srv, jwtoken); + }); + srv.ws.terminate(); + } + + srv.lastActivity = Date.now(); + srv.timeoutId = null; + srv.timeoutId = setTimeout(checkTimeout, state.activityTimeout); + + // Note that our websocket library automatically handles pong responses on ping requests + // before it even emits the event. + srv.ws.on('ping', refreshTimeout); + srv.ws.on('pong', refreshTimeout); + srv.ws.on('message', forwardMessage); + srv.ws.on('close', hangup); + srv.ws.on('error', hangup); + } +, init: function init(state, srv) { + Server._initCommandHandlers(state, srv); + Server._initPackerHandlers(state, srv); + Server._initSocketHandlers(state, srv); + + // Status Code '1' for Status 'hello' + Server.sendTunnelMsg(srv, null, [1, 'hello', [srv.unpacker._version], Object.keys(srv._commandHandlers)], 'control'); + } +, sendTunnelMsg: function sendTunnelMsg(srv, addr, data, service) { + srv.ws.send(Packer.pack(addr, data, service), {binary: true}); + } +, logName: function logName(state, srv) { + var result = Object.keys(srv.grants).map(function (jwtoken) { + return srv.grants[jwtoken].currentDesc; + }).join(';'); + + return result || srv.socketId; + } +, onAuth: function onAuth(state, srv, newAuth, grant) { + console.log('\n[relay.js] onAuth'); + console.log(newAuth); + console.log(grant); + //var stringauth; + var err; + if (!grant || 'object' !== typeof grant) { + console.log('[relay.js] invalid token', grant); + err = new Error("invalid access token"); + err.code = "E_INVALID_TOKEN"; + return state.Promise.reject(err); + } + + if ('string' !== typeof newAuth) { + newAuth = JSON.stringify(newAuth); + } + + console.log('check for upgrade token'); + if (grant.jwt && newAuth !== grant.jwt) { + console.log('new token to send back'); + // Access Token + Server.sendTunnelMsg( + srv + , null + , [ 3 + , 'access_token' + , { jwt: grant.jwt } + ] + , 'control' + ); + // these aren't needed internally once they're sent + grant.jwt = null; + } + + /* + if (!Array.isArray(grant.domains) || !grant.domains.length) { + err = new Error("invalid domains array"); + err.code = "E_INVALID_NAME"; + return state.Promise.reject(err); + } + */ + if (grant.domains.some(function (name) { return typeof name !== 'string'; })) { + console.log('bad domain names'); + err = new Error("invalid domain name(s)"); + err.code = "E_INVALID_NAME"; + return state.Promise.reject(err); + } + + console.log('strolling through pleasantries'); + // 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 grants. + grant.domains.forEach(function (domainname) { + console.log('add', domainname, 'to device lists'); + srv.domainsMap[domainname] = true; + Devices.add(state.deviceLists, domainname, srv); + }); + srv.domains = Object.keys(srv.domainsMap); + srv.currentDesc = (grant.device && (grant.device.id || grant.device.hostname)) || srv.domains.join(','); + grant.currentDesc = (grant.device && (grant.device.id || grant.device.hostname)) || grant.domains.join(','); + grant.srv = srv; + //grant.ws = srv.ws; + //grant.upgradeReq = srv.upgradeReq; + grant.clients = {}; + + if (!grant.ports) { grant.ports = []; } + + function openPort(serviceport) { + function tcpListener(conn) { + Server.onDynTcpConn(state, srv, srv.portsMap[serviceport], conn); + } + serviceport = parseInt(serviceport, 10) || 0; + if (!serviceport) { + // TODO error message about bad port + return; + } + if (PortServers[serviceport]) { + console.log('reuse', serviceport, 'for this connection'); + //grant.ports = []; + srv.portsMap[serviceport] = PortServers[serviceport]; + srv.portsMap[serviceport].on('connection', tcpListener); + srv.portsMap[serviceport].tcpListener = tcpListener; + Devices.add(state.deviceLists, serviceport, srv); + } else { + try { + console.log('use new', serviceport, 'for this connection'); + srv.portsMap[serviceport] = PortServers[serviceport] = require('net').createServer(tcpListener); + srv.portsMap[serviceport].tcpListener = tcpListener; + srv.portsMap[serviceport].listen(serviceport, function () { + console.info('[DynTcpConn] Port', serviceport, 'now open for', grant.currentDesc); + Devices.add(state.deviceLists, serviceport, srv); + }); + srv.portsMap[serviceport].on('error', function (e) { + // TODO try again with random port + console.error("Server Error assigning a dynamic port to a new connection:", e); + }); + } catch(e) { + // what a wonderful problem it will be the day that this bug needs to be fixed + // (i.e. there are enough users to run out of ports) + console.error("Error assigning a dynamic port to a new connection:", e); + } + } + } + grant.ports.forEach(openPort); + + srv.grants[newAuth] = grant; + console.info("[ws] authorized", srv.socketId, "for", grant.currentDesc); + + console.log('notify of grants', grant.domains, grant.ports); + Server.sendTunnelMsg( + srv + , null + , [ 2 + , 'grant' + , [ ['ssh+https', grant.domains[0], 443 ] + , ['ssh', 'ssh.' + state.config.sharedDomain, grant.ports ] + , ['tcp', 'tcp.' + state.config.sharedDomain, grant.ports ] + , ['https', grant.domains[0] ] + ] + ] + , 'control' + ); + return null; + } +, onDynTcpConn: function onDynTcpConn(state, srv, server, conn) { + var serviceport = server.address().port; console.log('[DynTcpConn] new connection on', serviceport); - var remote = Devices.next(state.deviceLists, serviceport) + var nextDevice = Devices.next(state.deviceLists, serviceport); - if (!remote) { + if (!nextDevice) { conn.write("[Sanity Error] I've got a blank space baby, but nowhere to write your name."); conn.end(); try { - this.close(); + server.close(); } catch(e) { console.error("[DynTcpConn] failed to close server:", e); } @@ -78,506 +408,198 @@ module.exports.create = function (state) { return; } - // pipeWs(servername, servicename, client, remote, serviceport) + // pipeWs(servername, servicename, srv, client, serviceport) // remote.clients is managed as part of the piping process - if (state.debug) { console.log("[DynTcp]", serviceport, "piping to remote"); } - pipeWs(null, 'tcp', conn, remote, serviceport) + if (state.debug) { console.log("[DynTcp]", serviceport, "piping to srv (via loadbal)"); } + pipeWs(null, 'tcp', nextDevice, conn, serviceport); process.nextTick(function () { conn.resume(); }); }); } +, addToken: function addToken(state, srv, newAuth) { + console.log("addToken", newAuth); + if (srv.grants[newAuth]) { + console.log("addToken - duplicate"); + // return { message: "token sent multiple times", code: "E_TOKEN_REPEAT" }; + return state.Promise.resolve(null); + } - function onWsConnection(ws, upgradeReq) { - var socketId = Packer.socketToId(upgradeReq.socket); - if (state.debug) { console.log('[ws] connection', socketId); } + return state.authenticate({ auth: newAuth }).then(function (authnToken) { - var remotes = {}; - var firstToken; - var authn = (upgradeReq.headers.authorization||'').split(/\s+/); + console.log('\n[relay.js] newAuth'); + console.log(newAuth); + + console.log('\n[relay.js] authnToken'); + console.log(authnToken); + + if (authnToken.id) { + state.srvs[authnToken.id] = state.srvs[authnToken.id] || {}; + state.srvs[authnToken.id].updateAuth = function (validToken) { + return Server.onAuth(state, srv, newAuth, validToken); + }; + } + + // will return rejection if necessary + return state.srvs[authnToken.id].updateAuth(authnToken); + }); + } +, removeToken: function removeToken(state, srv, jwtoken) { + var grant = srv.grants[jwtoken]; + if (!grant) { + return { message: 'specified token not present', code: 'E_INVALID_TOKEN'}; + } + + // Prevent any more browser connections for this grant being sent to this srv, + // and any existing connections from trying to send more data across the connection. + grant.domains.forEach(function (domainname) { + Devices.remove(state.deviceLists, domainname, srv); + }); + grant.ports.forEach(function (portnumber) { + Devices.remove(state.deviceLists, portnumber, srv); + if (!srv.portsMap[portnumber]) { return; } + try { + srv.portsMap[portnumber].close(function () { + console.log("[DynTcpConn] closing server for ", portnumber); + delete srv.portMap[portnumber]; + delete PortServers[portnumber]; + }); + } catch(e) { /*ignore*/ } + }); + + // Close all of the existing browser connections associated with this websocket connection. + Object.keys(grant.clients).forEach(function (cid) { + Server.closeBrowserConn(state, srv, cid); + }); + delete srv.grants[jwtoken]; + console.log("[ws] removed token '" + grant.currentDesc + "' from", srv.socketId); + return null; + } +, getBrowserConn: function getBrowserConn(state, srv, cid) { + return srv.clients[cid]; + } +, closeBrowserConn: function closeBrowserConn(state, srv, cid) { + if (!srv.clients[cid]) { + return; + } + + PromiseA.resolve().then(function () { + var conn = srv.clients[cid]; + conn.tunnelClosing = true; + conn.end(); + + // If no data is buffered for writing then we don't need to wait for it to drain. + if (!conn.bufferSize) { + return timeoutPromise(500); + } + // Otherwise we want the connection to be able to finish, but we also want to impose + // a time limit for it to drain, since it shouldn't have more than 1MB buffered. + return new PromiseA(function (resolve) { + var timeoutId = setTimeout(resolve, 60*1000); + conn.once('drain', function () { + clearTimeout(timeoutId); + setTimeout(resolve, 500); + }); + }); + }).then(function () { + if (srv.clients[cid]) { + console.warn(cid, 'browser connection still present after calling `end`'); + srv.clients[cid].destroy(); + return timeoutPromise(500); + } + }).then(function () { + if (srv.clients[cid]) { + console.error(cid, 'browser connection still present after calling `destroy`'); + delete srv.clients[cid]; + } + }).catch(function (err) { + console.warn('failed to close browser connection', cid, err); + }); + } +, parseAuth: function parseAuth(state, srv) { + var authn = (srv.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]; + return authn[1]; } catch (err) { } } + return url.parse(srv.upgradeReq.url, true).query.access_token; + } +}; - if (!firstToken) { - firstToken = url.parse(upgradeReq.url, true).query.access_token; - } - if (!firstToken) { - next(); - return; - } - if (firstToken) { - return addToken(firstToken, true).then(next).catch(function (err) { - sendTunnelMsg(null, [0, err], 'control'); - ws.close(); - }); - } +module.exports.store = { Devices: Devices }; +module.exports.create = function (state) { + state.deviceLists = {}; + state.deviceCallbacks = {}; + state.srvs = {}; - function logName() { - var result = Object.keys(remotes).map(function (jwtoken) { - return remotes[jwtoken].deviceId; - }).join(';'); + if (!parseInt(state.activityTimeout, 10)) { + state.activityTimeout = 2 * 60 * 1000; + } + if (!parseInt(state.pongTimeout, 10)) { + state.pongTimeout = 10 * 1000; + } + state.Devices = Devices; - return result || socketId; - } + // TODO Use a Single TCP Handler + // Issues: + // * dynamic ports are dedicated to a device or cluster + // * servernames could come in on ports that belong to a different device + // * servernames could come in that belong to no device + // * this could lead to an attack / security vulnerability with ACME certificates + // Solutions + // * Restrict dynamic ports to a particular device + // * Restrict the use of servernames - function sendTunnelMsg(addr, data, service) { - ws.send(Packer.pack(addr, data, service), {binary: true}); - } + function onWsConnection(_ws, _upgradeReq) { + var srv = {}; + var initToken; + srv.ws = _ws; + srv.upgradeReq = _upgradeReq; + srv.socketId = Packer.socketToId(srv.upgradeReq.socket); + srv.grants = {}; + srv.clients = {}; + srv.domainsMap = {}; + srv.portsMap = {}; + srv.pausedConns = []; - function getBrowserConn(cid) { - var browserConn; - Object.keys(remotes).some(function (jwtoken) { - if (remotes[jwtoken].clients[cid]) { - browserConn = remotes[jwtoken].clients[cid]; - return true; - } - }); + if (state.debug) { console.log('[ws] connection', srv.socketId); } - return browserConn; - } + initToken = Server.parseAuth(state, srv); - function closeBrowserConn(cid) { - var remote; - Object.keys(remotes).some(function (jwtoken) { - if (remotes[jwtoken].clients[cid]) { - remote = remotes[jwtoken]; - return true; - } - }); - if (!remote) { + srv.ws._socket.on('drain', function () { + // the websocket library has it's own buffer apart from node's socket buffer, but that one + // is much more difficult to watch, so we watch for the lower level buffer to drain and + // then check to see if the upper level buffer is still too full to write to. Note that + // the websocket library buffer has something to do with compression, so I'm not requiring + // that to be 0 before we start up again. + if (srv.ws.bufferedAmount > 128*1024) { return; } - PromiseA.resolve().then(function () { - var conn = remote.clients[cid]; - conn.tunnelClosing = true; - conn.end(); + srv.pausedConns.forEach(function (conn) { + if (!conn.manualPause) { + // console.log('resuming', conn.tunnelCid, 'now that the web socket has caught up'); + conn.resume(); + } + }); + srv.pausedConns.length = 0; + }); - // If no data is buffered for writing then we don't need to wait for it to drain. - if (!conn.bufferSize) { - return timeoutPromise(500); - } - // Otherwise we want the connection to be able to finish, but we also want to impose - // a time limit for it to drain, since it shouldn't have more than 1MB buffered. - return new PromiseA(function (resolve) { - var timeoutId = setTimeout(resolve, 60*1000); - conn.once('drain', function () { - clearTimeout(timeoutId); - setTimeout(resolve, 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]; - } + if (initToken) { + return Server.addToken(state, srv, initToken).then(function () { + Server.init(state, srv); }).catch(function (err) { - console.warn('failed to close browser connection', cid, err); + Server.sendTunnelMsg(srv, null, [0, err], 'control'); + srv.ws.close(); }); - } - - function addToken(jwtoken) { - - function onAuth(token) { - if ('string' !== typeof jwtoken) { - jwtoken = JSON.stringify(jwtoken); - } - var err; - if (!token) { - err = new Error("invalid access token"); - err.code = "E_INVALID_TOKEN"; - return state.Promise.reject(err); - } - - if (token.jwt && jwtoken !== token.jwt) { - // Access Token - sendTunnelMsg( - null - , [ 3 - , 'access_token' - , { jwt: token.jwt } - ] - , 'control' - ); - // these aren't needed internally once they're sent - token.jwt = null; - } - - if (!Array.isArray(token.domains)) { - if ('string' === typeof token.name) { - token.domains = [ token.name ]; - } - } - - if (!Array.isArray(token.domains) || !token.domains.length) { - err = new Error("invalid domains array"); - err.code = "E_INVALID_NAME"; - return state.Promise.reject(err); - } - if (token.domains.some(function (name) { return typeof name !== 'string'; })) { - err = new Error("invalid domain name(s)"); - err.code = "E_INVALID_NAME"; - return state.Promise.reject(err); - } - - // 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.upgradeReq = upgradeReq; - token.clients = {}; - - token.pausedConns = []; - ws._socket.on('drain', function () { - // the websocket library has it's own buffer apart from node's socket buffer, but that one - // is much more difficult to watch, so we watch for the lower level buffer to drain and - // then check to see if the upper level buffer is still too full to write to. Note that - // the websocket library buffer has something to do with compression, so I'm not requiring - // that to be 0 before we start up again. - if (ws.bufferedAmount > 128*1024) { - return; - } - - token.pausedConns.forEach(function (conn) { - if (!conn.manualPause) { - // console.log('resuming', conn.tunnelCid, 'now that the web socket has caught up'); - conn.resume(); - } - }); - token.pausedConns.length = 0; - }); - - token.domains.forEach(function (domainname) { - Devices.add(state.deviceLists, domainname, token); - }); - - function onDynTcpReadyHelper(serviceport) { - //token.dynamicPorts.push(serviceport); - Devices.add(state.deviceLists, serviceport, token); - //var hri = require('human-readable-ids').hri; - //var hrname = hri.random() + '.' + state.config.sharedDomain; - //token.dynamicNames.push(hrname); - // TODO restrict to authenticated device - // TODO pull servername from config - // TODO remove hrname on disconnect - //Devices.add(state.deviceLists, hrname, token); - sendTunnelMsg( - null - , [ 2 - , 'grant' - , [ ['ssh+https', token.domains[0], 443 ] - , ['ssh', 'ssh.' + state.config.sharedDomain, serviceport ] - , ['tcp', 'tcp.' + state.config.sharedDomain, serviceport] - , ['https', token.domains[0] ] - ] - ] - , 'control' - ); - } - - console.log('[DEBUG] got to firstToken check'); - - if (!token.ports) { - token.ports = []; - } - if (!firstToken || firstToken === jwtoken) { - if (!token.ports.length) { - token.ports.push( 0 ); - } - firstToken = token.jwt || jwtoken; - } - - //token.dynamicPorts = []; - //token.dynamicNames = []; - - var onePortForNow = parseInt(token.ports[0], 10) || 0; - if (portServers[onePortForNow]) { - //token.ports = []; - token.server = portServers[onePortForNow]; - token.server.on('connection', onDynTcpConn); - onDynTcpReadyHelper(onePortForNow); - } else { - try { - token.server = require('net').createServer(onDynTcpConn).listen(onePortForNow, function () { - var serviceport = this.address().port; - portServers[serviceport] = this; - console.info('[DynTcpConn] Port', serviceport, 'now open for', token.deviceId); - onDynTcpReadyHelper(serviceport); - }); - token.server.on('error', function (e) { - // TODO try again with random port - console.error("Server Error assigning a dynamic port to a new connection:", e); - }); - } catch(e) { - // what a wonderful problem it will be the day that this bug needs to be fixed - // (i.e. there are enough users to run out of ports) - console.error("Error assigning a dynamic port to a new connection:", e); - } - } - - remotes[jwtoken] = token; - console.info("[ws] authorized", socketId, "for", token.deviceId); - return null; - } - - if (remotes[jwtoken]) { - // return { message: "token sent multiple times", code: "E_TOKEN_REPEAT" }; - return state.Promise.resolve(null); - } - - return state.authenticate({ auth: jwtoken }).then(onAuth); - } - - 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(state.deviceLists, domainname, remote); - }); - remote.ports.forEach(function (portnumber) { - Devices.remove(state.deviceLists, portnumber, remote); - }); - remote.ws = null; - remote.upgradeReq = null; - if (remote.server) { - remote.serverPort = remote.server.address().port; - remote.server.close(function () { - console.log("[DynTcpConn] closing server for ", remote.serverPort); - remote.serverPort = null; - }); - remote.server = 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("[ws] removed token '" + remote.deviceId + "' from", socketId); - return null; - } - - function next() { - var commandHandlers = { - add_token: addToken - , auth: addToken - , authn: addToken - , authz: addToken - , delete_token: function (token) { - return state.Promise.resolve(function () { - var err; - - if (token !== '*') { - err = removeToken(token); - if (err) { return state.Promise.reject(err); } - } - - Object.keys(remotes).some(function (jwtoken) { - err = removeToken(jwtoken); - return err; - }); - if (err) { return state.Promise.reject(err); } - - return null; - }); - } - }; - - var packerHandlers = { - oncontrol: function (tun) { - var cmd; - try { - cmd = JSON.parse(tun.data.toString()); - } catch (e) {} - if (!Array.isArray(cmd) || typeof cmd[0] !== 'number') { - var msg = 'received bad command "' + tun.data.toString() + '"'; - console.warn(msg, 'from websocket', socketId); - sendTunnelMsg(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.warn('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; - } - - function onSuccess() { - sendTunnelMsg(null, [-cmd[0], null], 'control'); - } - function onError(err) { - sendTunnelMsg(null, [-cmd[0], err], 'control'); - } - - if (!commandHandlers[cmd[1]]) { - onError({ message: 'unknown command "'+cmd[1]+'"', code: 'E_UNKNOWN_COMMAND' }); - return; - } - - return commandHandlers[cmd[1]].apply(null, cmd.slice(2)).then(onSuccess, onError); - } - - , onmessage: function (tun) { - var cid = Packer.addrToId(tun); - if (state.debug) { console.log("remote '" + logName() + "' has data for '" + cid + "'", tun.data.byteLength); } - - var browserConn = getBrowserConn(cid); - if (!browserConn) { - sendTunnelMsg(tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error'); - return; - } - - browserConn.write(tun.data); - // tunnelRead is how many bytes we've read from the tunnel, and written to the browser. - browserConn.tunnelRead = (browserConn.tunnelRead || 0) + tun.data.byteLength; - // If we have more than 1MB buffered data we need to tell the other side to slow down. - // Once we've finished sending what we have we can tell the other side to keep going. - // If we've already sent the 'pause' message though don't send it again, because we're - // probably just dealing with data queued before our message got to them. - if (!browserConn.remotePaused && browserConn.bufferSize > 1024*1024) { - sendTunnelMsg(tun, browserConn.tunnelRead, 'pause'); - browserConn.remotePaused = true; - - browserConn.once('drain', function () { - sendTunnelMsg(tun, browserConn.tunnelRead, 'resume'); - browserConn.remotePaused = false; - }); - } - } - - , onpause: function (tun) { - var cid = Packer.addrToId(tun); - console.log('[TunnelPause]', cid); - var browserConn = getBrowserConn(cid); - if (browserConn) { - browserConn.manualPause = true; - browserConn.pause(); - } else { - sendTunnelMsg(tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error'); - } - } - - , onresume: function (tun) { - var cid = Packer.addrToId(tun); - console.log('[TunnelResume]', cid); - var browserConn = getBrowserConn(cid); - if (browserConn) { - browserConn.manualPause = false; - browserConn.resume(); - } else { - sendTunnelMsg(tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error'); - } - } - - , onend: function (tun) { - var cid = Packer.addrToId(tun); - console.log('[TunnelEnd]', cid); - closeBrowserConn(cid); - } - , onerror: function (tun) { - var cid = Packer.addrToId(tun); - console.warn('[TunnelError]', cid, tun.message); - closeBrowserConn(cid); - } - }; - var unpacker = Packer.create(packerHandlers); - - 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) { - if (state.debug) { 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.warn('home cloud', logName(), 'connection timed out'); - ws.close(1013, 'connection timeout'); - } - } - - function forwardMessage(chunk) { - refreshTimeout(); - if (state.debug) { console.log('[ws] device => client : demultiplexing message ', chunk.byteLength, 'bytes'); } - //console.log(chunk.toString()); - unpacker.fns.addChunk(chunk); - } - - function hangup() { - clearTimeout(timeoutId); - console.log('[ws] device hangup', logName(), 'connection closing'); - Object.keys(remotes).forEach(function (jwtoken) { - removeToken(jwtoken); - }); - ws.terminate(); - } - - var lastActivity = Date.now(); - var timeoutId; - - 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', forwardMessage); - ws.on('close', hangup); - ws.on('error', hangup); - - // Status Code '1' for Status 'hello' - sendTunnelMsg(null, [1, 'hello', [unpacker._version], Object.keys(commandHandlers)], 'control'); + } else { + return Server.init(state, srv); } } return { - tcp: onTcpConnection + tcp: require('./unwrap-tls').createTcpConnectionHandler(state) , ws: onWsConnection , isClientDomain: Devices.exist.bind(null, state.deviceLists) }; diff --git a/lib/unwrap-tls.js b/lib/unwrap-tls.js index 1a6374a..58bb588 100644 --- a/lib/unwrap-tls.js +++ b/lib/unwrap-tls.js @@ -19,6 +19,11 @@ module.exports.createTcpConnectionHandler = function (state) { //return; conn.once('data', function (firstChunk) { + var service = 'tcp'; + var servername; + var str; + var m; + conn.pause(); conn.unshift(firstChunk); @@ -31,18 +36,13 @@ module.exports.createTcpConnectionHandler = function (state) { // defer after return (instead of being in many places) function deferData(fn) { if (fn) { - state[fn](servername, conn) + state[fn](servername, conn); } process.nextTick(function () { conn.resume(); }); } - var service = 'tcp'; - var servername; - var str; - var m; - function tryTls() { var vhost; @@ -76,9 +76,9 @@ module.exports.createTcpConnectionHandler = function (state) { return; } - if (state.debug) { console.log("pipeWs(servername, service, socket, deviceLists['" + servername + "'])"); } + if (state.debug) { console.log("pipeWs(servername, service, deviceLists['" + servername + "'], socket)"); } deferData(); - pipeWs(servername, service, conn, nextDevice, serviceport); + pipeWs(servername, service, nextDevice, conn, serviceport); } // TODO don't run an fs check if we already know this is working elsewhere @@ -86,11 +86,11 @@ module.exports.createTcpConnectionHandler = function (state) { if (state.config.vhost) { vhost = state.config.vhost.replace(/:hostname/, (servername||'reallydoesntexist')); if (state.debug) { console.log("[tcp] [vhost]", state.config.vhost, "=>", vhost); } - //state.httpsVhost(servername, conn); + //state.httpsVhost(servername, conn); //return; require('fs').readdir(vhost, function (err, nodes) { if (state.debug && err) { console.log("VHOST error", err); } - if (err) { run(); return; } + if (err || !nodes) { run(); return; } //if (nodes) { deferData('httpsVhost'); return; } deferData('httpsVhost'); }); @@ -131,7 +131,7 @@ module.exports.createTcpConnectionHandler = function (state) { // HTTP if (Devices.exist(state.deviceLists, servername)) { deferData(); - pipeWs(servername, service, conn, Devices.next(state.deviceLists, servername), serviceport); + pipeWs(servername, service, Devices.next(state.deviceLists, servername), conn, serviceport); return; } deferData('handleHttp');