add runCommands to server peering #6404
Signed-off-by: Simon Smith <simonsmith5521@gmail.com>
Simon Smith committed
Sep 26, 2024 at 22:56 UTC
df64c750cc8c0dcfad18478165f91bf60b1facd0
2 files changed
+62
-32
meshuser.js
+48
-32
@@ -2967,13 +2967,19 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2967
return;
2968
}
2969
2970
- // Send the commands to the agent
2971
- var agent = parent.wsagents[node._id];
2972
- if ((agent != null) && (agent.authenticated == 2) && (agent.agentInfo != null)) {
2973
- try { agent.send(JSON.stringify({ action: 'msg', type: 'console', value: command.cmds, rights: rights, sessionid: ws.sessionId })); } catch (ex) { }
2970
+ var theCommand = { action: 'msg', type: 'console', value: command.cmds, rights: rights, sessionid: ws.sessionId };
2971
+ if (parent.parent.multiServer != null) { // peering setup
2972
+ parent.parent.multiServer.DispatchMessage({ action: 'agentCommand', nodeid: node._id, command: theCommand});
2973
if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
2974
} else {
2976
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Agent not connected' })); } catch (ex) { } }
2975
+ // Send the commands to the agent
2976
+ var agent = parent.wsagents[node._id];
2977
+ if ((agent != null) && (agent.authenticated == 2) && (agent.agentInfo != null)) {
2978
+ try { agent.send(JSON.stringify(theCommand)); } catch (ex) { }
2979
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
2980
+ } else {
2981
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Agent not connected' })); } catch (ex) { } }
2982
+ }
2983
}
2984
} else {
2985
// This is a standard (bash/shell/powershell) command.
@@ -2984,40 +2990,50 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
2990
return;
2991
}
2992
2987
- // Get the agent and run the commands
2988
- var agent = parent.wsagents[node._id];
2989
- if ((agent != null) && (agent.authenticated == 2) && (agent.agentInfo != null)) {
2990
- // Check if this agent is correct for this command type
2991
- // command.type 1 = Windows Command, 2 = Windows PowerShell, 3 = Linux/BSD/macOS
2992
- var commandsOk = false;
2993
- if ((agent.agentInfo.agentId > 0) && (agent.agentInfo.agentId < 5)) {
2994
- // Windows Agent
2995
- if ((command.type == 1) || (command.type == 2)) { commandsOk = true; }
2996
- else if (command.type === 0) { command.type = 1; commandsOk = true; } // Set the default type of this agent
2997
- } else {
2998
- // Non-Windows Agent
2999
- if (command.type == 3) { commandsOk = true; }
3000
- else if (command.type === 0) { command.type = 3; commandsOk = true; } // Set the default type of this agent
3001
- }
3002
- if (commandsOk == true) {
2993
+ if (typeof command.reply != 'boolean') command.reply = false;
2994
+ if (typeof command.responseid != 'string') command.responseid = null;
2995
+ var msgid = 24; // "Running commands"
2996
+ if (command.type == 1) { msgid = 99; } // "Running commands as user"
2997
+ if (command.type == 2) { msgid = 100; } // "Running commands as user if possible"
2998
+ // Check if this agent is correct for this command type
2999
+ // command.type 1 = Windows Command, 2 = Windows PowerShell, 3 = Linux/BSD/macOS
3000
+ var commandsOk = false;
3001
+ if ((node.agent.id > 0) && (node.agent.id < 5)) {
3002
+ // Windows Agent
3003
+ if ((command.type == 1) || (command.type == 2)) { commandsOk = true; }
3004
+ else if (command.type === 0) { command.type = 1; commandsOk = true; } // Set the default type of this agent
3005
+ } else {
3006
+ // Non-Windows Agent
3007
+ if (command.type == 3) { commandsOk = true; }
3008
+ else if (command.type === 0) { command.type = 3; commandsOk = true; } // Set the default type of this agent
3009
+ }
3010
+ if (commandsOk == true) {
3011
+ var theCommand = { action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser, reply: command.reply, responseid: command.responseid };
3012
+ if (parent.parent.multiServer != null) { // peering setup
3013
// Send the commands to the agent
3004
- if (typeof command.reply != 'boolean') command.reply = false;
3005
- if (typeof command.responseid != 'string') command.responseid = null;
3006
- try { agent.send(JSON.stringify({ action: 'runcommands', type: command.type, cmds: command.cmds, runAsUser: command.runAsUser, reply: command.reply, responseid: command.responseid })); } catch (ex) { }
3014
+ parent.parent.multiServer.DispatchMessage({ action: 'agentCommand', nodeid: node._id, command: theCommand});
3015
if (command.responseid != null && command.reply == false) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
3008
-
3016
// Send out an event that these commands where run on this device
3017
var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
3011
- var msgid = 24; // "Running commands"
3012
- if (command.type == 1) { msgid = 99; } // "Running commands as user"
3013
- if (command.type == 2) { msgid = 100; } // "Running commands as user if possible"
3018
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 };
3015
- parent.parent.DispatchEvent(targets, obj, event);
3016
- } else {
3017
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Invalid command type' })); } catch (ex) { } }
3019
+ parent.parent.multiServer.DispatchEvent(targets, obj, event);
3020
+ } else { // normal setup
3021
+ // Get the agent and run the commands
3022
+ var agent = parent.wsagents[node._id];
3023
+ if ((agent != null) && (agent.authenticated == 2) && (agent.agentInfo != null)) {
3024
+ // Send the commands to the agent
3025
+ try { agent.send(JSON.stringify(theCommand)); } catch (ex) { }
3026
+ if (command.responseid != null && command.reply == false) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'OK' })); } catch (ex) { } }
3027
+ // Send out an event that these commands where run on this device
3028
+ var targets = parent.CreateNodeDispatchTargets(node.meshid, node._id, ['server-users', user._id]);
3029
+ 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 };
3030
+ parent.parent.DispatchEvent(targets, obj, event);
3031
+ } else {
3032
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Agent not connected' })); } catch (ex) { } }
3033
+ }
3034
}
3035
} else {
3020
- if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Agent not connected' })); } catch (ex) { } }
3036
+ if (command.responseid != null) { try { ws.send(JSON.stringify({ action: 'runcommands', responseid: command.responseid, result: 'Invalid command type' })); } catch (ex) { } }
3037
}
3038
}
3039
});
multiserver.js
+14
@@ -586,6 +586,20 @@ module.exports.CreateMultiServer = function (parent, args) {
586
}
587
break;
588
}
589
+ case 'agentCommand': {
590
+ if (msg.nodeid != null) {
591
+ // Route this message to a connected agent
592
+ var agent = obj.parent.webserver.wsagents[msg.nodeid];
593
+ if (agent != null) { agent.send(JSON.stringify(msg.command)); }
594
+ } else if (msg.meshid != null) {
595
+ // Route this message to all connected agents of this mesh
596
+ for (var nodeid in obj.parent.webserver.wsagents) {
597
+ var agent = obj.parent.webserver.wsagents[nodeid];
598
+ if (agent.dbMeshKey == msg.meshid) { try { agent.send(JSON.stringify(msg.command)); } catch (ex) { } }
599
+ }
600
+ }
601
+ break;
602
+ }
603
default: {
604
// Unknown peer server command
605
console.log('Unknown action from peer server ' + peerServerId + ': ' + msg.action + '.');