feat: refactor maintenance authentication

This commit is contained in:
rjindael 2023-08-01 21:25:23 -07:00
parent f759602120
commit c48216c540
No known key found for this signature in database
GPG Key ID: D069369C906CCF31
3 changed files with 26 additions and 87 deletions

View File

@ -11,59 +11,6 @@ namespace Kiseki.Launcher.Windows
public static void Install()
{
Directory.CreateDirectory(Directories.Base);
int response = Web.CheckHealth();
if (response != Web.RESPONSE_SUCCESS)
{
if (response != Web.RESPONSE_MAINTENANCE)
{
// The Kiseki website is either down or we can't connect to the internet.
MessageBox.Show($"Failed to connect to the {Constants.PROJECT_NAME} website. Please check your internet connection.", Constants.PROJECT_NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
else
{
// We are in maintenance mode, so let's ask for a license.
if (!File.Exists(Directories.License))
{
AskForLicense(Directories.License);
}
// ... load the license ...
while (!Web.LoadLicense(File.ReadAllText(Directories.License)))
{
// ... and if it's invalid, keep asking for a new one.
File.Delete(Directories.License);
MessageBox.Show($"Corrupt license file! Please verify the contents of your license file (it should be named \"license.bin\".)", Constants.PROJECT_NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
AskForLicense(Directories.License, false);
}
// ... and then try this again;
Install();
}
}
// okay, now download the launcher from the Kiseki website...
}
private static void AskForLicense(string licensePath, bool showDialog = true)
{
DialogResult answer = showDialog ? MessageBox.Show($"{Constants.PROJECT_NAME} is currently under maintenance and requires a license in order to access games. Would you like to look for the license file now?", Constants.PROJECT_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) : DialogResult.Yes;
if (answer == DialogResult.Yes)
{
using OpenFileDialog dialog = new()
{
Title = "Select your license file",
Filter = "License files (*.bin)|*.bin",
InitialDirectory = Win32.GetDownloadsPath()
};
if (dialog.ShowDialog() == DialogResult.OK)
{
File.Copy(dialog.FileName, licensePath, true);
}
}
}
// TODO: Implement this

View File

@ -19,7 +19,11 @@ namespace Kiseki.Launcher.Windows
Directories.Initialize(Path.Combine(Directories.LocalAppData, Constants.PROJECT_NAME));
}
Web.Initialize();
if (!Web.Initialize())
{
MessageBox.Show($"Failed to connect to {Constants.PROJECT_NAME}. Please check your internet connection and try again.", Constants.PROJECT_NAME, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (!File.Exists(Directories.Application))
{
@ -32,7 +36,7 @@ namespace Kiseki.Launcher.Windows
{
// Nothing for us to do :P
Process.Start(Web.Url("/games"));
Environment.Exit(0);
return;
}
ApplicationConfiguration.Initialize();

View File

@ -14,9 +14,27 @@ namespace Kiseki.Launcher
public static readonly HttpClient HttpClient = new();
public static string CurrentUrl { get; private set; } = "";
public static bool MaintenanceMode { get; private set; } = false;
public static void Initialize() => CurrentUrl = BASE_URL;
public static bool Initialize()
{
CurrentUrl = BASE_URL;
int response = CheckHealth();
if (response != RESPONSE_SUCCESS)
{
if (response == RESPONSE_MAINTENANCE)
{
CurrentUrl = $"{MAINTENANCE_DOMAIN}.{BASE_URL}";
return Initialize();
}
return false;
}
return true;
}
public static string Url(string path) => $"https://{CurrentUrl}{path}";
public static int CheckHealth()
@ -25,35 +43,5 @@ namespace Kiseki.Launcher
return response is null ? RESPONSE_FAILURE : response.Status;
}
public static bool LoadLicense(string license)
{
if (!MaintenanceMode)
{
CurrentUrl = $"{MAINTENANCE_DOMAIN}.{CurrentUrl}";
MaintenanceMode = true;
}
// the "license" is actually just headers required to access the website.
// this can be cloudflare zero-trust headers (like what Kiseki does), or however
// else you'd like to do auth-walls. either way; it's just a JSON document
try
{
HttpClient.DefaultRequestHeaders.Clear();
var headers = JsonSerializer.Deserialize<Dictionary<string, string>>(license)!;
for (int i = 0; i < headers.Count; i++)
{
HttpClient.DefaultRequestHeaders.Add(headers.ElementAt(i).Key, headers.ElementAt(i).Value);
}
}
catch
{
return false;
}
return true;
}
}
}