Use shared Random across ASF

This also removes PublicAPI of ASF's "shared random"
This commit is contained in:
Archi
2021-11-11 19:34:21 +01:00
parent 951d9dc99f
commit 260875da7e
3 changed files with 9 additions and 32 deletions

View File

@@ -132,7 +132,9 @@ internal sealed class SteamTokenDumperPlugin : OfficialPlugin, IASF, IBot, IBotS
}
}
TimeSpan startIn = TimeSpan.FromMinutes(Utilities.RandomNext(SharedInfo.MinimumMinutesBeforeFirstUpload, SharedInfo.MaximumMinutesBeforeFirstUpload));
#pragma warning disable CA5394 // This call isn't used in a security-sensitive manner
TimeSpan startIn = TimeSpan.FromMinutes(Random.Shared.Next(SharedInfo.MinimumMinutesBeforeFirstUpload, SharedInfo.MaximumMinutesBeforeFirstUpload));
#pragma warning restore CA5394 // This call isn't used in a security-sensitive manner
// ReSharper disable once SuspiciousLockOverSynchronizationPrimitive - this is not a mistake, we need extra synchronization, and we can re-use the semaphore object for that
lock (SubmissionSemaphore) {
@@ -517,7 +519,9 @@ internal sealed class SteamTokenDumperPlugin : OfficialPlugin, IASF, IBot, IBotS
#else
if (response.StatusCode == HttpStatusCode.TooManyRequests) {
#endif
TimeSpan startIn = TimeSpan.FromMinutes(Utilities.RandomNext(SharedInfo.MinimumMinutesBeforeFirstUpload, SharedInfo.MaximumMinutesBeforeFirstUpload));
#pragma warning disable CA5394 // This call isn't used in a security-sensitive manner
TimeSpan startIn = TimeSpan.FromMinutes(Random.Shared.Next(SharedInfo.MinimumMinutesBeforeFirstUpload, SharedInfo.MaximumMinutesBeforeFirstUpload));
#pragma warning restore CA5394 // This call isn't used in a security-sensitive manner
// ReSharper disable once SuspiciousLockOverSynchronizationPrimitive - this is not a mistake, we need extra synchronization, and we can re-use the semaphore object for that
lock (SubmissionSemaphore) {

View File

@@ -49,9 +49,6 @@ public static class Utilities {
// normally we'd just use words like "steam" and "farm", but the library we're currently using is a bit iffy about banned words, so we need to also add combinations such as "steamfarm"
private static readonly ImmutableHashSet<string> ForbiddenPasswordPhrases = ImmutableHashSet.Create(StringComparer.InvariantCultureIgnoreCase, "archisteamfarm", "archi", "steam", "farm", "archisteam", "archifarm", "steamfarm", "asf", "asffarm", "password");
// Normally we wouldn't need to use this singleton, but we want to ensure decent randomness across entire program's lifetime
private static readonly Random Random = new();
[PublicAPI]
public static string GetArgsAsText(string[] args, byte argsToSkip, string delimiter) {
if (args == null) {
@@ -209,32 +206,6 @@ public static class Utilities {
return (text.Length % 2 == 0) && text.All(Uri.IsHexDigit);
}
[PublicAPI]
public static int RandomNext() {
lock (Random) {
#pragma warning disable CA5394 // This call isn't used in a security-sensitive manner
return Random.Next();
#pragma warning restore CA5394 // This call isn't used in a security-sensitive manner
}
}
[PublicAPI]
public static int RandomNext(int minValue, int maxValue) {
if (minValue > maxValue) {
throw new InvalidOperationException($"{nameof(minValue)} && {nameof(maxValue)}");
}
if (minValue >= maxValue - 1) {
return minValue;
}
lock (Random) {
#pragma warning disable CA5394 // This call isn't used in a security-sensitive manner
return Random.Next(minValue, maxValue);
#pragma warning restore CA5394 // This call isn't used in a security-sensitive manner
}
}
[PublicAPI]
public static IEnumerable<IElement> SelectNodes(this IDocument document, string xpath) {
if (document == null) {

View File

@@ -1180,7 +1180,9 @@ public sealed class CardsFarmer : IAsyncDisposable {
break;
case BotConfig.EFarmingOrder.Random:
orderedGamesToFarm = orderedGamesToFarm.ThenBy(static _ => Utilities.RandomNext());
#pragma warning disable CA5394 // This call isn't used in a security-sensitive manner
orderedGamesToFarm = orderedGamesToFarm.ThenBy(static _ => Random.Shared.Next());
#pragma warning restore CA5394 // This call isn't used in a security-sensitive manner
break;
case BotConfig.EFarmingOrder.RedeemDateTimesAscending: