From 6399ef07d501c9746f01ebb471d32c0419be2cde Mon Sep 17 00:00:00 2001 From: UrbanCMC Date: Wed, 9 Aug 2023 21:05:20 +0200 Subject: [PATCH] Fix manual downloads from secure servers --- .../View Models/BrowserWindowViewModel.cs | 23 +++++++++++++------ Wabbajack.Common/HttpExtensions.cs | 11 ++------- .../Interventions/ManualDownload.cs | 6 ++--- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Wabbajack.App.Wpf/View Models/BrowserWindowViewModel.cs b/Wabbajack.App.Wpf/View Models/BrowserWindowViewModel.cs index bd05085c..d0aa4a4c 100644 --- a/Wabbajack.App.Wpf/View Models/BrowserWindowViewModel.cs +++ b/Wabbajack.App.Wpf/View Models/BrowserWindowViewModel.cs @@ -23,7 +23,7 @@ public abstract class BrowserWindowViewModel : ViewModel [Reactive] public string HeaderText { get; set; } [Reactive] public string Instructions { get; set; } - + [Reactive] public string Address { get; set; } public BrowserWindow? Browser { get; set; } @@ -83,6 +83,11 @@ public abstract class BrowserWindowViewModel : ViewModel public async Task GetCookies(string domainEnding, CancellationToken token) { + // Strip www. before searching for cookies on a domain to handle websites saving their cookies like .example.org + if (domainEnding.StartsWith("www.")) + { + domainEnding = domainEnding[4..]; + } var cookies = (await _browser.CoreWebView2.CookieManager.GetCookiesAsync("")) .Where(c => c.Domain.EndsWith(domainEnding)); return cookies.Select(c => new Cookie @@ -125,7 +130,7 @@ public abstract class BrowserWindowViewModel : ViewModel { source.SetCanceled(); } - + args.Cancel = true; args.Handled = true; }; @@ -146,12 +151,16 @@ public abstract class BrowserWindowViewModel : ViewModel } var cookies = await GetCookies(uri.Host, token); - return new ManualDownload.BrowserDownloadState(uri, cookies, new[] - { - ("Referer", referer.ToString()) - }); + return new ManualDownload.BrowserDownloadState( + uri, + cookies, + new[] + { + ("Referer", referer?.ToString() ?? uri.ToString()) + }, + _browser.CoreWebView2.Settings.UserAgent); } - + public async Task WaitForDownload(AbsolutePath path, CancellationToken token) { var source = new TaskCompletionSource(); diff --git a/Wabbajack.Common/HttpExtensions.cs b/Wabbajack.Common/HttpExtensions.cs index a0900120..35ff173c 100644 --- a/Wabbajack.Common/HttpExtensions.cs +++ b/Wabbajack.Common/HttpExtensions.cs @@ -19,7 +19,7 @@ public static class HttpExtensions msg.Headers.Add("Cookie", string.Join(";", cookies.Select(c => $"{c.Name}={c.Value}"))); return msg; } - + public static HttpRequestMessage AddHeaders(this HttpRequestMessage msg, IEnumerable<(string Key, string Value)> headers) { foreach (var header in headers) @@ -29,17 +29,10 @@ public static class HttpExtensions return msg; } - public static HttpRequestMessage AddChromeAgent(this HttpRequestMessage msg) - { - msg.Headers.Add("User-Agent", - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"); - return msg; - } - public static HttpRequestMessage ToHttpRequestMessage(this ManualDownload.BrowserDownloadState browserState) { var msg = new HttpRequestMessage(HttpMethod.Get, browserState.Uri); - msg.AddChromeAgent(); + msg.Headers.Add("User-Agent", browserState.UserAgent); msg.AddCookies(browserState.Cookies); msg.AddHeaders(browserState.Headers); return msg; diff --git a/Wabbajack.DTOs/Interventions/ManualDownload.cs b/Wabbajack.DTOs/Interventions/ManualDownload.cs index 15a3be24..84dda1dd 100644 --- a/Wabbajack.DTOs/Interventions/ManualDownload.cs +++ b/Wabbajack.DTOs/Interventions/ManualDownload.cs @@ -10,14 +10,14 @@ namespace Wabbajack.DTOs.Interventions; public class ManualDownload : AUserIntervention { public Archive Archive { get; } - + public ManualDownload(Archive archive) { Archive = archive; } - public record BrowserDownloadState(Uri Uri, Cookie[] Cookies, (string Key, string Value)[] Headers) + public record BrowserDownloadState(Uri Uri, Cookie[] Cookies, (string Key, string Value)[] Headers, string UserAgent) { - + } } \ No newline at end of file