Added inter-user messaging support.

Ylian Saint-Hilaire committed Sep 10, 2021 at 21:10 UTC 29c34ab5b20a1be20182c0d7777a4cb07e102352
4 files changed +33 -4
meshcentral-config-schema.json
+3 -2
@@ -131,8 +131,9 @@
131 "agentAllowedIP": { "type": [ "string", "array" ] },
132 "agentBlockedIP": { "type": [ "string", "array" ] },
133 "authLog": { "type": "string", "default": null, "description": "File path and name of the authentication log to be created. This log can be parsed by Fail2ban." },
134 - "manageAllDeviceGroups": { "type": "array", "uniqueItems": true, "items": { "type": "string" } },
135 - "manageCrossDomain": { "type": "array", "uniqueItems": true, "items": { "type": "string" } },
134 + "InterUserMessaging": { "type": "array", "uniqueItems": true, "items": { "type": "string" }, "description": "Users in this list are allowed to send and receive inter-user messages. This can be used to implement bots or other software where MeshCentral is used as data transport. See \"interuser\" websocket command in the code." },
135 + "manageAllDeviceGroups": { "type": "array", "uniqueItems": true, "items": { "type": "string" }, "description": "Users in this list are allowed to see and manage all device groups within their domain." },
136 + "manageCrossDomain": { "type": "array", "uniqueItems": true, "items": { "type": "string" }, "description": "Users in this list are allowed to manage all users in all domains." },
137 "localDiscovery": {
138 "type": "object",
139 "description": "When this server is in LAN mode, you may discover this server using a multicast discovery tool. When discovery happens, the name and info fields are sent back to the discovery tool.",
meshctrl.js
+2 -2
@@ -13,7 +13,7 @@ if (args.proxy != null) { try { require('https-proxy-agent'); } catch (ex) { con
13
14 if (args['_'].length == 0) {
15 console.log("MeshCtrl performs command line actions on a MeshCentral server.");
16 - console.log("Information at: https://meshcommander.com/meshcentral");
16 + console.log("Information at: https://meshcentral.com");
17 console.log("No action specified, use MeshCtrl like this:\r\n\r\n meshctrl [action] [arguments]\r\n");
18 console.log("Supported actions:");
19 console.log(" Help [action] - Get help on an action.");
@@ -1139,7 +1139,7 @@ function serverConnect() {
1139 }
1140
1141 const ws = new WebSocket(url, options);
1142 - console.log('Connecting to ' + url);
1142 + //console.log('Connecting to ' + url);
1143
1144 ws.on('open', function open() {
1145 //console.log('Connected.');
meshuser.js
+27
@@ -618,6 +618,33 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
618
619 // TODO: Send the message of user sessions connected to other servers.
620
621 + break;
622 + }
623 + case 'interuser':
624 + {
625 + // Sends data between users only if allowed.
626 + if (command.data == null) return;
627 + if (typeof command.sessionid == 'string') { var userSessionId = command.sessionid.split('/'); if (userSessionId.length != 4) return; command.userid = userSessionId[0] + '/' + userSessionId[1] + '/' + userSessionId[2]; }
628 + if (common.validateString(command.userid, 0, 2014) == false) return;
629 + var userSplit = command.userid.split('/');
630 + if (userSplit.length == 1) { command.userid = 'user/' + domain.id + '/' + command.userid; userSplit = command.userid.split('/'); }
631 + if ((userSplit.length != 3) || (userSplit[0] != 'user') || (userSplit[1] != domain.id) || (parent.users[command.userid] == null)) return; // Make sure the target userid is valid and within the domain
632 + const allowed = ((parent.parent.config.settings.interusermessaging === true) || (parent.parent.config.settings.interusermessaging.indexOf(obj.user._id) >= 0) || (parent.parent.config.settings.interusermessaging.indexOf(command.userid) >= 0));
633 + if (allowed == false) return;
634 +
635 + // Get sessions
636 + var sessions = parent.wssessions[command.userid];
637 + if (sessions == null) break;
638 +
639 + // Create the notification message and send on all sessions except our own (no echo back).
640 + var notification = JSON.stringify({ action: 'interuser', sessionid: ws.sessionId, data: command.data, scope: (command.sessionid != null)?'session':'user' });
641 + for (var i in sessions) {
642 + if ((command.sessionid != null) && (sessions[i].sessionId != command.sessionid)) continue; // Send to a specific session
643 + if (sessions[i] != obj.ws) { try { sessions[i].send(notification); } catch (ex) { } }
644 + }
645 +
646 + // TODO: Send the message of user sessions connected to other servers.
647 +
648 break;
649 }
650 case 'authcookie':
sample-config-advanced.json
+1
@@ -60,6 +60,7 @@
60 "_agentAllowedIP": "192.168.0.100/24",
61 "_agentBlockedIP": "127.0.0.1,::1",
62 "_authLog": "c:\\temp\\auth.log",
63 + "_InterUserMessaging": [ "user//admin" ],
64 "_manageAllDeviceGroups": [ "user//admin" ],
65 "_manageCrossDomain": [ "user//admin" ],
66 "_localDiscovery": {