1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2026-01-01 05:55:20 +01:00

5 Commits

Author SHA1 Message Date
ccf3ecb180 WIP 2025-12-31 17:29:36 +01:00
366735d351 Refactor Optional 2025-12-31 17:29:12 +01:00
ea66f9f056 Prevent Ctrl-C from killing Agent's child processes on Windows 2025-12-30 04:02:46 +01:00
9a69a6b2bb Update to .NET 10 and C# 14 2025-12-29 23:26:51 +01:00
27e70d47c3 Rework Agent configuration and authorization 2025-12-28 22:22:55 +01:00
28 changed files with 566 additions and 107 deletions

View File

@@ -3,11 +3,11 @@
"isRoot": true, "isRoot": true,
"tools": { "tools": {
"dotnet-ef": { "dotnet-ef": {
"version": "9.0.9", "version": "10.0.1",
"commands": [ "commands": [
"dotnet-ef" "dotnet-ef"
], ],
"rollForward": false "rollForward": false
} }
} }
} }

View File

@@ -1,5 +1,4 @@
using System.Collections.ObjectModel; using System.Text;
using System.Text;
using Phantom.Agent.Minecraft.Instance; using Phantom.Agent.Minecraft.Instance;
using Phantom.Agent.Minecraft.Java; using Phantom.Agent.Minecraft.Java;
using Phantom.Agent.Minecraft.Server; using Phantom.Agent.Minecraft.Server;
@@ -12,7 +11,7 @@ public abstract class BaseLauncher : IServerLauncher {
private readonly InstanceProperties instanceProperties; private readonly InstanceProperties instanceProperties;
protected string MinecraftVersion => instanceProperties.ServerVersion; protected string MinecraftVersion => instanceProperties.ServerVersion;
protected string InstanceFolder => instanceProperties.InstanceFolder;
private protected BaseLauncher(InstanceProperties instanceProperties) { private protected BaseLauncher(InstanceProperties instanceProperties) {
this.instanceProperties = instanceProperties; this.instanceProperties = instanceProperties;
} }
@@ -52,14 +51,17 @@ public abstract class BaseLauncher : IServerLauncher {
var processConfigurator = new ProcessConfigurator { var processConfigurator = new ProcessConfigurator {
FileName = javaRuntimeExecutable.ExecutablePath, FileName = javaRuntimeExecutable.ExecutablePath,
WorkingDirectory = InstanceFolder, WorkingDirectory = instanceProperties.InstanceFolder,
RedirectInput = true, RedirectInput = true,
UseShellExecute = false, UseShellExecute = false,
}; };
var processArguments = processConfigurator.ArgumentList; var processArguments = processConfigurator.ArgumentList;
PrepareJvmArguments(serverJar).Build(processArguments); PrepareJvmArguments(serverJar).Build(processArguments);
PrepareJavaProcessArguments(processArguments, serverJar.FilePath); processArguments.Add("-jar");
processArguments.Add(serverJar.FilePath);
processArguments.Add("nogui");
var process = processConfigurator.CreateProcess(); var process = processConfigurator.CreateProcess();
var instanceProcess = new InstanceProcess(instanceProperties, process); var instanceProcess = new InstanceProcess(instanceProperties, process);
@@ -96,12 +98,7 @@ public abstract class BaseLauncher : IServerLauncher {
} }
private protected virtual void CustomizeJvmArguments(JvmArgumentBuilder arguments) {} private protected virtual void CustomizeJvmArguments(JvmArgumentBuilder arguments) {}
protected virtual void PrepareJavaProcessArguments(Collection<string> processArguments, string serverJarFilePath) {
processArguments.Add("-jar");
processArguments.Add(serverJarFilePath);
processArguments.Add("nogui");
}
private protected virtual Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) { private protected virtual Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) {
return Task.FromResult(new ServerJarInfo(serverJarPath)); return Task.FromResult(new ServerJarInfo(serverJarPath));
} }

View File

@@ -1,29 +0,0 @@
using System.Collections.ObjectModel;
using Phantom.Agent.Minecraft.Instance;
using Phantom.Agent.Minecraft.Java;
using Serilog;
namespace Phantom.Agent.Minecraft.Launcher.Types;
public sealed class ForgeLauncher : BaseLauncher {
public ForgeLauncher(InstanceProperties instanceProperties) : base(instanceProperties) {}
private protected override void CustomizeJvmArguments(JvmArgumentBuilder arguments) {
arguments.AddProperty("terminal.ansi", "true"); // TODO
}
protected override void PrepareJavaProcessArguments(Collection<string> processArguments, string serverJarFilePath) {
if (OperatingSystem.IsWindows()) {
processArguments.Add("@libraries/net/minecraftforge/forge/1.20.1-47.2.0/win_args.txt");
}
else {
processArguments.Add("@libraries/net/minecraftforge/forge/1.20.1-47.2.0/unix_args.txt");
}
processArguments.Add("nogui");
}
private protected override Task<ServerJarInfo> PrepareServerJar(ILogger logger, string serverJarPath, CancellationToken cancellationToken) {
return Task.FromResult(new ServerJarInfo(Path.Combine(InstanceFolder, "run.sh")));
}
}

View File

@@ -97,7 +97,6 @@ sealed class InstanceManagerActor : ReceiveActor<InstanceManagerActor.ICommand>
IServerLauncher launcher = configuration.MinecraftServerKind switch { IServerLauncher launcher = configuration.MinecraftServerKind switch {
MinecraftServerKind.Vanilla => new VanillaLauncher(properties), MinecraftServerKind.Vanilla => new VanillaLauncher(properties),
MinecraftServerKind.Fabric => new FabricLauncher(properties), MinecraftServerKind.Fabric => new FabricLauncher(properties),
MinecraftServerKind.Forge => new ForgeLauncher(properties),
_ => InvalidLauncher.Instance, _ => InvalidLauncher.Instance,
}; };

View File

@@ -15,4 +15,32 @@ public sealed partial record Agent(
) { ) {
[MemoryPackIgnore] [MemoryPackIgnore]
public RamAllocationUnits? AvailableMemory => RuntimeInfo.MaxMemory - Stats?.RunningInstanceMemory; public RamAllocationUnits? AvailableMemory => RuntimeInfo.MaxMemory - Stats?.RunningInstanceMemory;
public Agent With(Update update) => new (
update.AgentGuid.Or(AgentGuid),
update.Configuration.Or(Configuration),
update.ConnectionKey.Or(ConnectionKey),
update.RuntimeInfo.Or(RuntimeInfo),
update.Stats.Or(Stats),
update.ConnectionStatus.Or(ConnectionStatus)
);
[MemoryPackable(GenerateType.VersionTolerant)]
public sealed partial record Update(
[property: MemoryPackOrder(0)] Optional<Guid> AgentGuid,
[property: MemoryPackOrder(1)] Optional<AgentConfiguration> Configuration,
[property: MemoryPackOrder(2)] Optional<ImmutableArray<byte>> ConnectionKey,
[property: MemoryPackOrder(3)] Optional<AgentRuntimeInfo> RuntimeInfo,
[property: MemoryPackOrder(4)] OptionalNullable<AgentStats> Stats,
[property: MemoryPackOrder(5)] Optional<IAgentConnectionStatus> ConnectionStatus
) {
public Update Merge(Update newer) => new (
newer.AgentGuid.Or(AgentGuid),
newer.Configuration.Or(Configuration),
newer.ConnectionKey.Or(ConnectionKey),
newer.RuntimeInfo.Or(RuntimeInfo),
newer.Stats.Or(Stats),
newer.ConnectionStatus.Or(ConnectionStatus)
);
}
} }

View File

@@ -3,5 +3,4 @@
public enum MinecraftServerKind : ushort { public enum MinecraftServerKind : ushort {
Vanilla = 1, Vanilla = 1,
Fabric = 2, Fabric = 2,
Forge = 3,
} }

View File

@@ -3,8 +3,47 @@
namespace Phantom.Common.Data; namespace Phantom.Common.Data;
[MemoryPackable] [MemoryPackable]
public readonly partial record struct Optional<T>(T? Value) { public readonly partial struct Optional<T> {
public static implicit operator Optional<T>(T? value) { [MemoryPackOrder(0)]
public bool HasValue { get; }
[MemoryPackOrder(1)]
public T Value {
get {
if (HasValue) {
return field!;
}
else {
throw new InvalidOperationException();
}
}
}
[MemoryPackIgnore]
public T? ValueOrDefault => HasValue ? Value : default;
public Optional() : this(hasValue: false, value: default) {}
public Optional(T value) : this(hasValue: true, value) {}
[MemoryPackConstructor]
private Optional(bool hasValue, T? value) {
this.HasValue = hasValue;
this.Value = value;
}
public Optional<R> Map<R>(Func<T, R> func) {
return HasValue ? new Optional<R>(func(Value)) : new Optional<R>();
}
public T Or(T fallbackValue) {
return HasValue ? Value : fallbackValue;
}
public Optional<T> Or(Optional<T> fallbackOptional) {
return HasValue ? Value : fallbackOptional;
}
public static implicit operator Optional<T>(T value) {
return new Optional<T>(value); return new Optional<T>(value);
} }
} }

View File

@@ -0,0 +1,42 @@
using MemoryPack;
namespace Phantom.Common.Data;
[MemoryPackable]
public readonly partial struct OptionalNullable<T> {
[MemoryPackOrder(0)]
public bool HasValue { get; }
[MemoryPackOrder(1)]
public T? Value {
get {
if (HasValue) {
return field;
}
else {
throw new InvalidOperationException();
}
}
}
public OptionalNullable() : this(hasValue: false, value: default) {}
public OptionalNullable(T? value) : this(hasValue: true, value) {}
[MemoryPackConstructor]
private OptionalNullable(bool hasValue, T? value) {
this.HasValue = hasValue;
this.Value = value;
}
public T? Or(T? fallbackValue) {
return HasValue ? Value : fallbackValue;
}
public OptionalNullable<T> Or(OptionalNullable<T> fallbackOptional) {
return HasValue ? Value : fallbackOptional;
}
public static implicit operator OptionalNullable<T>(T? value) {
return new OptionalNullable<T>(value);
}
}

View File

@@ -0,0 +1,357 @@
// <auto-generated />
using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Phantom.Controller.Database;
#nullable disable
namespace Phantom.Controller.Database.Postgres.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20251228211902_AgentAuthSecretNullability")]
partial class AgentAuthSecretNullability
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.9")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Phantom.Controller.Database.Entities.AgentEntity", b =>
{
b.Property<Guid>("AgentGuid")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<byte[]>("AuthSecret")
.IsRequired()
.HasMaxLength(12)
.HasColumnType("bytea");
b.Property<string>("BuildVersion")
.HasColumnType("text");
b.Property<int?>("MaxInstances")
.HasColumnType("integer");
b.Property<int?>("MaxMemory")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<int?>("ProtocolVersion")
.HasColumnType("integer");
b.HasKey("AgentGuid");
b.ToTable("Agents", "agents");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.AuditLogEntity", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
b.Property<JsonDocument>("Data")
.HasColumnType("jsonb");
b.Property<string>("EventType")
.IsRequired()
.HasColumnType("text");
b.Property<string>("SubjectId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("SubjectType")
.IsRequired()
.HasColumnType("text");
b.Property<Guid?>("UserGuid")
.HasColumnType("uuid");
b.Property<DateTime>("UtcTime")
.HasColumnType("timestamp with time zone");
b.HasKey("Id");
b.HasIndex("UserGuid");
b.ToTable("AuditLog", "system");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.EventLogEntity", b =>
{
b.Property<Guid>("EventGuid")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid?>("AgentGuid")
.HasColumnType("uuid");
b.Property<JsonDocument>("Data")
.HasColumnType("jsonb");
b.Property<string>("EventType")
.IsRequired()
.HasColumnType("text");
b.Property<string>("SubjectId")
.IsRequired()
.HasColumnType("text");
b.Property<string>("SubjectType")
.IsRequired()
.HasColumnType("text");
b.Property<DateTime>("UtcTime")
.HasColumnType("timestamp with time zone");
b.HasKey("EventGuid");
b.ToTable("EventLog", "system");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.InstanceEntity", b =>
{
b.Property<Guid>("InstanceGuid")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<Guid>("AgentGuid")
.HasColumnType("uuid");
b.Property<string>("InstanceName")
.IsRequired()
.HasColumnType("text");
b.Property<Guid>("JavaRuntimeGuid")
.HasColumnType("uuid");
b.Property<string>("JvmArguments")
.IsRequired()
.HasColumnType("text");
b.Property<bool>("LaunchAutomatically")
.HasColumnType("boolean");
b.Property<int>("MemoryAllocation")
.HasColumnType("integer");
b.Property<string>("MinecraftServerKind")
.IsRequired()
.HasColumnType("text");
b.Property<string>("MinecraftVersion")
.IsRequired()
.HasColumnType("text");
b.Property<int>("RconPort")
.HasColumnType("integer");
b.Property<int>("ServerPort")
.HasColumnType("integer");
b.HasKey("InstanceGuid");
b.ToTable("Instances", "agents");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.PermissionEntity", b =>
{
b.Property<string>("Id")
.HasColumnType("text");
b.HasKey("Id");
b.ToTable("Permissions", "identity");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.RoleEntity", b =>
{
b.Property<Guid>("RoleGuid")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.HasKey("RoleGuid");
b.ToTable("Roles", "identity");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.RolePermissionEntity", b =>
{
b.Property<Guid>("RoleGuid")
.HasColumnType("uuid");
b.Property<string>("PermissionId")
.HasColumnType("text");
b.HasKey("RoleGuid", "PermissionId");
b.HasIndex("PermissionId");
b.ToTable("RolePermissions", "identity");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.UserAgentAccessEntity", b =>
{
b.Property<Guid>("UserGuid")
.HasColumnType("uuid");
b.Property<Guid>("AgentGuid")
.HasColumnType("uuid");
b.HasKey("UserGuid", "AgentGuid");
b.HasIndex("AgentGuid");
b.ToTable("UserAgentAccess", "identity");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.UserEntity", b =>
{
b.Property<Guid>("UserGuid")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("text");
b.Property<string>("PasswordHash")
.IsRequired()
.HasColumnType("text");
b.HasKey("UserGuid");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Users", "identity");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.UserPermissionEntity", b =>
{
b.Property<Guid>("UserGuid")
.HasColumnType("uuid");
b.Property<string>("PermissionId")
.HasColumnType("text");
b.HasKey("UserGuid", "PermissionId");
b.HasIndex("PermissionId");
b.ToTable("UserPermissions", "identity");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.UserRoleEntity", b =>
{
b.Property<Guid>("UserGuid")
.HasColumnType("uuid");
b.Property<Guid>("RoleGuid")
.HasColumnType("uuid");
b.HasKey("UserGuid", "RoleGuid");
b.HasIndex("RoleGuid");
b.ToTable("UserRoles", "identity");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.AuditLogEntity", b =>
{
b.HasOne("Phantom.Controller.Database.Entities.UserEntity", "User")
.WithMany()
.HasForeignKey("UserGuid")
.OnDelete(DeleteBehavior.SetNull);
b.Navigation("User");
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.RolePermissionEntity", b =>
{
b.HasOne("Phantom.Controller.Database.Entities.PermissionEntity", null)
.WithMany()
.HasForeignKey("PermissionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Phantom.Controller.Database.Entities.RoleEntity", null)
.WithMany()
.HasForeignKey("RoleGuid")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.UserAgentAccessEntity", b =>
{
b.HasOne("Phantom.Controller.Database.Entities.AgentEntity", null)
.WithMany()
.HasForeignKey("AgentGuid")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Phantom.Controller.Database.Entities.UserEntity", null)
.WithMany()
.HasForeignKey("UserGuid")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.UserPermissionEntity", b =>
{
b.HasOne("Phantom.Controller.Database.Entities.PermissionEntity", null)
.WithMany()
.HasForeignKey("PermissionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Phantom.Controller.Database.Entities.UserEntity", null)
.WithMany()
.HasForeignKey("UserGuid")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Phantom.Controller.Database.Entities.UserRoleEntity", b =>
{
b.HasOne("Phantom.Controller.Database.Entities.RoleEntity", "Role")
.WithMany()
.HasForeignKey("RoleGuid")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Phantom.Controller.Database.Entities.UserEntity", "User")
.WithMany()
.HasForeignKey("UserGuid")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Role");
b.Navigation("User");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,42 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Phantom.Controller.Database.Postgres.Migrations
{
/// <inheritdoc />
public partial class AgentAuthSecretNullability : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<byte[]>(
name: "AuthSecret",
schema: "agents",
table: "Agents",
type: "bytea",
maxLength: 12,
nullable: false,
defaultValue: new byte[0],
oldClrType: typeof(byte[]),
oldType: "bytea",
oldMaxLength: 12,
oldNullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<byte[]>(
name: "AuthSecret",
schema: "agents",
table: "Agents",
type: "bytea",
maxLength: 12,
nullable: true,
oldClrType: typeof(byte[]),
oldType: "bytea",
oldMaxLength: 12);
}
}
}

View File

@@ -30,6 +30,7 @@ namespace Phantom.Controller.Database.Postgres.Migrations
.HasColumnType("uuid"); .HasColumnType("uuid");
b.Property<byte[]>("AuthSecret") b.Property<byte[]>("AuthSecret")
.IsRequired()
.HasMaxLength(12) .HasMaxLength(12)
.HasColumnType("bytea"); .HasColumnType("bytea");

View File

@@ -20,7 +20,7 @@ public sealed class AgentEntity {
public RamAllocationUnits? MaxMemory { get; set; } public RamAllocationUnits? MaxMemory { get; set; }
[MaxLength(AuthSecret.Length)] [MaxLength(AuthSecret.Length)]
public AuthSecret? AuthSecret { get; set; } public AuthSecret AuthSecret { get; set; }
public AgentConfiguration Configuration => new (Name); public AgentConfiguration Configuration => new (Name);
public AgentVersionInfo? VersionInfo => ProtocolVersion is {} protocolVersion && BuildVersion is {} buildVersion ? new AgentVersionInfo(protocolVersion, buildVersion) : null; public AgentVersionInfo? VersionInfo => ProtocolVersion is {} protocolVersion && BuildVersion is {} buildVersion ? new AgentVersionInfo(protocolVersion, buildVersion) : null;
@@ -30,5 +30,6 @@ public sealed class AgentEntity {
AgentGuid = agentGuid; AgentGuid = agentGuid;
Name = null!; Name = null!;
BuildVersion = null!; BuildVersion = null!;
AuthSecret = null!;
} }
} }

View File

@@ -11,7 +11,6 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="System.Linq.Async" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -1,7 +1,6 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Immutable; using System.Collections.Immutable;
using Akka.Actor; using Akka.Actor;
using Microsoft.EntityFrameworkCore;
using Phantom.Common.Data; using Phantom.Common.Data;
using Phantom.Common.Data.Replies; using Phantom.Common.Data.Replies;
using Phantom.Common.Data.Web.Agent; using Phantom.Common.Data.Web.Agent;
@@ -9,7 +8,6 @@ using Phantom.Common.Data.Web.Users;
using Phantom.Common.Messages.Agent.Handshake; using Phantom.Common.Messages.Agent.Handshake;
using Phantom.Common.Messages.Agent.ToAgent; using Phantom.Common.Messages.Agent.ToAgent;
using Phantom.Controller.Database; using Phantom.Controller.Database;
using Phantom.Controller.Database.Entities;
using Phantom.Controller.Minecraft; using Phantom.Controller.Minecraft;
using Phantom.Controller.Services.Users.Sessions; using Phantom.Controller.Services.Users.Sessions;
using Phantom.Utils.Actor; using Phantom.Utils.Actor;
@@ -33,12 +31,11 @@ sealed class AgentManager(
public async Task Initialize() { public async Task Initialize() {
await using var ctx = dbProvider.Eager(); await using var ctx = dbProvider.Eager();
await Migrate(ctx);
await foreach (var entity in ctx.Agents.AsAsyncEnumerable().WithCancellation(cancellationToken)) { await foreach (var entity in ctx.Agents.AsAsyncEnumerable().WithCancellation(cancellationToken)) {
var agentGuid = entity.AgentGuid; var agentGuid = entity.AgentGuid;
if (AddAgent(loggedInUserGuid: null, agentGuid, entity.Configuration, entity.AuthSecret!, entity.RuntimeInfo)) { if (AddAgent(loggedInUserGuid: null, agentGuid, entity.Configuration, entity.AuthSecret, entity.RuntimeInfo)) {
Logger.Information("Loaded agent \"{AgentName}\" (GUID {AgentGuid}) from database.", entity.Name, agentGuid); Logger.Information("Loaded agent \"{AgentName}\" (GUID {AgentGuid}) from database.", entity.Name, agentGuid);
} }
} }
@@ -50,19 +47,6 @@ sealed class AgentManager(
return agentsByAgentGuid.TryAdd(agentGuid, actorSystem.ActorOf(AgentActor.Factory(init), name)); return agentsByAgentGuid.TryAdd(agentGuid, actorSystem.ActorOf(AgentActor.Factory(init), name));
} }
private async Task Migrate(ApplicationDbContext ctx) {
List<AgentEntity> agentsWithoutSecrets = await ctx.Agents.Where(static entity => entity.AuthSecret == null).ToListAsync(cancellationToken);
if (agentsWithoutSecrets.Count == 0) {
return;
}
foreach (var entity in agentsWithoutSecrets) {
entity.AuthSecret = AuthSecret.Generate();
}
await ctx.SaveChangesAsync(cancellationToken);
}
public async Task<ImmutableArray<ConfigureInstanceMessage>?> RegisterAgent(Guid agentGuid, AgentRegistration registration) { public async Task<ImmutableArray<ConfigureInstanceMessage>?> RegisterAgent(Guid agentGuid, AgentRegistration registration) {
if (!agentsByAgentGuid.TryGetValue(agentGuid, out var agentActor)) { if (!agentsByAgentGuid.TryGetValue(agentGuid, out var agentActor)) {
return null; return null;

View File

@@ -1,4 +1,5 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Phantom.Common.Data.Web.Users; using Phantom.Common.Data.Web.Users;
using Phantom.Controller.Database; using Phantom.Controller.Database;
using Phantom.Controller.Database.Entities; using Phantom.Controller.Database.Entities;
@@ -9,7 +10,7 @@ namespace Phantom.Controller.Services.Users.Sessions;
sealed class AuthenticatedUserCache { sealed class AuthenticatedUserCache {
private readonly ConcurrentDictionary<Guid, AuthenticatedUserInfo> authenticatedUsersByGuid = new (); private readonly ConcurrentDictionary<Guid, AuthenticatedUserInfo> authenticatedUsersByGuid = new ();
public bool TryGet(Guid userGuid, out AuthenticatedUserInfo? userInfo) { public bool TryGet(Guid userGuid, [NotNullWhen(true)] out AuthenticatedUserInfo? userInfo) {
return authenticatedUsersByGuid.TryGetValue(userGuid, out userInfo); return authenticatedUsersByGuid.TryGetValue(userGuid, out userInfo);
} }

View File

@@ -77,8 +77,13 @@ sealed class UserLoginManager {
return userGuid != null && authenticatedUserCache.TryGet(userGuid.Value, out var userInfo) ? new LoggedInUser(userInfo) : default; return userGuid != null && authenticatedUserCache.TryGet(userGuid.Value, out var userInfo) ? new LoggedInUser(userInfo) : default;
} }
public AuthenticatedUserInfo? GetAuthenticatedUser(Guid userGuid, ImmutableArray<byte> authToken) { public Optional<AuthenticatedUserInfo> GetAuthenticatedUser(Guid userGuid, ImmutableArray<byte> authToken) {
return authenticatedUserCache.TryGet(userGuid, out var userInfo) && GetSessionBucket(authToken).Contains(userGuid, authToken) ? userInfo : null; if (authenticatedUserCache.TryGet(userGuid, out var userInfo) && GetSessionBucket(authToken).Contains(userGuid, authToken)) {
return userInfo;
}
else {
return default;
}
} }
private sealed class UserSessionBucket { private sealed class UserSessionBucket {

View File

@@ -1,8 +1,8 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<TargetFramework>net9.0</TargetFramework> <TargetFramework>net10.0</TargetFramework>
<LangVersion>13</LangVersion> <LangVersion>14</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>

View File

@@ -1,7 +1,7 @@
# +---------------+ # +---------------+
# | Prepare build | # | Prepare build |
# +---------------+ # +---------------+
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/nightly/sdk:9.0 AS phantom-builder FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/nightly/sdk:10.0 AS phantom-builder
ARG TARGETARCH ARG TARGETARCH
ADD . /app ADD . /app
@@ -19,7 +19,7 @@ RUN find .artifacts/publish/*/* -maxdepth 0 -execdir mv '{}' 'release' \;
# +---------------------+ # +---------------------+
# | Phantom Agent image | # | Phantom Agent image |
# +---------------------+ # +---------------------+
FROM mcr.microsoft.com/dotnet/nightly/runtime:9.0 AS phantom-agent FROM mcr.microsoft.com/dotnet/nightly/runtime:10.0 AS phantom-agent
RUN mkdir /data && chmod 777 /data RUN mkdir /data && chmod 777 /data
WORKDIR /data WORKDIR /data
@@ -46,7 +46,7 @@ ENTRYPOINT ["dotnet", "/app/Phantom.Agent.dll"]
# +--------------------------+ # +--------------------------+
# | Phantom Controller image | # | Phantom Controller image |
# +--------------------------+ # +--------------------------+
FROM mcr.microsoft.com/dotnet/nightly/runtime:9.0 AS phantom-controller FROM mcr.microsoft.com/dotnet/nightly/runtime:10.0 AS phantom-controller
RUN mkdir /data && chmod 777 /data RUN mkdir /data && chmod 777 /data
WORKDIR /data WORKDIR /data

View File

@@ -1,15 +1,14 @@
<Project> <Project>
<ItemGroup> <ItemGroup>
<PackageReference Update="Microsoft.AspNetCore.Components.Authorization" Version="9.0.9" /> <PackageReference Update="Microsoft.AspNetCore.Components.Authorization" Version="10.0.1" />
<PackageReference Update="Microsoft.AspNetCore.Components.Web" Version="9.0.9" /> <PackageReference Update="Microsoft.AspNetCore.Components.Web" Version="10.0.1" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Update="Microsoft.EntityFrameworkCore.Relational" Version="9.0.9" /> <PackageReference Update="Microsoft.EntityFrameworkCore.Tools" Version="10.0.1" />
<PackageReference Update="Microsoft.EntityFrameworkCore.Tools" Version="9.0.9" /> <PackageReference Update="Microsoft.EntityFrameworkCore.Relational" Version="10.0.1" />
<PackageReference Update="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" /> <PackageReference Update="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.0" />
<PackageReference Update="System.Linq.Async" Version="6.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -23,7 +22,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Update="Serilog" Version="4.3.0" /> <PackageReference Update="Serilog" Version="4.3.0" />
<PackageReference Update="Serilog.AspNetCore" Version="9.0.0" /> <PackageReference Update="Serilog.AspNetCore" Version="10.0.0" />
<PackageReference Update="Serilog.Sinks.Async" Version="2.1.0" /> <PackageReference Update="Serilog.Sinks.Async" Version="2.1.0" />
<PackageReference Update="Serilog.Sinks.Console" Version="6.0.0" /> <PackageReference Update="Serilog.Sinks.Console" Version="6.0.0" />
</ItemGroup> </ItemGroup>

View File

@@ -2,17 +2,9 @@
namespace Phantom.Utils.Processes; namespace Phantom.Utils.Processes;
public sealed class OneShotProcess { public sealed class OneShotProcess(ILogger logger, ProcessConfigurator configurator) {
private readonly ILogger logger;
private readonly ProcessConfigurator configurator;
public event EventHandler<Process.Output>? OutputReceived; public event EventHandler<Process.Output>? OutputReceived;
public OneShotProcess(ILogger logger, ProcessConfigurator configurator) {
this.logger = logger;
this.configurator = configurator;
}
public async Task<bool> Run(CancellationToken cancellationToken) { public async Task<bool> Run(CancellationToken cancellationToken) {
using var process = configurator.CreateProcess(); using var process = configurator.CreateProcess();
process.OutputReceived += OutputReceived; process.OutputReceived += OutputReceived;

View File

@@ -31,6 +31,12 @@ public sealed class ProcessConfigurator {
set => startInfo.UseShellExecute = value; set => startInfo.UseShellExecute = value;
} }
public ProcessConfigurator() {
if (OperatingSystem.IsWindows()) {
startInfo.CreateNewProcessGroup = true;
}
}
public Process CreateProcess() { public Process CreateProcess() {
return new Process(new System.Diagnostics.Process { StartInfo = startInfo }); return new Process(new System.Diagnostics.Process { StartInfo = startInfo });
} }

View File

@@ -87,12 +87,7 @@ public sealed class CustomAuthenticationStateProvider : ServerAuthenticationStat
} }
var session = await controllerConnection.Send<GetAuthenticatedUser, Optional<AuthenticatedUserInfo>>(new GetAuthenticatedUser(userGuid, authToken), TimeSpan.FromSeconds(30), cancellationToken); var session = await controllerConnection.Send<GetAuthenticatedUser, Optional<AuthenticatedUserInfo>>(new GetAuthenticatedUser(userGuid, authToken), TimeSpan.FromSeconds(30), cancellationToken);
if (session.Value is {} userInfo) { return session.HasValue ? new AuthenticatedUser(session.Value, authToken) : null;
return new AuthenticatedUser(userInfo, authToken);
}
else {
return null;
}
} }
private void SetLoadedSession(AuthenticatedUser authenticatedUser) { private void SetLoadedSession(AuthenticatedUser authenticatedUser) {

View File

@@ -19,10 +19,12 @@ public sealed class UserLoginManager(Navigation navigation, UserSessionBrowserSt
return false; return false;
} }
if (result.Value is not var (userInfo, authToken)) { if (!result.HasValue) {
return false; return false;
} }
var (userInfo, authToken) = result.Value;
Logger.Information("Successfully logged in {Username}.", username); Logger.Information("Successfully logged in {Username}.", username);
authenticationStateProvider.SetUnloadedSession(); authenticationStateProvider.SetUnloadedSession();

View File

@@ -1,8 +1,9 @@
@using Phantom.Web.Services @using Phantom.Web.Errors
@using Phantom.Web.Services
@inject Navigation Navigation @inject Navigation Navigation
<CascadingAuthenticationState> <CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly"> <Router AppAssembly="@typeof(App).Assembly" NotFoundPage="typeof(NotFound)">
<Found Context="routeData"> <Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized> <NotAuthorized>
@@ -17,11 +18,5 @@
</AuthorizeRouteView> </AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1" /> <FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found> </Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<h1>Not Found</h1>
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router> </Router>
</CascadingAuthenticationState> </CascadingAuthenticationState>

View File

@@ -0,0 +1,5 @@
@page "/error/404"
@layout MainLayout
<h1>Not Found</h1>
<p role="alert">Sorry, there's nothing at this address.</p>

View File

@@ -34,7 +34,7 @@
<script src="_framework/blazor.server.js"></script> <script src="_framework/blazor.server.js"></script>
@* ReSharper restore Html.PathError *@ @* ReSharper restore Html.PathError *@
<script src="lib/bootstrap/bootstrap.bundle.min.js"></script> <script src="lib/bootstrap/bootstrap.bundle.min.js"></script>
<script src="js/site.js"></script> <script src="js/site.js?v=2"></script>
</body> </body>
</html> </html>

View File

@@ -93,7 +93,7 @@
<Cell> <Cell>
<PermissionView Permission="Permission.ManageAllAgents"> <PermissionView Permission="Permission.ManageAllAgents">
<a href="agents/@agent.AgentGuid/edit" type="button" class="btn btn-primary btn-sm">Edit Agent</a> <a href="agents/@agent.AgentGuid/edit" type="button" class="btn btn-primary btn-sm">Edit Agent</a>
<button type="button" class="btn btn-danger btn-sm" data-clipboard="@connectionKey" onclick="copyToClipboard(this);">Copy Agent Key</button> <button type="button" class="btn btn-warning btn-sm" data-clipboard="@connectionKey" onclick="copyToClipboard(this);">Copy Agent Key</button>
</PermissionView> </PermissionView>
</Cell> </Cell>
</ItemRow> </ItemRow>

View File

@@ -1,6 +1,6 @@
{ {
"sdk": { "sdk": {
"version": "9.0.0", "version": "10.0.0",
"rollForward": "latestMinor", "rollForward": "latestMinor",
"allowPrerelease": true "allowPrerelease": true
} }