mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 23:53:13 +09:00
#replace stray whitespaces with tabs again
This commit is contained in:
1 parent
6caef9bda9
commit
a204163a6c
1 file changed
+103
-103
@@ -65,7 +65,7 @@ function randomStringAsBase64Url(size) {
|
|||||||
}
|
}
|
||||||
if (!fs.existsSync("secret-api-token.txt")) {
|
if (!fs.existsSync("secret-api-token.txt")) {
|
||||||
config.masterAuthSecret = randomStringAsBase64Url(256);
|
config.masterAuthSecret = randomStringAsBase64Url(256);
|
||||||
fs.writeFileSync("config.json",JSON.stringify(config, null, 4));
|
fs.writeFileSync("config.json",JSON.stringify(config, null, 4));
|
||||||
fs.writeFileSync("secret-api-token.txt", jwt.sign({ id: "api" }, config.masterAuthSecret, {
|
fs.writeFileSync("secret-api-token.txt", jwt.sign({ id: "api" }, config.masterAuthSecret, {
|
||||||
expiresIn: 86400*365 // expires in 1 year
|
expiresIn: 86400*365 // expires in 1 year
|
||||||
}));
|
}));
|
||||||
@@ -179,7 +179,7 @@ const prometheusMasterInventoryGauge = new Prometheus.Gauge({
|
|||||||
labelNames: ["itemName"],
|
labelNames: ["itemName"],
|
||||||
});
|
});
|
||||||
setInterval(()=>{
|
setInterval(()=>{
|
||||||
fs.writeFileSync("database/items.json", JSON.stringify(db.items));
|
fs.writeFileSync("database/items.json", JSON.stringify(db.items));
|
||||||
},config.autosaveInterval || 60000);
|
},config.autosaveInterval || 60000);
|
||||||
/**
|
/**
|
||||||
GET Prometheus metrics endpoint. Returns performance and usage metrics in a prometheus readable format.
|
GET Prometheus metrics endpoint. Returns performance and usage metrics in a prometheus readable format.
|
||||||
@@ -352,8 +352,8 @@ app.post("/api/editSlaveMeta", authenticate.middleware, function(req,res) {
|
|||||||
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) {
|
if(metaPortion.length > 50) {
|
||||||
metaPortion = metaPortion.substring(0,20) + "...";
|
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 {
|
||||||
@@ -379,7 +379,7 @@ app.post("/api/getSlaveMeta", function (req, res) {
|
|||||||
}
|
}
|
||||||
res.send(JSON.stringify(slaves[req.body.instanceID].meta))
|
res.send(JSON.stringify(slaves[req.body.instanceID].meta))
|
||||||
} else {
|
} else {
|
||||||
res.status(404);
|
res.status(404);
|
||||||
res.send('{"status": 404, "info": "Slave not registered"}')
|
res.send('{"status": 404, "info": "Slave not registered"}')
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -433,7 +433,7 @@ app.post("/api/uploadMod", authenticate.middleware, function(req,res) {
|
|||||||
} else {
|
} else {
|
||||||
res.send('No files were uploaded.');
|
res.send('No files were uploaded.');
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
/**
|
/**
|
||||||
Prepare the slaveCache for both /api/slaves calls
|
Prepare the slaveCache for both /api/slaves calls
|
||||||
@@ -472,15 +472,15 @@ POST endpoint for getting the rconPasswords of all our slaves
|
|||||||
@alias /api/rconPasswords
|
@alias /api/rconPasswords
|
||||||
*/
|
*/
|
||||||
app.post("/api/rconPasswords", authenticate.middleware, function(req, res) {
|
app.post("/api/rconPasswords", authenticate.middleware, function(req, res) {
|
||||||
endpointHitCounter.labels(req.route.path).inc();
|
endpointHitCounter.labels(req.route.path).inc();
|
||||||
res.header("Access-Control-Allow-Origin", "*");
|
res.header("Access-Control-Allow-Origin", "*");
|
||||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||||
let slaveCache = getSlaves();
|
let slaveCache = getSlaves();
|
||||||
let validKeys = ['rconPort', 'rconPassword', 'publicIP'];
|
let validKeys = ['rconPort', 'rconPassword', 'publicIP'];
|
||||||
for(key in slaveCache.cache) {
|
for(key in slaveCache.cache) {
|
||||||
slaveCache.cache[key] = {'publicIP': slaveCache.cache[key].publicIP, 'rconPort': slaveCache.cache[key].rconPort, 'rconPassword': slaveCache.cache[key].rconPassword};
|
slaveCache.cache[key] = {'publicIP': slaveCache.cache[key].publicIP, 'rconPort': slaveCache.cache[key].rconPort, 'rconPassword': slaveCache.cache[key].rconPassword};
|
||||||
}
|
}
|
||||||
res.send(slaveCache.cache);
|
res.send(slaveCache.cache);
|
||||||
});
|
});
|
||||||
/*
|
/*
|
||||||
var recievedItemStatistics = new averagedTimeSeries({
|
var recievedItemStatistics = new averagedTimeSeries({
|
||||||
@@ -694,19 +694,19 @@ app.get("/api/inventoryAsObject", function(req, res) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// wrap a request in an promise
|
// wrap a request in an promise
|
||||||
async function downloadPage(url) {
|
async function downloadPage(url) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
request(url, (error, response, body) => {
|
request(url, (error, response, body) => {
|
||||||
resolve(body);
|
resolve(body);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
app.get("/api/modmeta", async function(req, res) {
|
app.get("/api/modmeta", async function(req, res) {
|
||||||
endpointHitCounter.labels(req.route.path).inc();
|
endpointHitCounter.labels(req.route.path).inc();
|
||||||
res.header("Access-Control-Allow-Origin", "*");
|
res.header("Access-Control-Allow-Origin", "*");
|
||||||
res.setHeader('Content-Type', 'application/json');
|
res.setHeader('Content-Type', 'application/json');
|
||||||
let modData = await downloadPage("https://mods.factorio.com/api/mods/" + req.query.modname);
|
let modData = await downloadPage("https://mods.factorio.com/api/mods/" + req.query.modname);
|
||||||
res.send(modData);
|
res.send(modData);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -723,36 +723,36 @@ Requires x-access-token header to be set. Find you api token in secret-api-token
|
|||||||
*/
|
*/
|
||||||
app.post("/api/runCommand", authenticate.middleware, function(req,res) {
|
app.post("/api/runCommand", authenticate.middleware, function(req,res) {
|
||||||
endpointHitCounter.labels(req.route.path).inc();
|
endpointHitCounter.labels(req.route.path).inc();
|
||||||
// validate request.body
|
// validate request.body
|
||||||
let body = req.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"});
|
res.status(200).send({auth: true, message: "success", response: "Cluster wide messaging initiated"});
|
||||||
} else if (
|
} else if (
|
||||||
body.instanceID
|
body.instanceID
|
||||||
&& wsSlaves[body.instanceID]
|
&& wsSlaves[body.instanceID]
|
||||||
&& body.command
|
&& body.command
|
||||||
&& typeof body.command == "string"
|
&& typeof body.command == "string"
|
||||||
&& body.command[0] == "/")
|
&& body.command[0] == "/")
|
||||||
{
|
{
|
||||||
// execute command
|
// execute command
|
||||||
wsSlaves[body.instanceID].runCommand(body.command, data => {
|
wsSlaves[body.instanceID].runCommand(body.command, data => {
|
||||||
res.status(200).send({auth: true, message: "success", data});
|
res.status(200).send({auth: true, message: "success", data});
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
res.status(400).send({auth: true, message: "Error: invalid request.body"});
|
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.
|
||||||
@@ -926,61 +926,61 @@ class wsSlave {
|
|||||||
}
|
}
|
||||||
|
|
||||||
io.on('connection', function (socket) {
|
io.on('connection', function (socket) {
|
||||||
// cleanup dead sockets from disconnected people
|
// cleanup dead sockets from disconnected people
|
||||||
let terminatedConnections = 0;
|
let terminatedConnections = 0;
|
||||||
let currentConnections = Object.keys(mapRequesters).length + Object.keys(slaveMappers).length + Object.keys(wsSlaves).length;
|
let currentConnections = Object.keys(mapRequesters).length + Object.keys(slaveMappers).length + Object.keys(wsSlaves).length;
|
||||||
[mapRequesters, slaveMappers, wsSlaves].forEach(list => {
|
[mapRequesters, slaveMappers, wsSlaves].forEach(list => {
|
||||||
Object.keys(list).forEach(connectionID => {
|
Object.keys(list).forEach(connectionID => {
|
||||||
let connection = list[connectionID];
|
let connection = list[connectionID];
|
||||||
if(connection.lastBeat < (Date.now() - 30000)){
|
if(connection.lastBeat < (Date.now() - 30000)){
|
||||||
terminatedConnections++;
|
terminatedConnections++;
|
||||||
delete list[connectionID];
|
delete list[connectionID];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
if(terminatedConnections > 0) console.log("SOCKET | There are currently "+currentConnections+" websocket connections, deleting "+terminatedConnections+" on timeout");
|
if(terminatedConnections > 0) console.log("SOCKET | There are currently "+currentConnections+" websocket connections, deleting "+terminatedConnections+" on timeout");
|
||||||
|
|
||||||
// tell our friend that we are listening
|
// tell our friend that we are listening
|
||||||
setTimeout(()=>socket.emit('hello', { hello: 'world' }), 5000);
|
setTimeout(()=>socket.emit('hello', { hello: 'world' }), 5000);
|
||||||
|
|
||||||
/* initial processing for remoteMap */
|
/* initial processing for remoteMap */
|
||||||
socket.on('registerSlaveMapper', function (data) {
|
socket.on('registerSlaveMapper', function (data) {
|
||||||
prometheusWsUsageCounter.labels('registerSlaveMapper', "other").inc();
|
prometheusWsUsageCounter.labels('registerSlaveMapper', "other").inc();
|
||||||
slaveMappers[data.instanceID] = new slaveMapper(data.instanceID, socket);
|
slaveMappers[data.instanceID] = new slaveMapper(data.instanceID, socket);
|
||||||
console.log("remoteMap | SOCKET registered map provider for "+data.instanceID);
|
console.log("remoteMap | SOCKET registered map provider for "+data.instanceID);
|
||||||
});
|
});
|
||||||
socket.on('registerMapRequester', function(data){
|
socket.on('registerMapRequester', function(data){
|
||||||
// data {instanceID:""}
|
// data {instanceID:""}
|
||||||
prometheusWsUsageCounter.labels('registerMapRequester', "other").inc();
|
prometheusWsUsageCounter.labels('registerMapRequester', "other").inc();
|
||||||
let requesterID = Math.random().toString();
|
let requesterID = Math.random().toString();
|
||||||
mapRequesters[requesterID] = new mapRequester(requesterID, socket, data.instanceID);
|
mapRequesters[requesterID] = new mapRequester(requesterID, socket, data.instanceID);
|
||||||
socket.emit("mapRequesterReady", true);
|
socket.emit("mapRequesterReady", true);
|
||||||
console.log("remoteMap | SOCKET registered map requester for "+data.instanceID);
|
console.log("remoteMap | SOCKET registered map requester for "+data.instanceID);
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Websockets for send and recieve combinators */
|
/* Websockets for send and recieve combinators */
|
||||||
socket.on("registerSlave", function(data) {
|
socket.on("registerSlave", function(data) {
|
||||||
prometheusWsUsageCounter.labels('registerSlave', "other").inc();
|
prometheusWsUsageCounter.labels('registerSlave', "other").inc();
|
||||||
if(data && data.instanceID){
|
if(data && data.instanceID){
|
||||||
wsSlaves[data.instanceID] = new wsSlave(data.instanceID, socket);
|
wsSlaves[data.instanceID] = new wsSlave(data.instanceID, socket);
|
||||||
console.log("SOCKET | Created new wsSlave: "+ data.instanceID);
|
console.log("SOCKET | Created new wsSlave: "+ data.instanceID);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
socket.on("registerChatReciever", function(data){
|
socket.on("registerChatReciever", function(data){
|
||||||
prometheusWsUsageCounter.labels("registerChatReciever", "other").inc();
|
prometheusWsUsageCounter.labels("registerChatReciever", "other").inc();
|
||||||
if(!global.wsChatRecievers) global.wsChatRecievers = [];
|
if(!global.wsChatRecievers) global.wsChatRecievers = [];
|
||||||
global.wsChatRecievers.push(socket);
|
global.wsChatRecievers.push(socket);
|
||||||
});
|
});
|
||||||
socket.on("registerAlertReciever", function(data){
|
socket.on("registerAlertReciever", function(data){
|
||||||
prometheusWsUsageCounter.labels("registerAlertReciever", "other").inc();
|
prometheusWsUsageCounter.labels("registerAlertReciever", "other").inc();
|
||||||
if(!global.wsAlertRecievers) global.wsAlertRecievers = [];
|
if(!global.wsAlertRecievers) global.wsAlertRecievers = [];
|
||||||
global.wsAlertRecievers.push(socket);
|
global.wsAlertRecievers.push(socket);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// handle plugins on the master
|
// handle plugins on the master
|
||||||
async function getPlugins(){
|
async function getPlugins(){
|
||||||
let startPluginLoad = Date.now();
|
let startPluginLoad = Date.now();
|
||||||
|
|
||||||
const pluginManager = require("./lib/manager/pluginManager.js")(config);
|
const pluginManager = require("./lib/manager/pluginManager.js")(config);
|
||||||
let plugins = [];
|
let plugins = [];
|
||||||
@@ -1012,7 +1012,7 @@ async function getPlugins(){
|
|||||||
let plugin = plugins[i];
|
let plugin = plugins[i];
|
||||||
if(plugin.main.onLoadFinish && typeof plugin.main.onLoadFinish == "function") await plugin.main.onLoadFinish({plugins});
|
if(plugin.main.onLoadFinish && typeof plugin.main.onLoadFinish == "function") await plugin.main.onLoadFinish({plugins});
|
||||||
}
|
}
|
||||||
console.log("All plugins loaded in "+(Date.now() - startPluginLoad)+"ms");
|
console.log("All plugins loaded in "+(Date.now() - startPluginLoad)+"ms");
|
||||||
return plugins;
|
return plugins;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user