From cc9f1d3b4a0cf866dee6bc0f93f84a0aef482bb5 Mon Sep 17 00:00:00 2001 From: Dixie J Date: Tue, 8 Mar 2022 01:41:57 +0800 Subject: [PATCH] PATCH v3.1.5 (Added better auth checking+Crash when saving while client is closed fix) --- ValorantCC/MainWindow.xaml.cs | 1 - ValorantCC/ValorantCC.csproj | 2 +- ValorantCC/src/BackgroundAuth.cs | 45 ++++++++++++++++++----------- ValorantCC/src/Processor.cs | 49 +++++++++++++++++++++++++++++--- ValorantCC/src/Utils.cs | 2 ++ 5 files changed, 77 insertions(+), 22 deletions(-) diff --git a/ValorantCC/MainWindow.xaml.cs b/ValorantCC/MainWindow.xaml.cs index d9b9880..2fb6fd2 100644 --- a/ValorantCC/MainWindow.xaml.cs +++ b/ValorantCC/MainWindow.xaml.cs @@ -57,7 +57,6 @@ private async void btnSave_Click(object sender, RoutedEventArgs e) if (await DataProcessor.SaveNewColor(SelectedColors, profiles.SelectedIndex, profiles.Text)) { - await DataProcessor.Construct(); profiles.Items.Refresh(); profiles.SelectedIndex = DataProcessor.CurrentProfile; Utilities.Utils.MessageText("Saved! Restart Valorant.", Brushes.Lime); diff --git a/ValorantCC/ValorantCC.csproj b/ValorantCC/ValorantCC.csproj index e7ed8f5..0702ca4 100644 --- a/ValorantCC/ValorantCC.csproj +++ b/ValorantCC/ValorantCC.csproj @@ -14,7 +14,7 @@ false - 3.1.4 + 3.1.5 true diff --git a/ValorantCC/src/BackgroundAuth.cs b/ValorantCC/src/BackgroundAuth.cs index 58797cc..451811f 100644 --- a/ValorantCC/src/BackgroundAuth.cs +++ b/ValorantCC/src/BackgroundAuth.cs @@ -5,13 +5,22 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Media; +using RestSharp; +using Newtonsoft.Json; namespace ValorantCC { + public partial class FlagObject + { + public string errorCode { get; init; } + public int httpStatus { get; init; } + public string message { get; init; } + } class BackgroundAuth { MainWindow main = (MainWindow)Application.Current.MainWindow; Processor processor; + LockfileData _lockfileData; public BackgroundAuth(Processor processor1) { processor = processor1; @@ -31,6 +40,7 @@ public async void LoopCheck() { main.StatusTxt.Foreground = Brushes.Yellow; main.StatusTxt.Text = "Waiting for session. . ."; + _lockfileData = AuthObj.ObtainLockfileData(LockfilePath); lockfilexists = true; } if (!await LoginFlagExists() && !main.PressedForceLogin) @@ -41,7 +51,7 @@ public async void LoopCheck() main.ForceLoginBtn.Visibility = Visibility.Visible; continue; } - if (lockfilexists && AuthObj.ObtainLockfileData(LockfilePath).Success) + if (lockfilexists && _lockfileData.Success) { main.StatusTxt.Text = "Logging in. . ."; AuthResponse AuthResponse = await processor.Login(); @@ -85,22 +95,25 @@ public async void LoopCheck() } } - public async static Task LoginFlagExists() + public async Task LoginFlagExists() { - DirectoryInfo LogDir = new DirectoryInfo(Environment.GetEnvironmentVariable("LocalAppData") + "\\Riot Games\\Riot Client\\Logs\\Riot Client Logs"); - var log = LogDir.GetFiles().OrderByDescending(f => f.LastWriteTime).First(); - - string content; - using (FileStream fileStream = File.Open(log.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - using (StreamReader sr = new StreamReader(fileStream)) - content = (String)sr.ReadToEnd().Clone(); - - bool t = false; - if (content.Contains("riot-messaging-service: State is now Connected")) - t = true; - - await Task.Delay(1); - return t; + RestClient wsClient = new RestClient(new RestClientOptions() { RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true }); + wsClient.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("riot",_lockfileData.Key); + RestRequest entitlementsReq = new RestRequest($"{_lockfileData.Protocol}://127.0.0.1:{_lockfileData.Port}/entitlements/v1/token"); + var resp = await wsClient.ExecuteAsync(entitlementsReq); + if (!resp.IsSuccessful) + { + try + { + var err = JsonConvert.DeserializeObject(resp.Content.ToString()); + Utilities.Utils.Log($"FETCH AUTH - {err.errorCode}: {err.message}"); + }catch (NullReferenceException) + { + Utilities.Utils.Log("User exited the client"); + } + return false; + } + return true; } } } diff --git a/ValorantCC/src/Processor.cs b/ValorantCC/src/Processor.cs index 0b6a3b2..f87a9d3 100644 --- a/ValorantCC/src/Processor.cs +++ b/ValorantCC/src/Processor.cs @@ -19,6 +19,7 @@ public class Processor public List ProfileNames; private ProfileList FetchedProfiles; public int CurrentProfile; + private Dictionary _headers; //Haruki's "bug" fix private Color[] DefaultColors = new Color[8]; @@ -49,6 +50,7 @@ public async Task Construct() { Utilities.Utils.Log("Constructing Properties -->"); client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("riot", AuthResponse.LockfileData.Key); + _headers = Utilities.Utils.ConstructHeaders(AuthResponse); UserSettings = await FetchUserSettings(); if (UserSettings.settingsProfiles == null) return false; @@ -135,9 +137,34 @@ private async Task FetchUserSettings() RestRequest request = new RestRequest($"{AuthResponse.LockfileData.Protocol}://127.0.0.1:{AuthResponse.LockfileData.Port}/player-preferences/v1/data-json/Ares.PlayerSettings", Method.Get); RestResponse resp = await client.ExecuteAsync(request); - if (!resp.IsSuccessful) return new Data(); - string responseContent = resp.Content; FetchResponseData response; + if (!resp.IsSuccessful) + { + try + { + Utilities.Utils.Log("Fetch User Settings failed for WS. Trying playerpref: "+resp.Content.ToString()); + } catch (NullReferenceException ex) + { + Utilities.Utils.Log("WS Failed to fetch settings error: " + ex.StackTrace.ToString()); + request = new RestRequest("https://playerpreferences.riotgames.com/playerPref/v3/getPreference/Ares.PlayerSettings", Method.Get); + request.AddHeaders(_headers); + resp = await (new RestClient().ExecuteAsync(request)); + if (!resp.IsSuccessful) return new Data(); + var responseData = JsonConvert.DeserializeObject>(resp.Content); + Data settings; + try + { + settings = Utilities.Utils.Decompress(Convert.ToString(responseData["data"])); + }catch (KeyNotFoundException) + { + Utilities.Utils.Log("Player pref failed to fetch settings"); + return new Data(); + } + return settings; + + } + } + string responseContent = resp.Content; try { response = JsonConvert.DeserializeObject(responseContent); @@ -159,8 +186,22 @@ private async Task putUserSettings(Data newData) RestResponse response = await client.ExecuteAsync(request); if (!response.IsSuccessful) { - Utilities.Utils.Log("savePreference Unsuccessfull: " + response.Content.ToString()); - return false; + try + { + Utilities.Utils.Log("savePreference Unsuccessfull: " + response.Content.ToString()); + return false; + } catch (NullReferenceException) + { + request = new RestRequest("https://playerpreferences.riotgames.com/playerPref/v3/savePreference", Method.Put); + request.AddJsonBody(new { type = "Ares.PlayerSettings", data = Utilities.Utils.Compress(newData) }); + request.AddHeaders(_headers); + response = await (new RestClient().ExecuteAsync(request)); + if (!response.IsSuccessful) + { + Utilities.Utils.Log("savePreference Unsuccessfull: " + response.Content.ToString()); + return false; + } + } } Dictionary responseDict = JsonConvert.DeserializeObject>(response.Content); diff --git a/ValorantCC/src/Utils.cs b/ValorantCC/src/Utils.cs index ba729a6..680adf5 100644 --- a/ValorantCC/src/Utils.cs +++ b/ValorantCC/src/Utils.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.IO.Compression; using System.Linq; @@ -97,6 +98,7 @@ public static Dictionary ConstructHeaders(AuthResponse auth) public static void Log(string Text) { StringBuilder.Append($"{DateTimeOffset.Now} | {Text}\n"); + Trace.WriteLine(StringBuilder.ToString()); File.AppendAllText(LoggingFile, StringBuilder.ToString()); StringBuilder.Clear(); }