Removed old manual access token checking and switched to asp.net jwt handling. Removed old console subscriber handling and switched to full signal r solution + asp.net core auth

This commit is contained in:
2025-04-13 00:09:06 +02:00
parent ec0c336825
commit 36cbc83c63
15 changed files with 181 additions and 380 deletions

View File

@@ -1,5 +1,6 @@
using System.Text.RegularExpressions;
using Docker.DotNet.Models;
using Microsoft.AspNetCore.SignalR;
using MoonlightServers.Daemon.Enums;
using MoonlightServers.Daemon.Extensions;
using Stateless;
@@ -98,21 +99,28 @@ public partial class Server
Logger.LogInformation("State: {state}", transition.Destination);
});
// Proxy the events so outside subscribes can react to it
// Proxy the events so outside subscribes can react to it and notify websockets
StateMachine.OnTransitionCompletedAsync(async transition =>
{
// Notify all clients interested in the server
await WebSocketHub.Clients
.Group(Id.ToString()) //TODO: Consider saving the string value in memory
.SendAsync("StateChanged", transition.Destination.ToString());
// Notify all external listeners
if (OnStateChanged != null)
{
await OnStateChanged(transition.Destination);
}
});
Console.OnOutput += (async message =>
{
// Notify all clients interested in the server
await WebSocketHub.Clients
.Group(Id.ToString()) //TODO: Consider saving the string value in memory
.SendAsync("ConsoleOutput", message);
if (OnConsoleOutput != null)
{
await OnConsoleOutput(message);
}
});
return Task.CompletedTask;

View File

@@ -1,5 +1,7 @@
using Docker.DotNet.Models;
using Microsoft.AspNetCore.SignalR;
using MoonlightServers.Daemon.Enums;
using MoonlightServers.Daemon.Http.Hubs;
using MoonlightServers.Daemon.Models;
using MoonlightServers.Daemon.Models.Cache;
using Stateless;
@@ -11,24 +13,26 @@ public partial class Server
// Exposed configuration/state values
public int Id => Configuration.Id;
public ServerState State => StateMachine.State;
// Exposed container names and ids
public string RuntimeContainerName { get; private set; }
public string? RuntimeContainerId { get; private set; }
public string InstallationContainerName { get; private set; }
public string? InstallationContainerId { get; private set; }
// Events
public event Func<ServerState, Task> OnStateChanged;
public event Func<string, Task> OnConsoleOutput;
public event Func<ServerState, Task>? OnStateChanged;
public event Func<string, Task>? OnConsoleOutput;
// Private stuff
private readonly ILogger Logger;
private readonly IServiceProvider ServiceProvider;
private readonly ServerConsole Console;
private readonly IHubContext<ServerWebSocketHub> WebSocketHub;
private StateMachine<ServerState, ServerTrigger> StateMachine;
private ServerConfiguration Configuration;
private CancellationTokenSource Cancellation;
@@ -36,12 +40,14 @@ public partial class Server
public Server(
ILogger logger,
IServiceProvider serviceProvider,
ServerConfiguration configuration
ServerConfiguration configuration,
IHubContext<ServerWebSocketHub> webSocketHub
)
{
Logger = logger;
ServiceProvider = serviceProvider;
Configuration = configuration;
WebSocketHub = webSocketHub;
Console = new();
Cancellation = new();