From 7d01743bac02a61c242ddbdea765baded1d9a796 Mon Sep 17 00:00:00 2001 From: Alistair Chapman Date: Sun, 18 Jul 2021 17:39:16 +1000 Subject: [PATCH] Add support for providing single `sid` cookie; Allow skipping setting file as Vortex default; --- src/NexusUploader/FileOptions.cs | 2 +- .../Http/MessageHandlerBuilder.cs | 29 --------------- src/NexusUploader/Http/NexusCookieHandler.cs | 35 +++++++++++++++++++ src/NexusUploader/Services/CookieService.cs | 19 +++++----- src/NexusUploader/Services/ManageClient.cs | 6 +++- src/NexusUploader/UploadCommand.cs | 8 +++++ 6 files changed, 57 insertions(+), 42 deletions(-) create mode 100644 src/NexusUploader/Http/NexusCookieHandler.cs diff --git a/src/NexusUploader/FileOptions.cs b/src/NexusUploader/FileOptions.cs index b1ceda7..5354376 100644 --- a/src/NexusUploader/FileOptions.cs +++ b/src/NexusUploader/FileOptions.cs @@ -12,7 +12,7 @@ public FileOptions(string name, string version, string category = "Main Files") public string Description {get;set;} = string.Empty; // public string Category {get;set;} public bool UpdateMainVersion {get;set;} = true; - public bool SetAsMainVortex {get;set;} = true; + public bool? SetAsMainVortex {get;set;} public int? PreviousFileId {get;set;} public override string ToString() { diff --git a/src/NexusUploader/Http/MessageHandlerBuilder.cs b/src/NexusUploader/Http/MessageHandlerBuilder.cs index 633e37a..6c3c9f1 100644 --- a/src/NexusUploader/Http/MessageHandlerBuilder.cs +++ b/src/NexusUploader/Http/MessageHandlerBuilder.cs @@ -49,33 +49,4 @@ private CookieContainer GetCookies() return c; } } - - public class NexusCookieHandler : HttpClientHandler - { - private readonly CookieService _cookies; - private readonly ILogger _logger; - - public NexusCookieHandler(CookieService cookieService, ILogger logger) - { - _cookies = cookieService; - _logger = logger; - base.CookieContainer = GetCookies(); - } - - private CookieContainer GetCookies() - { - var c = new CookieContainer(); - try { - foreach (var cookie in _cookies.GetCookies()) - { - if (!string.IsNullOrWhiteSpace(cookie.Key) && !string.IsNullOrWhiteSpace(cookie.Value)) { - c.Add(new Cookie(cookie.Key, cookie.Value) { Domain = "nexusmods.com"}); - } - } - } catch { - _logger.LogError("Error encountered while loading cookies! [bold] This probably won't work![/]"); - } - return c; - } - } } \ No newline at end of file diff --git a/src/NexusUploader/Http/NexusCookieHandler.cs b/src/NexusUploader/Http/NexusCookieHandler.cs new file mode 100644 index 0000000..f0a6eeb --- /dev/null +++ b/src/NexusUploader/Http/NexusCookieHandler.cs @@ -0,0 +1,35 @@ +using System.Linq; +using System.Net; +using System.Net.Http; +using Microsoft.Extensions.Logging; +using NexusUploader.Nexus.Services; + +namespace NexusUploader.Nexus.Http +{ + public class NexusCookieHandler : HttpClientHandler + { + private readonly CookieService _cookies; + private readonly ILogger _logger; + + public NexusCookieHandler(CookieService cookieService, ILogger logger) + { + _cookies = cookieService; + _logger = logger; + base.CookieContainer = GetCookies(); + } + + private CookieContainer GetCookies() + { + var c = new CookieContainer(); + try { + foreach (var (name, value) in _cookies.GetCookies().Where(cookie => + !string.IsNullOrWhiteSpace(cookie.Key) && !string.IsNullOrWhiteSpace(cookie.Value))) { + c.Add(new Cookie(name, value) {Domain = "nexusmods.com"}); + } + } catch { + _logger.LogError("Error encountered while loading cookies! [bold] This probably won't work![/]"); + } + return c; + } + } +} \ No newline at end of file diff --git a/src/NexusUploader/Services/CookieService.cs b/src/NexusUploader/Services/CookieService.cs index dbf3a76..fa55d0b 100644 --- a/src/NexusUploader/Services/CookieService.cs +++ b/src/NexusUploader/Services/CookieService.cs @@ -1,6 +1,8 @@ +using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.Encodings.Web; namespace NexusUploader.Nexus.Services { @@ -13,17 +15,8 @@ public CookieService(ModConfiguration config) _config = config; } /* - * At a minimum it seems like the following keys are required: - * - member_id - * - pass_hash - * - sfc - * - sfct - * - _app_session - * - sid - * - mqtids - * - jwt_fingerprint - * - session_id - */ + * Not even close, past me: only truly required one seems to be sid + */ public Dictionary GetCookies() { @@ -31,6 +24,10 @@ public Dictionary GetCookies() { var ckTxt = File.ReadAllLines(Path.GetFullPath(_config.Cookies)); var ckSet = ParseCookiesTxt(ckTxt); return ckSet; + } else if (_config.Cookies.StartsWith("{") || _config.Cookies.StartsWith("%7B")) { + //almost certainly a raw sid, we'll assume it is + var raw = Uri.UnescapeDataString(_config.Cookies); + return new Dictionary {["sid"] = Uri.EscapeDataString(raw)}; } else { if (_config.Cookies.Contains('\n')) { var ckSet = ParseCookiesTxt(_config.Cookies.Split('\n')); diff --git a/src/NexusUploader/Services/ManageClient.cs b/src/NexusUploader/Services/ManageClient.cs index 5e342a9..5fef1c4 100644 --- a/src/NexusUploader/Services/ManageClient.cs +++ b/src/NexusUploader/Services/ManageClient.cs @@ -85,13 +85,17 @@ public async Task AddFile(GameRef game, int modId, UploadedFile upload, Fi content.Add(options.Name.ToContent(), "name"); content.Add(options.Version.ToContent(), "file-version"); content.Add(options.UpdateMainVersion ? 1.ToContent() : 0.ToContent(), "update-version"); - content.Add(options.UpdateMainVersion ? 1.ToContent() : 0.ToContent(), "set_as_main_nmm"); content.Add(1.ToContent(), "category"); if (options.PreviousFileId.HasValue) { content.Add(1.ToContent(), "new-existing"); content.Add(options.PreviousFileId.Value.ToContent(), "old_file_id"); } content.Add(options.Description.ToContent(), "brief-overview"); + content.Add(options.SetAsMainVortex != null + ? options.SetAsMainVortex.Value + ? 1.ToContent() + : 0.ToContent() + : options.UpdateMainVersion ? 1.ToContent() : 0.ToContent(), "set_as_main_nmm"); content.Add(upload.Id.ToContent(), "file_uuid"); content.Add(upload.FileSize.ToContent(), "file_size"); content.Add(modId.ToContent(), "mod_id"); diff --git a/src/NexusUploader/UploadCommand.cs b/src/NexusUploader/UploadCommand.cs index 15a9bcb..02593d3 100644 --- a/src/NexusUploader/UploadCommand.cs +++ b/src/NexusUploader/UploadCommand.cs @@ -47,6 +47,10 @@ public override async Task ExecuteAsync(CommandContext context, Settings se _logger.LogWarning("Skipping mod version update!"); fileOpts.UpdateMainVersion = false; } + if (settings.SetMainVortexFile.IsSet) { + _logger.LogInformation($"Setting file as main Vortex file: {settings.SetMainVortexFile.Value}"); + fileOpts.SetAsMainVortex = settings.SetMainVortexFile.Value; + } if (!IsConfigurationValid(settings) && !settings.AllowInteractive) { AnsiConsole.MarkupLine("[bold red]ERROR[/]: not all configuration is set correctly and unex is not running interactively. Exiting!"); return -1; @@ -128,6 +132,10 @@ public Settings(ModConfiguration config) [Description("Skips updating your mod's main version to match this file's version")] public FlagValue SkipMainVersionUpdate {get;set;} + [CommandOption("--set-main-vortex")] + [Description("Sets this file as the main Vortex file (for the Download with Manager buttons)")] + public FlagValue SetMainVortexFile {get;set;} + private bool IsSettingsValid() { return ModFilePath.IsSet() && FileVersion.IsSet()