using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.Models.Spt.Config; using SPTarkov.Server.Core.Servers; namespace SPTarkov.Server.Core.Helpers; [Injectable(InjectionType.Singleton)] public class HttpServerHelper(ConfigServer configServer) { protected readonly HttpConfig _httpConfig = configServer.GetConfig(); protected Dictionary mime = new() { { "css", "text/css" }, { "bin", "application/octet-stream" }, { "html", "text/html" }, { "jpg", "image/jpeg" }, { "js", "text/javascript" }, { "json", "application/json" }, { "png", "image/png" }, { "svg", "image/svg+xml" }, { "txt", "text/plain" } }; public string? GetMimeText(string key) { return mime.GetValueOrDefault(key); } /// /// Combine ip and port into address /// /// URI public string BuildUrl() { return $"{_httpConfig.BackendIp}:{_httpConfig.BackendPort}"; } /// /// Prepend http to the url:port /// /// URI public string GetBackendUrl() { return $"https://{BuildUrl()}"; } /// /// Get websocket url + port /// /// wss:// address public string GetWebsocketUrl() { 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"]); resp.StatusCode = 200; /* TODO: figure this one out resp.writeHead(200, "OK", { "Content-Type": this.mime.json }); resp.end(output); */ } }