From c48216c5401342fba10c63d40684ba33bc6dd943 Mon Sep 17 00:00:00 2001 From: rjindael Date: Tue, 1 Aug 2023 21:25:23 -0700 Subject: [PATCH] feat: refactor maintenance authentication --- Kiseki.Launcher.Windows/Launcher.cs | 53 ----------------------------- Kiseki.Launcher.Windows/Program.cs | 8 +++-- Kiseki.Launcher/Web.cs | 52 +++++++++++----------------- 3 files changed, 26 insertions(+), 87 deletions(-) diff --git a/Kiseki.Launcher.Windows/Launcher.cs b/Kiseki.Launcher.Windows/Launcher.cs index e2ce691..df41f63 100644 --- a/Kiseki.Launcher.Windows/Launcher.cs +++ b/Kiseki.Launcher.Windows/Launcher.cs @@ -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 diff --git a/Kiseki.Launcher.Windows/Program.cs b/Kiseki.Launcher.Windows/Program.cs index c99ad51..a5ae784 100644 --- a/Kiseki.Launcher.Windows/Program.cs +++ b/Kiseki.Launcher.Windows/Program.cs @@ -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(); diff --git a/Kiseki.Launcher/Web.cs b/Kiseki.Launcher/Web.cs index 81ca983..96c19ba 100644 --- a/Kiseki.Launcher/Web.cs +++ b/Kiseki.Launcher/Web.cs @@ -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>(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; - } } } \ No newline at end of file