Added new console streaming
This commit is contained in:
7
Moonlight/App/Helpers/Wings/Data/ConsoleMessage.cs
Normal file
7
Moonlight/App/Helpers/Wings/Data/ConsoleMessage.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Moonlight.App.Helpers.Wings.Data;
|
||||
|
||||
public class ConsoleMessage
|
||||
{
|
||||
public string Content { get; set; } = "";
|
||||
public bool IsInternal { get; set; } = false;
|
||||
}
|
||||
36
Moonlight/App/Helpers/Wings/Data/ServerResource.cs
Normal file
36
Moonlight/App/Helpers/Wings/Data/ServerResource.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Moonlight.App.Helpers.Wings.Data;
|
||||
|
||||
public class ServerResource
|
||||
{
|
||||
[JsonProperty("memory_bytes")]
|
||||
public long MemoryBytes { get; set; }
|
||||
|
||||
[JsonProperty("memory_limit_bytes")]
|
||||
public long MemoryLimitBytes { get; set; }
|
||||
|
||||
[JsonProperty("cpu_absolute")]
|
||||
public float CpuAbsolute { get; set; }
|
||||
|
||||
[JsonProperty("network")]
|
||||
public NetworkData Network { get; set; }
|
||||
|
||||
[JsonProperty("uptime")]
|
||||
public double Uptime { get; set; }
|
||||
|
||||
[JsonProperty("state")]
|
||||
public string State { get; set; }
|
||||
|
||||
[JsonProperty("disk_bytes")]
|
||||
public long DiskBytes { get; set; }
|
||||
|
||||
public class NetworkData
|
||||
{
|
||||
[JsonProperty("rx_bytes")]
|
||||
public long RxBytes { get; set; }
|
||||
|
||||
[JsonProperty("tx_bytes")]
|
||||
public long TxBytes { get; set; }
|
||||
}
|
||||
}
|
||||
8
Moonlight/App/Helpers/Wings/Enums/ConsoleState.cs
Normal file
8
Moonlight/App/Helpers/Wings/Enums/ConsoleState.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Moonlight.App.Helpers.Wings.Enums;
|
||||
|
||||
public enum ConsoleState
|
||||
{
|
||||
Disconnected,
|
||||
Connecting,
|
||||
Connected
|
||||
}
|
||||
10
Moonlight/App/Helpers/Wings/Enums/ServerState.cs
Normal file
10
Moonlight/App/Helpers/Wings/Enums/ServerState.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Moonlight.App.Helpers.Wings.Enums;
|
||||
|
||||
public enum ServerState
|
||||
{
|
||||
Starting,
|
||||
Running,
|
||||
Stopping,
|
||||
Offline,
|
||||
Installing
|
||||
}
|
||||
7
Moonlight/App/Helpers/Wings/Events/BaseEvent.cs
Normal file
7
Moonlight/App/Helpers/Wings/Events/BaseEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Moonlight.App.Helpers.Wings.Events;
|
||||
|
||||
public class BaseEvent
|
||||
{
|
||||
public string Event { get; set; } = "";
|
||||
public string[] Args { get; set; } = Array.Empty<string>();
|
||||
}
|
||||
7
Moonlight/App/Helpers/Wings/Events/SendTokenEvent.cs
Normal file
7
Moonlight/App/Helpers/Wings/Events/SendTokenEvent.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Moonlight.App.Helpers.Wings.Events;
|
||||
|
||||
public class SendTokenEvent
|
||||
{
|
||||
public string Event { get; set; } = "auth";
|
||||
public List<string> Args = new();
|
||||
}
|
||||
377
Moonlight/App/Helpers/Wings/WingsConsole.cs
Normal file
377
Moonlight/App/Helpers/Wings/WingsConsole.cs
Normal file
@@ -0,0 +1,377 @@
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using Logging.Net;
|
||||
using Moonlight.App.Helpers.Wings.Data;
|
||||
using Moonlight.App.Helpers.Wings.Enums;
|
||||
using Moonlight.App.Helpers.Wings.Events;
|
||||
using Newtonsoft.Json;
|
||||
using ConsoleMessage = Moonlight.App.Helpers.Wings.Data.ConsoleMessage;
|
||||
|
||||
namespace Moonlight.App.Helpers.Wings;
|
||||
|
||||
public class WingsConsole : IDisposable
|
||||
{
|
||||
private ClientWebSocket WebSocket;
|
||||
public List<ConsoleMessage> Messages;
|
||||
private Task? ConsoleTask;
|
||||
|
||||
private string Socket = "";
|
||||
private string Origin = "";
|
||||
private string Token = "";
|
||||
|
||||
private bool Disconnecting;
|
||||
|
||||
public ConsoleState ConsoleState { get; private set; }
|
||||
public ServerState ServerState { get; private set; }
|
||||
public ServerResource Resource { get; private set; }
|
||||
|
||||
public EventHandler<ConsoleState> OnConsoleStateUpdated { get; set; }
|
||||
public EventHandler<ServerState> OnServerStateUpdated { get; set; }
|
||||
public EventHandler<ServerResource> OnResourceUpdated { get; set; }
|
||||
public EventHandler<ConsoleMessage> OnMessage { get; set; }
|
||||
public Func<WingsConsole, Task<string>> OnRequestNewToken { get; set; }
|
||||
|
||||
public WingsConsole()
|
||||
{
|
||||
ConsoleState = ConsoleState.Disconnected;
|
||||
ServerState = ServerState.Offline;
|
||||
Messages = new();
|
||||
|
||||
Resource = new()
|
||||
{
|
||||
Network = new()
|
||||
{
|
||||
RxBytes = 0,
|
||||
TxBytes = 0
|
||||
},
|
||||
State = "offline",
|
||||
Uptime = 0,
|
||||
CpuAbsolute = 0,
|
||||
DiskBytes = 0,
|
||||
MemoryBytes = 0,
|
||||
MemoryLimitBytes = 0
|
||||
};
|
||||
}
|
||||
|
||||
public Task Connect(string origin, string socket, string token)
|
||||
{
|
||||
Disconnecting = false;
|
||||
WebSocket = new();
|
||||
ConsoleState = ConsoleState.Disconnected;
|
||||
ServerState = ServerState.Offline;
|
||||
Messages = new();
|
||||
|
||||
Resource = new()
|
||||
{
|
||||
Network = new()
|
||||
{
|
||||
RxBytes = 0,
|
||||
TxBytes = 0
|
||||
},
|
||||
State = "offline",
|
||||
Uptime = 0,
|
||||
CpuAbsolute = 0,
|
||||
DiskBytes = 0,
|
||||
MemoryBytes = 0,
|
||||
MemoryLimitBytes = 0
|
||||
};
|
||||
|
||||
Socket = socket;
|
||||
Origin = origin;
|
||||
Token = token;
|
||||
|
||||
WebSocket.Options.SetRequestHeader("Origin", Origin);
|
||||
WebSocket.Options.SetRequestHeader("Authorization", "Bearer " + Token);
|
||||
|
||||
ConsoleTask = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await Work();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Warn("Error connecting to wings console");
|
||||
Logger.Warn(e);
|
||||
}
|
||||
});
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task Work()
|
||||
{
|
||||
await UpdateConsoleState(ConsoleState.Connecting);
|
||||
|
||||
await WebSocket.ConnectAsync(
|
||||
new Uri(Socket),
|
||||
CancellationToken.None
|
||||
);
|
||||
|
||||
if (WebSocket.State != WebSocketState.Connecting && WebSocket.State != WebSocketState.Open)
|
||||
{
|
||||
await SaveMessage("Unable to connect to websocket", true);
|
||||
await UpdateConsoleState(ConsoleState.Disconnected);
|
||||
return;
|
||||
}
|
||||
|
||||
await UpdateConsoleState(ConsoleState.Connected);
|
||||
|
||||
await Send(new SendTokenEvent()
|
||||
{
|
||||
Args = { Token }
|
||||
});
|
||||
|
||||
while (WebSocket.State == WebSocketState.Open)
|
||||
{
|
||||
try
|
||||
{
|
||||
var raw = await ReceiveRaw();
|
||||
|
||||
if(string.IsNullOrEmpty(raw))
|
||||
continue;
|
||||
|
||||
var eventData = JsonConvert.DeserializeObject<BaseEvent>(raw);
|
||||
|
||||
if (eventData == null)
|
||||
{
|
||||
await SaveMessage("Unable to parse event", true);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (eventData.Event)
|
||||
{
|
||||
case "jwt error":
|
||||
await WebSocket.CloseAsync(WebSocketCloseStatus.Empty, "Jwt error detected",
|
||||
CancellationToken.None);
|
||||
|
||||
await UpdateServerState(ServerState.Offline);
|
||||
await UpdateConsoleState(ConsoleState.Disconnected);
|
||||
|
||||
await SaveMessage("Received a jwt error", true);
|
||||
break;
|
||||
|
||||
case "token expired":
|
||||
await WebSocket.CloseAsync(WebSocketCloseStatus.Empty, "Jwt error detected",
|
||||
CancellationToken.None);
|
||||
|
||||
await UpdateServerState(ServerState.Offline);
|
||||
await UpdateConsoleState(ConsoleState.Disconnected);
|
||||
|
||||
await SaveMessage("Token expired", true);
|
||||
|
||||
break;
|
||||
|
||||
case "token expiring":
|
||||
await SaveMessage("Token will expire soon. Generating a new one", true);
|
||||
|
||||
Token = await OnRequestNewToken.Invoke(this);
|
||||
|
||||
await Send(new SendTokenEvent()
|
||||
{
|
||||
Args = { Token }
|
||||
});
|
||||
break;
|
||||
|
||||
case "auth success":
|
||||
// Send intents
|
||||
await SendRaw("{\"event\":\"send logs\",\"args\":[null]}");
|
||||
await SendRaw("{\"event\":\"send stats\",\"args\":[null]}");
|
||||
break;
|
||||
|
||||
case "stats":
|
||||
var stats = JsonConvert.DeserializeObject<ServerResource>(eventData.Args[0]);
|
||||
|
||||
if (stats == null)
|
||||
break;
|
||||
|
||||
var serverState = ParseServerState(stats.State);
|
||||
|
||||
if (ServerState != serverState)
|
||||
await UpdateServerState(serverState);
|
||||
|
||||
await UpdateResource(stats);
|
||||
break;
|
||||
|
||||
case "status":
|
||||
var serverStateParsed = ParseServerState(eventData.Args[0]);
|
||||
|
||||
if (ServerState != serverStateParsed)
|
||||
await UpdateServerState(serverStateParsed);
|
||||
break;
|
||||
|
||||
case "console output":
|
||||
foreach (var line in eventData.Args)
|
||||
{
|
||||
await SaveMessage(line);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "install output":
|
||||
foreach (var line in eventData.Args)
|
||||
{
|
||||
await SaveMessage(line);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "daemon message":
|
||||
foreach (var line in eventData.Args)
|
||||
{
|
||||
await SaveMessage(line);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "install started":
|
||||
await UpdateServerState(ServerState.Installing);
|
||||
break;
|
||||
|
||||
case "install completed":
|
||||
await UpdateServerState(ServerState.Offline);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (!Disconnecting)
|
||||
{
|
||||
Logger.Warn("Error while performing websocket actions");
|
||||
Logger.Warn(e);
|
||||
|
||||
await SaveMessage("A unknown error occured while processing websocket", true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Task UpdateConsoleState(ConsoleState consoleState)
|
||||
{
|
||||
ConsoleState = consoleState;
|
||||
OnConsoleStateUpdated?.Invoke(this, consoleState);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
private Task UpdateServerState(ServerState serverState)
|
||||
{
|
||||
ServerState = serverState;
|
||||
OnServerStateUpdated?.Invoke(this, serverState);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
private Task UpdateResource(ServerResource resource)
|
||||
{
|
||||
Resource = resource;
|
||||
OnResourceUpdated?.Invoke(this, Resource);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private Task SaveMessage(string content, bool internalMessage = false)
|
||||
{
|
||||
var msg = new ConsoleMessage()
|
||||
{
|
||||
Content = content,
|
||||
IsInternal = internalMessage
|
||||
};
|
||||
|
||||
lock (Messages)
|
||||
{
|
||||
Messages.Add(msg);
|
||||
}
|
||||
|
||||
OnMessage?.Invoke(this, msg);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
private ServerState ParseServerState(string raw)
|
||||
{
|
||||
switch (raw)
|
||||
{
|
||||
case "offline":
|
||||
return ServerState.Offline;
|
||||
case "starting":
|
||||
return ServerState.Starting;
|
||||
case "running":
|
||||
return ServerState.Running;
|
||||
case "stopping":
|
||||
return ServerState.Stopping;
|
||||
case "installing":
|
||||
return ServerState.Installing;
|
||||
default:
|
||||
return ServerState.Offline;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task EnterCommand(string content)
|
||||
{
|
||||
if (ConsoleState == ConsoleState.Connected)
|
||||
{
|
||||
await SendRaw("{\"event\":\"send command\",\"args\":[\"" + content + "\"]}");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SetPowerState(string state)
|
||||
{
|
||||
if (ConsoleState == ConsoleState.Connected)
|
||||
{
|
||||
await SendRaw("{\"event\":\"set state\",\"args\":[\"" + state + "\"]}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Send(object data)
|
||||
{
|
||||
await SendRaw(JsonConvert.SerializeObject(data));
|
||||
}
|
||||
|
||||
private async Task SendRaw(string data)
|
||||
{
|
||||
if (WebSocket.State == WebSocketState.Open)
|
||||
{
|
||||
byte[] byteContentBuffer = Encoding.UTF8.GetBytes(data);
|
||||
await WebSocket.SendAsync(new ArraySegment<byte>(byteContentBuffer), WebSocketMessageType.Text, true,
|
||||
CancellationToken.None);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> ReceiveRaw()
|
||||
{
|
||||
ArraySegment<byte> receivedBytes = new ArraySegment<byte>(new byte[1024]);
|
||||
WebSocketReceiveResult result = await WebSocket.ReceiveAsync(receivedBytes, CancellationToken.None);
|
||||
return Encoding.UTF8.GetString(receivedBytes.Array!, 0, result.Count);
|
||||
}
|
||||
|
||||
public async Task Disconnect()
|
||||
{
|
||||
Disconnecting = true;
|
||||
|
||||
if (WebSocket != null)
|
||||
{
|
||||
if (WebSocket.State == WebSocketState.Connecting || WebSocket.State == WebSocketState.Open)
|
||||
await WebSocket.CloseAsync(WebSocketCloseStatus.Empty, null, CancellationToken.None);
|
||||
|
||||
WebSocket.Dispose();
|
||||
}
|
||||
|
||||
if(ConsoleTask != null && ConsoleTask.IsCompleted)
|
||||
ConsoleTask.Dispose();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Disconnecting = true;
|
||||
|
||||
if (WebSocket != null)
|
||||
{
|
||||
if (WebSocket.State == WebSocketState.Connecting || WebSocket.State == WebSocketState.Open)
|
||||
WebSocket.CloseAsync(WebSocketCloseStatus.Empty, null, CancellationToken.None).Wait();
|
||||
|
||||
WebSocket.Dispose();
|
||||
}
|
||||
|
||||
if(ConsoleTask != null && ConsoleTask.IsCompleted)
|
||||
ConsoleTask.Dispose();
|
||||
}
|
||||
}
|
||||
101
Moonlight/App/Helpers/Wings/WingsConsoleHelper.cs
Normal file
101
Moonlight/App/Helpers/Wings/WingsConsoleHelper.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using JWT.Algorithms;
|
||||
using JWT.Builder;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.App.Database.Entities;
|
||||
using Moonlight.App.Repositories.Servers;
|
||||
using Moonlight.App.Services;
|
||||
|
||||
namespace Moonlight.App.Helpers.Wings;
|
||||
|
||||
public class WingsConsoleHelper
|
||||
{
|
||||
private readonly ServerRepository ServerRepository;
|
||||
private readonly string AppUrl;
|
||||
|
||||
public WingsConsoleHelper(
|
||||
ServerRepository serverRepository,
|
||||
ConfigService configService)
|
||||
{
|
||||
ServerRepository = serverRepository;
|
||||
|
||||
AppUrl = configService.GetSection("Moonlight").GetValue<string>("AppUrl");
|
||||
}
|
||||
|
||||
public async Task ConnectWings(WingsConsole console, Server server)
|
||||
{
|
||||
var serverData = ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Node)
|
||||
.First(x => x.Id == server.Id);
|
||||
|
||||
var token = await GenerateToken(serverData);
|
||||
|
||||
if (serverData.Node.Ssl)
|
||||
{
|
||||
await console.Connect(
|
||||
AppUrl,
|
||||
$"wss://{serverData.Node.Fqdn}:{serverData.Node.HttpPort}/api/servers/{serverData.Uuid}/ws",
|
||||
token
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
await console.Connect(
|
||||
AppUrl,
|
||||
$"ws://{serverData.Node.Fqdn}:{serverData.Node.HttpPort}/api/servers/{serverData.Uuid}/ws",
|
||||
token
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GenerateToken(Server server)
|
||||
{
|
||||
var serverData = ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Node)
|
||||
.First(x => x.Id == server.Id);
|
||||
|
||||
var userid = 1;
|
||||
var secret = serverData.Node.Token;
|
||||
|
||||
|
||||
using (MD5 md5 = MD5.Create())
|
||||
{
|
||||
var inputBytes = Encoding.ASCII.GetBytes(userid + server.Uuid.ToString());
|
||||
var outputBytes = md5.ComputeHash(inputBytes);
|
||||
|
||||
var identifier = Convert.ToHexString(outputBytes).ToLower();
|
||||
var weirdId = StringHelper.GenerateString(16);
|
||||
|
||||
var token = JwtBuilder.Create()
|
||||
.AddHeader("jti", identifier)
|
||||
.WithAlgorithm(new HMACSHA256Algorithm())
|
||||
.WithSecret(secret)
|
||||
.AddClaim("user_id", userid)
|
||||
.AddClaim("server_uuid", server.Uuid.ToString())
|
||||
.AddClaim("permissions", new[]
|
||||
{
|
||||
"*",
|
||||
"admin.websocket.errors",
|
||||
"admin.websocket.install",
|
||||
"admin.websocket.transfer"
|
||||
})
|
||||
.AddClaim("jti", identifier)
|
||||
.AddClaim("unique_id", weirdId)
|
||||
.AddClaim("iat", DateTimeOffset.Now.ToUnixTimeSeconds())
|
||||
.AddClaim("nbf", DateTimeOffset.Now.AddSeconds(-10).ToUnixTimeSeconds())
|
||||
.AddClaim("exp", DateTimeOffset.Now.AddMinutes(10).ToUnixTimeSeconds())
|
||||
.AddClaim("iss", AppUrl)
|
||||
.AddClaim("aud", new[]
|
||||
{
|
||||
serverData.Node.Ssl ? $"https://{serverData.Node.Fqdn}" : $"http://{serverData.Node.Fqdn}"
|
||||
})
|
||||
.MustVerifySignature()
|
||||
.Encode();
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
||||
}
|
||||
56
Moonlight/App/Helpers/Wings/WingsJwtHelper.cs
Normal file
56
Moonlight/App/Helpers/Wings/WingsJwtHelper.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using JWT.Algorithms;
|
||||
using JWT.Builder;
|
||||
using Moonlight.App.Services;
|
||||
|
||||
namespace Moonlight.App.Helpers.Wings;
|
||||
|
||||
public class WingsJwtHelper
|
||||
{
|
||||
private readonly ConfigService ConfigService;
|
||||
private readonly string AppUrl;
|
||||
|
||||
public WingsJwtHelper(ConfigService configService)
|
||||
{
|
||||
ConfigService = configService;
|
||||
|
||||
AppUrl = ConfigService.GetSection("Moonlight").GetValue<string>("AppUrl");
|
||||
}
|
||||
|
||||
public string Generate(string secret, Action<Dictionary<string, string>> claimsAction)
|
||||
{
|
||||
var userid = 1;
|
||||
|
||||
using MD5 md5 = MD5.Create();
|
||||
var inputBytes = Encoding.ASCII.GetBytes(userid + Guid.NewGuid().ToString());
|
||||
var outputBytes = md5.ComputeHash(inputBytes);
|
||||
|
||||
var identifier = Convert.ToHexString(outputBytes).ToLower();
|
||||
var weirdId = StringHelper.GenerateString(16);
|
||||
|
||||
var builder = JwtBuilder.Create()
|
||||
.AddHeader("jti", identifier)
|
||||
.WithAlgorithm(new HMACSHA256Algorithm())
|
||||
.WithSecret(secret)
|
||||
.AddClaim("user_id", userid)
|
||||
.AddClaim("jti", identifier)
|
||||
.AddClaim("unique_id", weirdId)
|
||||
.AddClaim("iat", DateTimeOffset.Now.ToUnixTimeSeconds())
|
||||
.AddClaim("nbf", DateTimeOffset.Now.AddSeconds(-10).ToUnixTimeSeconds())
|
||||
.AddClaim("exp", DateTimeOffset.Now.AddMinutes(10).ToUnixTimeSeconds())
|
||||
.AddClaim("iss", AppUrl)
|
||||
.MustVerifySignature();
|
||||
|
||||
var additionalClaims = new Dictionary<string, string>();
|
||||
|
||||
claimsAction.Invoke(additionalClaims);
|
||||
|
||||
foreach (var claim in additionalClaims)
|
||||
{
|
||||
builder = builder.AddClaim(claim.Key, claim.Value);
|
||||
}
|
||||
|
||||
return builder.Encode();
|
||||
}
|
||||
}
|
||||
142
Moonlight/App/Helpers/Wings/WingsServerConverter.cs
Normal file
142
Moonlight/App/Helpers/Wings/WingsServerConverter.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.App.Database.Entities;
|
||||
using Moonlight.App.Http.Resources.Wings;
|
||||
using Moonlight.App.Repositories;
|
||||
using Moonlight.App.Repositories.Servers;
|
||||
|
||||
namespace Moonlight.App.Helpers.Wings;
|
||||
|
||||
public class WingsServerConverter
|
||||
{
|
||||
private readonly ServerRepository ServerRepository;
|
||||
private readonly ImageRepository ImageRepository;
|
||||
|
||||
public WingsServerConverter(ServerRepository serverRepository, ImageRepository imageRepository)
|
||||
{
|
||||
ServerRepository = serverRepository;
|
||||
ImageRepository = imageRepository;
|
||||
}
|
||||
|
||||
public WingsServer FromServer(Server s)
|
||||
{
|
||||
var server = ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Allocations)
|
||||
.Include(x => x.Backups)
|
||||
.Include(x => x.Variables)
|
||||
.Include(x => x.Image)
|
||||
.Include(x => x.MainAllocation)
|
||||
.First(x => x.Id == s.Id);
|
||||
|
||||
var wingsServer = new WingsServer
|
||||
{
|
||||
Uuid = server.Uuid
|
||||
};
|
||||
|
||||
// Allocations
|
||||
var def = server.MainAllocation;
|
||||
|
||||
wingsServer.Settings.Allocations.Default.Ip = "0.0.0.0";
|
||||
wingsServer.Settings.Allocations.Default.Port = def.Port;
|
||||
|
||||
foreach (var a in server.Allocations)
|
||||
{
|
||||
wingsServer.Settings.Allocations.Mappings.Ports.Add(a.Port);
|
||||
}
|
||||
|
||||
// Build
|
||||
wingsServer.Settings.Build.Swap = server.Memory * 2; //TODO: Add config option
|
||||
wingsServer.Settings.Build.Threads = null!;
|
||||
wingsServer.Settings.Build.Cpu_Limit = server.Cpu;
|
||||
wingsServer.Settings.Build.Disk_Space = server.Disk;
|
||||
wingsServer.Settings.Build.Io_Weight = 500;
|
||||
wingsServer.Settings.Build.Memory_Limit = server.Memory;
|
||||
wingsServer.Settings.Build.Oom_Disabled = true;
|
||||
wingsServer.Settings.Build.Oom_Killer = false;
|
||||
|
||||
var image = ImageRepository
|
||||
.Get()
|
||||
.Include(x => x.DockerImages)
|
||||
.First(x => x.Id == server.Image.Id);
|
||||
|
||||
// Container
|
||||
wingsServer.Settings.Container.Image = image.DockerImages[server.DockerImageIndex].Name;
|
||||
|
||||
// Egg
|
||||
wingsServer.Settings.Egg.Id = image.Uuid;
|
||||
|
||||
// Settings
|
||||
wingsServer.Settings.Skip_Egg_Scripts = false;
|
||||
wingsServer.Settings.Suspended = server.Suspended;
|
||||
wingsServer.Settings.Invocation = string.IsNullOrEmpty(server.OverrideStartup) ? image.Startup : server.OverrideStartup;
|
||||
wingsServer.Settings.Uuid = server.Uuid;
|
||||
|
||||
|
||||
// Environment
|
||||
foreach (var v in server.Variables)
|
||||
{
|
||||
if (!wingsServer.Settings.Environment.ContainsKey(v.Key))
|
||||
{
|
||||
wingsServer.Settings.Environment.Add(v.Key, v.Value);
|
||||
}
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
foreach (var allocation in server.Allocations)
|
||||
{
|
||||
wingsServer.Settings.Environment.Add("ML_PORT_" + i, allocation.Port.ToString());
|
||||
i++;
|
||||
}
|
||||
|
||||
// Stop
|
||||
if (image.StopCommand.StartsWith("!"))
|
||||
{
|
||||
wingsServer.Process_Configuration.Stop.Type = "stop";
|
||||
wingsServer.Process_Configuration.Stop.Value = null!;
|
||||
}
|
||||
else
|
||||
{
|
||||
wingsServer.Process_Configuration.Stop.Type = "command";
|
||||
wingsServer.Process_Configuration.Stop.Value = image.StopCommand;
|
||||
}
|
||||
|
||||
// Done
|
||||
|
||||
wingsServer.Process_Configuration.Startup.Done = new() { image.StartupDetection };
|
||||
wingsServer.Process_Configuration.Startup.Strip_Ansi = false;
|
||||
wingsServer.Process_Configuration.Startup.User_Interaction = new();
|
||||
|
||||
// Configs
|
||||
var configData = new ConfigurationBuilder().AddJsonStream(
|
||||
new MemoryStream(Encoding.ASCII.GetBytes(image.ConfigFiles!))
|
||||
).Build();
|
||||
|
||||
foreach (var child in configData.GetChildren())
|
||||
{
|
||||
List<WingsServer.WingsServerReplace> replaces = new();
|
||||
|
||||
foreach (var section in child.GetSection("find").GetChildren())
|
||||
{
|
||||
if (section.Value != null)
|
||||
{
|
||||
replaces.Add(new()
|
||||
{
|
||||
Match = section.Key,
|
||||
Replace_With = section.Value
|
||||
.Replace("{{server.build.default.port}}", def.Port.ToString())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
wingsServer.Process_Configuration.Configs.Add(new()
|
||||
{
|
||||
Parser = child.GetValue<string>("parser"),
|
||||
File = child.Key,
|
||||
Replace = replaces
|
||||
});
|
||||
}
|
||||
|
||||
return wingsServer;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user