/api/slaves POST (optimized) (#176)

* Adds a /api/slaves POST call that requires authentication. It also returns the rconPasswords, unlike the GET call.
This commit is contained in:
Master-Guy authored and Arvids Godjuks committed 2019-07-19 15:37:49 +03:00
1 parent ee6ab6ab72
commit 49611a2062
1 file changed
+41 -14
+41 -14
View File
@@ -440,37 +440,64 @@ app.post("/api/uploadMod", authenticate.middleware, function(req,res) {
}
});
/**
Prepare the slaveCache for both /api/slaves calls
*/
let slaveCache = {
timestamp: Date.now(),
};
function getSlaves() {
if(!slaveCache.cache || Date.now() - slaveCache.timestamp > 5000) {
let copyOfSlaves = JSON.parse(JSON.stringify(slaves));
slaveCache.cache = copyOfSlaves;
slaveCache.timestamp = Date.now();
}
return slaveCache;
}
/**
GET endpoint for getting information about all our slaves
@memberof clusterioMaster
@instance
@alias /api/slaves
*/
let slaveCache = {
timestamp: Date.now(),
};
app.get("/api/slaves", function(req, res) {
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");
if(!slaveCache.cache || Date.now() - slaveCache.timestamp > 5000){
let copyOfSlaves = JSON.parse(JSON.stringify(slaves));
// filter out the rcon password because thats kindof not a safe thing to share
for(key in copyOfSlaves){
copyOfSlaves[key].rconPassword = "hidden";
}
slaveCache.cache = copyOfSlaves;
slaveCache.timestamp = Date.now();
let slaveCache = getSlaves();
for(key in slaveCache) {
slaveCache[key].rconPassword = "hidden";
}
res.send(slaveCache.cache);
});
var recievedItemStatisticsBySlaveID = {};
var sentItemStatisticsBySlaveID = {};
/**
POST endpoint for getting information about all our slaves, requires auth, responds including RCON passwords
@memberof clusterioMaster
@instance
@alias /api/slaves
*/
app.post("/api/slaves", function(req, res) {
let token = req.headers['x-access-token'];
if(!token) {
return res.status(401).send({ auth: false, message: 'No token provided.' });
}
jwt.verify(token, config.masterAuthSecret, function(err, decoded) {
if(err) {
return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
}
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({
maxEntries: config.itemStats.maxEntries,
entriesPerSecond: config.itemStats.entriesPerSecond,
}, console.log);*/
var recievedItemStatisticsBySlaveID = {};
var sentItemStatisticsBySlaveID = {};
//
/**
POST endpoint for storing items in master's inventory.