Remove duplicate user agent code

This commit is contained in:
UrbanCMC 2023-08-09 21:29:34 +02:00
parent 6399ef07d5
commit 63eaffd9e3
5 changed files with 36 additions and 33 deletions

View File

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.CompilerServices; using Microsoft.VisualBasic.CompilerServices;
using Newtonsoft.Json; using Newtonsoft.Json;
using Wabbajack.Common;
using Wabbajack.Downloaders; using Wabbajack.Downloaders;
using Wabbajack.DTOs; using Wabbajack.DTOs;
using Wabbajack.DTOs.DownloadStates; using Wabbajack.DTOs.DownloadStates;
@ -27,9 +28,9 @@ namespace Wabbajack
private readonly HttpClient _client; private readonly HttpClient _client;
private readonly Client _wjclient; private readonly Client _wjclient;
private readonly DTOSerializer _dtos; private readonly DTOSerializer _dtos;
private readonly DownloadDispatcher _downloader; private readonly DownloadDispatcher _downloader;
private static Uri GITHUB_REPO_RELEASES = new("https://api.github.com/repos/wabbajack-tools/wabbajack/releases"); private static Uri GITHUB_REPO_RELEASES = new("https://api.github.com/repos/wabbajack-tools/wabbajack/releases");
public LauncherUpdater(ILogger<LauncherUpdater> logger, HttpClient client, Client wjclient, DTOSerializer dtos, public LauncherUpdater(ILogger<LauncherUpdater> logger, HttpClient client, Client wjclient, DTOSerializer dtos,
@ -41,8 +42,8 @@ namespace Wabbajack
_dtos = dtos; _dtos = dtos;
_downloader = downloader; _downloader = downloader;
} }
public static Lazy<AbsolutePath> CommonFolder = new (() => public static Lazy<AbsolutePath> CommonFolder = new (() =>
{ {
var entryPoint = KnownFolders.EntryPoint; var entryPoint = KnownFolders.EntryPoint;
@ -105,7 +106,7 @@ namespace Wabbajack
var launcherFolder = KnownFolders.EntryPoint.Parent; var launcherFolder = KnownFolders.EntryPoint.Parent;
var exePath = launcherFolder.Combine("Wabbajack.exe"); var exePath = launcherFolder.Combine("Wabbajack.exe");
var launcherVersion = FileVersionInfo.GetVersionInfo(exePath.ToString()); var launcherVersion = FileVersionInfo.GetVersionInfo(exePath.ToString());
if (release != default && release.version > Version.Parse(launcherVersion.FileVersion!)) if (release != default && release.version > Version.Parse(launcherVersion.FileVersion!))
@ -119,7 +120,7 @@ namespace Wabbajack
Name = release.asset.Name, Name = release.asset.Name,
Size = release.asset.Size Size = release.asset.Size
}, tempPath, CancellationToken.None); }, tempPath, CancellationToken.None);
if (tempPath.Size() != release.asset.Size) if (tempPath.Size() != release.asset.Size)
{ {
_logger.LogInformation( _logger.LogInformation(
@ -130,12 +131,12 @@ namespace Wabbajack
if (exePath.FileExists()) if (exePath.FileExists())
exePath.Delete(); exePath.Delete();
await tempPath.MoveToAsync(exePath, true, CancellationToken.None); await tempPath.MoveToAsync(exePath, true, CancellationToken.None);
_logger.LogInformation("Finished updating wabbajack"); _logger.LogInformation("Finished updating wabbajack");
await _wjclient.SendMetric("updated_launcher", $"{launcherVersion.FileVersion} -> {release.version}"); await _wjclient.SendMetric("updated_launcher", $"{launcherVersion.FileVersion} -> {release.version}");
} }
} }
private async Task<Release[]> GetReleases() private async Task<Release[]> GetReleases()
{ {
_logger.LogInformation("Getting new Wabbajack version list"); _logger.LogInformation("Getting new Wabbajack version list");
@ -146,7 +147,7 @@ namespace Wabbajack
private HttpRequestMessage MakeMessage(Uri uri) private HttpRequestMessage MakeMessage(Uri uri)
{ {
var msg = new HttpRequestMessage(HttpMethod.Get, uri); var msg = new HttpRequestMessage(HttpMethod.Get, uri);
msg.UseChromeUserAgent(); msg.AddChromeAgent();
return msg; return msg;
} }

View File

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using System.Web; using System.Web;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using ReactiveUI; using ReactiveUI;
using Wabbajack.Common;
using Wabbajack.DTOs.Interventions; using Wabbajack.DTOs.Interventions;
using Wabbajack.DTOs.Logins; using Wabbajack.DTOs.Logins;
using Wabbajack.Messages; using Wabbajack.Messages;
@ -40,7 +41,7 @@ public abstract class OAuth2LoginHandler<TLoginType> : BrowserWindowViewModel
var tcs = new TaskCompletionSource<Uri>(); var tcs = new TaskCompletionSource<Uri>();
await NavigateTo(tlogin.AuthorizationEndpoint); await NavigateTo(tlogin.AuthorizationEndpoint);
Browser!.Browser.CoreWebView2.Settings.UserAgent = "Wabbajack"; Browser!.Browser.CoreWebView2.Settings.UserAgent = "Wabbajack";
Browser!.Browser.NavigationStarting += (sender, args) => Browser!.Browser.NavigationStarting += (sender, args) =>
{ {
@ -50,7 +51,7 @@ public abstract class OAuth2LoginHandler<TLoginType> : BrowserWindowViewModel
tcs.TrySetResult(uri); tcs.TrySetResult(uri);
} }
}; };
Instructions = $"Please log in and allow Wabbajack to access your {tlogin.SiteName} account"; Instructions = $"Please log in and allow Wabbajack to access your {tlogin.SiteName} account";
var scopes = string.Join(" ", tlogin.Scopes); var scopes = string.Join(" ", tlogin.Scopes);
@ -88,8 +89,7 @@ public abstract class OAuth2LoginHandler<TLoginType> : BrowserWindowViewModel
var msg = new HttpRequestMessage(); var msg = new HttpRequestMessage();
msg.Method = HttpMethod.Post; msg.Method = HttpMethod.Post;
msg.RequestUri = tlogin.TokenEndpoint; msg.RequestUri = tlogin.TokenEndpoint;
msg.Headers.Add("User-Agent", msg.AddChromeAgent();
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36");
msg.Headers.Add("Cookie", string.Join(";", cookies.Select(c => $"{c.Name}={c.Value}"))); msg.Headers.Add("Cookie", string.Join(";", cookies.Select(c => $"{c.Name}={c.Value}")));
msg.Content = new FormUrlEncodedContent(formData.ToList()); msg.Content = new FormUrlEncodedContent(formData.ToList());
@ -101,6 +101,6 @@ public abstract class OAuth2LoginHandler<TLoginType> : BrowserWindowViewModel
Cookies = cookies, Cookies = cookies,
ResultState = data! ResultState = data!
}); });
} }
} }

View File

@ -14,12 +14,21 @@ namespace Wabbajack.Common;
public static class HttpExtensions public static class HttpExtensions
{ {
private const string ChromeUserAgent =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36";
public static HttpRequestMessage AddCookies(this HttpRequestMessage msg, Cookie[] cookies) public static HttpRequestMessage AddCookies(this HttpRequestMessage msg, Cookie[] cookies)
{ {
msg.Headers.Add("Cookie", string.Join(";", cookies.Select(c => $"{c.Name}={c.Value}"))); msg.Headers.Add("Cookie", string.Join(";", cookies.Select(c => $"{c.Name}={c.Value}")));
return msg; return msg;
} }
public static HttpRequestMessage AddChromeAgent(this HttpRequestMessage msg, string? overrideUserAgent = null)
{
msg.Headers.UserAgent.Clear();
msg.Headers.Add("User-Agent", overrideUserAgent ?? ChromeUserAgent);
return msg;
}
public static HttpRequestMessage AddHeaders(this HttpRequestMessage msg, IEnumerable<(string Key, string Value)> headers) public static HttpRequestMessage AddHeaders(this HttpRequestMessage msg, IEnumerable<(string Key, string Value)> headers)
{ {
foreach (var header in headers) foreach (var header in headers)
@ -32,7 +41,7 @@ public static class HttpExtensions
public static HttpRequestMessage ToHttpRequestMessage(this ManualDownload.BrowserDownloadState browserState) public static HttpRequestMessage ToHttpRequestMessage(this ManualDownload.BrowserDownloadState browserState)
{ {
var msg = new HttpRequestMessage(HttpMethod.Get, browserState.Uri); var msg = new HttpRequestMessage(HttpMethod.Get, browserState.Uri);
msg.Headers.Add("User-Agent", browserState.UserAgent); msg.AddChromeAgent(browserState.UserAgent);
msg.AddCookies(browserState.Cookies); msg.AddCookies(browserState.Cookies);
msg.AddHeaders(browserState.Headers); msg.AddHeaders(browserState.Headers);
return msg; return msg;

View File

@ -65,7 +65,7 @@ public class GoogleDriveDownloader : ADownloader<DTOs.DownloadStates.GoogleDrive
return new Uri( return new Uri(
$"https://drive.google.com/uc?id={(state as DTOs.DownloadStates.GoogleDrive)?.Id}&export=download"); $"https://drive.google.com/uc?id={(state as DTOs.DownloadStates.GoogleDrive)?.Id}&export=download");
} }
public override IDownloadState? Resolve(IReadOnlyDictionary<string, string> iniData) public override IDownloadState? Resolve(IReadOnlyDictionary<string, string> iniData)
{ {
if (iniData.ContainsKey("directURL") && Uri.TryCreate(iniData["directURL"].CleanIniString(), UriKind.Absolute, out var uri)) if (iniData.ContainsKey("directURL") && Uri.TryCreate(iniData["directURL"].CleanIniString(), UriKind.Absolute, out var uri))
@ -74,8 +74,8 @@ public class GoogleDriveDownloader : ADownloader<DTOs.DownloadStates.GoogleDrive
} }
public override Priority Priority => Priority.Normal; public override Priority Priority => Priority.Normal;
public async Task<T> DownloadStream<T>(Archive archive, Func<Stream, Task<T>> fn, CancellationToken token) public async Task<T> DownloadStream<T>(Archive archive, Func<Stream, Task<T>> fn, CancellationToken token)
{ {
var state = archive.State as DTOs.DownloadStates.GoogleDrive; var state = archive.State as DTOs.DownloadStates.GoogleDrive;
@ -112,8 +112,8 @@ public class GoogleDriveDownloader : ADownloader<DTOs.DownloadStates.GoogleDrive
{ {
var initialUrl = $"https://drive.google.com/uc?id={state.Id}&export=download"; var initialUrl = $"https://drive.google.com/uc?id={state.Id}&export=download";
var msg = new HttpRequestMessage(HttpMethod.Get, initialUrl); var msg = new HttpRequestMessage(HttpMethod.Get, initialUrl);
msg.UseChromeUserAgent(); msg.AddChromeAgent();
using var response = await _client.SendAsync(msg, token); using var response = await _client.SendAsync(msg, token);
var cookies = response.GetSetCookies(); var cookies = response.GetSetCookies();
var warning = cookies.FirstOrDefault(c => c.Key.StartsWith("download_warning_")); var warning = cookies.FirstOrDefault(c => c.Key.StartsWith("download_warning_"));
@ -124,7 +124,7 @@ public class GoogleDriveDownloader : ADownloader<DTOs.DownloadStates.GoogleDrive
var txt = await response.Content.ReadAsStringAsync(token); var txt = await response.Content.ReadAsStringAsync(token);
if (txt.Contains("<title>Google Drive - Quota exceeded</title>")) if (txt.Contains("<title>Google Drive - Quota exceeded</title>"))
throw new Exception("Google Drive - Quota Exceeded"); throw new Exception("Google Drive - Quota Exceeded");
doc.LoadHtml(txt); doc.LoadHtml(txt);
var action = doc.DocumentNode.DescendantsAndSelf() var action = doc.DocumentNode.DescendantsAndSelf()
@ -133,7 +133,7 @@ public class GoogleDriveDownloader : ADownloader<DTOs.DownloadStates.GoogleDrive
.Select(d => d.GetAttributeValue("action", "")) .Select(d => d.GetAttributeValue("action", ""))
.FirstOrDefault(); .FirstOrDefault();
if (action != null) if (action != null)
warning = ("download_warning_", "t"); warning = ("download_warning_", "t");
} }
@ -145,18 +145,18 @@ public class GoogleDriveDownloader : ADownloader<DTOs.DownloadStates.GoogleDrive
var url = $"https://drive.google.com/uc?export=download&confirm={warning.Value}&id={state.Id}"; var url = $"https://drive.google.com/uc?export=download&confirm={warning.Value}&id={state.Id}";
var httpState = new HttpRequestMessage(HttpMethod.Get, url); var httpState = new HttpRequestMessage(HttpMethod.Get, url);
httpState.UseChromeUserAgent(); httpState.AddChromeAgent();
return httpState; return httpState;
} }
else else
{ {
var url = $"https://drive.google.com/file/d/{state.Id}/edit"; var url = $"https://drive.google.com/file/d/{state.Id}/edit";
var msg = new HttpRequestMessage(HttpMethod.Get, url); var msg = new HttpRequestMessage(HttpMethod.Get, url);
msg.UseChromeUserAgent(); msg.AddChromeAgent();
using var response = await _client.SendAsync(msg, token); using var response = await _client.SendAsync(msg, token);
msg = new HttpRequestMessage(HttpMethod.Get, url); msg = new HttpRequestMessage(HttpMethod.Get, url);
msg.UseChromeUserAgent(); msg.AddChromeAgent();
return !response.IsSuccessStatusCode ? null : msg; return !response.IsSuccessStatusCode ? null : msg;
} }
} }

View File

@ -10,13 +10,6 @@ namespace Wabbajack.Networking.Http;
public static class Extensions public static class Extensions
{ {
public static HttpRequestMessage UseChromeUserAgent(this HttpRequestMessage msg)
{
msg.Headers.UserAgent.Clear();
msg.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36");
return msg;
}
public static async Task<T> GetJsonFromSendAsync<T>(this HttpClient client, HttpRequestMessage msg, public static async Task<T> GetJsonFromSendAsync<T>(this HttpClient client, HttpRequestMessage msg,
JsonSerializerOptions opts, CancellationToken? token = null) JsonSerializerOptions opts, CancellationToken? token = null)
{ {