Added new console streaming
This commit is contained in:
@@ -3,6 +3,7 @@ using Moonlight.App.ApiClients.Wings;
|
||||
using Moonlight.App.ApiClients.Wings.Requests;
|
||||
using Moonlight.App.ApiClients.Wings.Resources;
|
||||
using Moonlight.App.Database.Entities;
|
||||
using Moonlight.App.Helpers.Wings;
|
||||
using Moonlight.App.Services;
|
||||
using RestSharp;
|
||||
|
||||
|
||||
@@ -8,7 +8,14 @@ public static class Formatter
|
||||
{
|
||||
TimeSpan t = TimeSpan.FromMilliseconds(uptime);
|
||||
|
||||
return $"{t.Days}d {t.Hours}h {t.Minutes}m {t.Seconds}s";
|
||||
if (t.Days > 0)
|
||||
{
|
||||
return $"{t.Days}d {t.Hours}h {t.Minutes}m {t.Seconds}s";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"{t.Hours}h {t.Minutes}m {t.Seconds}s";
|
||||
}
|
||||
}
|
||||
|
||||
private static double Round(this double d, int decimals)
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -7,37 +7,34 @@ using Moonlight.App.Database.Entities;
|
||||
using Moonlight.App.Repositories.Servers;
|
||||
using Moonlight.App.Services;
|
||||
|
||||
namespace Moonlight.App.Helpers;
|
||||
namespace Moonlight.App.Helpers.Wings;
|
||||
|
||||
public class WingsConsoleHelper
|
||||
{
|
||||
private readonly ServerRepository ServerRepository;
|
||||
private readonly WingsJwtHelper WingsJwtHelper;
|
||||
private readonly string AppUrl;
|
||||
|
||||
public WingsConsoleHelper(
|
||||
ServerRepository serverRepository,
|
||||
ConfigService configService,
|
||||
WingsJwtHelper wingsJwtHelper)
|
||||
ConfigService configService)
|
||||
{
|
||||
ServerRepository = serverRepository;
|
||||
WingsJwtHelper = wingsJwtHelper;
|
||||
|
||||
AppUrl = configService.GetSection("Moonlight").GetValue<string>("AppUrl");
|
||||
}
|
||||
|
||||
public async Task ConnectWings(PteroConsole.NET.PteroConsole pteroConsole, Server server)
|
||||
public async Task ConnectWings(WingsConsole console, Server server)
|
||||
{
|
||||
var serverData = ServerRepository
|
||||
.Get()
|
||||
.Include(x => x.Node)
|
||||
.First(x => x.Id == server.Id);
|
||||
|
||||
var token = GenerateToken(serverData);
|
||||
|
||||
var token = await GenerateToken(serverData);
|
||||
|
||||
if (serverData.Node.Ssl)
|
||||
{
|
||||
await pteroConsole.Connect(
|
||||
await console.Connect(
|
||||
AppUrl,
|
||||
$"wss://{serverData.Node.Fqdn}:{serverData.Node.HttpPort}/api/servers/{serverData.Uuid}/ws",
|
||||
token
|
||||
@@ -45,7 +42,7 @@ public class WingsConsoleHelper
|
||||
}
|
||||
else
|
||||
{
|
||||
await pteroConsole.Connect(
|
||||
await console.Connect(
|
||||
AppUrl,
|
||||
$"ws://{serverData.Node.Fqdn}:{serverData.Node.HttpPort}/api/servers/{serverData.Uuid}/ws",
|
||||
token
|
||||
@@ -53,20 +50,20 @@ public class WingsConsoleHelper
|
||||
}
|
||||
}
|
||||
|
||||
public string GenerateToken(Server server)
|
||||
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 + serverData.Uuid.ToString());
|
||||
var inputBytes = Encoding.ASCII.GetBytes(userid + server.Uuid.ToString());
|
||||
var outputBytes = md5.ComputeHash(inputBytes);
|
||||
|
||||
var identifier = Convert.ToHexString(outputBytes).ToLower();
|
||||
@@ -77,7 +74,7 @@ public class WingsConsoleHelper
|
||||
.WithAlgorithm(new HMACSHA256Algorithm())
|
||||
.WithSecret(secret)
|
||||
.AddClaim("user_id", userid)
|
||||
.AddClaim("server_uuid", serverData.Uuid.ToString())
|
||||
.AddClaim("server_uuid", server.Uuid.ToString())
|
||||
.AddClaim("permissions", new[]
|
||||
{
|
||||
"*",
|
||||
@@ -4,7 +4,7 @@ using JWT.Algorithms;
|
||||
using JWT.Builder;
|
||||
using Moonlight.App.Services;
|
||||
|
||||
namespace Moonlight.App.Helpers;
|
||||
namespace Moonlight.App.Helpers.Wings;
|
||||
|
||||
public class WingsJwtHelper
|
||||
{
|
||||
@@ -5,7 +5,7 @@ using Moonlight.App.Http.Resources.Wings;
|
||||
using Moonlight.App.Repositories;
|
||||
using Moonlight.App.Repositories.Servers;
|
||||
|
||||
namespace Moonlight.App.Helpers;
|
||||
namespace Moonlight.App.Helpers.Wings;
|
||||
|
||||
public class WingsServerConverter
|
||||
{
|
||||
Reference in New Issue
Block a user