fix runcommands in a peering environment (#6825)

* fix adding meshes and user groups in a peering environment * fix runcommands in a peering environment

ijustw0rkhere committed Feb 27, 2025 at 10:12 UTC 0d65080a8a0f382f676dc12a8b8d2af5e342f430
1 file changed +12 -15
meshuser.js
+12 -15
@@ -3079,7 +3079,16 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3079 }
3080 if (commandsOk == true) {
3081 var theCommand = { action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser, reply: command.reply, responseid: command.responseid };
3082 - if (parent.parent.multiServer != null) { // peering setup
3082 + var agent = parent.wsagents[node._id];
3083 + if ((agent != null) && (agent.authenticated == 2) && (agent.agentInfo != null)) {
3084 + // Send the commands to the agent
3085 + try { agent.send(JSON.stringify(theCommand)); } catch (ex) { }
3086 + if (command.responseid != null && command.reply == false) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
3087 + // Send out an event that these commands where run on this device
3088 + var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
3089 + var event = { etype: 'node', userid: user._id, username: user.name, nodeid: node._id, action: 'runcommands', msg: 'Running commands', msgid: msgid, cmds: command.cmds, cmdType: command.type, runAsUser: command.runAsUser, domain: domain.id };
3090 + parent.parent.DispatchEvent(targets, obj, event);
3091 + } else if (parent.parent.multiServer != null) { // peering setup
3092 // Send the commands to the agent
3093 parent.parent.multiServer.DispatchMessage({ action: 'agentCommand', nodeid: node._id, command: theCommand});
3094 if (command.responseid != null && command.reply == false) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
@@ -3087,20 +3096,8 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3096 var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
3097 var event = { etype: 'node', userid: user._id, username: user.name, nodeid: node._id, action: 'runcommands', msg: 'Running commands', msgid: msgid, cmds: command.cmds, cmdType: command.type, runAsUser: command.runAsUser, domain: domain.id };
3098 parent.parent.multiServer.DispatchEvent(targets, obj, event);
3090 - } else { // normal setup
3091 - // Get the agent and run the commands
3092 - var agent = parent.wsagents[node._id];
3093 - if ((agent != null) && (agent.authenticated == 2) && (agent.agentInfo != null)) {
3094 - // Send the commands to the agent
3095 - try { agent.send(JSON.stringify(theCommand)); } catch (ex) { }
3096 - if (command.responseid != null && command.reply == false) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
3097 - // Send out an event that these commands where run on this device
3098 - var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
3099 - var event = { etype: 'node', userid: user._id, username: user.name, nodeid: node._id, action: 'runcommands', msg: 'Running commands', msgid: msgid, cmds: command.cmds, cmdType: command.type, runAsUser: command.runAsUser, domain: domain.id };
3100 - parent.parent.DispatchEvent(targets, obj, event);
3101 - } else {
3102 - if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Agent not connected' })); } catch (ex) { } }
3103 - }
3099 + } else {
3100 + if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Agent not connected' })); } catch (ex) { } }
3101 }
3102 } else {
3103 if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Invalid command type' })); } catch (ex) { } }