From d6f90f135eb0856036f678d969eade3de8944148 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 5 Mar 2018 11:47:39 +0100 Subject: [PATCH] Further improve previous fix --- ArchiSteamFarm/ArchiWebHandler.cs | 60 ++++++++++++++----------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index 2cb950e97..172901ab3 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -1353,12 +1353,12 @@ namespace ArchiSteamFarm { Bot.ArchiLogger.LogGenericInfo(Strings.UnlockingParentalAccount); - if (!await UnlockParentalCommunityAccount(parentalPin).ConfigureAwait(false)) { + if (!await UnlockParentalAccountForService(SteamCommunityURL, parentalPin).ConfigureAwait(false)) { Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); return false; } - if (!await UnlockParentalStoreAccount(parentalPin).ConfigureAwait(false)) { + if (!await UnlockParentalAccountForService(SteamStoreURL, parentalPin).ConfigureAwait(false)) { Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); return false; } @@ -1367,32 +1367,28 @@ namespace ArchiSteamFarm { return true; } - private async Task UnlockParentalCommunityAccount(string parentalPin) { - if (string.IsNullOrEmpty(parentalPin)) { - Bot.ArchiLogger.LogNullError(nameof(parentalPin)); + private async Task UnlockParentalAccountForService(string serviceURL, string parentalPin) { + if (string.IsNullOrEmpty(serviceURL) || string.IsNullOrEmpty(parentalPin)) { + Bot.ArchiLogger.LogNullError(nameof(serviceURL) + " || " + nameof(parentalPin)); return false; } + // This request doesn't go through UrlPostRetryWithSession as we have no access to session refresh capability (this is in fact session initialization) const string request = "/parental/ajaxunlock"; - // Extra entry for sessionID - Dictionary data = new Dictionary(2) { { "pin", parentalPin } }; - - return await UrlPostRetryWithSession(SteamCommunityURL, request, data, SteamCommunityURL, waitForInitialization: false).ConfigureAwait(false); - } - - private async Task UnlockParentalStoreAccount(string parentalPin) { - if (string.IsNullOrEmpty(parentalPin)) { - Bot.ArchiLogger.LogNullError(nameof(parentalPin)); + string sessionID = WebBrowser.CookieContainer.GetCookieValue(serviceURL, "sessionid"); + if (string.IsNullOrEmpty(sessionID)) { + Bot.ArchiLogger.LogNullError(nameof(sessionID)); return false; } - const string request = "/parental/ajaxunlock"; + Dictionary data = new Dictionary(2) { + { "pin", parentalPin }, + { "sessionid", sessionID } + }; - // Extra entry for sessionID - Dictionary data = new Dictionary(2) { { "pin", parentalPin } }; - - return await UrlPostRetryWithSession(SteamStoreURL, request, data, SteamStoreURL, waitForInitialization: false).ConfigureAwait(false); + WebBrowser.BasicResponse response = await WebBrowser.UrlPostRetry(serviceURL + request, data, serviceURL).ConfigureAwait(false); + return (response != null) && !IsSessionExpiredUri(response.FinalUri); } private async Task UrlGetToHtmlDocumentRetryWithSession(string host, string request, byte maxTries = WebBrowser.MaxTries) { @@ -1571,7 +1567,7 @@ namespace ArchiSteamFarm { return false; } - private async Task UrlPostRetryWithSession(string host, string request, Dictionary data = null, string referer = null, bool includeSessionInData = true, bool waitForInitialization = true, byte maxTries = WebBrowser.MaxTries) { + private async Task UrlPostRetryWithSession(string host, string request, Dictionary data = null, string referer = null, bool includeSessionInData = true, byte maxTries = WebBrowser.MaxTries) { if (string.IsNullOrEmpty(host) || string.IsNullOrEmpty(request)) { Bot.ArchiLogger.LogNullError(nameof(host) + " || " + nameof(request)); return false; @@ -1583,20 +1579,18 @@ namespace ArchiSteamFarm { return false; } - if (waitForInitialization) { - // If session refresh is already in progress, wait for it - await SessionSemaphore.WaitAsync().ConfigureAwait(false); - SessionSemaphore.Release(); + // If session refresh is already in progress, wait for it + await SessionSemaphore.WaitAsync().ConfigureAwait(false); + SessionSemaphore.Release(); - for (byte i = 0; (i < Program.GlobalConfig.ConnectionTimeout) && (SteamID == 0) && Bot.IsConnectedAndLoggedOn; i++) { - await Task.Delay(1000).ConfigureAwait(false); - } + for (byte i = 0; (i < Program.GlobalConfig.ConnectionTimeout) && (SteamID == 0) && Bot.IsConnectedAndLoggedOn; i++) { + await Task.Delay(1000).ConfigureAwait(false); + } - if (SteamID == 0) { - Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); - Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); - return false; - } + if (SteamID == 0) { + Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed); + Bot.ArchiLogger.LogGenericDebug(string.Format(Strings.ErrorFailingRequest, host + request)); + return false; } if (includeSessionInData) { @@ -1624,7 +1618,7 @@ namespace ArchiSteamFarm { } if (await RefreshSession(host).ConfigureAwait(false)) { - return await UrlPostRetryWithSession(host, request, data, referer, includeSessionInData, waitForInitialization, --maxTries).ConfigureAwait(false); + return await UrlPostRetryWithSession(host, request, data, referer, includeSessionInData, --maxTries).ConfigureAwait(false); } Bot.ArchiLogger.LogGenericWarning(Strings.WarningFailed);