mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 16:23:12 +09:00
Fix for /issues/177 (#178)
/api/editSlaveMeta: Removed unneeded rcon check /api/getSlaveMeta: Removed useless password check /api/checkMod: Added authenticate.middleware /api/slaves: Renamed to /api/rconPasswords and changed to authenticate.middleware /api/runCommand: Changed to authenticate.middleware
This commit is contained in:
1 parent
49611a2062
commit
d909b4aa47
1 file changed
+47
-60
@@ -352,15 +352,16 @@ app.post("/api/editSlaveMeta", authenticate.middleware, function(req,res) {
|
|||||||
// request.body should be an object
|
// request.body should be an object
|
||||||
// {instanceID, pass, meta:{x,y,z}}
|
// {instanceID, pass, meta:{x,y,z}}
|
||||||
|
|
||||||
if(req.body && req.body.instanceID && req.body.password && req.body.meta){
|
if(req.body && req.body.instanceID && req.body.meta){
|
||||||
// check for editing permissions
|
if(slaves[req.body.instanceID]){
|
||||||
if(slaves[req.body.instanceID] && slaves[req.body.instanceID].rconPassword == req.body.password){
|
if(!slaves[req.body.instanceID].meta) {
|
||||||
if(!slaves[req.body.instanceID].meta){
|
|
||||||
slaves[req.body.instanceID].meta = {};
|
slaves[req.body.instanceID].meta = {};
|
||||||
}
|
}
|
||||||
slaves[req.body.instanceID].meta = deepmerge(slaves[req.body.instanceID].meta, req.body.meta, {clone:true});
|
slaves[req.body.instanceID].meta = deepmerge(slaves[req.body.instanceID].meta, req.body.meta, {clone:true});
|
||||||
let metaPortion = JSON.stringify(req.body.meta);
|
let metaPortion = JSON.stringify(req.body.meta);
|
||||||
if(metaPortion.length > 50) metaPortion = metaPortion.substring(0,20) + "...";
|
if(metaPortion.length > 50) {
|
||||||
|
metaPortion = metaPortion.substring(0,20) + "...";
|
||||||
|
}
|
||||||
// console.log("Updating slave "+slaves[req.body.instanceID].instanceName+": " + slaves[req.body.instanceID].mac + " : " + slaves[req.body.instanceID].serverPort+" at " + slaves[req.body.instanceID].publicIP, metaPortion);
|
// console.log("Updating slave "+slaves[req.body.instanceID].instanceName+": " + slaves[req.body.instanceID].mac + " : " + slaves[req.body.instanceID].serverPort+" at " + slaves[req.body.instanceID].publicIP, metaPortion);
|
||||||
res.sendStatus(200);
|
res.sendStatus(200);
|
||||||
} else {
|
} else {
|
||||||
@@ -378,7 +379,7 @@ POST Get metadata from a single slave. For whenever you find the data returned b
|
|||||||
app.post("/api/getSlaveMeta", function (req, res) {
|
app.post("/api/getSlaveMeta", function (req, res) {
|
||||||
endpointHitCounter.labels(req.route.path).inc();
|
endpointHitCounter.labels(req.route.path).inc();
|
||||||
console.log("body", req.body);
|
console.log("body", req.body);
|
||||||
if(req.body && req.body.instanceID && req.body.password){
|
if(req.body && req.body.instanceID){
|
||||||
if (slaves[req.body.instanceID]) {
|
if (slaves[req.body.instanceID]) {
|
||||||
console.log("returning meta for ", req.body.instanceID);
|
console.log("returning meta for ", req.body.instanceID);
|
||||||
res.send(JSON.stringify(slaves[req.body.instanceID].meta))
|
res.send(JSON.stringify(slaves[req.body.instanceID].meta))
|
||||||
@@ -399,7 +400,7 @@ POST Check if a mod has been uploaded to the master before. Only checks against
|
|||||||
@instance
|
@instance
|
||||||
@alias /api/checkMod
|
@alias /api/checkMod
|
||||||
*/
|
*/
|
||||||
app.post("/api/checkMod", function(req,res) {
|
app.post("/api/checkMod", authenticate.middleware, function(req,res) {
|
||||||
endpointHitCounter.labels(req.route.path).inc();
|
endpointHitCounter.labels(req.route.path).inc();
|
||||||
let files = fs.readdirSync(masterModFolder);
|
let files = fs.readdirSync(masterModFolder);
|
||||||
let found = false;
|
let found = false;
|
||||||
@@ -470,26 +471,19 @@ app.get("/api/slaves", function(req, res) {
|
|||||||
res.send(slaveCache.cache);
|
res.send(slaveCache.cache);
|
||||||
});
|
});
|
||||||
/**
|
/**
|
||||||
POST endpoint for getting information about all our slaves, requires auth, responds including RCON passwords
|
POST endpoint for getting the rconPasswords of all our slaves
|
||||||
@memberof clusterioMaster
|
@memberof clusterioMaster
|
||||||
@instance
|
@instance
|
||||||
@alias /api/slaves
|
@alias /api/rconPasswords
|
||||||
*/
|
*/
|
||||||
app.post("/api/slaves", function(req, res) {
|
app.post("/api/rconPasswords", authenticate.middleware, function(req, res) {
|
||||||
let token = req.headers['x-access-token'];
|
endpointHitCounter.labels(req.route.path).inc();
|
||||||
if(!token) {
|
res.header("Access-Control-Allow-Origin", "*");
|
||||||
return res.status(401).send({ auth: false, message: 'No token provided.' });
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||||
}
|
let slaveCache = getSlaves();
|
||||||
jwt.verify(token, config.masterAuthSecret, function(err, decoded) {
|
let serverInfo = [];
|
||||||
if(err) {
|
serverInfo[key] = {id: key, rconPassword: slaveCache[key].rconPassword};
|
||||||
return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
|
res.send(serverInfo);
|
||||||
}
|
|
||||||
endpointHitCounter.labels(req.route.path).inc();
|
|
||||||
res.header("Access-Control-Allow-Origin", "*");
|
|
||||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
|
||||||
let slaveCache = getSlaves();
|
|
||||||
res.send(slaveCache.cache);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
/*
|
/*
|
||||||
var recievedItemStatistics = new averagedTimeSeries({
|
var recievedItemStatistics = new averagedTimeSeries({
|
||||||
@@ -730,45 +724,38 @@ Requires x-access-token header to be set. Find you api token in secret-api-token
|
|||||||
@param {object} JSON {instanceID:19412312, broadcast:false, command:"/c game.print('hello')"}
|
@param {object} JSON {instanceID:19412312, broadcast:false, command:"/c game.print('hello')"}
|
||||||
@returns {object} Status {auth: bool, message: "Informative error", data:{}}
|
@returns {object} Status {auth: bool, message: "Informative error", data:{}}
|
||||||
*/
|
*/
|
||||||
app.post("/api/runCommand", (req,res) => {
|
app.post("/api/runCommand", authenticate.middleware, function(req,res) {
|
||||||
endpointHitCounter.labels(req.route.path).inc();
|
endpointHitCounter.labels(req.route.path).inc();
|
||||||
var token = req.headers['x-access-token'];
|
// validate request.body
|
||||||
if(!token) return res.status(401).send({ auth: false, message: 'No token provided.' });
|
let body = req.body;
|
||||||
|
|
||||||
jwt.verify(token, config.masterAuthSecret, function(err, decoded) {
|
|
||||||
if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
|
|
||||||
|
|
||||||
// validate request.body
|
|
||||||
let body = req.body;
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
body.broadcast
|
body.broadcast
|
||||||
&& body.command
|
&& body.command
|
||||||
&& typeof body.command == "string"
|
&& typeof body.command == "string"
|
||||||
&& body.command[0] == "/")
|
&& body.command[0] == "/")
|
||||||
{
|
{
|
||||||
let instanceResponses = [];
|
let instanceResponses = [];
|
||||||
for (let instanceID in wsSlaves) {
|
for (let instanceID in wsSlaves) {
|
||||||
// skip loop if the property is from prototype
|
// skip loop if the property is from prototype
|
||||||
if (!wsSlaves.hasOwnProperty(instanceID)) continue;
|
if (!wsSlaves.hasOwnProperty(instanceID)) continue;
|
||||||
wsSlaves[instanceID].runCommand(body.command);
|
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"});
|
|
||||||
}
|
}
|
||||||
});
|
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"});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
/**
|
/**
|
||||||
GET endpoint. Returns factorio's base locale as a JSON object.
|
GET endpoint. Returns factorio's base locale as a JSON object.
|
||||||
|
|||||||
Reference in new issue
Block a user