feat: overhaul Web with new arbiter code

This commit is contained in:
rjindael 2023-09-10 02:31:23 -07:00
parent 1e0648a005
commit 9e4cb6bfce
No known key found for this signature in database
GPG Key ID: D069369C906CCF31
5 changed files with 43 additions and 49 deletions

View File

@ -111,7 +111,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
}
}
var clientRelease = await Http.GetJson<ClientRelease>(Web.Url($"/api/setup/{Arguments["Version"]}"));
var clientRelease = await Http.GetJson<ClientRelease>(Web.FormatUrl($"/api/setup/{Arguments["Version"]}"));
bool clientUpToDate = true;
bool createStudioShortcut = false;
@ -226,7 +226,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
StartInfo = new()
{
FileName = Path.Combine(Paths.Versions, Arguments["Version"], $"{Constants.PROJECT_NAME}.Player.exe"),
Arguments = $"-a \"{Web.Url("/Login/Negotiate.ashx")}\" -t \"{Arguments["Ticket"]}\" -j \"{Arguments["JoinScript"]}\"",
Arguments = $"-a \"{Web.FormatUrl("/Login/Negotiate.ashx")}\" -t \"{Arguments["Ticket"]}\" -j \"{Arguments["JoinScript"]}\"",
UseShellExecute = true,
}
};
@ -446,7 +446,7 @@ public class Bootstrapper : Interfaces.IBootstrapper
}
// Load the license...
while (!Web.LoadLicense(File.ReadAllText(Paths.License)))
while (!Web.License(File.ReadAllText(Paths.License)))
{
// ...and if it's corrupt, keep asking for a new one.
File.Delete(Paths.License);

View File

@ -0,0 +1,8 @@
namespace Kiseki.Launcher.Enums;
public enum HealthCheckStatus
{
Failure,
Maintenance,
Success
}

View File

@ -1,9 +1,7 @@
namespace Kiseki.Launcher.Models;
using System.Text.Json.Serialization;
public class HealthCheck
{
[JsonPropertyName("status")]
public int Status { get; set; } = -1;
public HealthCheckStatus Status { get; set; }
}

View File

@ -1,13 +0,0 @@
namespace Kiseki.Launcher.Models;
public readonly struct WebResponse
{
public WebResponse(int status, object? data)
{
Status = status;
Data = data;
}
public int Status { get; }
public object? Data { get; }
}

View File

@ -1,51 +1,33 @@
namespace Kiseki.Launcher;
using System.Text.Json;
public static class Web
{
public const int RESPONSE_FAILURE = -1;
public const int RESPONSE_SUCCESS = 0;
public const int RESPONSE_MAINTENANCE = 1;
public static string CurrentUrl { get; private set; } = "";
public static string? CurrentUrl { get; private set; } = null;
public static bool IsConnected { get; private set; } = false;
public static bool IsInMaintenance { get; private set; } = false;
public static readonly HttpClient HttpClient = new();
public static bool Initialize()
public static async void Initialize()
{
CurrentUrl = IsInMaintenance ? $"{Constants.MAINTENANCE_DOMAIN}.{Constants.BASE_URL}" : Constants.BASE_URL;
// Synchronous block is intentional
Task<Models.WebResponse> task = CheckHealth();
task.Wait();
int response = task.Result.Status;
if (response != RESPONSE_SUCCESS)
HealthCheckStatus status = await GetHealthStatus();
if (status != HealthCheckStatus.Success)
{
if (response == RESPONSE_MAINTENANCE) IsInMaintenance = true;
if (status == HealthCheckStatus.Maintenance)
{
IsInMaintenance = true;
}
return false;
return;
}
return true;
IsConnected = true;
}
public static string Url(string path) => $"https://{CurrentUrl}{path}";
public static async Task<Models.WebResponse> CheckHealth()
public static bool License(string license)
{
var response = await Utilities.Http.GetJson<Models.HealthCheck>(Url("/api/health"));
return new Models.WebResponse(response?.Status ?? RESPONSE_FAILURE, response);
}
public static bool LoadLicense(string license)
{
// The license is just headers required to access the website in a JSON document
Dictionary<string, string> headers;
try
@ -64,4 +46,23 @@ public static class Web
return true;
}
public static async Task<HealthCheckStatus> GetHealthStatus()
{
var response = await Http.GetJson<HealthCheck>(FormatUrl("/health-check"));
return response?.Status ?? HealthCheckStatus.Failure;
}
public static string FormatUrl(string path, string? subdomain = null)
{
string scheme = "https";
string url = subdomain == null ? CurrentUrl! : $"{subdomain!}.{CurrentUrl!}";
#if DEBUG
scheme = "http";
#endif
return $"{scheme}://{url}{path}";
}
}