mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2024-11-25 16:42:54 +01:00
Compare commits
2 Commits
55d8c6bcfc
...
340b236282
Author | SHA1 | Date | |
---|---|---|---|
340b236282 | |||
a0721ccc2f |
@ -9,10 +9,10 @@
|
||||
<env name="AGENT_NAME" value="Agent 1" />
|
||||
<env name="ALLOWED_RCON_PORTS" value="25575,27000,27001" />
|
||||
<env name="ALLOWED_SERVER_PORTS" value="25565,26000,26001" />
|
||||
<env name="CONTROLLER_HOST" value="localhost" />
|
||||
<env name="JAVA_SEARCH_PATH" value="~/.jdks" />
|
||||
<env name="MAX_INSTANCES" value="3" />
|
||||
<env name="MAX_MEMORY" value="12G" />
|
||||
<env name="SERVER_HOST" value="localhost" />
|
||||
</envs>
|
||||
<option name="USE_EXTERNAL_CONSOLE" value="0" />
|
||||
<option name="USE_MONO" value="0" />
|
||||
|
@ -9,10 +9,10 @@
|
||||
<env name="AGENT_NAME" value="Agent 2" />
|
||||
<env name="ALLOWED_RCON_PORTS" value="27002-27006" />
|
||||
<env name="ALLOWED_SERVER_PORTS" value="26002-26006" />
|
||||
<env name="CONTROLLER_HOST" value="localhost" />
|
||||
<env name="JAVA_SEARCH_PATH" value="~/.jdks" />
|
||||
<env name="MAX_INSTANCES" value="5" />
|
||||
<env name="MAX_MEMORY" value="10G" />
|
||||
<env name="SERVER_HOST" value="localhost" />
|
||||
</envs>
|
||||
<option name="USE_EXTERNAL_CONSOLE" value="0" />
|
||||
<option name="USE_MONO" value="0" />
|
||||
|
@ -9,10 +9,10 @@
|
||||
<env name="AGENT_NAME" value="Agent 3" />
|
||||
<env name="ALLOWED_RCON_PORTS" value="27007" />
|
||||
<env name="ALLOWED_SERVER_PORTS" value="26007" />
|
||||
<env name="CONTROLLER_HOST" value="localhost" />
|
||||
<env name="JAVA_SEARCH_PATH" value="~/.jdks" />
|
||||
<env name="MAX_INSTANCES" value="1" />
|
||||
<env name="MAX_MEMORY" value="2560M" />
|
||||
<env name="SERVER_HOST" value="localhost" />
|
||||
</envs>
|
||||
<option name="USE_EXTERNAL_CONSOLE" value="0" />
|
||||
<option name="USE_MONO" value="0" />
|
||||
|
24
.run/Web.run.xml
Normal file
24
.run/Web.run.xml
Normal file
@ -0,0 +1,24 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Web" type="DotNetProject" factoryName=".NET Project">
|
||||
<option name="EXE_PATH" value="$PROJECT_DIR$/.artifacts/bin/Phantom.Web/debug/Phantom.Web.exe" />
|
||||
<option name="PROGRAM_PARAMETERS" value="" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/.workdir/Web" />
|
||||
<option name="PASS_PARENT_ENVS" value="1" />
|
||||
<envs>
|
||||
<env name="ASPNETCORE_ENVIRONMENT" value="Development" />
|
||||
<env name="WEB_SERVER_HOST" value="localhost" />
|
||||
</envs>
|
||||
<option name="USE_EXTERNAL_CONSOLE" value="0" />
|
||||
<option name="USE_MONO" value="0" />
|
||||
<option name="RUNTIME_ARGUMENTS" value="" />
|
||||
<option name="PROJECT_PATH" value="$PROJECT_DIR$/Web/Phantom.Web/Phantom.Web.csproj" />
|
||||
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
|
||||
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
|
||||
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
|
||||
<option name="PROJECT_KIND" value="DotNetCore" />
|
||||
<option name="PROJECT_TFM" value="net8.0" />
|
||||
<method v="2">
|
||||
<option name="Build" />
|
||||
</method>
|
||||
</configuration>
|
||||
</component>
|
@ -82,10 +82,10 @@ public sealed class RpcLauncher : RpcRuntime<ClientSocket> {
|
||||
}
|
||||
|
||||
if (data.Length > 0 && MessageRegistries.ToAgent.TryGetType(data, out var type)) {
|
||||
logger.Verbose("Received {MessageType} ({Bytes} B) from server.", type.Name, data.Length);
|
||||
logger.Verbose("Received {MessageType} ({Bytes} B) from controller.", type.Name, data.Length);
|
||||
}
|
||||
else {
|
||||
logger.Verbose("Received {Bytes} B message from server.", data.Length);
|
||||
logger.Verbose("Received {Bytes} B message from controller.", data.Length);
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,7 +93,7 @@ public sealed class RpcLauncher : RpcRuntime<ClientSocket> {
|
||||
var unregisterTimeoutTask = Task.Delay(TimeSpan.FromSeconds(5), CancellationToken.None);
|
||||
var finishedTask = await Task.WhenAny(ServerMessaging.Send(new UnregisterAgentMessage(agentGuid)), unregisterTimeoutTask);
|
||||
if (finishedTask == unregisterTimeoutTask) {
|
||||
config.RuntimeLogger.Error("Timed out communicating agent shutdown with the server.");
|
||||
config.RuntimeLogger.Error("Timed out communicating agent shutdown with the controller.");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,9 +52,9 @@ static class AgentKey {
|
||||
|
||||
private static (NetMQCertificate, AgentAuthToken)? LoadFromBytes(byte[] agentKey) {
|
||||
var (publicKey, agentToken) = AgentKeyData.FromBytes(agentKey);
|
||||
var serverCertificate = NetMQCertificate.FromPublicKey(publicKey);
|
||||
var controllerCertificate = NetMQCertificate.FromPublicKey(publicKey);
|
||||
|
||||
Logger.Information("Loaded agent key.");
|
||||
return (serverCertificate, agentToken);
|
||||
return (controllerCertificate, agentToken);
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ try {
|
||||
PhantomLogger.Root.InformationHeading("Initializing Phantom Panel agent...");
|
||||
PhantomLogger.Root.Information("Agent version: {Version}", fullVersion);
|
||||
|
||||
var (serverHost, serverPort, javaSearchPath, agentKeyToken, agentKeyFilePath, agentName, maxInstances, maxMemory, allowedServerPorts, allowedRconPorts, maxConcurrentBackupCompressionTasks) = Variables.LoadOrStop();
|
||||
var (controllerHost, controllerPort, javaSearchPath, agentKeyToken, agentKeyFilePath, agentName, maxInstances, maxMemory, allowedServerPorts, allowedRconPorts, maxConcurrentBackupCompressionTasks) = Variables.LoadOrStop();
|
||||
|
||||
var agentKey = await AgentKey.Load(agentKeyToken, agentKeyFilePath);
|
||||
if (agentKey == null) {
|
||||
@ -43,7 +43,7 @@ try {
|
||||
return 1;
|
||||
}
|
||||
|
||||
var (serverCertificate, agentToken) = agentKey.Value;
|
||||
var (controllerCertificate, agentToken) = agentKey.Value;
|
||||
var agentInfo = new AgentInfo(agentGuid.Value, agentName, ProtocolVersion, fullVersion, maxInstances, maxMemory, allowedServerPorts, allowedRconPorts);
|
||||
var agentServices = new AgentServices(agentInfo, folders, new AgentServiceConfiguration(maxConcurrentBackupCompressionTasks));
|
||||
|
||||
@ -56,7 +56,7 @@ try {
|
||||
await agentServices.Initialize();
|
||||
|
||||
var rpcDisconnectSemaphore = new SemaphoreSlim(0, 1);
|
||||
var rpcConfiguration = new RpcConfiguration(PhantomLogger.Create("Rpc"), PhantomLogger.Create<TaskManager>("Rpc"), serverHost, serverPort, serverCertificate);
|
||||
var rpcConfiguration = new RpcConfiguration(PhantomLogger.Create("Rpc"), PhantomLogger.Create<TaskManager>("Rpc"), controllerHost, controllerPort, controllerCertificate);
|
||||
var rpcTask = RpcLauncher.Launch(rpcConfiguration, agentToken, agentInfo, MessageListenerFactory, rpcDisconnectSemaphore, shutdownCancellationToken);
|
||||
try {
|
||||
await rpcTask.WaitAsync(shutdownCancellationToken);
|
||||
|
@ -6,8 +6,8 @@ using Phantom.Utils.Runtime;
|
||||
namespace Phantom.Agent;
|
||||
|
||||
sealed record Variables(
|
||||
string ServerHost,
|
||||
ushort ServerPort,
|
||||
string ControllerHost,
|
||||
ushort ControllerPort,
|
||||
string JavaSearchPath,
|
||||
string? AgentKeyToken,
|
||||
string? AgentKeyFilePath,
|
||||
@ -23,8 +23,8 @@ sealed record Variables(
|
||||
var javaSearchPath = EnvironmentVariables.GetString("JAVA_SEARCH_PATH").WithDefaultGetter(GetDefaultJavaSearchPath);
|
||||
|
||||
return new Variables(
|
||||
EnvironmentVariables.GetString("SERVER_HOST").Require,
|
||||
EnvironmentVariables.GetPortNumber("SERVER_PORT").WithDefault(9401),
|
||||
EnvironmentVariables.GetString("CONTROLLER_HOST").Require,
|
||||
EnvironmentVariables.GetPortNumber("CONTROLLER_PORT").WithDefault(9401),
|
||||
javaSearchPath,
|
||||
agentKeyToken,
|
||||
agentKeyFilePath,
|
||||
|
82
README.md
82
README.md
@ -4,20 +4,21 @@ Phantom Panel is a **work-in-progress** web interface for managing Minecraft ser
|
||||
|
||||
# Architecture
|
||||
|
||||
Phantom Panel is built on what I'm calling a **Server-Agent architecture**:
|
||||
Phantom Panel has 3 types of services:
|
||||
|
||||
* The **Server** is provides a web interface, persists data in a database, and sends commands to the **Agents**.
|
||||
* One or more **Agents** receive commands from the **Server**, manage the Minecraft server processes, and report on their status.
|
||||
* The **Web** provides a web interface for the **Controller**.
|
||||
* The **Controller** manages all state and persists it in a database, and communicates with **Agents**.
|
||||
* One or more **Agents** receive commands from the **Controller**, manage the Minecraft server processes, and report on their status.
|
||||
|
||||
This architecture has several goals and benefits:
|
||||
|
||||
1. The Server and Agents can run on separate computers, in separate containers, or a mixture of both.
|
||||
2. The Server and Agents can be updated independently.
|
||||
- The Server can receive new features, bug fixes, and security updates without the need to shutdown every Minecraft server.
|
||||
1. The services can run on separate computers, in separate containers, or a mixture of both.
|
||||
2. The services can be updated independently.
|
||||
- The Controller or Web can receive new features, bug fixes, and security updates without the need to shutdown every Minecraft server.
|
||||
- Agent updates can be staggered or delayed. For example, if you have Agents in different geographical locations, you could schedule around timezones and update them at times when people are unlikely to be online.
|
||||
3. Agents are lightweight processes which should have minimal impact on the performance of Minecraft servers.
|
||||
|
||||
When an official Server update is released, it will work with older versions of Agents. There is no guarantee it will also work in reverse (updated Agents and an older Server), but if there is an Agent update that is compatible with older Servers, it will be mentioned in the release notes.
|
||||
When an official Controller update is released, it will work with older versions of Agents. There is no guarantee it will also work in reverse (updated Agents and an older Controller), but if there is an Agent update that is compatible with an older Controller, it will be mentioned in the release notes.
|
||||
|
||||
Note that compatibility is only guaranteed when using official releases. If you build the project from a version of the source between two official releases, you have to understand which changes break compatibility.
|
||||
|
||||
@ -25,30 +26,30 @@ Note that compatibility is only guaranteed when using official releases. If you
|
||||
|
||||
This project is **work-in-progress**, and currently has no official releases. Feel free to try it and experiment, but there will be missing features, bugs, and breaking changes.
|
||||
|
||||
For a quick start, I recommend using [Docker](https://www.docker.com/) or another containerization platform. The `Dockerfile` in the root of the repository can build two target images: `phantom-server` and `phantom-agent`.
|
||||
For a quick start, I recommend using [Docker](https://www.docker.com/) or another containerization platform. The `Dockerfile` in the root of the repository can build three target images: `phantom-web`, `phantom-controller`, and `phantom-agent`.
|
||||
|
||||
Both images put the built application into the `/app` folder. The Agent image also installs Java 8, 16, 17, and 18.
|
||||
All images put the built application into the `/app` folder. The Agent image also installs Java 8, 16, 17, and 18.
|
||||
|
||||
Files are stored relative to the working directory. In the provided images, the working directory is set to `/data`.
|
||||
|
||||
## Server
|
||||
## Controller
|
||||
|
||||
The Server comprises 3 key areas:
|
||||
The Controller comprises 3 key areas:
|
||||
|
||||
* **Web server** that provides the web interface.
|
||||
* **RPC server** that Agents connect to.
|
||||
* **Database connection** that requires a PostgreSQL database server in order to persist data.
|
||||
* **Agent RPC server** that Agents connect to.
|
||||
* **Web RPC server** that Web connects to.
|
||||
* **PostgreSQL database connection** to persist data.
|
||||
|
||||
The configuration for these is set via environment variables.
|
||||
|
||||
### Agent Key
|
||||
### Agent & Web Keys
|
||||
|
||||
When the Server starts for the first time, it will generate and an **Agent Key**. The Agent Key contains an encryption certificate and an authorization token, which are needed for the Agents to connect to the Server.
|
||||
When the Controller starts for the first time, it will generate an **Agent Key** and **Web Key**. These contain encryption certificates and authorization tokens, which are needed for the Agents and Web to connect to the Controller.
|
||||
|
||||
The Agent Key has two forms:
|
||||
Each key has two forms:
|
||||
|
||||
* A binary file stored in `/data/secrets/agent.key` that the Agents can read.
|
||||
* A plaintext-encoded version the Server outputs into the logs on every startup, that can be passed to the Agents in an environemnt variable.
|
||||
* A binary file stored in `/data/secrets/agent.key` or `/data/secrets/web.key` that can be distributed to the other services.
|
||||
* A plaintext-encoded version printed into the logs on every startup, that can be passed to the other services in an environment variable.
|
||||
|
||||
### Storage
|
||||
|
||||
@ -56,14 +57,13 @@ Use volumes to persist the whole `/data` folder.
|
||||
|
||||
### Environment variables
|
||||
|
||||
* **Web Server**
|
||||
- `WEB_SERVER_HOST` is the host. Default: `0.0.0.0`
|
||||
- `WEB_SERVER_PORT` is the port. Default: `9400`
|
||||
- `WEB_BASE_PATH` is the base path of every URL. Must begin with a slash. Default: `/`
|
||||
* **RPC Server**
|
||||
- `RPC_SERVER_HOST` is the host. Default: `0.0.0.0`
|
||||
- `RPC_SERVER_PORT` is the port. Default: `9401`
|
||||
* **PostgreSQL Database Server**
|
||||
* **Agent RPC Server**
|
||||
- `AGENT_RPC_SERVER_HOST` is the host. Default: `0.0.0.0`
|
||||
- `AGENT_RPC_SERVER_PORT` is the port. Default: `9401`
|
||||
* **Web RPC Server**
|
||||
- `WEB_RPC_SERVER_HOST` is the host. Default: `0.0.0.0`
|
||||
- `WEB_RPC_SERVER_PORT` is the port. Default: `9402`
|
||||
* **PostgreSQL Database Connection**
|
||||
- `PG_HOST` is the hostname.
|
||||
- `PG_PORT` is the port.
|
||||
- `PG_USER` is the username.
|
||||
@ -81,11 +81,11 @@ The `/data` folder will contain two folders:
|
||||
|
||||
Use volumes to persist either the whole `/data` folder, or just `/data/data` if you don't want to persist the volatile files.
|
||||
|
||||
### Environment variables:
|
||||
### Environment variables
|
||||
|
||||
* **Server Communication**
|
||||
- `SERVER_HOST` is the hostname of the Server.
|
||||
- `SERVER_PORT` is the RPC port of the Server. Default: `9401`
|
||||
* **Controller Communication**
|
||||
- `CONTROLLER_HOST` is the hostname of the Controller.
|
||||
- `CONTROLLER_PORT` is the Agent RPC port of the Controller. Default: `9401`
|
||||
- `AGENT_NAME` is the display name of the Agent. Emoji are allowed.
|
||||
- `AGENT_KEY` is the plaintext-encoded version of [Agent Key](#agent-key).
|
||||
- `AGENT_KEY_FILE` is a path to the [Agent Key](#agent-key) binary file.
|
||||
@ -98,6 +98,24 @@ Use volumes to persist either the whole `/data` folder, or just `/data/data` if
|
||||
- `ALLOWED_SERVER_PORTS` is a comma-separated list of ports and port ranges that can be used as Minecraft Server ports. Example: `25565,25900,26000-27000`
|
||||
- `ALLOWED_RCON_PORTS` is a comma-separated list of ports and port ranges that can be used as Minecraft RCON ports. Example: `25575,25901,36000-37000`
|
||||
|
||||
## Web
|
||||
|
||||
### Storage
|
||||
|
||||
Use volumes to persist the whole `/data` folder.
|
||||
|
||||
### Environment variables
|
||||
|
||||
* **Controller Communication**
|
||||
- `CONTROLLER_HOST` is the hostname of the Controller.
|
||||
- `CONTROLLER_PORT` is the Web RPC port of the Controller.
|
||||
- `WEB_KEY` is the plaintext-encoded version of [Web Key](#agent--web-keys).
|
||||
- `WEB_KEY_FILE` is a path to the [Web Key](#agent--web-keys) binary file.
|
||||
* **Web Server**
|
||||
- `WEB_SERVER_HOST` is the host. Default: `0.0.0.0`
|
||||
- `WEB_SERVER_PORT` is the port. Default: `9400`
|
||||
- `WEB_BASE_PATH` is the base path of every URL. Must begin with a slash. Default: `/`
|
||||
|
||||
## Logging
|
||||
|
||||
Both the Server and Agent support a `LOG_LEVEL` environment variable to set the minimum log level. Possible values:
|
||||
@ -116,7 +134,7 @@ The repository includes a [Rider](https://www.jetbrains.com/rider/) projects wit
|
||||
|
||||
1. You will need a local PostgreSQL instance. If you have [Docker](https://www.docker.com/), you can enter the `Docker` folder in this repository, and run `docker compose up`. Otherwise, you will need to set it up manually with the following configuration:
|
||||
- Host: `localhost`
|
||||
- Port: `9402`
|
||||
- Port: `9403`
|
||||
- User: `postgres`
|
||||
- Password: `development`
|
||||
- Database: `postgres`
|
||||
|
@ -1,112 +0,0 @@
|
||||
using System.Collections.Immutable;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Phantom.Common.Logging;
|
||||
using Phantom.Controller.Database;
|
||||
using Phantom.Controller.Database.Entities;
|
||||
using Phantom.Controller.Services.Users;
|
||||
using Phantom.Utils.Collections;
|
||||
using Phantom.Utils.Runtime;
|
||||
using Phantom.Utils.Tasks;
|
||||
using Phantom.Web.Identity.Data;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace Phantom.Web.Identity;
|
||||
|
||||
public sealed class PhantomIdentityConfigurator {
|
||||
private static readonly ILogger Logger = PhantomLogger.Create<PhantomIdentityConfigurator>();
|
||||
|
||||
public static async Task MigrateDatabase(IServiceProvider serviceProvider) {
|
||||
await using var scope = serviceProvider.CreateAsyncScope();
|
||||
await scope.ServiceProvider.GetRequiredService<PhantomIdentityConfigurator>().Initialize();
|
||||
}
|
||||
|
||||
private readonly ApplicationDbContext db;
|
||||
private readonly RoleManager roleManager;
|
||||
|
||||
public PhantomIdentityConfigurator(ApplicationDbContext db, RoleManager roleManager) {
|
||||
this.db = db;
|
||||
this.roleManager = roleManager;
|
||||
}
|
||||
|
||||
private async Task Initialize() {
|
||||
await CreatePermissions();
|
||||
await CreateDefaultRoles();
|
||||
await AssignDefaultRolePermissions();
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
private async Task CreatePermissions() {
|
||||
var existingPermissionIds = await db.Permissions.Select(static p => p.Id).AsAsyncEnumerable().ToImmutableSetAsync();
|
||||
var missingPermissionIds = GetMissingPermissionsOrdered(Permission.All, existingPermissionIds);
|
||||
|
||||
if (!missingPermissionIds.IsEmpty) {
|
||||
Logger.Information("Adding permissions: {Permissions}", string.Join(", ", missingPermissionIds));
|
||||
foreach (var permissionId in missingPermissionIds) {
|
||||
db.Permissions.Add(new PermissionEntity(permissionId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CreateDefaultRoles() {
|
||||
Logger.Information("Creating default roles.");
|
||||
|
||||
var allRoleNames = await roleManager.GetAllNames();
|
||||
|
||||
foreach (var (guid, name, _) in Role.All) {
|
||||
if (allRoleNames.Contains(name)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
var result = await roleManager.Create(guid, name);
|
||||
if (result is Result<RoleEntity, AddRoleError>.Fail fail) {
|
||||
switch (fail.Error) {
|
||||
case AddRoleError.NameIsEmpty:
|
||||
Logger.Fatal("Error creating default role \"{Name}\", name is empty!", name);
|
||||
throw StopProcedureException.Instance;
|
||||
|
||||
case AddRoleError.NameIsTooLong:
|
||||
Logger.Fatal("Error creating default role \"{Name}\", name is too long!", name);
|
||||
throw StopProcedureException.Instance;
|
||||
|
||||
case AddRoleError.NameAlreadyExists:
|
||||
Logger.Warning("Error creating default role \"{Name}\", a role with this name already exists!", name);
|
||||
throw StopProcedureException.Instance;
|
||||
|
||||
default:
|
||||
Logger.Fatal("Error creating default role \"{Name}\", unknown error!", name);
|
||||
throw StopProcedureException.Instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AssignDefaultRolePermissions() {
|
||||
Logger.Information("Assigning default role permissions.");
|
||||
|
||||
foreach (var role in Role.All) {
|
||||
var roleEntity = await roleManager.GetByGuid(role.Guid);
|
||||
if (roleEntity == null) {
|
||||
Logger.Fatal("Error assigning default role permissions, role \"{Name}\" with GUID {Guid} not found.", role.Name, role.Guid);
|
||||
throw StopProcedureException.Instance;
|
||||
}
|
||||
|
||||
var existingPermissionIds = await db.RolePermissions
|
||||
.Where(rp => rp.RoleGuid == roleEntity.RoleGuid)
|
||||
.Select(static rp => rp.PermissionId)
|
||||
.AsAsyncEnumerable()
|
||||
.ToImmutableSetAsync();
|
||||
|
||||
var missingPermissionIds = GetMissingPermissionsOrdered(role.Permissions, existingPermissionIds);
|
||||
if (!missingPermissionIds.IsEmpty) {
|
||||
Logger.Information("Assigning default permission to role \"{Name}\": {Permissions}", role.Name, string.Join(", ", missingPermissionIds));
|
||||
foreach (var permissionId in missingPermissionIds) {
|
||||
db.RolePermissions.Add(new RolePermissionEntity(roleEntity.RoleGuid, permissionId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ImmutableArray<string> GetMissingPermissionsOrdered(IEnumerable<Permission> allPermissions, ImmutableHashSet<string> existingPermissionIds) {
|
||||
return allPermissions.Select(static permission => permission.Id).Except(existingPermissionIds).Order().ToImmutableArray();
|
||||
}
|
||||
}
|
@ -2,10 +2,8 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.Components.Server;
|
||||
using Phantom.Controller.Services.Users;
|
||||
using Phantom.Web.Identity.Authentication;
|
||||
using Phantom.Web.Identity.Authorization;
|
||||
using Phantom.Web.Identity.Data;
|
||||
|
||||
namespace Phantom.Web.Identity;
|
||||
|
||||
@ -17,14 +15,8 @@ public static class PhantomIdentityExtensions {
|
||||
services.AddSingleton(PhantomLoginStore.Create(cancellationToken));
|
||||
services.AddScoped<PhantomLoginManager>();
|
||||
|
||||
services.AddScoped<PhantomIdentityConfigurator>();
|
||||
services.AddScoped<IAuthorizationHandler, PermissionBasedPolicyHandler>();
|
||||
services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
|
||||
|
||||
services.AddScoped<UserManager>();
|
||||
services.AddScoped<RoleManager>();
|
||||
services.AddScoped<UserRoleManager>();
|
||||
services.AddTransient<PermissionManager>();
|
||||
}
|
||||
|
||||
public static void UsePhantomIdentity(this IApplicationBuilder application) {
|
||||
|
@ -1,4 +1,6 @@
|
||||
using Microsoft.AspNetCore.DataProtection;
|
||||
using Phantom.Controller.Services;
|
||||
using Phantom.Utils.Tasks;
|
||||
using Phantom.Web.Base;
|
||||
using Phantom.Web.Identity;
|
||||
using Phantom.Web.Identity.Interfaces;
|
||||
@ -7,7 +9,7 @@ using Serilog;
|
||||
namespace Phantom.Web;
|
||||
|
||||
public static class Launcher {
|
||||
public static async Task<WebApplication> CreateApplication(Configuration config, IConfigurator configurator, Action<DbContextOptionsBuilder> dbOptionsBuilder) {
|
||||
public static WebApplication CreateApplication(Configuration config, ServiceConfiguration serviceConfiguration, TaskManager taskManager) {
|
||||
var assembly = typeof(Launcher).Assembly;
|
||||
var builder = WebApplication.CreateBuilder(new WebApplicationOptions {
|
||||
ApplicationName = assembly.GetName().Name,
|
||||
@ -23,32 +25,24 @@ public static class Launcher {
|
||||
builder.WebHost.UseStaticWebAssets();
|
||||
}
|
||||
|
||||
configurator.ConfigureServices(builder.Services);
|
||||
builder.Services.AddSingleton(serviceConfiguration);
|
||||
builder.Services.AddSingleton(taskManager);
|
||||
|
||||
builder.Services.AddSingleton<IHostLifetime>(new NullLifetime());
|
||||
builder.Services.AddScoped<INavigation>(Navigation.Create(config.BasePath));
|
||||
|
||||
builder.Services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(config.KeyFolderPath));
|
||||
|
||||
builder.Services.AddDbContext<ApplicationDbContext>(dbOptionsBuilder, ServiceLifetime.Transient);
|
||||
builder.Services.AddSingleton<DatabaseProvider>();
|
||||
|
||||
builder.Services.AddPhantomIdentity(config.CancellationToken);
|
||||
builder.Services.AddScoped<ILoginEvents, LoginEvents>();
|
||||
|
||||
builder.Services.AddRazorPages(static options => options.RootDirectory = "/Layout");
|
||||
builder.Services.AddServerSideBlazor();
|
||||
|
||||
var application = builder.Build();
|
||||
|
||||
await MigrateDatabase(config, application.Services.GetRequiredService<DatabaseProvider>());
|
||||
await PhantomIdentityConfigurator.MigrateDatabase(application.Services);
|
||||
await configurator.LoadFromDatabase(application.Services);
|
||||
|
||||
return application;
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
public static async Task Launch(Configuration config, WebApplication application) {
|
||||
public static Task Launch(Configuration config, WebApplication application) {
|
||||
var logger = config.Logger;
|
||||
|
||||
application.UseSerilogRequestLogging();
|
||||
@ -67,7 +61,7 @@ public static class Launcher {
|
||||
application.MapFallbackToPage("/_Host");
|
||||
|
||||
logger.Information("Starting Web server on port {Port}...", config.Port);
|
||||
await application.RunAsync(config.CancellationToken);
|
||||
return application.RunAsync(config.CancellationToken);
|
||||
}
|
||||
|
||||
private sealed class NullLifetime : IHostLifetime {
|
||||
@ -79,9 +73,4 @@ public static class Launcher {
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public interface IConfigurator {
|
||||
void ConfigureServices(IServiceCollection services);
|
||||
Task LoadFromDatabase(IServiceProvider serviceProvider);
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Library</OutputType>
|
||||
<OutputType>Exe</OutputType>
|
||||
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
@ -19,8 +20,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Controller\Phantom.Controller.Database\Phantom.Controller.Database.csproj" />
|
||||
<ProjectReference Include="..\..\Controller\Phantom.Controller.Services\Phantom.Controller.Services.csproj" />
|
||||
<ProjectReference Include="..\Phantom.Web.Components\Phantom.Web.Components.csproj" />
|
||||
<ProjectReference Include="..\Phantom.Web.Identity\Phantom.Web.Identity.csproj" />
|
||||
</ItemGroup>
|
||||
|
69
Web/Phantom.Web/Program.cs
Normal file
69
Web/Phantom.Web/Program.cs
Normal file
@ -0,0 +1,69 @@
|
||||
using System.Reflection;
|
||||
using Phantom.Common.Logging;
|
||||
using Phantom.Controller.Services;
|
||||
using Phantom.Utils.Cryptography;
|
||||
using Phantom.Utils.IO;
|
||||
using Phantom.Utils.Runtime;
|
||||
using Phantom.Utils.Tasks;
|
||||
using Phantom.Web;
|
||||
|
||||
var cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
PosixSignals.RegisterCancellation(cancellationTokenSource, static () => {
|
||||
PhantomLogger.Root.InformationHeading("Stopping Phantom Panel web...");
|
||||
});
|
||||
|
||||
static void CreateFolderOrStop(string path, UnixFileMode chmod) {
|
||||
if (!Directory.Exists(path)) {
|
||||
try {
|
||||
Directories.Create(path, chmod);
|
||||
} catch (Exception e) {
|
||||
PhantomLogger.Root.Fatal(e, "Error creating folder: {FolderName}", path);
|
||||
throw StopProcedureException.Instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
var fullVersion = AssemblyAttributes.GetFullVersion(Assembly.GetExecutingAssembly());
|
||||
|
||||
PhantomLogger.Root.InformationHeading("Initializing Phantom Panel web...");
|
||||
PhantomLogger.Root.Information("Web version: {Version}", fullVersion);
|
||||
|
||||
var (webServerHost, webServerPort, webBasePath) = Variables.LoadOrStop();
|
||||
|
||||
string webKeysPath = Path.GetFullPath("./keys");
|
||||
CreateFolderOrStop(webKeysPath, Chmod.URWX);
|
||||
|
||||
PhantomLogger.Root.InformationHeading("Launching Phantom Panel web...");
|
||||
|
||||
var taskManager = new TaskManager(PhantomLogger.Create<TaskManager>("Web"));
|
||||
try {
|
||||
var configuration = new Configuration(PhantomLogger.Create("Web"), webServerHost, webServerPort, webBasePath, webKeysPath, cancellationTokenSource.Token);
|
||||
|
||||
var administratorToken = TokenGenerator.Create(60);
|
||||
PhantomLogger.Root.Information("Your administrator token is: {AdministratorToken}", administratorToken);
|
||||
PhantomLogger.Root.Information("For administrator setup, visit: {HttpUrl}{SetupPath}", configuration.HttpUrl, configuration.BasePath + "setup");
|
||||
|
||||
var serviceConfiguration = new ServiceConfiguration(fullVersion, TokenGenerator.GetBytesOrThrow(administratorToken), cancellationTokenSource.Token);
|
||||
var webApplication = Launcher.CreateApplication(configuration, serviceConfiguration, taskManager);
|
||||
|
||||
await Launcher.Launch(configuration, webApplication);
|
||||
} finally {
|
||||
cancellationTokenSource.Cancel();
|
||||
await taskManager.Stop();
|
||||
}
|
||||
|
||||
return 0;
|
||||
} catch (OperationCanceledException) {
|
||||
return 0;
|
||||
} catch (StopProcedureException) {
|
||||
return 1;
|
||||
} catch (Exception e) {
|
||||
PhantomLogger.Root.Fatal(e, "Caught exception in entry point.");
|
||||
return 1;
|
||||
} finally {
|
||||
cancellationTokenSource.Dispose();
|
||||
PhantomLogger.Root.Information("Bye!");
|
||||
PhantomLogger.Dispose();
|
||||
}
|
27
Web/Phantom.Web/Variables.cs
Normal file
27
Web/Phantom.Web/Variables.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using Phantom.Common.Logging;
|
||||
using Phantom.Utils.Runtime;
|
||||
|
||||
namespace Phantom.Web;
|
||||
|
||||
sealed record Variables(
|
||||
string WebServerHost,
|
||||
ushort WebServerPort,
|
||||
string WebBasePath
|
||||
) {
|
||||
private static Variables LoadOrThrow() {
|
||||
return new Variables(
|
||||
EnvironmentVariables.GetString("WEB_SERVER_HOST").WithDefault("0.0.0.0"),
|
||||
EnvironmentVariables.GetPortNumber("WEB_SERVER_PORT").WithDefault(9400),
|
||||
EnvironmentVariables.GetString("WEB_BASE_PATH").Validate(static value => value.StartsWith('/') && value.EndsWith('/'), "Environment variable must begin and end with '/'").WithDefault("/")
|
||||
);
|
||||
}
|
||||
|
||||
public static Variables LoadOrStop() {
|
||||
try {
|
||||
return LoadOrThrow();
|
||||
} catch (Exception e) {
|
||||
PhantomLogger.Root.Fatal(e.Message);
|
||||
throw StopProcedureException.Instance;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user