2016-09-27 21:56:06 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var url = require('url');
|
2017-09-08 17:24:30 +00:00
|
|
|
var PromiseA = require('bluebird');
|
2016-09-27 21:56:06 +00:00
|
|
|
var jwt = require('jsonwebtoken');
|
2018-05-31 05:24:43 +00:00
|
|
|
var Packer = require('proxy-packer');
|
2016-09-28 05:26:16 +00:00
|
|
|
|
2017-09-08 17:24:30 +00:00
|
|
|
function timeoutPromise(duration) {
|
|
|
|
return new PromiseA(function (resolve) {
|
|
|
|
setTimeout(resolve, duration);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-25 17:41:20 +00:00
|
|
|
var Devices = require('./lib/device-tracker');
|
2018-06-01 06:41:32 +00:00
|
|
|
var pipeWs = require('./lib/pipe-ws.js');
|
2017-04-05 02:31:58 +00:00
|
|
|
|
|
|
|
module.exports.store = { Devices: Devices };
|
2018-05-31 05:24:43 +00:00
|
|
|
module.exports.create = function (state) {
|
|
|
|
state.deviceLists = {};
|
2018-04-25 17:41:20 +00:00
|
|
|
//var deviceLists = {};
|
2018-05-31 05:24:43 +00:00
|
|
|
var activityTimeout = state.activityTimeout || 2*60*1000;
|
|
|
|
var pongTimeout = state.pongTimeout || 10*1000;
|
|
|
|
state.Devices = Devices;
|
|
|
|
var onTcpConnection = require('./lib/unwrap-tls').createTcpConnectionHandler(state);
|
2016-09-27 21:56:06 +00:00
|
|
|
|
2018-06-01 06:41:32 +00:00
|
|
|
// 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;
|
|
|
|
console.log('[DynTcpConn] new connection on', serviceport);
|
|
|
|
var remote = Devices.next(state.deviceLists, serviceport)
|
|
|
|
|
|
|
|
if (!remote) {
|
|
|
|
conn.write("[Sanity Error] I've got a blank space baby, but nowhere to write your name.");
|
|
|
|
conn.end();
|
|
|
|
try {
|
|
|
|
this.close();
|
|
|
|
} catch(e) {
|
|
|
|
console.error("[DynTcpConn] failed to close server:", e);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.once('data', function (firstChunk) {
|
|
|
|
console.log("[DynTcp] examining firstChunk", serviceport);
|
|
|
|
conn.pause();
|
|
|
|
conn.unshift(firstChunk);
|
|
|
|
|
|
|
|
var servername;
|
|
|
|
var hostname;
|
|
|
|
var str;
|
|
|
|
var m;
|
|
|
|
|
|
|
|
if (22 === firstChunk[0]) {
|
|
|
|
servername = (sni(firstChunk)||'').toLowerCase();
|
|
|
|
} else if (firstChunk[0] > 32 && firstChunk[0] < 127) {
|
|
|
|
str = firstChunk.toString();
|
|
|
|
m = str.match(/(?:^|[\r\n])Host: ([^\r\n]+)[\r\n]*/im);
|
|
|
|
hostname = (m && m[1].toLowerCase() || '').split(':')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (servername || hostname) {
|
|
|
|
if (servername) {
|
|
|
|
conn.write("TLS with sni is allowed only on standard ports. If you've registered '" + servername + "' use port 443.");
|
|
|
|
} else {
|
|
|
|
conn.write("HTTP with Host headers is not allowed on dynamic ports. If you've registered '" + hostname + "' use port 80.");
|
|
|
|
}
|
|
|
|
conn.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pipeWs(servername, servicename, client, remote, serviceport)
|
|
|
|
// remote.clients is managed as part of the piping process
|
|
|
|
console.log("[DynTcp] piping to remote", serviceport);
|
|
|
|
pipeWs(null, 'tcp', conn, remote, serviceport)
|
|
|
|
|
|
|
|
process.nextTick(function () { conn.resume(); });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:34:20 +00:00
|
|
|
function onWsConnection(ws, upgradeReq) {
|
2018-06-01 06:41:32 +00:00
|
|
|
if (state.debug) { console.log('[ws] connection'); }
|
2018-05-31 05:24:43 +00:00
|
|
|
var socketId = Packer.socketToId(upgradeReq.socket);
|
2017-04-27 01:52:30 +00:00
|
|
|
var remotes = {};
|
2017-04-05 02:31:58 +00:00
|
|
|
|
2017-04-27 01:52:30 +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
|
|
|
|
2017-04-27 01:52:30 +00:00
|
|
|
return result || socketId;
|
2016-09-27 21:56:06 +00:00
|
|
|
}
|
2017-09-07 23:10:47 +00:00
|
|
|
function sendTunnelMsg(addr, data, service) {
|
2018-05-31 05:24:43 +00:00
|
|
|
ws.send(Packer.pack(addr, data, service), {binary: true});
|
2017-09-07 23:10:47 +00:00
|
|
|
}
|
2016-09-27 21:56:06 +00:00
|
|
|
|
2017-09-07 00:43:34 +00:00
|
|
|
function getBrowserConn(cid) {
|
|
|
|
var browserConn;
|
2017-04-27 01:52:30 +00:00
|
|
|
Object.keys(remotes).some(function (jwtoken) {
|
|
|
|
if (remotes[jwtoken].clients[cid]) {
|
2017-09-07 00:43:34 +00:00
|
|
|
browserConn = remotes[jwtoken].clients[cid];
|
2017-04-27 01:52:30 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2017-04-26 18:05:44 +00:00
|
|
|
|
2017-09-07 00:43:34 +00:00
|
|
|
return browserConn;
|
2017-04-26 18:05:44 +00:00
|
|
|
}
|
2017-09-08 17:24:30 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
PromiseA.resolve().then(function () {
|
|
|
|
var conn = remote.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 (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);
|
|
|
|
});
|
|
|
|
}
|
2017-04-26 18:05:44 +00:00
|
|
|
|
2017-04-27 01:52:30 +00:00
|
|
|
function addToken(jwtoken) {
|
|
|
|
if (remotes[jwtoken]) {
|
2017-04-28 01:36:28 +00:00
|
|
|
// return { message: "token sent multiple times", code: "E_TOKEN_REPEAT" };
|
|
|
|
return null;
|
2017-04-27 01:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var token;
|
|
|
|
try {
|
2018-05-31 05:24:43 +00:00
|
|
|
token = jwt.verify(jwtoken, state.secret);
|
2017-04-27 01:52:30 +00:00
|
|
|
} catch (e) {
|
|
|
|
token = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!token) {
|
2017-04-28 01:36:28 +00:00
|
|
|
return { message: "invalid access token", code: "E_INVALID_TOKEN" };
|
2017-04-27 01:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2017-04-28 01:36:28 +00:00
|
|
|
return { message: "invalid server name", code: "E_INVALID_NAME" };
|
2017-04-27 01:52:30 +00:00
|
|
|
}
|
|
|
|
if (token.domains.some(function (name) { return typeof name !== 'string'; })) {
|
2017-04-28 01:36:28 +00:00
|
|
|
return { message: "invalid server name", code: "E_INVALID_NAME" };
|
2017-04-27 01:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2018-04-24 16:34:20 +00:00
|
|
|
token.upgradeReq = upgradeReq;
|
2017-04-27 01:52:30 +00:00
|
|
|
token.clients = {};
|
2018-06-01 06:41:32 +00:00
|
|
|
token.dynamicPorts = [];
|
2017-04-27 01:52:30 +00:00
|
|
|
|
2017-09-07 23:10:47 +00:00
|
|
|
token.pausedConns = [];
|
|
|
|
ws._socket.on('drain', function () {
|
2017-09-11 21:45:17 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-09-07 23:10:47 +00:00
|
|
|
token.pausedConns.forEach(function (conn) {
|
|
|
|
if (!conn.manualPause) {
|
2017-09-11 21:45:17 +00:00
|
|
|
// console.log('resuming', conn.tunnelCid, 'now that the web socket has caught up');
|
2017-09-07 23:10:47 +00:00
|
|
|
conn.resume();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
token.pausedConns.length = 0;
|
|
|
|
});
|
|
|
|
|
2017-04-27 01:52:30 +00:00
|
|
|
token.domains.forEach(function (domainname) {
|
2018-05-31 05:24:43 +00:00
|
|
|
Devices.add(state.deviceLists, domainname, token);
|
2017-04-27 01:52:30 +00:00
|
|
|
});
|
2018-06-01 06:41:32 +00:00
|
|
|
|
|
|
|
function handleTcpServer() {
|
|
|
|
var serviceport = this.address().port;
|
|
|
|
console.info('[DynTcpConn] Port', serviceport, 'now open for', token.deviceId);
|
|
|
|
token.dynamicPorts.push(serviceport);
|
|
|
|
Devices.add(state.deviceLists, serviceport, token);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
token.server = require('net').createServer(onDynTcpConn).listen(0, handleTcpServer);
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|
2017-04-27 01:52:30 +00:00
|
|
|
remotes[jwtoken] = token;
|
2018-06-01 06:41:32 +00:00
|
|
|
console.info("[ws] authorized", socketId, "for", token.deviceId);
|
2017-04-28 01:36:28 +00:00
|
|
|
return null;
|
2017-04-27 01:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeToken(jwtoken) {
|
|
|
|
var remote = remotes[jwtoken];
|
|
|
|
if (!remote) {
|
2017-04-28 01:36:28 +00:00
|
|
|
return { message: 'specified token not present', code: 'E_INVALID_TOKEN'};
|
2017-04-27 01:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2018-05-31 05:24:43 +00:00
|
|
|
Devices.remove(state.deviceLists, domainname, remote);
|
2017-04-27 01:52:30 +00:00
|
|
|
});
|
2018-06-01 06:41:32 +00:00
|
|
|
remote.dynamicPorts.forEach(function (portnumber) {
|
|
|
|
Devices.remove(state.deviceLists, portnumber, remote);
|
|
|
|
});
|
2017-04-27 01:52:30 +00:00
|
|
|
remote.ws = null;
|
2018-04-24 16:34:20 +00:00
|
|
|
remote.upgradeReq = null;
|
2018-06-01 06:41:32 +00:00
|
|
|
remote.server.close(function () {
|
|
|
|
console.log("[DynTcpConn] closing server for ", remote.server.address().port);
|
|
|
|
});
|
|
|
|
remote.server = null;
|
2017-04-27 01:52:30 +00:00
|
|
|
|
|
|
|
// Close all of the existing browser connections associated with this websocket connection.
|
|
|
|
Object.keys(remote.clients).forEach(function (cid) {
|
2017-09-08 17:24:30 +00:00
|
|
|
closeBrowserConn(cid);
|
2017-04-27 01:52:30 +00:00
|
|
|
});
|
|
|
|
delete remotes[jwtoken];
|
2018-06-01 06:41:32 +00:00
|
|
|
console.log("[ws] removed token '" + remote.deviceId + "' from", socketId);
|
2017-04-28 01:36:28 +00:00
|
|
|
return null;
|
2017-04-27 01:52:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var firstToken;
|
2018-04-24 16:34:20 +00:00
|
|
|
var authn = (upgradeReq.headers.authorization||'').split(/\s+/);
|
2017-04-27 01:52:30 +00:00
|
|
|
if (authn[0] && 'basic' === authn[0].toLowerCase()) {
|
|
|
|
try {
|
|
|
|
authn = new Buffer(authn[1], 'base64').toString('ascii').split(':');
|
|
|
|
firstToken = authn[1];
|
|
|
|
} catch (err) { }
|
|
|
|
}
|
|
|
|
if (!firstToken) {
|
2018-04-24 16:34:20 +00:00
|
|
|
firstToken = url.parse(upgradeReq.url, true).query.access_token;
|
2017-04-27 01:52:30 +00:00
|
|
|
}
|
2017-04-28 01:36:28 +00:00
|
|
|
if (firstToken) {
|
|
|
|
var err = addToken(firstToken);
|
|
|
|
if (err) {
|
2017-09-07 23:10:47 +00:00
|
|
|
sendTunnelMsg(null, [0, err], 'control');
|
2017-04-28 01:36:28 +00:00
|
|
|
ws.close();
|
|
|
|
return;
|
|
|
|
}
|
2017-04-27 01:52:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 18:50:50 +00:00
|
|
|
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 = {
|
2018-05-31 05:24:43 +00:00
|
|
|
oncontrol: function (tun) {
|
2017-04-28 01:36:28 +00:00
|
|
|
var cmd, err;
|
|
|
|
try {
|
2018-05-31 05:24:43 +00:00
|
|
|
cmd = JSON.parse(tun.data.toString());
|
2017-04-28 01:36:28 +00:00
|
|
|
} catch (err) {}
|
|
|
|
if (!Array.isArray(cmd) || typeof cmd[0] !== 'number') {
|
2018-05-31 05:24:43 +00:00
|
|
|
var msg = 'received bad command "' + tun.data.toString() + '"';
|
2017-04-28 20:19:56 +00:00
|
|
|
console.warn(msg, 'from websocket', socketId);
|
2017-09-07 23:10:47 +00:00
|
|
|
sendTunnelMsg(null, [0, {message: msg, code: 'E_BAD_COMMAND'}], 'control');
|
2017-04-28 01:36:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd[0] < 0) {
|
2017-04-28 18:50:50 +00:00
|
|
|
// 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]) {
|
2018-06-01 06:41:32 +00:00
|
|
|
console.warn('received error response to hello from', socketId, cmd[1]);
|
2017-04-28 18:50:50 +00:00
|
|
|
}
|
2017-04-28 01:36:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-04-28 18:50:50 +00:00
|
|
|
console.warn('received response to unknown command', cmd, 'from', socketId);
|
2017-04-28 01:36:28 +00:00
|
|
|
}
|
2017-04-28 18:50:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-28 20:19:56 +00:00
|
|
|
if (cmd[0] === 0) {
|
|
|
|
console.warn('received dis-associated error from', socketId, cmd[1]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-28 18:50:50 +00:00
|
|
|
if (commandHandlers[cmd[1]]) {
|
|
|
|
err = commandHandlers[cmd[1]].apply(null, cmd.slice(2));
|
2017-04-28 01:36:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-04-28 18:50:50 +00:00
|
|
|
err = { message: 'unknown command "'+cmd[1]+'"', code: 'E_UNKNOWN_COMMAND' };
|
2017-04-28 01:36:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 23:10:47 +00:00
|
|
|
sendTunnelMsg(null, [-cmd[0], err], 'control');
|
2017-04-28 01:36:28 +00:00
|
|
|
}
|
2017-09-07 23:10:47 +00:00
|
|
|
|
2018-05-31 05:24:43 +00:00
|
|
|
, onmessage: function (tun) {
|
2018-05-31 06:12:37 +00:00
|
|
|
var cid = Packer.addrToId(tun);
|
2018-06-01 06:41:32 +00:00
|
|
|
if (state.debug) { console.log("remote '" + logName() + "' has data for '" + cid + "'", tun.data.byteLength); }
|
2016-09-30 06:46:02 +00:00
|
|
|
|
2017-09-07 23:10:47 +00:00
|
|
|
var browserConn = getBrowserConn(cid);
|
|
|
|
if (!browserConn) {
|
2018-05-31 05:24:43 +00:00
|
|
|
sendTunnelMsg(tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error');
|
2017-09-07 23:10:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-31 05:24:43 +00:00
|
|
|
browserConn.write(tun.data);
|
2017-09-11 21:45:17 +00:00
|
|
|
// tunnelRead is how many bytes we've read from the tunnel, and written to the browser.
|
2018-05-31 05:24:43 +00:00
|
|
|
browserConn.tunnelRead = (browserConn.tunnelRead || 0) + tun.data.byteLength;
|
2017-09-07 23:10:47 +00:00
|
|
|
// 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.
|
2017-09-08 17:24:30 +00:00
|
|
|
// 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) {
|
2018-05-31 05:24:43 +00:00
|
|
|
sendTunnelMsg(tun, browserConn.tunnelRead, 'pause');
|
2017-09-08 17:24:30 +00:00
|
|
|
browserConn.remotePaused = true;
|
|
|
|
|
2017-09-07 23:10:47 +00:00
|
|
|
browserConn.once('drain', function () {
|
2018-05-31 05:24:43 +00:00
|
|
|
sendTunnelMsg(tun, browserConn.tunnelRead, 'resume');
|
2017-09-08 17:24:30 +00:00
|
|
|
browserConn.remotePaused = false;
|
2017-09-07 23:10:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 05:24:43 +00:00
|
|
|
, onpause: function (tun) {
|
|
|
|
var cid = Packer.addrToId(tun);
|
2017-09-07 23:10:47 +00:00
|
|
|
console.log('[TunnelPause]', cid);
|
2017-09-07 00:43:34 +00:00
|
|
|
var browserConn = getBrowserConn(cid);
|
2017-04-27 01:52:30 +00:00
|
|
|
if (browserConn) {
|
2017-09-07 23:10:47 +00:00
|
|
|
browserConn.manualPause = true;
|
|
|
|
browserConn.pause();
|
2017-09-07 00:43:34 +00:00
|
|
|
} else {
|
2018-05-31 05:24:43 +00:00
|
|
|
sendTunnelMsg(tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error');
|
2016-09-30 06:46:02 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-31 05:24:43 +00:00
|
|
|
|
|
|
|
, onresume: function (tun) {
|
|
|
|
var cid = Packer.addrToId(tun);
|
2017-09-07 23:10:47 +00:00
|
|
|
console.log('[TunnelResume]', cid);
|
|
|
|
var browserConn = getBrowserConn(cid);
|
|
|
|
if (browserConn) {
|
|
|
|
browserConn.manualPause = false;
|
|
|
|
browserConn.resume();
|
|
|
|
} else {
|
2018-05-31 05:24:43 +00:00
|
|
|
sendTunnelMsg(tun, {message: 'no matching connection', code: 'E_NO_CONN'}, 'error');
|
2017-09-07 23:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-31 05:24:43 +00:00
|
|
|
, onend: function (tun) {
|
|
|
|
var cid = Packer.addrToId(tun);
|
2016-09-30 06:46:02 +00:00
|
|
|
console.log('[TunnelEnd]', cid);
|
2017-09-08 17:24:30 +00:00
|
|
|
closeBrowserConn(cid);
|
2016-09-30 06:46:02 +00:00
|
|
|
}
|
2018-05-31 05:24:43 +00:00
|
|
|
, onerror: function (tun) {
|
|
|
|
var cid = Packer.addrToId(tun);
|
2018-06-01 06:41:32 +00:00
|
|
|
console.warn('[TunnelError]', cid, tun.message);
|
2017-09-08 17:24:30 +00:00
|
|
|
closeBrowserConn(cid);
|
2016-09-30 06:46:02 +00:00
|
|
|
}
|
|
|
|
};
|
2018-05-31 05:24:43 +00:00
|
|
|
var unpacker = Packer.create(packerHandlers);
|
2017-04-05 02:31:58 +00:00
|
|
|
|
2017-04-07 21:46:00 +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) {
|
2018-06-01 06:41:32 +00:00
|
|
|
if (state.debug) { console.log('pinging', logName()); }
|
2017-04-07 21:46:00 +00:00
|
|
|
try {
|
2017-04-27 01:52:30 +00:00
|
|
|
ws.ping();
|
2017-04-07 21:46:00 +00:00
|
|
|
} catch (err) {
|
2017-04-27 01:52:30 +00:00
|
|
|
console.warn('failed to ping home cloud', logName());
|
2017-04-07 21:46:00 +00:00
|
|
|
}
|
|
|
|
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 {
|
2018-06-01 06:41:32 +00:00
|
|
|
console.warn('home cloud', logName(), 'connection timed out');
|
2017-04-27 01:52:30 +00:00
|
|
|
ws.close(1013, 'connection timeout');
|
2017-04-07 21:46:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
2018-06-01 06:41:32 +00:00
|
|
|
if (state.debug) { console.log('[ws] device => client : demultiplexing message ', chunk.byteLength, 'bytes'); }
|
2016-09-30 07:11:24 +00:00
|
|
|
//console.log(chunk.toString());
|
2017-04-27 01:52:30 +00:00
|
|
|
unpacker.fns.addChunk(chunk);
|
2017-04-07 21:46:00 +00:00
|
|
|
});
|
|
|
|
|
2017-04-05 02:31:58 +00:00
|
|
|
function hangup() {
|
2017-04-07 21:46:00 +00:00
|
|
|
clearTimeout(timeoutId);
|
2018-06-01 06:41:32 +00:00
|
|
|
console.log('[ws] device hangup', logName(), 'connection closing');
|
2017-04-27 01:52:30 +00:00
|
|
|
Object.keys(remotes).forEach(function (jwtoken) {
|
|
|
|
removeToken(jwtoken);
|
2017-04-26 18:05:44 +00:00
|
|
|
});
|
2017-05-26 23:26:44 +00:00
|
|
|
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);
|
2017-04-07 00:21:21 +00:00
|
|
|
ws.on('error', hangup);
|
2017-04-28 18:50:50 +00:00
|
|
|
|
|
|
|
// We only ever send one command and we send it once, so we just hard code the ID as 1
|
2017-09-07 23:10:47 +00:00
|
|
|
sendTunnelMsg(null, [1, 'hello', [unpacker._version], Object.keys(commandHandlers)], 'control');
|
2017-04-07 00:21:21 +00:00
|
|
|
}
|
2016-09-27 21:56:06 +00:00
|
|
|
|
2017-06-22 00:55:24 +00:00
|
|
|
return {
|
|
|
|
tcp: onTcpConnection
|
|
|
|
, ws: onWsConnection
|
2018-05-31 05:24:43 +00:00
|
|
|
, isClientDomain: Devices.exist.bind(null, state.deviceLists)
|
2017-06-22 00:55:24 +00:00
|
|
|
};
|
2016-10-01 05:52:37 +00:00
|
|
|
};
|