From 2c4485292d9268ac98197db40016653d289042d1 Mon Sep 17 00:00:00 2001 From: EntenKoeniq <81123713+EntenKoeniq@users.noreply.github.com> Date: Mon, 12 Jul 2021 00:38:34 +0200 Subject: [PATCH] New private MasterServer --- MasterServer/blocked.txt | 0 MasterServer/index.mjs | 86 --------------------------------------- MasterServer/package.json | 11 ----- Server/Server.cs | 64 +++++++++++++++++------------ 4 files changed, 37 insertions(+), 124 deletions(-) delete mode 100644 MasterServer/blocked.txt delete mode 100644 MasterServer/index.mjs delete mode 100644 MasterServer/package.json diff --git a/MasterServer/blocked.txt b/MasterServer/blocked.txt deleted file mode 100644 index e69de29..0000000 diff --git a/MasterServer/index.mjs b/MasterServer/index.mjs deleted file mode 100644 index cdefd41..0000000 --- a/MasterServer/index.mjs +++ /dev/null @@ -1,86 +0,0 @@ -import { createServer } from 'net'; -import { readFileSync } from 'fs'; - -var serverList = []; -const blockedIps = readFileSync('blocked.txt', 'utf-8').split('\n'); - -const server = createServer(); - -server.on('connection', (socket) => -{ - if (blockedIps.includes(socket.remoteAddress)) - { - console.log(`IP '${socket.remoteAddress}' blocked`); - socket.destroy(); - return; - } - - var lastData = 0; - - const remoteAddress = socket.remoteAddress + ":" + socket.remotePort; - - socket.on('data', async (data) => - { - // NOT SPAM! - if (lastData !== 0 && (Date.now() - lastData) < 14500) - { - console.log("[WARNING] Spam from %s", remoteAddress); - socket.destroy(); - return; - } - - lastData = Date.now(); - - var incomingMessage; - try - { - incomingMessage = await JSON.parse(data.toString()); - } - catch - { - socket.destroy(); - return; - } - - if (incomingMessage.method) - { - if (incomingMessage.method === 'POST' && incomingMessage.data) - { - // Check if the server is already in the serverList - const alreadyExist = serverList.some((val) => - { - const found = val.remoteAddress === remoteAddress; - - if (found) - { - // Replace old data with new data - val.data = { ...val.data, ...incomingMessage.data }; - } - - return found; - }); - - // Server doesn't exist in serverList so add the server - if (!alreadyExist) - { - serverList.push({ remoteAddress: remoteAddress, data: incomingMessage.data }); - } - return; - } - else if (incomingMessage.method === 'GET') - { - socket.write(JSON.stringify(serverList)); - return; - } - } - - // method or data does not exist or method is not POST or GET - socket.destroy(); - }); - - socket.on('close', () => serverList = serverList.filter(val => val.remoteAddress !== remoteAddress)); - - socket.on('error', (e) => { /*console.error(e)*/ }); -}); - -server.listen(11000, () => console.log("MasterServer started!")); \ No newline at end of file diff --git a/MasterServer/package.json b/MasterServer/package.json deleted file mode 100644 index 88c8376..0000000 --- a/MasterServer/package.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "name": "masterserver", - "version": "1.0.0", - "description": "", - "main": "index.mjs", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "EntenKoeniq", - "license": "MIT" -} diff --git a/Server/Server.cs b/Server/Server.cs index 7691f04..4bef78d 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -23,33 +23,43 @@ namespace CoopServer private async void Listen() { - //try - //{ - // HttpClient client = new(); - // - // while (true) - // { - // // Encode the data string into a byte array - // string msg = - // "{ " + - // "\"port\": \"" + Server.MainSettings.ServerPort + "\", " + - // "\"name\": \"" + Server.MainSettings.ServerName + "\", " + - // "\"version\": \"" + Server.CurrentModVersion.Replace("_", ".") + "\", " + - // "\"players\": \"" + Server.MainNetServer.ConnectionsCount + "\", " + - // "\"maxPlayers\": \"" + Server.MainSettings.MaxPlayers + "\", " + - // "\"allowlist\": \"" + Server.MainSettings.Allowlist + "\"" + - // " }"; - // - // HttpResponseMessage response = await client.PostAsync("http://localhost:8081/servers", new StringContent(msg, Encoding.UTF8, "application/json")); - // - // // Sleep for 10s - // Thread.Sleep(10000); - // } - //} - //catch (Exception ex) - //{ - // Logging.Error(ex.Message); - //} + try + { + bool responseError = false; + HttpClient client = new(); + + while (!responseError) + { + string msg = + "{ " + + "\"port\": \"" + Server.MainSettings.ServerPort + "\", " + + "\"name\": \"" + Server.MainSettings.ServerName + "\", " + + "\"version\": \"" + Server.CurrentModVersion.Replace("_", ".") + "\", " + + "\"players\": \"" + Server.MainNetServer.ConnectionsCount + "\", " + + "\"maxPlayers\": \"" + Server.MainSettings.MaxPlayers + "\", " + + "\"allowlist\": \"" + Server.MainSettings.Allowlist + "\"" + + " }"; + + HttpResponseMessage response = await client.PostAsync("http://localhost:8081/servers", new StringContent(msg, Encoding.UTF8, "application/json")); + + string responseContent = await response.Content.ReadAsStringAsync(); + + if (responseContent != "OK!") + { + Logging.Error(responseContent); + responseError = true; + } + else + { + // Sleep for 10s + Thread.Sleep(10000); + } + } + } + catch (Exception ex) + { + Logging.Error(ex.Message); + } } }