From 1df2c3b042aee8436b9830c0f2cd070e0cc04329 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Fri, 16 Sep 2016 23:40:52 +0200 Subject: [PATCH] Implement my previous idea Thanks to that, we can guarantee some room for networking, but also make users more happy as they'll never get 2FA tokens no human is capable of entering in time --- ArchiSteamFarm/ArchiWebHandler.cs | 2 +- ArchiSteamFarm/Bot.cs | 8 ++++++-- ArchiSteamFarm/MobileAuthenticator.cs | 27 ++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/ArchiSteamFarm/ArchiWebHandler.cs b/ArchiSteamFarm/ArchiWebHandler.cs index a75ed39c2..d8f0c0c61 100644 --- a/ArchiSteamFarm/ArchiWebHandler.cs +++ b/ArchiSteamFarm/ArchiWebHandler.cs @@ -323,7 +323,7 @@ namespace ArchiSteamFarm { string request = SteamCommunityURL + "/mobileconf/details/" + confirmation.ID + "?l=english&p=" + deviceID + "&a=" + SteamID + "&k=" + WebUtility.UrlEncode(confirmationHash) + "&t=" + time + "&m=android&tag=conf"; Steam.ConfirmationDetails response = await WebBrowser.UrlGetToJsonResultRetry(request).ConfigureAwait(false); - if (response == null) { + if ((response == null) || !response.Success) { return null; } diff --git a/ArchiSteamFarm/Bot.cs b/ArchiSteamFarm/Bot.cs index 5d9558110..593d29096 100755 --- a/ArchiSteamFarm/Bot.cs +++ b/ArchiSteamFarm/Bot.cs @@ -871,8 +871,12 @@ namespace ArchiSteamFarm { return "That bot doesn't have ASF 2FA enabled!"; } - byte timeLeft = (byte) (30 - await BotDatabase.MobileAuthenticator.GetSteamTime().ConfigureAwait(false) % 30); - return "2FA Token: " + await BotDatabase.MobileAuthenticator.GenerateToken().ConfigureAwait(false) + " (expires in " + timeLeft + " seconds)"; + Tuple tokenTimePair = await BotDatabase.MobileAuthenticator.GenerateTokenTimePair().ConfigureAwait(false); + if (tokenTimePair == null) { + return "Error!"; + } + + return "2FA Token: " + tokenTimePair.Item1 + " (expires in " + tokenTimePair.Item2 + " seconds)"; } private static async Task Response2FA(ulong steamID, string botName) { diff --git a/ArchiSteamFarm/MobileAuthenticator.cs b/ArchiSteamFarm/MobileAuthenticator.cs index 576f0c366..fd7e017ec 100644 --- a/ArchiSteamFarm/MobileAuthenticator.cs +++ b/ArchiSteamFarm/MobileAuthenticator.cs @@ -55,6 +55,7 @@ namespace ArchiSteamFarm { private const byte CodeDigits = 5; private const byte CodeInterval = 30; + private const byte MinimumTimeLeft = 5; private static readonly char[] CodeCharacters = { '2', '3', '4', '5', '6', '7', '8', '9', 'B', 'C', @@ -186,15 +187,31 @@ namespace ArchiSteamFarm { } internal async Task GenerateToken() { - uint time = await GetSteamTime().ConfigureAwait(false); - if (time != 0) { - return GenerateTokenForTime(time); + Tuple tokenTimePair = await GenerateTokenTimePair().ConfigureAwait(false); + if (tokenTimePair != null) { + return tokenTimePair.Item1; } - Logging.LogNullError(nameof(time), Bot.BotName); + Logging.LogNullError(nameof(tokenTimePair), Bot.BotName); return null; } + internal async Task> GenerateTokenTimePair() { + uint time = await GetSteamTime().ConfigureAwait(false); + if (time == 0) { + Logging.LogNullError(nameof(time), Bot.BotName); + return null; + } + + byte timeLeft = (byte) (CodeInterval - time % CodeInterval); + if (timeLeft >= MinimumTimeLeft) { + return new Tuple(GenerateTokenForTime(time), timeLeft); + } + + await Task.Delay(timeLeft * 1000).ConfigureAwait(false); + return new Tuple(GenerateTokenForTime(time + timeLeft), CodeInterval); + } + internal async Task> GetConfirmations() { if (!HasCorrectDeviceID) { Logging.LogGenericWarning("Can't execute properly due to invalid DeviceID!", Bot.BotName); @@ -269,7 +286,7 @@ namespace ArchiSteamFarm { return result; } - internal async Task GetSteamTime() { + private async Task GetSteamTime() { if (SteamTimeDifference.HasValue) { return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.GetValueOrDefault()); }