From ebf2fd22042afb33454f2b74f9af6fda4869519d Mon Sep 17 00:00:00 2001 From: psihius Date: Thu, 26 Apr 2018 15:40:47 +0300 Subject: [PATCH] Added cluster wide command execution + fixed some checks failing due to type mismatch --- master.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/master.js b/master.js index f7831405..d57154d3 100644 --- a/master.js +++ b/master.js @@ -793,19 +793,35 @@ app.post("/api/runCommand", (req,res) => { if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' }); // validate request.body - let body = req.body; - if(body.instanceID - && Object.keys(wsSlaves).includes(body.instanceID) - && body.command - && typeof body.command == "string" - && body.command[0] == "/"){ - // execute command - wsSlaves[body.instanceID].runCommand(body.command, data => { - res.status(200).send({auth: true, message: "success", data}); - }); - } else { - res.status(400).send({auth: true, message: "Error: invalid request.body"}); - } + let body = req.body; + + if ( + body.broadcast + && body.command + && typeof body.command == "string" + && body.command[0] == "/") + { + let instanceResponses = []; + for (let instanceID in wsSlaves) { + // skip loop if the property is from prototype + if (!wsSlaves.hasOwnProperty(instanceID)) continue; + wsSlaves[instanceID].runCommand(body.command); + } + res.status(200).send({auth: true, message: "success", response: "Cluster wide messaging initiated"}); + } else if ( + body.instanceID + && wsSlaves[body.instanceID] + && body.command + && typeof body.command == "string" + && body.command[0] == "/") + { + // execute command + wsSlaves[body.instanceID].runCommand(body.command, data => { + res.status(200).send({auth: true, message: "success", data}); + }); + } else { + res.status(400).send({auth: true, message: "Error: invalid request.body"}); + } }); }); /**