Fixed MeshMessenger connection issue.

Ylian Saint-Hilaire committed Oct 1, 2020 at 10:30 UTC d55c102a8f314e88895537d259ae2e15b242e48b
3 files changed +51 -8
agents/meshcore.js
+24 -1
@@ -149,7 +149,11 @@ function createMeshCore(agent) {
149 };
150 this._daipc = c;
151 c.parent = this;
152 - c.on('end', function () { this.end(); this.parent._daipc = null; }); // TODO: Must call end() on self to close the named pipe correctly.
152 + c.on('end', function () {
153 + this.end(); // TODO: Must call end() on self to close the named pipe correctly.
154 + this.parent._daipc = null;
155 + if (this._registered != null) { try { mesh.SendCommand({ action: 'sessions', type: 'app', value: {} }); } catch (e) { } }
156 + });
157 c.on('data', function (chunk) {
158 if (chunk.length < 4) { this.unshift(chunk); return; }
159 var len = chunk.readUInt32LE(0);
@@ -162,6 +166,15 @@ function createMeshCore(agent) {
166
167 try {
168 switch (data.cmd) {
169 + case 'register':
170 + if (typeof data.value == 'string') {
171 + this._registered = data.value;
172 + var apps = {};
173 + apps[data.value] = 1;
174 + try { mesh.SendCommand({ action: 'sessions', type: 'app', value: apps }); } catch (e) { }
175 + this._send({ cmd: 'serverstate', value: meshServerConnectionState, url: require('MeshAgent').ConnectedServer });
176 + }
177 + break;
178 case 'query':
179 switch (data.value) {
180 case 'connection':
@@ -3572,6 +3585,16 @@ function createMeshCore(agent) {
3585 try { mesh.SendCommand({ action: 'sessions', type: 'msg', value: tunnelUserCount.msg }); } catch (e) { }
3586 }
3587 }
3588 +
3589 + // Send update to the registered application
3590 + if ((obj.DAIPC._daipc != null) && (obj.DAIPC._daipc._registered != null)) {
3591 + if (state == 1) {
3592 + var apps = {};
3593 + apps[obj.DAIPC._daipc._registered] = 1;
3594 + try { mesh.SendCommand({ action: 'sessions', type: 'app', value: apps }); } catch (e) { }
3595 + }
3596 + obj.DAIPC._daipc._send({ cmd: 'serverstate', value: meshServerConnectionState, url: require('MeshAgent').ConnectedServer });
3597 + }
3598 }
3599
3600 // Update the server with the latest network interface information
meshagent.js
+9 -6
@@ -1378,12 +1378,15 @@ module.exports.CreateMeshAgent = function (parent, db, ws, req, args, domain) {
1378 case 'sessions': {
1379 // This is a list of sessions provided by the agent
1380 if (obj.sessions == null) { obj.sessions = {}; }
1381 - if (command.type == 'kvm') { obj.sessions.kvm = command.value; }
1382 - else if (command.type == 'terminal') { obj.sessions.terminal = command.value; }
1383 - else if (command.type == 'files') { obj.sessions.files = command.value; }
1384 - else if (command.type == 'tcp') { obj.sessions.tcp = command.value; }
1385 - else if (command.type == 'udp') { obj.sessions.udp = command.value; }
1386 - else if (command.type == 'msg') { obj.sessions.msg = command.value; }
1381 + if (typeof command.value != null) {
1382 + if (command.type == 'kvm') { obj.sessions.kvm = command.value; }
1383 + else if (command.type == 'terminal') { obj.sessions.terminal = command.value; }
1384 + else if (command.type == 'files') { obj.sessions.files = command.value; }
1385 + else if (command.type == 'tcp') { obj.sessions.tcp = command.value; }
1386 + else if (command.type == 'udp') { obj.sessions.udp = command.value; }
1387 + else if (command.type == 'msg') { obj.sessions.msg = command.value; }
1388 + else if (command.type == 'app') { obj.sessions.app = command.value; }
1389 + }
1390 obj.updateSessions();
1391 break;
1392 }
views/messenger.handlebars
+18 -1
@@ -46,7 +46,7 @@
46 var userInputFocus = 0;
47 var socket = null; // Websocket object
48 var state = 0; // Connection state. 0 = Disconnected, 1 = Connecting, 2 = Connected.
49 - var args = parseUriArgs();
49 + var args = XparseUriArgs();
50 if (args.key && (isAlphaNumeric(args.key) == false)) { delete args.key; }
51 if (args.locale && (isAlphaNumeric(args.locale) == false)) { delete args.locale; }
52
@@ -656,6 +656,23 @@
656 if (socket != null) { try { socket.close(); } catch (e) { } socket = null; }
657 }
658
659 + 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)) };
660 +
661 + // Parse URL arguments, only keep safe values
662 + function XparseUriArgs() {
663 + var href = window.document.location.href;
664 + if (href.endsWith('#')) { href = href.substring(0, href.length - 1); }
665 + var name, r = {}, parsedUri = href.split(/[\?&|]/);
666 + parsedUri.splice(0, 1);
667 + for (var j in parsedUri) {
668 + var arg = parsedUri[j], i = arg.indexOf('=');
669 + name = arg.substring(0, i);
670 + r[name] = arg.substring(i + 1);
671 + if (!isSafeString3(r[name])) { delete r[name]; } else { var x = parseInt(r[name]); if (x == r[name]) { r[name] = x; } }
672 + }
673 + return r;
674 + }
675 +
676 </script>
677 </body>
678 </html>