mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-08-18 18:24:56 +02:00
.config
.run
.workdir
Agent
Common
Controller
Docker
Utils
Web
Phantom.Web
Base
Layout
Pages
Shared
Utils
wwwroot
App.razor
Configuration.cs
Phantom.Web.csproj
Program.cs
ServiceConfiguration.cs
Variables.cs
WebKey.cs
WebLauncher.cs
_Imports.razor
appsettings.json
Phantom.Web.Bootstrap
Phantom.Web.Components
Phantom.Web.Services
.dockerignore
.gitattributes
.gitignore
AddMigration.bat
AddMigration.sh
Directory.Build.props
Directory.Build.targets
Dockerfile
LICENSE
Packages.props
PhantomPanel.sln
README.md
global.json
61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using NetMQ;
|
|
using Phantom.Common.Data;
|
|
using Phantom.Utils.Cryptography;
|
|
using Phantom.Utils.IO;
|
|
using Phantom.Utils.Logging;
|
|
using ILogger = Serilog.ILogger;
|
|
|
|
namespace Phantom.Web;
|
|
|
|
static class WebKey {
|
|
private static ILogger Logger { get; } = PhantomLogger.Create(nameof(WebKey));
|
|
|
|
public static Task<(NetMQCertificate, AuthToken)?> Load(string? webKeyToken, string? webKeyFilePath) {
|
|
if (webKeyFilePath != null) {
|
|
return LoadFromFile(webKeyFilePath);
|
|
}
|
|
else if (webKeyToken != null) {
|
|
return Task.FromResult(LoadFromToken(webKeyToken));
|
|
}
|
|
else {
|
|
throw new InvalidOperationException();
|
|
}
|
|
}
|
|
|
|
private static async Task<(NetMQCertificate, AuthToken)?> LoadFromFile(string webKeyFilePath) {
|
|
if (!File.Exists(webKeyFilePath)) {
|
|
Logger.Fatal("Missing web key file: {WebKeyFilePath}", webKeyFilePath);
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
Files.RequireMaximumFileSize(webKeyFilePath, 64);
|
|
return LoadFromBytes(await File.ReadAllBytesAsync(webKeyFilePath));
|
|
} catch (IOException e) {
|
|
Logger.Fatal("Error loading web key from file: {WebKeyFilePath}", webKeyFilePath);
|
|
Logger.Fatal(e.Message);
|
|
return null;
|
|
} catch (Exception) {
|
|
Logger.Fatal("File does not contain a valid web key: {WebKeyFilePath}", webKeyFilePath);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static (NetMQCertificate, AuthToken)? LoadFromToken(string webKey) {
|
|
try {
|
|
return LoadFromBytes(TokenGenerator.DecodeBytes(webKey));
|
|
} catch (Exception) {
|
|
Logger.Fatal("Invalid web key: {WebKey}", webKey);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static (NetMQCertificate, AuthToken)? LoadFromBytes(byte[] webKey) {
|
|
var (publicKey, webToken) = ConnectionCommonKey.FromBytes(webKey);
|
|
var controllerCertificate = NetMQCertificate.FromPublicKey(publicKey);
|
|
|
|
Logger.Information("Loaded web key.");
|
|
return (controllerCertificate, webToken);
|
|
}
|
|
}
|