From bd8cc182255a551f9605f43d694199f15fa28484 Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Thu, 1 Oct 2020 10:30:26 -0700 Subject: [PATCH] Fixed MeshMessenger connection issue. --- agents/meshcore.js | 25 ++++++++++++++++++++++++- meshagent.js | 15 +++++++++------ views/messenger.handlebars | 19 ++++++++++++++++++- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/agents/meshcore.js b/agents/meshcore.js index 2bc004b9..5f32cc7f 100644 --- a/agents/meshcore.js +++ b/agents/meshcore.js @@ -149,7 +149,11 @@ function createMeshCore(agent) { }; this._daipc = c; c.parent = this; - c.on('end', function () { this.end(); this.parent._daipc = null; }); // TODO: Must call end() on self to close the named pipe correctly. + c.on('end', function () { + this.end(); // TODO: Must call end() on self to close the named pipe correctly. + this.parent._daipc = null; + if (this._registered != null) { try { mesh.SendCommand({ action: 'sessions', type: 'app', value: {} }); } catch (e) { } } + }); c.on('data', function (chunk) { if (chunk.length < 4) { this.unshift(chunk); return; } var len = chunk.readUInt32LE(0); @@ -162,6 +166,15 @@ function createMeshCore(agent) { try { switch (data.cmd) { + case 'register': + if (typeof data.value == 'string') { + this._registered = data.value; + var apps = {}; + apps[data.value] = 1; + try { mesh.SendCommand({ action: 'sessions', type: 'app', value: apps }); } catch (e) { } + this._send({ cmd: 'serverstate', value: meshServerConnectionState, url: require('MeshAgent').ConnectedServer }); + } + break; case 'query': switch (data.value) { case 'connection': @@ -3572,6 +3585,16 @@ function createMeshCore(agent) { try { mesh.SendCommand({ action: 'sessions', type: 'msg', value: tunnelUserCount.msg }); } catch (e) { } } } + + // Send update to the registered application + if ((obj.DAIPC._daipc != null) && (obj.DAIPC._daipc._registered != null)) { + if (state == 1) { + var apps = {}; + apps[obj.DAIPC._daipc._registered] = 1; + try { mesh.SendCommand({ action: 'sessions', type: 'app', value: apps }); } catch (e) { } + } + obj.DAIPC._daipc._send({ cmd: 'serverstate', value: meshServerConnectionState, url: require('MeshAgent').ConnectedServer }); + } } // Update the server with the latest network interface information diff --git a/meshagent.js b/meshagent.js index 230765d1..5feadd4f 100644 --- a/meshagent.js +++ b/meshagent.js @@ -1378,12 +1378,15 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) { case 'sessions': { // This is a list of sessions provided by the agent if (obj.sessions == null) { obj.sessions = {}; } - if (command.type == 'kvm') { obj.sessions.kvm = command.value; } - else if (command.type == 'terminal') { obj.sessions.terminal = command.value; } - else if (command.type == 'files') { obj.sessions.files = command.value; } - else if (command.type == 'tcp') { obj.sessions.tcp = command.value; } - else if (command.type == 'udp') { obj.sessions.udp = command.value; } - else if (command.type == 'msg') { obj.sessions.msg = command.value; } + if (typeof command.value != null) { + if (command.type == 'kvm') { obj.sessions.kvm = command.value; } + else if (command.type == 'terminal') { obj.sessions.terminal = command.value; } + else if (command.type == 'files') { obj.sessions.files = command.value; } + else if (command.type == 'tcp') { obj.sessions.tcp = command.value; } + else if (command.type == 'udp') { obj.sessions.udp = command.value; } + else if (command.type == 'msg') { obj.sessions.msg = command.value; } + else if (command.type == 'app') { obj.sessions.app = command.value; } + } obj.updateSessions(); break; } diff --git a/views/messenger.handlebars b/views/messenger.handlebars index e8eb370c..6e642f4d 100644 --- a/views/messenger.handlebars +++ b/views/messenger.handlebars @@ -46,7 +46,7 @@ var userInputFocus = 0; var socket = null; // Websocket object var state = 0; // Connection state. 0 = Disconnected, 1 = Connecting, 2 = Connected. - var args = parseUriArgs(); + var args = XparseUriArgs(); if (args.key && (isAlphaNumeric(args.key) == false)) { delete args.key; } if (args.locale && (isAlphaNumeric(args.locale) == false)) { delete args.locale; } @@ -656,6 +656,23 @@ if (socket != null) { try { socket.close(); } catch (e) { } socket = null; } } + function isSafeString3(str) { return ((typeof str == 'string') && (str.indexOf('<') == -1) && (str.indexOf('>') == -1) && (str.indexOf('&') == -1) && (str.indexOf('"') == -1) && (str.indexOf('\'') == -1) && (str.indexOf('+') == -1) && (str.indexOf('(') == -1) && (str.indexOf(')') == -1) && (str.indexOf('#') == -1) && (str.indexOf(':') == -1)) }; + + // Parse URL arguments, only keep safe values + function XparseUriArgs() { + var href = window.document.location.href; + if (href.endsWith('#')) { href = href.substring(0, href.length - 1); } + var name, r = {}, parsedUri = href.split(/[\?&|]/); + parsedUri.splice(0, 1); + for (var j in parsedUri) { + var arg = parsedUri[j], i = arg.indexOf('='); + name = arg.substring(0, i); + r[name] = arg.substring(i + 1); + if (!isSafeString3(r[name])) { delete r[name]; } else { var x = parseInt(r[name]); if (x == r[name]) { r[name] = x; } } + } + return r; + } +