340 lines
13 KiB
Plaintext
340 lines
13 KiB
Plaintext
@page "/servers/{ServerId:int}"
|
|
@page "/servers/{ServerId:int}/{TabPath:alpha}"
|
|
|
|
@using Microsoft.AspNetCore.SignalR.Client
|
|
@using MoonCore.Blazor.FlyonUi.Components
|
|
@using MoonCore.Exceptions
|
|
@using MoonCore.Helpers
|
|
@using MoonlightServers.Frontend.Interfaces
|
|
@using MoonlightServers.Frontend.Models
|
|
@using MoonlightServers.Frontend.Services
|
|
@using MoonlightServers.Shared.Enums
|
|
@using MoonlightServers.Shared.Http.Responses.Client.Servers
|
|
|
|
@inject ServerService ServerService
|
|
@inject NavigationManager Navigation
|
|
@inject IEnumerable<IServerTabProvider> TabProviders
|
|
|
|
@implements IAsyncDisposable
|
|
|
|
<LazyLoader Load="Load">
|
|
@if (NotFound)
|
|
{
|
|
<IconAlert Title="Server not found" Icon="icon-search" Color="text-primary">
|
|
The requested server could not be found
|
|
</IconAlert>
|
|
}
|
|
else
|
|
{
|
|
<div class="card card-body justify-between py-2.5 px-5 flex-row">
|
|
<div class="flex flex-row items-center">
|
|
@{
|
|
var statusClass = State switch
|
|
{
|
|
ServerState.Installing => "status-primary",
|
|
ServerState.Offline => "status-error",
|
|
ServerState.Starting => "status-warning",
|
|
ServerState.Stopping => "status-warning",
|
|
ServerState.Online => "status-success",
|
|
_ => "status-secondary"
|
|
};
|
|
}
|
|
|
|
<div class="inline-grid *:[grid-area:1/1] me-3">
|
|
@if (State != ServerState.Offline)
|
|
{
|
|
<div class="status status-xl @statusClass animate-ping"></div>
|
|
}
|
|
<div class="status status-xl @statusClass"></div>
|
|
</div>
|
|
|
|
<div class="flex flex-col">
|
|
<div class="hidden sm:flex text-lg font-semibold">@Server.Name</div>
|
|
<div class="hidden text-sm text-base-content/60 md:flex gap-x-3">
|
|
<span>
|
|
<i class="icon-sparkles me-0.5 align-middle"></i>
|
|
<span class="align-middle">@Server.StarName</span>
|
|
</span>
|
|
|
|
<span>
|
|
<i class="icon-database me-0.5 align-middle"></i>
|
|
<span class="align-middle">@Server.NodeName</span>
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="flex flex-row items-center">
|
|
<div class="flex gap-x-1.5">
|
|
@if (HasPermissionTo("power", ServerPermissionType.ReadWrite))
|
|
{
|
|
@if (State == ServerState.Offline)
|
|
{
|
|
<WButton CssClasses="btn btn-success" OnClick="_ => Start()">
|
|
<i class="icon-play align-middle"></i>
|
|
<span class="align-middle">Start</span>
|
|
</WButton>
|
|
}
|
|
else
|
|
{
|
|
<button type="button" class="btn btn-success" disabled="disabled">
|
|
<i class="icon-play align-middle"></i>
|
|
<span class="align-middle">Start</span>
|
|
</button>
|
|
}
|
|
|
|
@if (State == ServerState.Online)
|
|
{
|
|
<button type="button" class="btn btn-primary">
|
|
<i class="icon-rotate-ccw align-middle"></i>
|
|
<span class="align-middle">Restart</span>
|
|
</button>
|
|
}
|
|
else
|
|
{
|
|
<button type="button" class="btn btn-primary" disabled="disabled">
|
|
<i class="icon-rotate-ccw align-middle"></i>
|
|
<span class="align-middle">Restart</span>
|
|
</button>
|
|
}
|
|
|
|
@if (State == ServerState.Starting || State == ServerState.Online || State == ServerState.Stopping)
|
|
{
|
|
if (State == ServerState.Stopping)
|
|
{
|
|
<WButton CssClasses="btn btn-error" OnClick="_ => Kill()">
|
|
<i class="icon-bomb align-middle"></i>
|
|
<span class="align-middle">Kill</span>
|
|
</WButton>
|
|
}
|
|
else
|
|
{
|
|
<WButton CssClasses="btn btn-error" OnClick="_ => Stop()">
|
|
<i class="icon-squircle align-middle"></i>
|
|
<span class="align-middle">Stop</span>
|
|
</WButton>
|
|
}
|
|
}
|
|
else
|
|
{
|
|
<button type="button" class="btn btn-error" disabled="disabled">
|
|
<i class="icon-squircle align-middle"></i>
|
|
<span class="align-middle">Stop</span>
|
|
</button>
|
|
}
|
|
}
|
|
else
|
|
{
|
|
<button type="button" class="btn btn-success" disabled="disabled">
|
|
<i class="icon-play align-middle"></i>
|
|
<span class="align-middle">Start</span>
|
|
</button>
|
|
|
|
<button type="button" class="btn btn-primary" disabled="disabled">
|
|
<i class="icon-rotate-ccw align-middle"></i>
|
|
<span class="align-middle">Restart</span>
|
|
</button>
|
|
|
|
<button type="button" class="btn btn-error" disabled="disabled">
|
|
<i class="icon-squircle align-middle"></i>
|
|
<span class="align-middle">Stop</span>
|
|
</button>
|
|
}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="mt-3">
|
|
|
|
<nav class="tabs space-x-2 overflow-x-auto" aria-label="Tabs" role="tablist" aria-orientation="horizontal">
|
|
@foreach (var tab in Tabs)
|
|
{
|
|
<a href="/servers/@(ServerId)/@(tab.Path)"
|
|
class="btn btn-text active-tab:bg-primary active-tab:text-primary-content hover:text-primary hover:bg-primary/20 bg-base-150 text-sm py-0.5 px-3 rounded-xl @(tab == CurrentTab ? "active" : "")"
|
|
data-tab="na"
|
|
role="tab"
|
|
@onclick:preventDefault
|
|
@onclick="() => SwitchTab(tab)">
|
|
@tab.Name
|
|
</a>
|
|
}
|
|
</nav>
|
|
|
|
<div class="mt-5">
|
|
@if (CurrentTab == null)
|
|
{
|
|
<IconAlert Title="No tabs found" Color="text-primary" Icon="icon-app-window">
|
|
Seems like you are missing access to all tabs or no tabs for this server could be found
|
|
</IconAlert>
|
|
}
|
|
else
|
|
{
|
|
var rf = ComponentHelper.FromType(CurrentTab!.ComponentType, parameters =>
|
|
{
|
|
parameters.Add("Server", Server);
|
|
parameters.Add("State", State);
|
|
parameters.Add("InitialConsoleMessage", InitialConsoleMessage);
|
|
parameters.Add("HubConnection", HubConnection!);
|
|
parameters.Add("Parent", this);
|
|
});
|
|
|
|
@rf
|
|
}
|
|
</div>
|
|
</div>
|
|
}
|
|
</LazyLoader>
|
|
|
|
@code
|
|
{
|
|
[Parameter] public int ServerId { get; set; }
|
|
[Parameter] public string? TabPath { get; set; }
|
|
|
|
private ServerTab[] Tabs;
|
|
private ServerTab? CurrentTab;
|
|
|
|
private ServerDetailResponse Server;
|
|
private bool NotFound = false;
|
|
private ServerState State;
|
|
private string InitialConsoleMessage = "";
|
|
|
|
private HubConnection? HubConnection;
|
|
|
|
public ConcurrentList<string> CommandHistory = new();
|
|
|
|
private async Task Load(LazyLoader _)
|
|
{
|
|
try
|
|
{
|
|
// Load meta data
|
|
Server = await ServerService.GetServer(ServerId);
|
|
|
|
// Load server tabs
|
|
var tmpTabs = new List<ServerTab>();
|
|
|
|
foreach (var serverTabProvider in TabProviders)
|
|
tmpTabs.AddRange(await serverTabProvider.GetTabs(Server));
|
|
|
|
// If we are accessing a shared server, we need to handle permissions
|
|
if (Server.Share != null)
|
|
{
|
|
// This removes all tabs where the permission filter is not set
|
|
tmpTabs.RemoveAll(tab =>
|
|
{
|
|
if (tab.PermissionFilter == null)
|
|
return false;
|
|
|
|
return !Server.Share.Permissions.Any(tab.PermissionFilter);
|
|
});
|
|
}
|
|
|
|
Tabs = tmpTabs.OrderBy(x => x.Priority).ToArray();
|
|
|
|
// Find current tab
|
|
if (!string.IsNullOrEmpty(TabPath))
|
|
{
|
|
CurrentTab = Tabs.FirstOrDefault(x => TabPath.Equals(x.Path, StringComparison.InvariantCultureIgnoreCase)
|
|
);
|
|
}
|
|
|
|
if (CurrentTab == null)
|
|
CurrentTab = Tabs.FirstOrDefault();
|
|
|
|
// Load initial status for first render
|
|
var status = await ServerService.GetStatus(ServerId);
|
|
|
|
State = status.State;
|
|
|
|
if (!HasPermissionTo("console", ServerPermissionType.Read))
|
|
return; // Exit early if we don't have permissions to load the console
|
|
|
|
// Load initial messages
|
|
var initialLogs = await ServerService.GetLogs(ServerId);
|
|
|
|
InitialConsoleMessage = "";
|
|
|
|
foreach (var message in initialLogs.Messages)
|
|
InitialConsoleMessage += message;
|
|
|
|
// Load websocket meta
|
|
var websocketDetails = await ServerService.GetWebSocket(ServerId);
|
|
|
|
// Build signal r
|
|
HubConnection = new HubConnectionBuilder()
|
|
.WithUrl(websocketDetails.Target, options =>
|
|
{
|
|
options.AccessTokenProvider = async () =>
|
|
{
|
|
var details = await ServerService.GetWebSocket(ServerId);
|
|
return details.AccessToken;
|
|
};
|
|
})
|
|
.WithAutomaticReconnect()
|
|
.Build();
|
|
|
|
// Define handlers
|
|
HubConnection.On<string>("StateChanged", async stateStr =>
|
|
{
|
|
if (!Enum.TryParse(stateStr, out ServerState receivedState))
|
|
return;
|
|
|
|
State = receivedState;
|
|
await InvokeAsync(StateHasChanged);
|
|
});
|
|
|
|
HubConnection.On<string>("ConsoleOutput", async content =>
|
|
{
|
|
// Update initial message
|
|
InitialConsoleMessage += content;
|
|
await InvokeAsync(StateHasChanged);
|
|
});
|
|
|
|
// Connect
|
|
await HubConnection.StartAsync();
|
|
}
|
|
catch (HttpApiException e)
|
|
{
|
|
if (e.Status == 404)
|
|
NotFound = true;
|
|
else
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private bool HasPermissionTo(string name, ServerPermissionType type)
|
|
{
|
|
// All non shares have permissions
|
|
if (Server.Share == null)
|
|
return true;
|
|
|
|
return Server.Share.Permissions.Any(x => x.Name == name && x.Type >= type);
|
|
}
|
|
|
|
private async Task SwitchTab(ServerTab tab)
|
|
{
|
|
CurrentTab = tab;
|
|
Navigation.NavigateTo($"/servers/{ServerId}/{tab.Path}");
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
private async Task Start()
|
|
=> await ServerService.Start(ServerId);
|
|
|
|
private async Task Stop()
|
|
=> await ServerService.Stop(ServerId);
|
|
|
|
private async Task Kill()
|
|
=> await ServerService.Kill(ServerId);
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (HubConnection != null)
|
|
{
|
|
if (HubConnection.State == HubConnectionState.Connected)
|
|
await HubConnection.StopAsync();
|
|
|
|
await HubConnection.DisposeAsync();
|
|
}
|
|
}
|
|
}
|