From d3e913daa5ec565ed6fd7092e5136672f62287d8 Mon Sep 17 00:00:00 2001 From: Andrii Lavrenko Date: Thu, 30 May 2024 21:13:50 +0300 Subject: [PATCH] Remove WebClient --- .../SteamDataClient/Client.cs | 7 +- .../SteamDataClient/SteamWebClient.cs | 86 ------------------- 2 files changed, 1 insertion(+), 92 deletions(-) delete mode 100644 SteamAccountDataFetcher/SteamDataClient/SteamWebClient.cs diff --git a/SteamAccountDataFetcher/SteamDataClient/Client.cs b/SteamAccountDataFetcher/SteamDataClient/Client.cs index e59746d..dcf69f6 100644 --- a/SteamAccountDataFetcher/SteamDataClient/Client.cs +++ b/SteamAccountDataFetcher/SteamDataClient/Client.cs @@ -4,11 +4,10 @@ namespace SteamAccountDataFetcher.SteamDataClient; -public class Client : IDisposable +public class Client { SteamClient _steamClient; AutoTwoFactorAuthenticator _autoTwoFactorAuthenticator; - SteamWebClient _steamWebClient; CallbackManager _callbackManager; SteamUser _steamUser; SteamApps _steamApps; @@ -83,7 +82,6 @@ internal Client(string username, string password, string sharedSecret) _callbackManager.Subscribe(OnIsLimitedAccount); _autoTwoFactorAuthenticator = new(this, sharedSecret); - _steamWebClient = new(this); } internal static void LoadPackagesCache(List packagesInfo) => _packagesInfo = packagesInfo; @@ -211,7 +209,6 @@ void OnLoggedOn(SteamUser.LoggedOnCallback callback) } _responseAccountInfo.SteamId = callback.ClientSteamID.ConvertToUInt64(); Log("Logged into Steam."); - _steamWebClient.InitAsync(); } void OnIsLimitedAccount(DataFetcher.IsLimitedAccountCallback callback) @@ -348,8 +345,6 @@ async Task WaitOrProceed() } } - public void Dispose() => _steamWebClient?.Dispose(); - internal void Log(string message, Logger.Level level = Logger.Level.Info, [CallerMemberName] string callerName = "") => Logger.Log($"{_instance},{Username.ToLower()} - {message}", level, callerName); } diff --git a/SteamAccountDataFetcher/SteamDataClient/SteamWebClient.cs b/SteamAccountDataFetcher/SteamDataClient/SteamWebClient.cs deleted file mode 100644 index 7c3bea6..0000000 --- a/SteamAccountDataFetcher/SteamDataClient/SteamWebClient.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System.Net; -using System.Text.RegularExpressions; -using System.Web; - -namespace SteamAccountDataFetcher.SteamDataClient; - -class SteamWebClient: IDisposable -{ - static readonly Uri API_KEY_URI = new("https://steamcommunity.com/dev/apikey"); - static readonly Uri REGISTER_API_KEY_URI = new("https://steamcommunity.com/dev/registerkey"); - const string API_GROUP_KEY = "apiKey"; - static readonly Regex API_KEY_EXPRESSION = new($@"

.*:*.(?'{API_GROUP_KEY}'[0-9A-F]{{32}})

", RegexOptions.Compiled); - - HttpClient? _httpClient; - Client _steamClient; - - internal string SessionID { get; private set; } = string.Empty; - - static bool _isFirstRequestBefore = false; - - internal SteamWebClient(Client steamClient) - { - _steamClient = steamClient; - } - - internal void InitAsync() - { - if (_steamClient.SteamID == null || !_steamClient.SteamID.IsValid || !_steamClient.SteamID.IsIndividualAccount) - { - var msg = $"{nameof(_steamClient.SteamID)} is invalid."; - _steamClient.Log(msg, Logger.Level.Error); - throw new InvalidOperationException(msg); - } - - if (string.IsNullOrEmpty(_steamClient.AccessToken)) - { - var msg = $"{nameof(_steamClient.AccessToken)} is empty."; - _steamClient.Log(msg, Logger.Level.Error); - throw new InvalidOperationException(msg); - } - - string steamLoginSecure = HttpUtility.UrlEncode($"{_steamClient.SteamID.ConvertToUInt64()}||{_steamClient.AccessToken}"); - - Random rnd = new Random(); - byte[] sessionBytes = new byte[12]; - rnd.NextBytes(sessionBytes); - SessionID = Convert.ToHexString(sessionBytes); - - string timeZoneOffset = $"{(int)DateTimeOffset.Now.Offset.TotalSeconds}{Uri.EscapeDataString(",")}0"; - CookieCollection cookieCollection = new() - { - new Cookie("sessionid", SessionID, "/", ".checkout.steampowered.com"), - new Cookie("sessionid", SessionID, "/", ".steamcommunity.com"), - new Cookie("sessionid", SessionID, "/", ".help.steampowered.com"), - new Cookie("sessionid", SessionID, "/", ".store.steampowered.com"), - new Cookie("steamLoginSecure", steamLoginSecure, "/", ".checkout.steampowered.com"), - new Cookie("steamLoginSecure", steamLoginSecure, "/", ".steamcommunity.com"), - new Cookie("steamLoginSecure", steamLoginSecure, "/", ".help.steampowered.com"), - new Cookie("steamLoginSecure", steamLoginSecure, "/", ".store.steampowered.com"), - new Cookie("timezoneOffset", timeZoneOffset, "/", ".checkout.steampowered.com"), - new Cookie("timezoneOffset", timeZoneOffset, "/", ".steamcommunity.com"), - new Cookie("timezoneOffset", timeZoneOffset, "/", ".help.steampowered.com"), - new Cookie("timezoneOffset", timeZoneOffset, "/", ".store.steampowered.com"), - }; - HttpClientHandler httpClientHandler = new(); - httpClientHandler.UseCookies = true; - httpClientHandler.CookieContainer.Add(cookieCollection); - - _httpClient = new HttpClient(httpClientHandler, true); - - return; - } - - async Task RunOrSleep() - { - if (_isFirstRequestBefore) - { - await Task.Delay(Configuration.DefaultWebRequestTimeout); - return; - } - _isFirstRequestBefore = true; - } - - public void Dispose() => - _httpClient?.Dispose(); -}