Skip to content

Commit

Permalink
Add support for providing single sid cookie;
Browse files Browse the repository at this point in the history
Allow skipping setting file as Vortex default;
  • Loading branch information
agc93 committed Jul 18, 2021
1 parent 2c795be commit 7d01743
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/NexusUploader/FileOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
29 changes: 0 additions & 29 deletions src/NexusUploader/Http/MessageHandlerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,33 +49,4 @@ private CookieContainer GetCookies()
return c;
}
}

public class NexusCookieHandler : HttpClientHandler
{
private readonly CookieService _cookies;
private readonly ILogger<NexusCookieHandler> _logger;

public NexusCookieHandler(CookieService cookieService, ILogger<NexusCookieHandler> 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;
}
}
}
35 changes: 35 additions & 0 deletions src/NexusUploader/Http/NexusCookieHandler.cs
Original file line number Diff line number Diff line change
@@ -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<NexusCookieHandler> _logger;

public NexusCookieHandler(CookieService cookieService, ILogger<NexusCookieHandler> 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;
}
}
}
19 changes: 8 additions & 11 deletions src/NexusUploader/Services/CookieService.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -13,24 +15,19 @@ 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<string, string> GetCookies() {

if (Path.HasExtension(_config.Cookies) && File.Exists(_config.Cookies)) {
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<string, string> {["sid"] = Uri.EscapeDataString(raw)};
} else {
if (_config.Cookies.Contains('\n')) {
var ckSet = ParseCookiesTxt(_config.Cookies.Split('\n'));
Expand Down
6 changes: 5 additions & 1 deletion src/NexusUploader/Services/ManageClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,17 @@ public async Task<bool> 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");
Expand Down
8 changes: 8 additions & 0 deletions src/NexusUploader/UploadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public override async Task<int> 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;
Expand Down Expand Up @@ -128,6 +132,10 @@ public Settings(ModConfiguration config)
[Description("Skips updating your mod's main version to match this file's version")]
public FlagValue<bool> SkipMainVersionUpdate {get;set;}

[CommandOption("--set-main-vortex")]
[Description("Sets this file as the main Vortex file (for the Download with Manager buttons)")]
public FlagValue<bool> SetMainVortexFile {get;set;}

private bool IsSettingsValid() {
return ModFilePath.IsSet()
&& FileVersion.IsSet()
Expand Down

0 comments on commit 7d01743

Please sign in to comment.