From bf6641c1515d11033fe480c29ed6ad9bf4aeedb8 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Wed, 14 Feb 2024 12:07:14 +0100 Subject: [PATCH] Reimplemented config in diagnose file and added node data --- .../Extensions/ConfigServiceExtensions.cs | 22 ++++++++++ Moonlight/Core/Services/MoonlightService.cs | 44 ++++++++++++------- 2 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 Moonlight/Core/Extensions/ConfigServiceExtensions.cs diff --git a/Moonlight/Core/Extensions/ConfigServiceExtensions.cs b/Moonlight/Core/Extensions/ConfigServiceExtensions.cs new file mode 100644 index 00000000..481096fa --- /dev/null +++ b/Moonlight/Core/Extensions/ConfigServiceExtensions.cs @@ -0,0 +1,22 @@ +using MoonCore.Services; +using Moonlight.Core.Configuration; +using Newtonsoft.Json; + +namespace Moonlight.Core.Extensions; + +public static class ConfigServiceExtensions +{ + public static string GetDiagnosticJson(this ConfigService configService) + { + var jsonUnsafe = JsonConvert.SerializeObject(configService.Get()); + var configUnsafe = JsonConvert.DeserializeObject(jsonUnsafe)!; + + // Remote sensitive data + configUnsafe.Database.Password = + string.IsNullOrEmpty(configUnsafe.Database.Password) ? "IS EMPTY" : "IS NOT EMPTY"; + configUnsafe.Security.Token = string.IsNullOrEmpty(configUnsafe.Security.Token) ? "IS EMPTY" : "IS NOT EMPTY"; + configUnsafe.MailServer.Password =string.IsNullOrEmpty(configUnsafe.MailServer.Password) ? "IS EMPTY" : "IS NOT EMPTY"; + + return JsonConvert.SerializeObject(configUnsafe, Formatting.Indented); + } +} \ No newline at end of file diff --git a/Moonlight/Core/Services/MoonlightService.cs b/Moonlight/Core/Services/MoonlightService.cs index cbfbc0f2..d0643539 100644 --- a/Moonlight/Core/Services/MoonlightService.cs +++ b/Moonlight/Core/Services/MoonlightService.cs @@ -1,11 +1,14 @@ using System.IO.Compression; +using MoonCore.Abstractions; using MoonCore.Attributes; using MoonCore.Helpers; using MoonCore.Services; using Moonlight.Core.Configuration; using Moonlight.Core.Event; using Moonlight.Core.Extensions; +using Moonlight.Features.Servers.Entities; using Moonlight.Features.Theming.Services; +using Newtonsoft.Json; namespace Moonlight.Core.Services; @@ -14,11 +17,11 @@ public class MoonlightService // This service can be used to perform strictly pa { private readonly ConfigService ConfigService; private readonly IServiceProvider ServiceProvider; - + public WebApplication Application { get; set; } // Do NOT modify using a plugin public string LogPath { get; set; } // Do NOT modify using a plugin public ThemeService Theme => ServiceProvider.GetRequiredService(); - + public MoonlightService(ConfigService configService, IServiceProvider serviceProvider) { ConfigService = configService; @@ -28,7 +31,7 @@ public class MoonlightService // This service can be used to perform strictly pa public async Task Restart() { Logger.Info("Restarting moonlight"); - + // Notify all users that this instance will restart await Events.OnMoonlightRestart.InvokeAsync(); await Task.Delay(TimeSpan.FromSeconds(3)); @@ -39,7 +42,7 @@ public class MoonlightService // This service can be used to perform strictly pa public async Task GenerateDiagnoseReport() { var scope = ServiceProvider.CreateScope(); - + // Prepare zip file var memoryStream = new MemoryStream(); var zip = new ZipArchive(memoryStream, ZipArchiveMode.Create, true); @@ -52,17 +55,26 @@ public class MoonlightService // This service can be used to perform strictly pa var log = await sr.ReadToEndAsync(); sr.Close(); fs.Close(); - + await zip.AddFromText("log.txt", log); - - // TODO: Add node settings here - - // TODO: Reimplement as extension here - + + // Add node config + var nodeRepo = scope.ServiceProvider.GetRequiredService>(); + var nodes = nodeRepo.Get().ToArray(); + + foreach (var node in nodes) + { + // Remove sensitive data + node.Token = string.IsNullOrEmpty(node.Token) ? "IS EMPTY" : "IS NOT EMPTY"; + } + + var nodesJson = JsonConvert.SerializeObject(nodes, Formatting.Indented); + await zip.AddFromText("nodes.json", nodesJson); + // Add config - //var config = ConfigService.GetDiagnoseJson(); - //await zip.AddFromText("config.json", config); - + var configJson = ConfigService.GetDiagnosticJson(); + await zip.AddFromText("config.json", configJson); + // Make a list of plugins var pluginService = scope.ServiceProvider.GetRequiredService(); var plugins = await pluginService.GetLoadedPlugins(); @@ -73,11 +85,11 @@ public class MoonlightService // This service can be used to perform strictly pa var assembly = plugin.GetType().Assembly; pluginList += $"{assembly.FullName} ({assembly.Location})\n"; } - + await zip.AddFromText("pluginList.txt", pluginList); - + // Add more information here - + // Finalize file zip.Dispose(); memoryStream.Close();