New private MasterServer

This commit is contained in:
EntenKoeniq 2021-07-12 00:38:34 +02:00
parent 49cc21b2cb
commit 2c4485292d
4 changed files with 37 additions and 124 deletions

View File

@ -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!"));

View File

@ -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"
}

View File

@ -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);
}
}
}