From 28e678bce9a9e6766887691620be3e01901f7893 Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Sat, 6 Jul 2024 18:09:07 +0200 Subject: [PATCH] feat(server): attempt to op the realm owner on join --- .../Realms/Controllers/WorldsController.cs | 14 +++++++++++++- .../Modes/Realms/Helpers/DockerHelper.cs | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs b/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs index 421085a..4633b14 100644 --- a/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs +++ b/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs @@ -907,9 +907,13 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers { var connection = _context.Connections.Include(c => c.World).FirstOrDefault(x => x.World.Id == wId); + var isRunning = new DockerHelper(connection.World).IsRunning(); var query = new MinecraftServerQuery().Query(connection.Address); - if (query == null) new DockerHelper(connection.World).StartServer(); + if (!isRunning) + { + new DockerHelper(connection.World).StartServer(); + } while (query == null) { @@ -917,6 +921,14 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers query = new MinecraftServerQuery().Query(connection.Address); } + string cookie = Request.Headers.Cookie; + string playerUUID = cookie.Split(";")[0].Split(":")[2]; + + if (connection.World.OwnerUUID == playerUUID) + { + new DockerHelper(connection.World).ExecuteCommand($"op {connection.World.Owner}"); + } + return Ok(connection); } diff --git a/Minecraft-Realms-Emulator/Modes/Realms/Helpers/DockerHelper.cs b/Minecraft-Realms-Emulator/Modes/Realms/Helpers/DockerHelper.cs index 9941992..958558e 100644 --- a/Minecraft-Realms-Emulator/Modes/Realms/Helpers/DockerHelper.cs +++ b/Minecraft-Realms-Emulator/Modes/Realms/Helpers/DockerHelper.cs @@ -17,6 +17,23 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Helpers serverProcess.Start(); } + public bool IsRunning() + { + ProcessStartInfo containerStateProcessInfo = new(); + + containerStateProcessInfo.FileName = "docker"; + containerStateProcessInfo.Arguments = $"inspect realm-server-{world.Id} -f {{{{.State.Running}}}}"; + + containerStateProcessInfo.RedirectStandardOutput = true; + + Process containerStateProcess = new(); + containerStateProcess.StartInfo = containerStateProcessInfo; + containerStateProcess.Start(); + + containerStateProcess.WaitForExit(); + return bool.Parse(containerStateProcess.StandardOutput.ReadToEnd()); + } + public void StartServer() { ProcessStartInfo serverProcessInfo = new();