DataTransfer/DataTransfer.Client/Services/BotCheckSolver.cs
2026-10-15 20:09:11 +02:00

21 lines
961 B
C#

using DataTransfer.Client.Shared;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
namespace DataTransfer.Client.Services;
/// <summary>Solves the server's proof-of-work bot check in web workers (<c>wwwroot/js/bot-check.js</c> of the server).</summary>
public sealed class BotCheckSolver(IJSRuntime js, NavigationManager navigation)
{
/// <returns>The solution, or <c>-1</c> if none was found.</returns>
public async Task<int> SolveAsync(BotCheckChallenge challenge)
{
ArgumentNullException.ThrowIfNull(challenge);
// An absolute URL, so the module resolves relative to the app's base URI under any path base.
await using var module = await js.InvokeAsync<IJSObjectReference>(
"import",
navigation.ToAbsoluteUri("js/bot-check.js").ToString());
return await module.InvokeAsync<int>("solve", challenge.Salt, challenge.Hash, challenge.MaxNumber);
}
}