From 8de2aa1bcaccf6f742314f0300a6c8dcd3f9e27f Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Wed, 12 Apr 2023 16:42:40 +0200 Subject: [PATCH 1/4] Update langs --- Moonlight/resources/lang/de_de.lang | 10 ++++++++++ Moonlight/resources/lang/en_us.lang | 1 + 2 files changed, 11 insertions(+) diff --git a/Moonlight/resources/lang/de_de.lang b/Moonlight/resources/lang/de_de.lang index 1b1909d..95a8293 100644 --- a/Moonlight/resources/lang/de_de.lang +++ b/Moonlight/resources/lang/de_de.lang @@ -536,3 +536,13 @@ Configure your domain;Configure your domain You reached the maximum amount of domains in your subscription;You reached the maximum amount of domains in your subscription You need to specify a shared domain;You need to specify a shared domain A domain with this name does already exist for this shared domain;A domain with this name does already exist for this shared domain +Cleanup exception;Cleanup exception +No shared domain found;No shared domain found +Searching for deploy plesk server;Searching for deploy plesk server +No plesk server found;No plesk server found +No plesk server found to deploy to;No plesk server found to deploy to +No node found to deploy to;No node found to deploy to +Website details;Website details +Configure your website;Configure your website +The name cannot be longer that 32 characters;The name cannot be longer that 32 characters +The name should only consist of lower case characters;The name should only consist of lower case characters diff --git a/Moonlight/resources/lang/en_us.lang b/Moonlight/resources/lang/en_us.lang index 88f53b6..2e2e1fc 100644 --- a/Moonlight/resources/lang/en_us.lang +++ b/Moonlight/resources/lang/en_us.lang @@ -83,3 +83,4 @@ Welcome to the support chat. Ask your question here and we will help you;Welcome less than a minute ago;less than a minute ago The support team has been notified. Please be patient;The support team has been notified. Please be patient is typing;is typing +Proccessing;Proccessing From c3d7f7e36e62c51f7c3ac555e4903bde859bdff7 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Wed, 12 Apr 2023 16:52:09 +0200 Subject: [PATCH 2/4] Implemented new soft error boundary crash handler --- .../ErrorBoundaries/SoftErrorBoundary.razor | 29 ++++++- Moonlight/Shared/Layouts/MainLayout.razor | 82 +++++++++---------- Moonlight/Shared/Views/Test.razor | 29 +------ 3 files changed, 67 insertions(+), 73 deletions(-) diff --git a/Moonlight/Shared/Components/ErrorBoundaries/SoftErrorBoundary.razor b/Moonlight/Shared/Components/ErrorBoundaries/SoftErrorBoundary.razor index 7f56120..3d59dd7 100644 --- a/Moonlight/Shared/Components/ErrorBoundaries/SoftErrorBoundary.razor +++ b/Moonlight/Shared/Components/ErrorBoundaries/SoftErrorBoundary.razor @@ -7,13 +7,35 @@ @inject AlertService AlertService @inject SmartTranslateService SmartTranslateService -@ChildContent +@if (Crashed) +{ +
+
+
+
+ + Ooops. This page is crashed + +
+
+ This page is crashed. The error has been reported to the moonlight team. Meanwhile you can try reloading the page +
+
+
+
+} +else +{ + @ChildContent +} @code { + private bool Crashed = false; + protected override async Task OnErrorAsync(Exception exception) { - Logger.Debug(exception); + Logger.Warn(exception); if (exception is DisplayException displayException) { @@ -49,7 +71,8 @@ } else { - throw exception; + Crashed = true; + await InvokeAsync(StateHasChanged); } } } \ No newline at end of file diff --git a/Moonlight/Shared/Layouts/MainLayout.razor b/Moonlight/Shared/Layouts/MainLayout.razor index 4c42ecb..46017b5 100644 --- a/Moonlight/Shared/Layouts/MainLayout.razor +++ b/Moonlight/Shared/Layouts/MainLayout.razor @@ -59,57 +59,55 @@
- - - @if (uri.LocalPath != "/login" && - uri.LocalPath != "/passwordreset" && - uri.LocalPath != "/register") + + @if (uri.LocalPath != "/login" && + uri.LocalPath != "/passwordreset" && + uri.LocalPath != "/register") + { + if (User == null) { - if (User == null) - { - - } - else - { - if (User.Status == UserStatus.Banned) - { - - } - else if (User.Status == UserStatus.Disabled) - { - - } - else if (User.Status == UserStatus.PasswordPending) - { - - } - else if (User.Status == UserStatus.DataPending) - { - - } - else - { - @Body - } - } + } else { - if (uri.LocalPath == "/login") + if (User.Status == UserStatus.Banned) { - + } - else if (uri.LocalPath == "/register") + else if (User.Status == UserStatus.Disabled) { - + } - else if (uri.LocalPath == "/passwordreset") + else if (User.Status == UserStatus.PasswordPending) { - + + } + else if (User.Status == UserStatus.DataPending) + { + + } + else + { + @Body } } - - + } + else + { + if (uri.LocalPath == "/login") + { + + } + else if (uri.LocalPath == "/register") + { + + } + else if (uri.LocalPath == "/passwordreset") + { + + } + } +
@@ -161,8 +159,8 @@ await JsRuntime.InvokeVoidAsync("document.body.removeAttribute", "data-kt-app-page-loading"); await JsRuntime.InvokeVoidAsync("KTMenu.createInstances"); await JsRuntime.InvokeVoidAsync("KTDrawer.createInstances"); - - //await JsRuntime.InvokeVoidAsync("createSnow"); + + //await JsRuntime.InvokeVoidAsync("createSnow"); await SessionService.Register(); diff --git a/Moonlight/Shared/Views/Test.razor b/Moonlight/Shared/Views/Test.razor index 88c12f0..906f424 100644 --- a/Moonlight/Shared/Views/Test.razor +++ b/Moonlight/Shared/Views/Test.razor @@ -1,39 +1,12 @@ @page "/test" -@using Moonlight.App.Repositories -@using Moonlight.App.Database.Entities -@using Moonlight.App.Models.Forms - -@inject UserRepository UserRepository - -
- -
-
- -
-
@code { - private User[] Users; - private TestDataModel Model = new(); - private Task Load(LazyLoader arg) { - Users = UserRepository.Get().ToArray(); - - return Task.CompletedTask; - } - - private Task OnValidSubmit() - { - return Task.CompletedTask; + throw new Exception("Nein"); } } From ba373b86ee4a0987665f377f8be661f8bc54c899 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Wed, 12 Apr 2023 17:06:29 +0200 Subject: [PATCH 3/4] Added ip locate. Fixed oauth2 mail send. Fixed mail send when generating tokens --- .../Api/Moonlight/OAuth2Controller.cs | 4 +-- Moonlight/App/Services/UserService.cs | 29 ++++++++++++++----- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs b/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs index 7452ce4..3d1abb4 100644 --- a/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs +++ b/Moonlight/App/Http/Controllers/Api/Moonlight/OAuth2Controller.cs @@ -58,7 +58,7 @@ public class OAuth2Controller : Controller } else { - token = await UserService.GenerateToken(user); + token = await UserService.GenerateToken(user, true); } Response.Cookies.Append("token", token, new () @@ -116,7 +116,7 @@ public class OAuth2Controller : Controller } else { - token = await UserService.GenerateToken(user); + token = await UserService.GenerateToken(user, true); } Response.Cookies.Append("token", token, new () diff --git a/Moonlight/App/Services/UserService.cs b/Moonlight/App/Services/UserService.cs index 9cf6aa7..6a6fe02 100644 --- a/Moonlight/App/Services/UserService.cs +++ b/Moonlight/App/Services/UserService.cs @@ -18,6 +18,7 @@ public class UserService private readonly AuditLogService AuditLogService; private readonly MailService MailService; private readonly IdentityService IdentityService; + private readonly IpLocateService IpLocateService; private readonly string JwtSecret; @@ -28,7 +29,7 @@ public class UserService SecurityLogService securityLogService, AuditLogService auditLogService, MailService mailService, - IdentityService identityService) + IdentityService identityService, IpLocateService ipLocateService) { UserRepository = userRepository; TotpService = totpService; @@ -36,6 +37,7 @@ public class UserService AuditLogService = auditLogService; MailService = mailService; IdentityService = identityService; + IpLocateService = ipLocateService; JwtSecret = configService .GetSection("Moonlight") @@ -77,6 +79,7 @@ public class UserService }); await MailService.SendMail(user!, "register", values => {}); + await AuditLogService.Log(AuditLogType.Register, x => { x.Add(user.Email); @@ -177,11 +180,13 @@ public class UserService } else { + var location = await IpLocateService.GetLocation(); + await MailService.SendMail(user!, "passwordChange", values => { values.Add("Ip", IdentityService.GetIp()); values.Add("Device", IdentityService.GetDevice()); - values.Add("Location", "In your walls"); + values.Add("Location", location); }); await AuditLogService.Log(AuditLogType.ChangePassword, x => @@ -201,6 +206,7 @@ public class UserService { x.Add(id); }); + throw new Exception("Invalid username"); } @@ -223,12 +229,17 @@ public class UserService public async Task GenerateToken(User user, bool sendMail = false) { - await MailService.SendMail(user!, "login", values => + var location = await IpLocateService.GetLocation(); + + if (sendMail) { - values.Add("Ip", IdentityService.GetIp()); - values.Add("Device", IdentityService.GetDevice()); - values.Add("Location", "In your walls"); - }); + await MailService.SendMail(user!, "login", values => + { + values.Add("Ip", IdentityService.GetIp()); + values.Add("Device", IdentityService.GetDevice()); + values.Add("Location", location); + }); + } var token = JwtBuilder.Create() .WithAlgorithm(new HMACSHA256Algorithm()) @@ -257,11 +268,13 @@ public class UserService await AuditLogService.Log(AuditLogType.PasswordReset, x => {}); + var location = await IpLocateService.GetLocation(); + await MailService.SendMail(user, "passwordReset", values => { values.Add("Ip", IdentityService.GetIp()); values.Add("Device", IdentityService.GetDevice()); - values.Add("Location", "In your walls"); + values.Add("Location", location); values.Add("Password", newPassword); }); } From cf287173b807d8f0268a841f93fdac81d92dc921 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Wed, 12 Apr 2023 17:42:24 +0200 Subject: [PATCH 4/4] Implemented multi allocation --- Moonlight/App/Services/ServerService.cs | 52 ++++++++++----------- Moonlight/Shared/Views/Servers/Create.razor | 4 +- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/Moonlight/App/Services/ServerService.cs b/Moonlight/App/Services/ServerService.cs index bc22343..ccde602 100644 --- a/Moonlight/App/Services/ServerService.cs +++ b/Moonlight/App/Services/ServerService.cs @@ -195,7 +195,7 @@ public class ServerService ServerRepository.Update(serverData); await MessageService.Emit("wings.backups.delete", backup); - + await AuditLogService.Log(AuditLogType.DeleteBackup, x => { @@ -213,7 +213,7 @@ public class ServerService claims.Add("server_uuid", server.Uuid.ToString()); claims.Add("backup_uuid", serverBackup.Uuid.ToString()); }); - + await AuditLogService.Log(AuditLogType.DownloadBackup, x => { @@ -221,7 +221,10 @@ public class ServerService x.Add(serverBackup.Uuid); }); - return $"https://{server.Node.Fqdn}:{server.Node.HttpPort}/download/backup?token={token}"; + if (server.Node.Ssl) + return $"https://{server.Node.Fqdn}:{server.Node.HttpPort}/download/backup?token={token}"; + else + return $"http://{server.Node.Fqdn}:{server.Node.HttpPort}/download/backup?token={token}"; } public Task CreateFileAccess(Server s, User user) // We need the user to create the launch url @@ -240,7 +243,7 @@ public class ServerService } public async Task Create(string name, int cpu, long memory, long disk, User u, Image i, Node? n = null, - Action? modifyDetails = null) + Action? modifyDetails = null, int allocations = 1) { var user = UserRepository .Get() @@ -264,22 +267,27 @@ public class ServerService else node = n; - NodeAllocation freeAllo; + NodeAllocation[] freeAllocations; try { - freeAllo = node.Allocations.First(a => !ServerRepository.Get() - .SelectMany(s => s.Allocations) - .Any(b => b.Id == a.Id)); // Thank you ChatGPT <3 + freeAllocations = node.Allocations + .Where(a => !ServerRepository.Get() + .SelectMany(s => s.Allocations) + .Any(b => b.Id == a.Id)) + .Take(allocations).ToArray(); } catch (Exception) { throw new DisplayException("No allocation found"); } - if (freeAllo == null) + if (!freeAllocations.Any()) throw new DisplayException("No allocation found"); + if (freeAllocations.Length != allocations) + throw new DisplayException("Not enough allocations found"); + var server = new Server() { Cpu = cpu, @@ -290,11 +298,8 @@ public class ServerService Owner = user, Node = node, Uuid = Guid.NewGuid(), - MainAllocation = freeAllo, - Allocations = new() - { - freeAllo - }, + MainAllocation = freeAllocations.First(), + Allocations = freeAllocations.ToList(), Backups = new(), OverrideStartup = "", DockerImageIndex = image.DockerImages.FindIndex(x => x.Default) @@ -322,10 +327,7 @@ public class ServerService StartOnCompletion = false }); - await AuditLogService.Log(AuditLogType.CreateServer, x => - { - x.Add(newServerData.Uuid); - }); + await AuditLogService.Log(AuditLogType.CreateServer, x => { x.Add(newServerData.Uuid); }); return newServerData; } @@ -333,7 +335,7 @@ public class ServerService { await ErrorLogService.Log(e, x => { - x.Add(newServerData.Uuid); + x.Add(newServerData.Uuid); x.Add(node.Id); }); @@ -349,10 +351,7 @@ public class ServerService await WingsApiHelper.Post(server.Node, $"api/servers/{server.Uuid}/reinstall", null); - await AuditLogService.Log(AuditLogType.ReinstallServer, x => - { - x.Add(server.Uuid); - }); + await AuditLogService.Log(AuditLogType.ReinstallServer, x => { x.Add(server.Uuid); }); } public async Task SftpServerLogin(int serverId, int id, string password) @@ -361,10 +360,7 @@ public class ServerService if (server == null) { - await SecurityLogService.LogSystem(SecurityLogType.SftpBruteForce, x => - { - x.Add(id); - }); + await SecurityLogService.LogSystem(SecurityLogType.SftpBruteForce, x => { x.Add(id); }); throw new Exception("Server not found"); } @@ -393,7 +389,7 @@ public class ServerService var server = EnsureNodeData(s); await WingsApiHelper.Delete(server.Node, $"api/servers/{server.Uuid}", null); - + ServerRepository.Delete(s); } diff --git a/Moonlight/Shared/Views/Servers/Create.razor b/Moonlight/Shared/Views/Servers/Create.razor index bc372c1..b6c6c31 100644 --- a/Moonlight/Shared/Views/Servers/Create.razor +++ b/Moonlight/Shared/Views/Servers/Create.razor @@ -209,7 +209,9 @@ disk, User, Model.Image, - DeployNode + DeployNode, + null, + Model.Image.Allocations ); NavigationManager.NavigateTo($"/server/{server.Uuid}");