Handle system clock updates

In very rare scenario of starting from time X, calculating SteamTimeDifference of Y, then changing time from X to Z, old Y difference might no longer be accurate, which could lead to temporary failure of generating tokens/confirmations for given time.
Steam accepts tokens for quite a while (15 minutes from their time IIRC), so this would only hit us if we started from really huge time gap X, and not just normal typical small max 1-2 minutes updates.
This commit is contained in:
JustArchi 2017-02-13 20:53:06 +01:00
parent fe3f72594d
commit 56983c0470
4 changed files with 29 additions and 15 deletions

View file

@ -32,6 +32,7 @@ using System.Threading;
using System.Threading.Tasks;
using ArchiSteamFarm.JSON;
using ArchiSteamFarm.Localization;
using Microsoft.Win32;
namespace ArchiSteamFarm {
internal static class ASF {
@ -227,7 +228,7 @@ namespace ArchiSteamFarm {
}
}
internal static void InitFileWatcher() {
internal static void InitEvents() {
if (FileSystemWatcher != null) {
return;
}
@ -242,6 +243,8 @@ namespace ArchiSteamFarm {
FileSystemWatcher.Renamed += OnRenamed;
FileSystemWatcher.EnableRaisingEvents = true;
SystemEvents.TimeChanged += OnTimeChanged;
}
private static async Task CreateBot(string botName) {
@ -383,6 +386,8 @@ namespace ArchiSteamFarm {
CreateBot(newBotName).Forget();
}
private static async void OnTimeChanged(object sender, EventArgs e) => await MobileAuthenticator.OnTimeChanged().ConfigureAwait(false);
private static async Task RestartOrExit() {
if (Program.GlobalConfig.AutoRestart) {
ArchiLogger.LogGenericInfo(Strings.Restarting);

View file

@ -44,7 +44,7 @@ namespace ArchiSteamFarm {
private static readonly char[] CodeCharacters = { '2', '3', '4', '5', '6', '7', '8', '9', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'T', 'V', 'W', 'X', 'Y' };
private static readonly SemaphoreSlim TimeSemaphore = new SemaphoreSlim(1);
private static short? SteamTimeDifference;
private static int? SteamTimeDifference;
// "ERROR" is being used by SteamDesktopAuthenticator
internal bool HasCorrectDeviceID => !string.IsNullOrEmpty(DeviceID) && !DeviceID.Equals("ERROR");
@ -256,6 +256,16 @@ namespace ArchiSteamFarm {
Bot = bot;
}
internal static async Task OnTimeChanged() {
await TimeSemaphore.WaitAsync().ConfigureAwait(false);
try {
SteamTimeDifference = null;
} finally {
TimeSemaphore.Release();
}
}
private string GenerateConfirmationKey(uint time, string tag = null) {
if (time == 0) {
Bot.ArchiLogger.LogNullError(nameof(time));
@ -349,24 +359,23 @@ namespace ArchiSteamFarm {
}
private async Task<uint> GetSteamTime() {
if (SteamTimeDifference.HasValue) {
return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.GetValueOrDefault());
}
await TimeSemaphore.WaitAsync().ConfigureAwait(false);
try {
if (!SteamTimeDifference.HasValue) {
uint serverTime = Bot.ArchiWebHandler.GetServerTime();
if (serverTime != 0) {
SteamTimeDifference = (short) (serverTime - Utilities.GetUnixTime());
}
if (SteamTimeDifference.HasValue) {
return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.Value);
}
uint serverTime = Bot.ArchiWebHandler.GetServerTime();
if (serverTime == 0) {
return Utilities.GetUnixTime();
}
SteamTimeDifference = (int) (serverTime - Utilities.GetUnixTime());
return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.Value);
} finally {
TimeSemaphore.Release();
}
return (uint) (Utilities.GetUnixTime() + SteamTimeDifference.GetValueOrDefault());
}
internal sealed class Confirmation {

View file

@ -181,7 +181,7 @@ namespace ArchiSteamFarm {
await ASF.CheckForUpdate().ConfigureAwait(false);
await ASF.InitBots().ConfigureAwait(false);
ASF.InitFileWatcher();
ASF.InitEvents();
}
private static void InitCore(string[] args) {

View file

@ -61,7 +61,7 @@ namespace ArchiSteamFarm {
await ASF.CheckForUpdate().ConfigureAwait(false);
await ASF.InitBots().ConfigureAwait(false);
ASF.InitFileWatcher();
ASF.InitEvents();
}
internal static void InitCore() {