From 41a6e880063aae4845eb2ed31a2c9bc04293529c Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Mon, 3 Apr 2017 13:31:09 +0200 Subject: [PATCH 01/17] Show how many are online --- static/slaveDisplay.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/slaveDisplay.js b/static/slaveDisplay.js index b473c8e9..0045fad7 100644 --- a/static/slaveDisplay.js +++ b/static/slaveDisplay.js @@ -19,7 +19,7 @@ setTimeout(function() { } HTML += "
"; HTML += '
'; - HTML += "

" + slaveData[key].instanceName + "

ID: " + slaveData[key].unique + "

Last seen: "+seenDate+"

IP: "+slaveData[key].publicIP +":"+ slaveData[key].serverPort+"

" + HTML += "

" + slaveData[key].instanceName + "

ID: " + slaveData[key].unique + "

Last seen: "+seenDate+"

Online players: "+slaveData[key].playerCount+"

IP: "+slaveData[key].publicIP +":"+ slaveData[key].serverPort+"

" HTML += "

Host: "+slaveData[key].mac+"

"; HTML += ""; HTML += ""; From 432c4e462df8565dfa1aafda758fad1033a5dbf2 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 6 Apr 2017 20:56:42 +0200 Subject: [PATCH 02/17] Setting to disable automatic storage screen updates --- static/main.js | 1 + static/storage.js | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/static/main.js b/static/main.js index 68f5f515..7524d242 100644 --- a/static/main.js +++ b/static/main.js @@ -2,6 +2,7 @@ (function (){ ensureSetting("Display offline slaves", "boolean", false); ensureSetting("Enable production graphs", "boolean", true); + ensureSetting("Periodically update storage screen", "boolean", true); }()) function ensureSetting(setting, type, defaultSetting) { diff --git a/static/storage.js b/static/storage.js index 899125fc..83e5930f 100644 --- a/static/storage.js +++ b/static/storage.js @@ -1,7 +1,7 @@ // function to draw data we recieve from ajax requests function drawcontents(data) { - var data = sortByKey(data, "count"); - result = ""; + data = sortByKey(data, "count"); + let result = "
"; for(i = 0;i < data.length; i++) { var img = ""; if(imagedata[data[i].name]) { @@ -18,17 +18,22 @@ function drawcontents(data) { } // get cluster inventory from master -setInterval(function() { +function updateInventory() { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { - data = JSON.parse(xmlhttp.responseText); + let data = JSON.parse(xmlhttp.responseText); drawcontents(data); } } xmlhttp.open("GET", "api/inventory", true); xmlhttp.send(); -}, 500) +} +if(JSON.parse(localStorage.settings)["Periodically update storage screen"]) { + setInterval(updateInventory, 500); +} else { + updateInventory(); +} // function to sort arrays of objects after a keys value function sortByKey(array, key) { From af345fb2ae5e4563ca7c59b470046802cf4b9c6b Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 7 Apr 2017 08:40:46 +0200 Subject: [PATCH 03/17] More docker --- Dockerfile | 10 ++++++++-- client.js | 4 ++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4f68ff19..8ca2cef0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,17 @@ FROM node:latest RUN apt-get update && apt install git -y && git clone https://github.com/Danielv123/factorioClusterio.git && cd factorioClusterio && npm install && curl -o factorio.tar.gz -L https://www.factorio.com/get-download/latest/headless/linux64 && tar -xvzf factorio.tar.gz +WORKDIR factorioClusterio + + +RUN mkdir instances sharedMods sharedPlugins +RUN node client.js download + LABEL maintainer "danielv@live.no" EXPOSE 8080 34167 - VOLUME /factorioClusterio/instances +VOLUME /factorioClusterio/sharedMods +VOLUME /factorioClusterio/sharedPlugins -WORKDIR factorioClusterio CMD node $MODE\.js start $INSTANCE \ No newline at end of file diff --git a/client.js b/client.js index 95e1f11e..3029864e 100644 --- a/client.js +++ b/client.js @@ -130,8 +130,8 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ fs.copySync('sharedMods', instancedirectory + "/mods") let instconf = { - "factorioPort": Math.floor(Math.random() * 65535), - "clientPort": Math.floor(Math.random() * 65535), + "factorioPort": process.env.FACTORIOPORT | Math.floor(Math.random() * 65535), + "clientPort": process.env.RCONPORT | Math.floor(Math.random() * 65535), "clientPassword": Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 8), } console.log("Clusterio | Moving shared mods...") From ae72814527ea7535e2f31fe18c4bbb102e9bf35d Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 7 Apr 2017 10:17:46 +0200 Subject: [PATCH 04/17] More docker trying to fix environent variables --- Dockerfile | 2 +- client.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 8ca2cef0..e3c31e92 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,4 +14,4 @@ VOLUME /factorioClusterio/instances VOLUME /factorioClusterio/sharedMods VOLUME /factorioClusterio/sharedPlugins -CMD node $MODE\.js start $INSTANCE \ No newline at end of file +CMD node RCONPORT="$RCONPORT" FACTORIOPORT="$FACTORIOPORT" MODE="$MODE" $MODE\.js start $INSTANCE \ No newline at end of file diff --git a/client.js b/client.js index 3029864e..6409aac4 100644 --- a/client.js +++ b/client.js @@ -175,6 +175,12 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ if(instancedirectory != "./instances/undefined"){ var instanceconfig = require(instancedirectory + '/config'); instanceconfig.unique = instanceconfig.clientPassword.hashCode(); + if(process.env.FACTORIOPORT){ + instanceconfig.factorioPort = process.env.FACTORIOPORT; + } + if(process.env.RCONPORT){ + instanceconfig.rconPort = process.env.RCONPORT; + } } else { process.exit(1) } From 8613c1deef96d3ce54dea940e384631b625c61bf Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 7 Apr 2017 10:35:24 +0200 Subject: [PATCH 05/17] Fixed dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index e3c31e92..d422550a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,4 +14,4 @@ VOLUME /factorioClusterio/instances VOLUME /factorioClusterio/sharedMods VOLUME /factorioClusterio/sharedPlugins -CMD node RCONPORT="$RCONPORT" FACTORIOPORT="$FACTORIOPORT" MODE="$MODE" $MODE\.js start $INSTANCE \ No newline at end of file +CMD RCONPORT="$RCONPORT" FACTORIOPORT="$FACTORIOPORT" MODE="$MODE" node $MODE\.js start $INSTANCE \ No newline at end of file From c9d56b8940bdf69c5a94c89f5fb2a0acefc815cd Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 8 Apr 2017 10:32:21 +0200 Subject: [PATCH 06/17] Debug --- client.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client.js b/client.js index 6409aac4..7e43a2ce 100644 --- a/client.js +++ b/client.js @@ -191,7 +191,9 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ fs.unlinkSync(instancedirectory + "/saves/" + savefiles[i]); } } - + setInterval(function(){ + console.log(process.env); + },5000); console.log("Deleting logs"); // clean old log file to avoid crash // file exists, delete so we don't get in trouble From f208540856ead2b3b4a868f4e48a7154d51ffcec Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 8 Apr 2017 16:13:59 +0200 Subject: [PATCH 07/17] Clone dev branch instead of master This caused me a lot of headaches. Sigh. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d422550a..cbaed124 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ FROM node:latest -RUN apt-get update && apt install git -y && git clone https://github.com/Danielv123/factorioClusterio.git && cd factorioClusterio && npm install && curl -o factorio.tar.gz -L https://www.factorio.com/get-download/latest/headless/linux64 && tar -xvzf factorio.tar.gz +RUN apt-get update && apt install git -y && git clone -b dev https://github.com/Danielv123/factorioClusterio.git && cd factorioClusterio && npm install && curl -o factorio.tar.gz -L https://www.factorio.com/get-download/latest/headless/linux64 && tar -xvzf factorio.tar.gz WORKDIR factorioClusterio From 1719ff9b075bfaf5b99635ca9cd2b453853653d4 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 8 Apr 2017 16:37:38 +0200 Subject: [PATCH 08/17] . --- client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client.js b/client.js index 7e43a2ce..da7dc4a2 100644 --- a/client.js +++ b/client.js @@ -223,10 +223,10 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ './' + config.factorioDirectory + '/bin/x64/factorio', [ '-c', instancedirectory + '/config.ini', '--start-server', latestSave.file, - '--rcon-port', instanceconfig.clientPort, + '--rcon-port', Number(process.env.RCONPORT) | instanceconfig.clientPort, '--rcon-password', instanceconfig.clientPassword, '--server-settings', instancedirectory + '/server-settings.json', - '--port', instanceconfig.factorioPort + '--port', Number(process.env.FACTORIOPORT) | instanceconfig.factorioPort ], { 'stdio': ['pipe', 'pipe', 'pipe'] } @@ -249,7 +249,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // IP, port, password client = new Rcon({ host: 'localhost', - port: instanceconfig.clientPort, + port: Number(process.env.RCONPORT) | instanceconfig.clientPort, password: instanceconfig.clientPassword, timeout: 0 }); From 8e0abf5e2de1c5de24918fe0170a9d9a8cd23cb5 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 8 Apr 2017 16:52:33 +0200 Subject: [PATCH 09/17] or is || not | --- client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/client.js b/client.js index da7dc4a2..b1af1b06 100644 --- a/client.js +++ b/client.js @@ -223,10 +223,10 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ './' + config.factorioDirectory + '/bin/x64/factorio', [ '-c', instancedirectory + '/config.ini', '--start-server', latestSave.file, - '--rcon-port', Number(process.env.RCONPORT) | instanceconfig.clientPort, + '--rcon-port', Number(process.env.RCONPORT) || instanceconfig.clientPort, '--rcon-password', instanceconfig.clientPassword, '--server-settings', instancedirectory + '/server-settings.json', - '--port', Number(process.env.FACTORIOPORT) | instanceconfig.factorioPort + '--port', Number(process.env.FACTORIOPORT) || instanceconfig.factorioPort ], { 'stdio': ['pipe', 'pipe', 'pipe'] } @@ -249,7 +249,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // IP, port, password client = new Rcon({ host: 'localhost', - port: Number(process.env.RCONPORT) | instanceconfig.clientPort, + port: Number(process.env.RCONPORT) || instanceconfig.clientPort, password: instanceconfig.clientPassword, timeout: 0 }); From 9971b9f5b383e055f15cbc9acf113b205611085a Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 13 Apr 2017 13:45:49 +0200 Subject: [PATCH 10/17] Add more logging to isolate case where slave stops reporting to master --- client.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/client.js b/client.js index b1af1b06..d5cc0efb 100644 --- a/client.js +++ b/client.js @@ -278,8 +278,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // getID(); }).on('disconnected', function () { console.log('Clusterio | Disconnected!'); - // now reconnect - client.connect(); + process.exit(0); // exit because RCON disconnecting is undefined behaviour and we rather just wanna restart now }); // set some globals @@ -391,9 +390,12 @@ function instanceManagement() { require('getmac').getMac(function (err, mac) { if (err) throw err payload.mac = mac - // console.log(payload) + console.log("Registered our precense with master at " + payload.time); needle.post(config.masterIP + ":" + config.masterPort + '/api/getID', payload, function (err, response, body) { - if (response && response.body) { + if (err){ + console.error("We got problems, something went wrong when contacting master"); + console.error(err); + } else if (response && response.body) { // In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care. console.log(response.body) } From 4db55a8857fb6689de6cc429ec9c96076a74d032 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 21 Apr 2017 09:20:22 +0200 Subject: [PATCH 11/17] Add reconnect when factorio complains about payload size Sometimes the rcon lib messes up and delivers a 4x package that makes factorio stop responding to rcon and throw an error. We catch that error and attempt to reconnect. Couldn't parse RCON data: Maximum payload size exceeded --- client.js | 13 ++++++++++++- package.json | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/client.js b/client.js index d5cc0efb..1ba280bb 100644 --- a/client.js +++ b/client.js @@ -8,6 +8,7 @@ var request = require("request") var ncp = require('ncp').ncp; var Rcon = require('simple-rcon'); var hashFiles = require('hash-files'); +var _ = require('underscore'): var libomega = require("./libomega.js"); // require config.json @@ -236,6 +237,15 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ console.log(`child process exited with code ${code}`); process.exit(); }); + serverprocess.stdout.on("data", (data) => { + // log("Stdout: " + data); + if(data.toString('utf8').includes("Couldn't parse RCON data: Maximum payload size exceeded")){ + console.error("ERROR: RCON CONNECTION BROKE DUE TO TOO LARGE PACKET!"); + console.error("Attempting reconnect..."); + client.close(); + client.connect(); + } + }); /* serverprocess.stdout.on('data', (chunk) => { console.log('OUT: ' + chunk); @@ -286,8 +296,9 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ lastSignalCheck = Date.now(); }) } - +_.once(instanceManagement); function instanceManagement() { + console.log("Started instanceManagement();") // load plugins and execute onLoad event let pluginDirectories = getDirectories("./sharedPlugins/"); let plugins = []; diff --git a/package.json b/package.json index 49ab47db..46f5a301 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "portfinder": "^1.0.9", "request": "^2.79.0", "simple-rcon": "^0.3.1", - "sync-request": "^4.0.1" + "sync-request": "^4.0.1", + "underscore": "^1.8.3" } } From 1c91727c8a0ce0a94e490d4ced4f7a51ade0ffff Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 21 Apr 2017 10:02:39 +0200 Subject: [PATCH 12/17] docker --- Dockerfile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index cbaed124..949a4018 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,12 @@ FROM node:latest -RUN apt-get update && apt install git -y && git clone -b dev https://github.com/Danielv123/factorioClusterio.git && cd factorioClusterio && npm install && curl -o factorio.tar.gz -L https://www.factorio.com/get-download/latest/headless/linux64 && tar -xvzf factorio.tar.gz +RUN apt-get update && apt install git -y + +RUN mkdir factorioClusterio +RUN git clone -b dev https://github.com/Danielv123/factorioClusterio.git && cd factorioClusterio && npm install && curl -o factorio.tar.gz -L https://www.factorio.com/get-download/latest/headless/linux64 && tar -xvzf factorio.tar.gz WORKDIR factorioClusterio - - RUN mkdir instances sharedMods sharedPlugins + RUN node client.js download LABEL maintainer "danielv@live.no" From 538c4f575ba9a2ab9f1624bfb2d46e1a09aef503 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 21 Apr 2017 10:05:14 +0200 Subject: [PATCH 13/17] ; not : --- client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client.js b/client.js index 383e2368..23669b5d 100644 --- a/client.js +++ b/client.js @@ -4,11 +4,11 @@ var needle = require("needle"); var child_process = require('child_process'); var path = require('path'); var syncRequest = require('sync-request'); -var request = require("request") +var request = require("request"); var ncp = require('ncp').ncp; var Rcon = require('simple-rcon'); var hashFiles = require('hash-files'); -var _ = require('underscore'): +var _ = require('underscore'); var libomega = require("./libomega.js"); // require config.json From 78334fc7e0809b0c4428ac0e836dc63010a9c606 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 21 Apr 2017 10:08:02 +0200 Subject: [PATCH 14/17] Less logs --- client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client.js b/client.js index 23669b5d..8355b625 100644 --- a/client.js +++ b/client.js @@ -192,9 +192,9 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ fs.unlinkSync(instancedirectory + "/saves/" + savefiles[i]); } } - setInterval(function(){ + /*setInterval(function(){ console.log(process.env); - },5000); + },5000);*/ console.log("Deleting logs"); // clean old log file to avoid crash // file exists, delete so we don't get in trouble From 31e4bab159321f5187937d4a450e533d33a9beaa Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Fri, 21 Apr 2017 10:20:32 +0200 Subject: [PATCH 15/17] Less logs --- client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client.js b/client.js index 8355b625..cb6bb10e 100644 --- a/client.js +++ b/client.js @@ -402,9 +402,9 @@ function instanceManagement() { require('getmac').getMac(function (err, mac) { if (err) throw err payload.mac = mac - console.log("Registered our precense with master at " + payload.time); + console.log("Registered our precense with master "+config.masterPort+" at " + payload.time); needle.post(config.masterIP + ":" + config.masterPort + '/api/getID', payload, function (err, response, body) { - if (err){ + if (err && err.code != "ECONNRESET"){ console.error("We got problems, something went wrong when contacting master"); console.error(err); } else if (response && response.body) { From 5d51e9e80acbd58096adedadd65dbd5f1405c3e9 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 22 Apr 2017 21:47:11 +0200 Subject: [PATCH 16/17] stdIn mode for linuxes to solve problem in rcon lib resolves #42 --- client.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/client.js b/client.js index cb6bb10e..de9f288d 100644 --- a/client.js +++ b/client.js @@ -63,7 +63,13 @@ function messageInterface(command, callback) { command = command.toString('utf8'); } - if(typeof command == "string" && client && client.exec && typeof client.exec == "function") { + if(process.platform == "linux" && typeof command == "string" && serverprocess) { + /* + to send to stdin, use: + serverprocess.stdin.write("/c command;\n") + */ + serverprocess.stdin.write(command+"\n"); + } else if(typeof command == "string" && client && client.exec && typeof client.exec == "function") { try { client.exec(command, callback); } catch (err) { @@ -220,7 +226,8 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // Spawn factorio server //var serverprocess = child_process.exec(commandline) getNewestFile(instancedirectory + "/saves/", fs.readdirSync(instancedirectory + "/saves/"),function(latestSave) { - var serverprocess = child_process.spawn( + // implicit global + serverprocess = child_process.spawn( './' + config.factorioDirectory + '/bin/x64/factorio', [ '-c', instancedirectory + '/config.ini', '--start-server', latestSave.file, @@ -402,7 +409,7 @@ function instanceManagement() { require('getmac').getMac(function (err, mac) { if (err) throw err payload.mac = mac - console.log("Registered our precense with master "+config.masterPort+" at " + payload.time); + console.log("Registered our precense with master "+config.masterIP+" at " + payload.time); needle.post(config.masterIP + ":" + config.masterPort + '/api/getID', payload, function (err, response, body) { if (err && err.code != "ECONNRESET"){ console.error("We got problems, something went wrong when contacting master"); From 4a2e62386f41db81427108ea28a35d71ee7b1ded Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sun, 23 Apr 2017 10:28:48 +0200 Subject: [PATCH 17/17] Fixed missing callback :S --- client.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/client.js b/client.js index de9f288d..1ebe80fd 100644 --- a/client.js +++ b/client.js @@ -69,6 +69,9 @@ function messageInterface(command, callback) { serverprocess.stdin.write("/c command;\n") */ serverprocess.stdin.write(command+"\n"); + if(typeof callback == "function"){ + callback(); + } } else if(typeof command == "string" && client && client.exec && typeof client.exec == "function") { try { client.exec(command, callback);