diff --git a/LauncherGamePlugin/Storage.cs b/LauncherGamePlugin/Storage.cs index e222b6e..6245f53 100644 --- a/LauncherGamePlugin/Storage.cs +++ b/LauncherGamePlugin/Storage.cs @@ -1,4 +1,5 @@ -using LauncherGamePlugin.Enums; +using System.Net.Http.Headers; +using LauncherGamePlugin.Enums; using LauncherGamePlugin.Interfaces; using Newtonsoft.Json; @@ -31,12 +32,13 @@ public static class Storage } public static Task ImageDownload(string? url) => ImageDownload((url == null) ? null : new Uri(url)); - public static async Task ImageDownload(Uri? uri) + public static async Task ImageDownload(Uri? uri, AuthenticationHeaderValue? auth = null) { if (uri == null) return null; using HttpClient client = new(); + client.DefaultRequestHeaders.Authorization = auth; try { HttpResponseMessage response = await client.GetAsync(uri); diff --git a/RemoteDownloaderPlugin/Game/GameDownload.cs b/RemoteDownloaderPlugin/Game/GameDownload.cs index 28e8392..7cef50a 100644 --- a/RemoteDownloaderPlugin/Game/GameDownload.cs +++ b/RemoteDownloaderPlugin/Game/GameDownload.cs @@ -1,4 +1,5 @@ -using ICSharpCode.SharpZipLib.Zip; +using System.Net.Http.Headers; +using ICSharpCode.SharpZipLib.Zip; using LauncherGamePlugin; using LauncherGamePlugin.Interfaces; using RemoteDownloaderPlugin.Utils; @@ -20,11 +21,13 @@ public class GameDownload : ProgressStatus public ContentTypes InstalledEntries { get; private set; } private DateTimeOffset _downloadStart = DateTimeOffset.Now; + private AuthenticationHeaderValue? _auth; - public GameDownload(OnlineGameDownload entry) + public GameDownload(OnlineGameDownload entry, AuthenticationHeaderValue? auth) { _entry = entry; InstalledEntries = new(); + _auth = auth; } private void OnProgressUpdate(object? obj, float progress) @@ -71,6 +74,7 @@ private async Task DownloadEmu(IApp app) Directory.CreateDirectory(extraFilesPath); using HttpClient client = new(); + client.DefaultRequestHeaders.Authorization = _auth; for (int i = 0; i < _entry.Files.Count; i++) { @@ -128,6 +132,7 @@ private async Task DownloadPc(IApp app) Directory.CreateDirectory(BasePath); using HttpClient client = new(); + client.DefaultRequestHeaders.Authorization = _auth; Progress progress = new(); progress.ProgressChanged += OnProgressUpdate; diff --git a/RemoteDownloaderPlugin/Game/OnlineGame.cs b/RemoteDownloaderPlugin/Game/OnlineGame.cs index a39bc69..b6b1b26 100644 --- a/RemoteDownloaderPlugin/Game/OnlineGame.cs +++ b/RemoteDownloaderPlugin/Game/OnlineGame.cs @@ -68,7 +68,7 @@ public async Task Download() return; } - _download = new GameDownload(Game); + _download = new GameDownload(Game, _plugin.Storage.Data.GetAuth()); OnUpdate?.Invoke(); try diff --git a/RemoteDownloaderPlugin/Gui/SettingsRemoteIndexGui.cs b/RemoteDownloaderPlugin/Gui/SettingsRemoteIndexGui.cs index 2b682bb..572f00a 100644 --- a/RemoteDownloaderPlugin/Gui/SettingsRemoteIndexGui.cs +++ b/RemoteDownloaderPlugin/Gui/SettingsRemoteIndexGui.cs @@ -8,46 +8,64 @@ public class SettingsRemoteIndexGui private IApp _app; private Plugin _instance; + private string _indexUrl; + private string _authUser; + private string _authPass; + public SettingsRemoteIndexGui(IApp app, Plugin instance) { _app = app; _instance = instance; } - public void ShowGui(string possibleError = "") + public void ShowGui() { List formEntries = new() { Form.TextBox("Enter remote index URL", FormAlignment.Left, "Bold"), - Form.TextInput("Index URL:", _instance.Storage.Data.IndexUrl), + Form.TextInput("Index URL:", _instance.Storage.Data.IndexUrl).NotEmpty(), + Form.TextInput("User:", _instance.Storage.Data.IndexUser).WhenNotEmpty(), + Form.TextInput("Pass:", _instance.Storage.Data.IndexPass).WhenNotEmpty(), Form.Button("Cancel", _ => _app.HideForm(), "Save", Process) }; - if (!string.IsNullOrWhiteSpace(possibleError)) - formEntries.Add(Form.TextBox(possibleError, fontWeight: "Bold")); + var errorEntry = Form.TextBox("", FormAlignment.Center); + formEntries.Add(errorEntry); + var form = new Form(formEntries) + { + ValidationFailureField = errorEntry + }; - _app.ShowForm(formEntries); + _app.ShowForm(form); } private async void Process(Form form) { - _app.ShowTextPrompt("Loading"); - var newUrl = form.GetValue("Index URL:"); - var origIndexUrl = _instance.Storage.Data.IndexUrl; - - if (string.IsNullOrWhiteSpace(newUrl)) + if (!form.Validate(_app)) { - ShowGui("Index URL is empty"); return; } + _app.ShowTextPrompt("Loading"); + var newUrl = form.GetValue("Index URL:")!; + var newUser = form.GetValue("User:")!; + var newPass = form.GetValue("Pass:")!; + var origIndexUrl = _instance.Storage.Data.IndexUrl; + var origUser = _instance.Storage.Data.IndexUser; + var origPass = _instance.Storage.Data.IndexPass; + if (newUrl != origIndexUrl) { _instance.Storage.Data.IndexUrl = newUrl; + _instance.Storage.Data.IndexUser = newUser; + _instance.Storage.Data.IndexPass = newPass; if (!await _instance.FetchRemote()) { _instance.Storage.Data.IndexUrl = origIndexUrl; - ShowGui("Failed to fetch data from given URL"); + _instance.Storage.Data.IndexUser = origUser; + _instance.Storage.Data.IndexPass = origPass; + form.ValidationFailureField!.Name = "Failed to fetch data from given URL"; + _app.ShowForm(form); return; } diff --git a/RemoteDownloaderPlugin/Plugin.cs b/RemoteDownloaderPlugin/Plugin.cs index 5c87504..6512bd6 100644 --- a/RemoteDownloaderPlugin/Plugin.cs +++ b/RemoteDownloaderPlugin/Plugin.cs @@ -1,4 +1,5 @@ -using LauncherGamePlugin; +using System.Net.Http.Headers; +using LauncherGamePlugin; using LauncherGamePlugin.Commands; using LauncherGamePlugin.Enums; using LauncherGamePlugin.Forms; @@ -102,6 +103,8 @@ public async Task FetchRemote() { using HttpClient client = new(); client.Timeout = TimeSpan.FromSeconds(10); + client.DefaultRequestHeaders.Authorization = Storage.Data.GetAuth(); + var data = await client.GetStringAsync(Storage.Data.IndexUrl); _cachedRemote = JsonConvert.DeserializeObject(data)!; diff --git a/RemoteDownloaderPlugin/Store.cs b/RemoteDownloaderPlugin/Store.cs index 82dae78..8dc881a 100644 --- a/RemoteDownloaderPlugin/Store.cs +++ b/RemoteDownloaderPlugin/Store.cs @@ -1,4 +1,5 @@ -using System.Text.Json.Serialization; +using System.Net.Http.Headers; +using System.Text.Json.Serialization; using Newtonsoft.Json; namespace RemoteDownloaderPlugin; @@ -136,6 +137,21 @@ public class Store public List EmuProfiles { get; set; } = new(); public List HiddenRemotePlatforms { get; set; } = new(); public string IndexUrl { get; set; } = ""; + public string IndexUser { get; set; } = ""; + public string IndexPass { get; set; } = ""; + + public AuthenticationHeaderValue? GetAuth() + { + if (string.IsNullOrWhiteSpace(IndexUser)) + return null; + + IndexPass ??= string.Empty; + var authenticationString = $"{IndexUser}:{IndexPass}"; + var base64String = Convert.ToBase64String( + System.Text.Encoding.ASCII.GetBytes(authenticationString)); + + return new AuthenticationHeaderValue("Basic", base64String); + } public void Migrate() {