From 173fc6b8a98fefc3aa96b3bfccd30231070a947c Mon Sep 17 00:00:00 2001 From: Chris Adamson Date: Wed, 28 May 2025 13:47:13 -0500 Subject: [PATCH] I saw a mudcrab the other day. Horrible creatures --- .../Helpers/HttpServerHelper.cs | 22 +++++++++++++++++++ Libraries/SPTarkov.Server.Core/Utils/App.cs | 13 ++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/HttpServerHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/HttpServerHelper.cs index a9f09294..ad844256 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/HttpServerHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/HttpServerHelper.cs @@ -54,6 +54,28 @@ public class HttpServerHelper(ConfigServer configServer) return $"wss://{BuildUrl()}"; } + /// + /// Method to determine if another version of the server is already running + /// + /// bool isAlreadyRunning + public async Task IsAlreadyRunning() + { + try + { + var handler = new HttpClientHandler() + { + ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator + }; + var http = new HttpClient(handler); + var res = await http.PostAsync($"{GetBackendUrl()}/launcher/ping", null); + return res.IsSuccessStatusCode; + } + catch (Exception ) + { + return false; + } + } + public void SendTextJson(HttpResponse resp, object output) { resp.Headers.Append("Content-Type", mime["json"]); diff --git a/Libraries/SPTarkov.Server.Core/Utils/App.cs b/Libraries/SPTarkov.Server.Core/Utils/App.cs index 98195eac..31f8a49f 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/App.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/App.cs @@ -1,5 +1,6 @@ using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.DI; +using SPTarkov.Server.Core.Helpers; using SPTarkov.Server.Core.Models.Spt.Config; using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Server.Core.Servers; @@ -17,6 +18,7 @@ public class App protected DatabaseService _databaseService; protected EncodingUtil _encodingUtil; protected HttpServer _httpServer; + protected HttpServerHelper _httpServerHelper; protected LocalisationService _localisationService; protected ISptLogger _logger; @@ -36,7 +38,8 @@ public class App HttpServer httpServer, DatabaseService databaseService, IEnumerable onLoadComponents, - IEnumerable onUpdateComponents + IEnumerable onUpdateComponents, + HttpServerHelper httpServerHelper ) { _logger = logger; @@ -46,6 +49,7 @@ public class App _configServer = configServer; _encodingUtil = encodingUtil; _httpServer = httpServer; + _httpServerHelper = httpServerHelper; _databaseService = databaseService; _onLoad = onLoadComponents; _onUpdate = onUpdateComponents; @@ -58,6 +62,13 @@ public class App // execute onLoad callbacks _logger.Info(_localisationService.GetText("executing_startup_callbacks")); + var isAlreadyRunning = await _httpServerHelper.IsAlreadyRunning(); + if (isAlreadyRunning) + { + _logger.Critical(_localisationService.GetText("webserver_already_running")); + Environment.Exit(1); + } + if (_logger.IsLogEnabled(LogLevel.Debug)) { _logger.Debug($"OS: {Environment.OSVersion.Version} | {Environment.OSVersion.Platform}");