From 708b7ebe6729731d8cffc6f3164d817325cbac70 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Tue, 18 Sep 2018 21:23:04 +0200 Subject: [PATCH] Validation fixes and improvements for @Aareksio --- ArchiSteamFarm/BotConfig.cs | 117 +++++++++--------- ArchiSteamFarm/GlobalConfig.cs | 63 +++++----- .../IPC/Controllers/Api/ASFController.cs | 5 + .../IPC/Controllers/Api/BotController.cs | 9 +- 4 files changed, 99 insertions(+), 95 deletions(-) diff --git a/ArchiSteamFarm/BotConfig.cs b/ArchiSteamFarm/BotConfig.cs index 63143e82b..af59ea6b8 100644 --- a/ArchiSteamFarm/BotConfig.cs +++ b/ArchiSteamFarm/BotConfig.cs @@ -58,7 +58,7 @@ namespace ArchiSteamFarm { private const ETradingPreferences DefaultTradingPreferences = ETradingPreferences.None; private const bool DefaultUseLoginKeys = true; - private static readonly ImmutableHashSet DefaultFarmingOrders = ImmutableHashSet.Empty; + private static readonly ImmutableList DefaultFarmingOrders = ImmutableList.Empty; private static readonly ImmutableHashSet DefaultGamesPlayedWhileIdle = ImmutableHashSet.Empty; private static readonly ImmutableHashSet DefaultLootableTypes = ImmutableHashSet.Create(Steam.Asset.EType.BoosterPack, Steam.Asset.EType.FoilTradingCard, Steam.Asset.EType.TradingCard); private static readonly ImmutableHashSet DefaultMatchableTypes = ImmutableHashSet.Create(Steam.Asset.EType.TradingCard); @@ -85,7 +85,7 @@ namespace ArchiSteamFarm { internal readonly bool Enabled = DefaultEnabled; [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, Required = Required.DisallowNull)] - internal readonly ImmutableHashSet FarmingOrders = DefaultFarmingOrders; + internal readonly ImmutableList FarmingOrders = DefaultFarmingOrders; [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, Required = Required.DisallowNull)] internal readonly ImmutableHashSet GamesPlayedWhileIdle = DefaultGamesPlayedWhileIdle; @@ -138,6 +138,56 @@ namespace ArchiSteamFarm { [JsonProperty(Required = Required.DisallowNull)] internal readonly bool UseLoginKeys = DefaultUseLoginKeys; + internal (bool Valid, string ErrorMessage) CheckValidation() { + if (BotBehaviour > EBotBehaviour.All) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(BotBehaviour), BotBehaviour)); + } + + foreach (EFarmingOrder farmingOrder in FarmingOrders) { + if (!Enum.IsDefined(typeof(EFarmingOrder), farmingOrder)) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(FarmingOrders), farmingOrder)); + } + } + + if (GamesPlayedWhileIdle.Count > ArchiHandler.MaxGamesPlayedConcurrently) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(GamesPlayedWhileIdle), GamesPlayedWhileIdle.Count + " > " + ArchiHandler.MaxGamesPlayedConcurrently)); + } + + foreach (Steam.Asset.EType lootableType in LootableTypes) { + if (!Enum.IsDefined(typeof(Steam.Asset.EType), lootableType)) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(LootableTypes), lootableType)); + } + } + + foreach (Steam.Asset.EType matchableType in MatchableTypes) { + if (!Enum.IsDefined(typeof(Steam.Asset.EType), matchableType)) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(MatchableTypes), matchableType)); + } + } + + if ((OnlineStatus < EPersonaState.Offline) || (OnlineStatus >= EPersonaState.Max)) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(OnlineStatus), OnlineStatus)); + } + + if (!Enum.IsDefined(typeof(ArchiCryptoHelper.ECryptoMethod), PasswordFormat)) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(PasswordFormat), PasswordFormat)); + } + + if (RedeemingPreferences > ERedeemingPreferences.All) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(RedeemingPreferences), RedeemingPreferences)); + } + + if ((SteamMasterClanID != 0) && !new SteamID(SteamMasterClanID).IsClanAccount) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(SteamMasterClanID), SteamMasterClanID)); + } + + foreach (EPermission permission in SteamUserPermissions.Values.Where(permission => !Enum.IsDefined(typeof(EPermission), permission))) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(SteamUserPermissions), permission)); + } + + return TradingPreferences <= ETradingPreferences.All ? (true, null) : (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(TradingPreferences), TradingPreferences)); + } + internal string OriginalSteamPassword { get { if (string.IsNullOrEmpty(SteamPassword)) { @@ -219,64 +269,9 @@ namespace ArchiSteamFarm { return null; } - if (botConfig.BotBehaviour > EBotBehaviour.All) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.BotBehaviour), botConfig.BotBehaviour)); - return null; - } - - foreach (EFarmingOrder farmingOrder in botConfig.FarmingOrders) { - if (!Enum.IsDefined(typeof(EFarmingOrder), farmingOrder)) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.FarmingOrders), farmingOrder)); - return null; - } - } - - if (botConfig.GamesPlayedWhileIdle.Count > ArchiHandler.MaxGamesPlayedConcurrently) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.GamesPlayedWhileIdle), botConfig.GamesPlayedWhileIdle.Count + " > " + ArchiHandler.MaxGamesPlayedConcurrently)); - return null; - } - - foreach (Steam.Asset.EType lootableType in botConfig.LootableTypes) { - if (!Enum.IsDefined(typeof(Steam.Asset.EType), lootableType)) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.LootableTypes), lootableType)); - return null; - } - } - - foreach (Steam.Asset.EType matchableType in botConfig.MatchableTypes) { - if (!Enum.IsDefined(typeof(Steam.Asset.EType), matchableType)) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.MatchableTypes), matchableType)); - return null; - } - } - - if ((botConfig.OnlineStatus < EPersonaState.Offline) || (botConfig.OnlineStatus >= EPersonaState.Max)) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.OnlineStatus), botConfig.OnlineStatus)); - return null; - } - - if (!Enum.IsDefined(typeof(ArchiCryptoHelper.ECryptoMethod), botConfig.PasswordFormat)) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.PasswordFormat), botConfig.PasswordFormat)); - return null; - } - - if (botConfig.RedeemingPreferences > ERedeemingPreferences.All) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.RedeemingPreferences), botConfig.RedeemingPreferences)); - return null; - } - - if ((botConfig.SteamMasterClanID != 0) && !new SteamID(botConfig.SteamMasterClanID).IsClanAccount) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.SteamMasterClanID), botConfig.SteamMasterClanID)); - return null; - } - - foreach (EPermission permission in botConfig.SteamUserPermissions.Values.Where(permission => !Enum.IsDefined(typeof(EPermission), permission))) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.SteamUserPermissions), permission)); - return null; - } - - if (botConfig.TradingPreferences > ETradingPreferences.All) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(botConfig.TradingPreferences), botConfig.TradingPreferences)); + (bool valid, string errorMessage) = botConfig.CheckValidation(); + if (!valid) { + ASF.ArchiLogger.LogGenericError(errorMessage); return null; } @@ -383,7 +378,7 @@ namespace ArchiSteamFarm { public bool ShouldSerializeCustomGamePlayedWhileFarming() => ShouldSerializeEverything || (CustomGamePlayedWhileFarming != DefaultCustomGamePlayedWhileFarming); public bool ShouldSerializeCustomGamePlayedWhileIdle() => ShouldSerializeEverything || (CustomGamePlayedWhileIdle != DefaultCustomGamePlayedWhileIdle); public bool ShouldSerializeEnabled() => ShouldSerializeEverything || (Enabled != DefaultEnabled); - public bool ShouldSerializeFarmingOrders() => ShouldSerializeEverything || ((FarmingOrders != DefaultFarmingOrders) && !FarmingOrders.SetEquals(DefaultFarmingOrders)); + public bool ShouldSerializeFarmingOrders() => ShouldSerializeEverything || ((FarmingOrders != DefaultFarmingOrders) && !FarmingOrders.SequenceEqual(DefaultFarmingOrders)); public bool ShouldSerializeGamesPlayedWhileIdle() => ShouldSerializeEverything || ((GamesPlayedWhileIdle != DefaultGamesPlayedWhileIdle) && !GamesPlayedWhileIdle.SetEquals(DefaultGamesPlayedWhileIdle)); public bool ShouldSerializeHoursUntilCardDrops() => ShouldSerializeEverything || (HoursUntilCardDrops != DefaultHoursUntilCardDrops); public bool ShouldSerializeIdlePriorityQueueOnly() => ShouldSerializeEverything || (IdlePriorityQueueOnly != DefaultIdlePriorityQueueOnly); diff --git a/ArchiSteamFarm/GlobalConfig.cs b/ArchiSteamFarm/GlobalConfig.cs index 4e594180c..671eeb423 100644 --- a/ArchiSteamFarm/GlobalConfig.cs +++ b/ArchiSteamFarm/GlobalConfig.cs @@ -210,6 +210,34 @@ namespace ArchiSteamFarm { } } + internal (bool Valid, string ErrorMessage) CheckValidation() { + if (ConnectionTimeout == 0) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(ConnectionTimeout), ConnectionTimeout)); + } + + if (FarmingDelay == 0) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(FarmingDelay), FarmingDelay)); + } + + if (MaxFarmingTime == 0) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(MaxFarmingTime), MaxFarmingTime)); + } + + if (!Enum.IsDefined(typeof(EOptimizationMode), OptimizationMode)) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(OptimizationMode), OptimizationMode)); + } + + if (!string.IsNullOrEmpty(SteamMessagePrefix) && (SteamMessagePrefix.Length > Bot.MaxMessagePrefixLength)) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(SteamMessagePrefix), SteamMessagePrefix)); + } + + if ((SteamProtocols <= 0) || (SteamProtocols > ProtocolTypes.All)) { + return (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(SteamProtocols), SteamProtocols)); + } + + return Enum.IsDefined(typeof(EUpdateChannel), UpdateChannel) ? (true, null) : (false, string.Format(Strings.ErrorConfigPropertyInvalid, nameof(UpdateChannel), UpdateChannel)); + } + internal static async Task Load(string filePath) { if (string.IsNullOrEmpty(filePath)) { ASF.ArchiLogger.LogNullError(nameof(filePath)); @@ -234,38 +262,9 @@ namespace ArchiSteamFarm { return null; } - if (globalConfig.ConnectionTimeout == 0) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.ConnectionTimeout), globalConfig.ConnectionTimeout)); - return null; - } - - if (globalConfig.FarmingDelay == 0) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.FarmingDelay), globalConfig.FarmingDelay)); - return null; - } - - if (globalConfig.MaxFarmingTime == 0) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.MaxFarmingTime), globalConfig.MaxFarmingTime)); - return null; - } - - if (!Enum.IsDefined(typeof(EOptimizationMode), globalConfig.OptimizationMode)) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.OptimizationMode), globalConfig.OptimizationMode)); - return null; - } - - if (!string.IsNullOrEmpty(globalConfig.SteamMessagePrefix) && (globalConfig.SteamMessagePrefix.Length > Bot.MaxMessagePrefixLength)) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.SteamMessagePrefix), globalConfig.SteamMessagePrefix)); - return null; - } - - if ((globalConfig.SteamProtocols <= 0) || (globalConfig.SteamProtocols > ProtocolTypes.All)) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.SteamProtocols), globalConfig.SteamProtocols)); - return null; - } - - if (!Enum.IsDefined(typeof(EUpdateChannel), globalConfig.UpdateChannel)) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.UpdateChannel), globalConfig.UpdateChannel)); + (bool valid, string errorMessage) = globalConfig.CheckValidation(); + if (!valid) { + ASF.ArchiLogger.LogGenericError(errorMessage); return null; } diff --git a/ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs b/ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs index 1c668e945..dab030cc2 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/ASFController.cs @@ -54,6 +54,11 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(request)))); } + (bool valid, string errorMessage) = request.GlobalConfig.CheckValidation(); + if (!valid) { + return BadRequest(new GenericResponse(false, errorMessage)); + } + if (request.KeepSensitiveDetails) { if (string.IsNullOrEmpty(request.GlobalConfig.WebProxyPassword) && !string.IsNullOrEmpty(Program.GlobalConfig.WebProxyPassword)) { request.GlobalConfig.WebProxyPassword = Program.GlobalConfig.WebProxyPassword; diff --git a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs index 955061ea9..799b23310 100644 --- a/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs +++ b/ArchiSteamFarm/IPC/Controllers/Api/BotController.cs @@ -75,8 +75,8 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { } HashSet bots = Bot.GetBots(botNames); - if ((bots == null) || (bots.Count == 0)) { - return BadRequest(new GenericResponse>(false, string.Format(Strings.BotNotFound, botNames))); + if (bots == null) { + return BadRequest(new GenericResponse>(false, string.Format(Strings.ErrorIsInvalid, nameof(bots)))); } return Ok(new GenericResponse>(bots)); @@ -89,6 +89,11 @@ namespace ArchiSteamFarm.IPC.Controllers.Api { return BadRequest(new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(botName) + " || " + nameof(request)))); } + (bool valid, string errorMessage) = request.BotConfig.CheckValidation(); + if (!valid) { + return BadRequest(new GenericResponse(false, errorMessage)); + } + if (request.KeepSensitiveDetails && Bot.Bots.TryGetValue(botName, out Bot bot)) { if (string.IsNullOrEmpty(request.BotConfig.SteamLogin) && !string.IsNullOrEmpty(bot.BotConfig.SteamLogin)) { request.BotConfig.SteamLogin = bot.BotConfig.SteamLogin;