From 1ec090eca4d12bc36dc6f0d180d19e075082ed42 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Mon, 7 Jun 2021 18:37:45 +0200 Subject: [PATCH] Add TestFingerprintCommand --- .../Cli/Commands/FingerprintCommands.cs | 69 ++++++++++++++++++- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/Akari.Prototype.Server/Cli/Commands/FingerprintCommands.cs b/Akari.Prototype.Server/Cli/Commands/FingerprintCommands.cs index 58c92e9..5dfda21 100644 --- a/Akari.Prototype.Server/Cli/Commands/FingerprintCommands.cs +++ b/Akari.Prototype.Server/Cli/Commands/FingerprintCommands.cs @@ -1,4 +1,5 @@ -using Akari.Prototype.Server.Services; +using Akari.Prototype.Server.Options; +using Akari.Prototype.Server.Services; using Akari.Prototype.Server.Utils; using Akari.Prototype.Server.Utils.Extensions; using CliFx; @@ -6,12 +7,17 @@ using CliFx.Attributes; using CliFx.Exceptions; using CliFx.Extensibility; using CliFx.Infrastructure; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Spectre.Console; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace Akari.Prototype.Server.Cli.Commands @@ -46,6 +52,11 @@ namespace Akari.Prototype.Server.Cli.Commands throw new CommandException("Name can't be more than 200 bytes (UTF-8)"); } + if (Name.Contains('$')) + { + throw new CommandException("Name can't contain a '$'"); + } + if (_fingerprintManager.Contains(Name)) { throw new CommandException("A fingerprint with this name is already registered."); @@ -88,12 +99,66 @@ namespace Akari.Prototype.Server.Cli.Commands throw new CommandException($"The fingerprint {Name} doesn't exist"); } - console.AsAnsiConsole().Markup($"[green]Successfully deleted {Name}[/]"); + console.AsAnsiConsole().MarkupLine($"[green]Successfully deleted {Name}[/]"); return default; } } + [Command("fp t", Description = "Test a fingerprint")] + public class TestFingerprintCommand : ICommand + { + public const int AuthTimeout = 5_000; + + [CommandParameter(0, Description = "The fingerprint name")] + public string Name { get; init; } + + private readonly IServiceProvider _serviceProvider; + private readonly IAuthManager _authManager; + private readonly IFingerprintManager _fingerprintManager; + + public TestFingerprintCommand(IServiceProvider serviceProvider, IAuthManager authManager, IFingerprintManager fingerprintManager) + { + _serviceProvider = serviceProvider; + _authManager = authManager; + _fingerprintManager = fingerprintManager; + } + + public async ValueTask ExecuteAsync(IConsole console) + { + var ansiConsole = console.AsAnsiConsole(); + + if (!_fingerprintManager.Contains(Name)) + { + throw new CommandException("This fingerprint does not exist"); + } + + using var scope = _serviceProvider.CreateScope(); + + var services = scope.ServiceProvider; + + using var provider = new TcpProviderService(services.GetRequiredService>(), + services.GetRequiredService>(), + services.GetRequiredService(), + services.GetRequiredService()); + + var source = new CancellationTokenSource(AuthTimeout); + + await provider.StartAsync(source.Token); + + ansiConsole.MarkupLine($"Waiting for [yellow]{Name}[/] auth"); + + await Task.Delay(AuthTimeout); + + if (!_authManager.TryGetKey(Name, out _)) + { + throw new CommandException("Timed out while waiting for fingerprint auth"); + } + + ansiConsole.MarkupLine($"[green]Successfully retrieved '{Name}' key[/]"); + } + } + public FingerprintCommands(IFingerprintManager fingerprintManager) { _fingerprintManager = fingerprintManager;