From 609cf8cfac3861d92f7fbba1353485418a91a731 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Sun, 2 Jul 2023 02:16:44 +0200 Subject: [PATCH] Switched to new config system --- Moonlight/App/Configuration/ConfigV1.cs | 226 ++++++++++++++++++ Moonlight/App/Database/DataContext.cs | 14 +- .../App/Helpers/DatabaseCheckupService.cs | 16 +- .../App/Helpers/Files/WingsFileAccess.cs | 2 +- .../App/Helpers/Wings/WingsConsoleHelper.cs | 2 +- Moonlight/App/Helpers/Wings/WingsJwtHelper.cs | 2 +- .../Api/Moonlight/DiscordBotController.cs | 8 +- .../App/Services/Background/CleanupService.cs | 18 +- .../Background/DiscordNotificationService.cs | 8 +- Moonlight/App/Services/ConfigService.cs | 48 ++-- .../DiscordBot/Commands/ServerListCommand.cs | 4 +- .../Services/DiscordBot/DiscordBotService.cs | 8 +- .../DiscordBot/Modules/EmbedBuilderModule.cs | 4 +- .../ServerListComponentHandlerModule.cs | 14 +- Moonlight/App/Services/DomainService.cs | 10 +- .../App/Services/Files/ResourceService.cs | 2 +- .../App/Services/Interop/ReCaptchaService.cs | 11 +- Moonlight/App/Services/Mail/MailService.cs | 14 +- Moonlight/App/Services/OAuth2Service.cs | 18 +- Moonlight/App/Services/OneTimeJwtService.cs | 14 +- Moonlight/App/Services/RatingService.cs | 10 +- .../App/Services/Sessions/IdentityService.cs | 5 +- Moonlight/App/Services/SmartDeployService.cs | 9 +- .../Statistics/StatisticsCaptureService.cs | 8 +- Moonlight/App/Services/UserService.cs | 5 +- Moonlight/Moonlight.csproj | 8 + Moonlight/Pages/_Layout.cshtml | 26 +- Moonlight/Program.cs | 10 +- .../Components/Alerts/BannedAlert.razor | 10 +- .../Components/Alerts/DisabledAlert.razor | 10 +- .../Shared/Components/Partials/Footer.razor | 14 +- .../Components/Partials/PageHeader.razor | 10 +- .../Shared/Components/Partials/Sidebar.razor | 16 +- .../Components/StateLogic/IsSetup.razor | 20 -- .../Components/StateLogic/NonSetup.razor | 20 -- Moonlight/Shared/Views/Admin/Index.razor | 4 +- .../Shared/Views/Admin/Nodes/Setup.razor | 4 +- .../Shared/Views/Profile/Subscriptions.razor | 9 +- Moonlight/defaultstorage/configs/config.json | 101 -------- 39 files changed, 399 insertions(+), 343 deletions(-) create mode 100644 Moonlight/App/Configuration/ConfigV1.cs delete mode 100644 Moonlight/Shared/Components/StateLogic/IsSetup.razor delete mode 100644 Moonlight/Shared/Components/StateLogic/NonSetup.razor delete mode 100644 Moonlight/defaultstorage/configs/config.json diff --git a/Moonlight/App/Configuration/ConfigV1.cs b/Moonlight/App/Configuration/ConfigV1.cs new file mode 100644 index 00000000..7836b7cb --- /dev/null +++ b/Moonlight/App/Configuration/ConfigV1.cs @@ -0,0 +1,226 @@ +namespace Moonlight.App.Configuration; + +using System; +using Newtonsoft.Json; + +public class ConfigV1 +{ + [JsonProperty("Moonlight")] public MoonlightData Moonlight { get; set; } = new(); + + public class MoonlightData + { + [JsonProperty("AppUrl")] public string AppUrl { get; set; } = "http://your-moonlight-url-without-slash"; + + [JsonProperty("Database")] public DatabaseData Database { get; set; } = new(); + + [JsonProperty("DiscordBotApi")] public DiscordBotData DiscordBotApi { get; set; } = new(); + + [JsonProperty("DiscordBot")] public DiscordBotData DiscordBot { get; set; } = new(); + + [JsonProperty("Domains")] public DomainsData Domains { get; set; } = new(); + + [JsonProperty("Html")] public HtmlData Html { get; set; } = new(); + + [JsonProperty("Marketing")] public MarketingData Marketing { get; set; } = new(); + + [JsonProperty("OAuth2")] public OAuth2Data OAuth2 { get; set; } = new(); + + [JsonProperty("Security")] public SecurityData Security { get; set; } = new(); + + [JsonProperty("Mail")] public MailData Mail { get; set; } = new(); + + [JsonProperty("Cleanup")] public CleanupData Cleanup { get; set; } = new(); + + [JsonProperty("Subscriptions")] public SubscriptionsData Subscriptions { get; set; } = new(); + + [JsonProperty("DiscordNotifications")] + public DiscordNotificationsData DiscordNotifications { get; set; } = new(); + + [JsonProperty("Statistics")] public StatisticsData Statistics { get; set; } = new(); + + [JsonProperty("Rating")] public RatingData Rating { get; set; } = new(); + + [JsonProperty("SmartDeploy")] public SmartDeployData SmartDeploy { get; set; } = new(); + + [JsonProperty("Sentry")] public SentryData Sentry { get; set; } = new(); + } + + public class CleanupData + { + [JsonProperty("Cpu")] public long Cpu { get; set; } = 90; + + [JsonProperty("Memory")] public long Memory { get; set; } = 8192; + + [JsonProperty("Wait")] public long Wait { get; set; } = 15; + + [JsonProperty("Uptime")] public long Uptime { get; set; } = 6; + + [JsonProperty("Enable")] public bool Enable { get; set; } = false; + + [JsonProperty("MinUptime")] public long MinUptime { get; set; } = 10; + } + + public class DatabaseData + { + [JsonProperty("Database")] public string Database { get; set; } = "moonlight_db"; + + [JsonProperty("Host")] public string Host { get; set; } = "your.database.host"; + + [JsonProperty("Password")] public string Password { get; set; } = "secret"; + + [JsonProperty("Port")] public long Port { get; set; } = 3306; + + [JsonProperty("Username")] public string Username { get; set; } = "moonlight_user"; + } + + public class DiscordBotData + { + [JsonProperty("Enable")] public bool Enable { get; set; } = false; + + [JsonProperty("Token")] public string Token { get; set; } = "discord token here"; + + [JsonProperty("PowerActions")] public bool PowerActions { get; set; } = false; + [JsonProperty("SendCommands")] public bool SendCommands { get; set; } = false; + } + + public class DiscordNotificationsData + { + [JsonProperty("Enable")] public bool Enable { get; set; } = false; + + [JsonProperty("WebHook")] public string WebHook { get; set; } = "http://your-discord-webhook-url"; + } + + public class DomainsData + { + [JsonProperty("Enable")] public bool Enable { get; set; } = false; + [JsonProperty("AccountId")] public string AccountId { get; set; } = "cloudflare acc id"; + + [JsonProperty("Email")] public string Email { get; set; } = "cloudflare@acc.email"; + + [JsonProperty("Key")] public string Key { get; set; } = "secret"; + } + + public class HtmlData + { + [JsonProperty("Headers")] public HeadersData Headers { get; set; } = new(); + } + + public class HeadersData + { + [JsonProperty("Color")] public string Color { get; set; } = "#4b27e8"; + + [JsonProperty("Description")] public string Description { get; set; } = "the next generation hosting panel"; + + [JsonProperty("Keywords")] public string Keywords { get; set; } = "moonlight"; + + [JsonProperty("Title")] public string Title { get; set; } = "Moonlight - endelon.link"; + } + + public class MailData + { + [JsonProperty("Email")] public string Email { get; set; } = "username@your.mail.host"; + + [JsonProperty("Server")] public string Server { get; set; } = "your.mail.host"; + + [JsonProperty("Password")] public string Password { get; set; } = "secret"; + + [JsonProperty("Port")] public int Port { get; set; } = 465; + + [JsonProperty("Ssl")] public bool Ssl { get; set; } = true; + } + + public class MarketingData + { + [JsonProperty("BrandName")] public string BrandName { get; set; } = "Endelon Hosting"; + + [JsonProperty("Imprint")] public string Imprint { get; set; } = "https://your-site.xyz/imprint"; + + [JsonProperty("Privacy")] public string Privacy { get; set; } = "https://your-site.xyz/privacy"; + [JsonProperty("About")] public string About { get; set; } = "https://your-site.xyz/about"; + [JsonProperty("Website")] public string Website { get; set; } = "https://your-site.xyz"; + } + + public class OAuth2Data + { + [JsonProperty("OverrideUrl")] public string OverrideUrl { get; set; } = "https://only-for-development.cases"; + + [JsonProperty("EnableOverrideUrl")] public bool EnableOverrideUrl { get; set; } = false; + + [JsonProperty("Providers")] + public OAuth2ProviderData[] Providers { get; set; } = Array.Empty(); + } + + public class OAuth2ProviderData + { + [JsonProperty("Id")] public string Id { get; set; } + + [JsonProperty("ClientId")] public string ClientId { get; set; } + + [JsonProperty("ClientSecret")] public string ClientSecret { get; set; } + } + + public class RatingData + { + [JsonProperty("Enabled")] public bool Enabled { get; set; } = false; + + [JsonProperty("Url")] public string Url { get; set; } = "https://link-to-google-or-smth"; + + [JsonProperty("MinRating")] public int MinRating { get; set; } = 4; + + [JsonProperty("DaysSince")] public int DaysSince { get; set; } = 5; + } + + public class SecurityData + { + [JsonProperty("Token")] public string Token { get; set; } = Guid.NewGuid().ToString(); + + [JsonProperty("ReCaptcha")] public ReCaptchaData ReCaptcha { get; set; } + } + + public class ReCaptchaData + { + [JsonProperty("Enable")] public bool Enable { get; set; } = false; + + [JsonProperty("SiteKey")] public string SiteKey { get; set; } = "recaptcha site key here"; + + [JsonProperty("SecretKey")] public string SecretKey { get; set; } = "recaptcha secret here"; + } + + public class SentryData + { + [JsonProperty("Enable")] public bool Enable { get; set; } = false; + + [JsonProperty("Dsn")] public string Dsn { get; set; } = "http://your-sentry-url-here"; + } + + public class SmartDeployData + { + [JsonProperty("Server")] public SmartDeployServerData Server { get; set; } = new(); + } + + public class SmartDeployServerData + { + [JsonProperty("EnableOverride")] public bool EnableOverride { get; set; } = false; + + [JsonProperty("OverrideNode")] public long OverrideNode { get; set; } = 1; + } + + public class StatisticsData + { + [JsonProperty("Enabled")] public bool Enabled { get; set; } = false; + + [JsonProperty("Wait")] public long Wait { get; set; } = 15; + } + + public class SubscriptionsData + { + [JsonProperty("SellPass")] public SellPassData SellPass { get; set; } = new(); + } + + public class SellPassData + { + [JsonProperty("Enable")] public bool Enable { get; set; } = false; + + [JsonProperty("Url")] public string Url { get; set; } = "https://not-implemented-yet"; + } +} \ No newline at end of file diff --git a/Moonlight/App/Database/DataContext.cs b/Moonlight/App/Database/DataContext.cs index a9cac99f..750ba18c 100644 --- a/Moonlight/App/Database/DataContext.cs +++ b/Moonlight/App/Database/DataContext.cs @@ -52,14 +52,14 @@ public class DataContext : DbContext if (!optionsBuilder.IsConfigured) { var config = ConfigService - .GetSection("Moonlight") - .GetSection("Database"); + .Get() + .Moonlight.Database; - var connectionString = $"host={config.GetValue("Host")};" + - $"port={config.GetValue("Port")};" + - $"database={config.GetValue("Database")};" + - $"uid={config.GetValue("Username")};" + - $"pwd={config.GetValue("Password")}"; + var connectionString = $"host={config.Host};" + + $"port={config.Port};" + + $"database={config.Database};" + + $"uid={config.Username};" + + $"pwd={config.Password}"; optionsBuilder.UseMySql( connectionString, diff --git a/Moonlight/App/Helpers/DatabaseCheckupService.cs b/Moonlight/App/Helpers/DatabaseCheckupService.cs index dc48c424..dbb7ba32 100644 --- a/Moonlight/App/Helpers/DatabaseCheckupService.cs +++ b/Moonlight/App/Helpers/DatabaseCheckupService.cs @@ -65,16 +65,14 @@ public class DatabaseCheckupService var configService = new ConfigService(new StorageService()); var dateTimeService = new DateTimeService(); - - var config = configService - .GetSection("Moonlight") - .GetSection("Database"); - var connectionString = $"host={config.GetValue("Host")};" + - $"port={config.GetValue("Port")};" + - $"database={config.GetValue("Database")};" + - $"uid={config.GetValue("Username")};" + - $"pwd={config.GetValue("Password")}"; + var config = configService.Get().Moonlight.Database; + + var connectionString = $"host={config.Host};" + + $"port={config.Port};" + + $"database={config.Database};" + + $"uid={config.Username};" + + $"pwd={config.Password}"; string file = PathBuilder.File("storage", "backups", $"{dateTimeService.GetCurrentUnix()}-mysql.sql"); diff --git a/Moonlight/App/Helpers/Files/WingsFileAccess.cs b/Moonlight/App/Helpers/Files/WingsFileAccess.cs index f28d65a2..70bebf6b 100644 --- a/Moonlight/App/Helpers/Files/WingsFileAccess.cs +++ b/Moonlight/App/Helpers/Files/WingsFileAccess.cs @@ -111,7 +111,7 @@ public class WingsFileAccess : FileAccess request.AddParameter("name", "files"); request.AddParameter("filename", name); request.AddHeader("Content-Type", "multipart/form-data"); - request.AddHeader("Origin", ConfigService.GetSection("Moonlight").GetValue("AppUrl")); + request.AddHeader("Origin", ConfigService.Get().Moonlight.AppUrl); request.AddFile("files", () => { return new StreamProgressHelper(dataStream) diff --git a/Moonlight/App/Helpers/Wings/WingsConsoleHelper.cs b/Moonlight/App/Helpers/Wings/WingsConsoleHelper.cs index af2fc7d5..6741d3ee 100644 --- a/Moonlight/App/Helpers/Wings/WingsConsoleHelper.cs +++ b/Moonlight/App/Helpers/Wings/WingsConsoleHelper.cs @@ -20,7 +20,7 @@ public class WingsConsoleHelper { ServerRepository = serverRepository; - AppUrl = configService.GetSection("Moonlight").GetValue("AppUrl"); + AppUrl = configService.Get().Moonlight.AppUrl; } public async Task ConnectWings(WingsConsole console, Server server) diff --git a/Moonlight/App/Helpers/Wings/WingsJwtHelper.cs b/Moonlight/App/Helpers/Wings/WingsJwtHelper.cs index 8e4af78a..e45e314c 100644 --- a/Moonlight/App/Helpers/Wings/WingsJwtHelper.cs +++ b/Moonlight/App/Helpers/Wings/WingsJwtHelper.cs @@ -15,7 +15,7 @@ public class WingsJwtHelper { ConfigService = configService; - AppUrl = ConfigService.GetSection("Moonlight").GetValue("AppUrl"); + AppUrl = ConfigService.Get().Moonlight.AppUrl; } public string Generate(string secret, Action> claimsAction) diff --git a/Moonlight/App/Http/Controllers/Api/Moonlight/DiscordBotController.cs b/Moonlight/App/Http/Controllers/Api/Moonlight/DiscordBotController.cs index 3db8ced8..94b65588 100644 --- a/Moonlight/App/Http/Controllers/Api/Moonlight/DiscordBotController.cs +++ b/Moonlight/App/Http/Controllers/Api/Moonlight/DiscordBotController.cs @@ -30,14 +30,14 @@ public class DiscordBotController : Controller ServerService = serverService; var config = configService - .GetSection("Moonlight") - .GetSection("DiscordBotApi"); + .Get() + .Moonlight.DiscordBotApi; - Enable = config.GetValue("Enable"); + Enable = config.Enable; if (Enable) { - Token = config.GetValue("Token"); + Token = config.Token; } } diff --git a/Moonlight/App/Services/Background/CleanupService.cs b/Moonlight/App/Services/Background/CleanupService.cs index eb210ac7..43543470 100644 --- a/Moonlight/App/Services/Background/CleanupService.cs +++ b/Moonlight/App/Services/Background/CleanupService.cs @@ -42,16 +42,16 @@ public class CleanupService StartedAt = DateTimeService.GetCurrent(); CompletedAt = DateTimeService.GetCurrent(); IsRunning = false; - - var config = ConfigService.GetSection("Moonlight").GetSection("Cleanup"); - if (!config.GetValue("Enable") || ConfigService.DebugMode) + var config = ConfigService.Get().Moonlight.Cleanup; + + if (!config.Enable || ConfigService.DebugMode) { Logger.Info("Disabling cleanup service"); return; } - Timer = new(TimeSpan.FromMinutes(config.GetValue("Wait"))); + Timer = new(TimeSpan.FromMinutes(config.Wait)); Task.Run(Run); } @@ -63,12 +63,12 @@ public class CleanupService IsRunning = true; using var scope = ServiceScopeFactory.CreateScope(); - var config = ConfigService.GetSection("Moonlight").GetSection("Cleanup"); + var config = ConfigService.Get().Moonlight.Cleanup; - var maxCpu = config.GetValue("Cpu"); - var minMemory = config.GetValue("Memory"); - var maxUptime = config.GetValue("Uptime"); - var minUptime = config.GetValue("MinUptime"); + var maxCpu = config.Cpu; + var minMemory = config.Memory; + var maxUptime = config.Uptime; + var minUptime = config.MinUptime; var nodeRepository = scope.ServiceProvider.GetRequiredService(); var nodeService = scope.ServiceProvider.GetRequiredService(); diff --git a/Moonlight/App/Services/Background/DiscordNotificationService.cs b/Moonlight/App/Services/Background/DiscordNotificationService.cs index c369d402..f4257d52 100644 --- a/Moonlight/App/Services/Background/DiscordNotificationService.cs +++ b/Moonlight/App/Services/Background/DiscordNotificationService.cs @@ -22,14 +22,14 @@ public class DiscordNotificationService Event = eventSystem; ResourceService = resourceService; - var config = configService.GetSection("Moonlight").GetSection("DiscordNotifications"); + var config = configService.Get().Moonlight.DiscordNotifications; - if (config.GetValue("Enable")) + if (config.Enable) { Logger.Info("Discord notifications enabled"); - Client = new(config.GetValue("WebHook")); - AppUrl = configService.GetSection("Moonlight").GetValue("AppUrl"); + Client = new(config.WebHook); + AppUrl = configService.Get().Moonlight.AppUrl; Event.On("supportChat.new", this, OnNewSupportChat); Event.On("supportChat.message", this, OnSupportChatMessage); diff --git a/Moonlight/App/Services/ConfigService.cs b/Moonlight/App/Services/ConfigService.cs index 1e4fbcb8..f6573e19 100644 --- a/Moonlight/App/Services/ConfigService.cs +++ b/Moonlight/App/Services/ConfigService.cs @@ -1,15 +1,14 @@ -using System.Text; -using Microsoft.Extensions.Primitives; +using Moonlight.App.Configuration; using Moonlight.App.Helpers; using Moonlight.App.Services.Files; +using Newtonsoft.Json; namespace Moonlight.App.Services; -public class ConfigService : IConfiguration +public class ConfigService { private readonly StorageService StorageService; - - private IConfiguration Configuration; + private ConfigV1 Configuration; public bool DebugMode { get; private set; } = false; public bool SqlDebugMode { get; private set; } = false; @@ -41,33 +40,22 @@ public class ConfigService : IConfiguration public void Reload() { - Configuration = new ConfigurationBuilder().AddJsonStream( - new MemoryStream(Encoding.ASCII.GetBytes( - File.ReadAllText( - PathBuilder.File("storage", "configs", "config.json") - ) - ) - )).Build(); + var path = PathBuilder.File("storage", "configs", "config.json"); + + if (!File.Exists(path)) + { + File.WriteAllText(path, "{}"); + } + + Configuration = JsonConvert.DeserializeObject( + File.ReadAllText(path) + ) ?? new ConfigV1(); + + File.WriteAllText(path, JsonConvert.SerializeObject(Configuration)); } - public IEnumerable GetChildren() + public ConfigV1 Get() { - return Configuration.GetChildren(); - } - - public IChangeToken GetReloadToken() - { - return Configuration.GetReloadToken(); - } - - public IConfigurationSection GetSection(string key) - { - return Configuration.GetSection(key); - } - - public string this[string key] - { - get => Configuration[key]; - set => Configuration[key] = value; + return Configuration; } } \ No newline at end of file diff --git a/Moonlight/App/Services/DiscordBot/Commands/ServerListCommand.cs b/Moonlight/App/Services/DiscordBot/Commands/ServerListCommand.cs index 15fbeb49..b2856331 100644 --- a/Moonlight/App/Services/DiscordBot/Commands/ServerListCommand.cs +++ b/Moonlight/App/Services/DiscordBot/Commands/ServerListCommand.cs @@ -29,7 +29,7 @@ public class ServerListCommand : BaseModule { embed = dcs.EmbedBuilderModule.StandardEmbed("Sorry ;( \n Please first create and/or link a Account to Discord! \n Press the Button to register/log in.", Color.Red, command.User); components = new ComponentBuilder(); - components.WithButton("Click Here", style: ButtonStyle.Link, url: ConfigService.GetSection("Moonlight").GetValue("AppUrl")); + components.WithButton("Click Here", style: ButtonStyle.Link, url: ConfigService.Get().Moonlight.AppUrl); await command.RespondAsync(embed: embed.Build(), components: components.Build(), ephemeral: true); return; @@ -57,7 +57,7 @@ public class ServerListCommand : BaseModule components.WithButton("Panel", emote: Emote.Parse(""), style: ButtonStyle.Link, - url: $"{ConfigService.GetSection("Moonlight").GetValue("AppUrl")}"); + url: $"{ConfigService.Get().Moonlight.AppUrl}"); if (servers.Count > 25) { diff --git a/Moonlight/App/Services/DiscordBot/DiscordBotService.cs b/Moonlight/App/Services/DiscordBot/DiscordBotService.cs index 4b3d39be..21c7b711 100644 --- a/Moonlight/App/Services/DiscordBot/DiscordBotService.cs +++ b/Moonlight/App/Services/DiscordBot/DiscordBotService.cs @@ -44,10 +44,10 @@ public DiscordBotService( ServiceScope = ServiceScopeFactory.CreateScope(); var discordConfig = ConfigService - .GetSection("Moonlight") - .GetSection("DiscordBot"); + .Get() + .Moonlight.DiscordBot; - if (!discordConfig.GetValue("Enable")) + if (!discordConfig.Enable) return; Client.Log += Log; @@ -67,7 +67,7 @@ public DiscordBotService( await ActivityStatusModule.UpdateActivityStatusList(); - await Client.LoginAsync(TokenType.Bot, discordConfig.GetValue("Token")); + await Client.LoginAsync(TokenType.Bot, discordConfig.Token); await Client.StartAsync(); await Task.Delay(-1); diff --git a/Moonlight/App/Services/DiscordBot/Modules/EmbedBuilderModule.cs b/Moonlight/App/Services/DiscordBot/Modules/EmbedBuilderModule.cs index 74952b72..8f604278 100644 --- a/Moonlight/App/Services/DiscordBot/Modules/EmbedBuilderModule.cs +++ b/Moonlight/App/Services/DiscordBot/Modules/EmbedBuilderModule.cs @@ -87,8 +87,8 @@ public class EmbedBuilderModule : BaseModule int[] randomNumbers = new int[] { 1, 3, 8, 11, 20 }; if (randomNumbers.Contains(random.Next(1, 24))) - return new EmbedAuthorBuilder().WithName(Client.CurrentUser.Username + " - The Rick version").WithUrl(ConfigService.GetSection("Moonlight").GetValue("AppUrl")).WithIconUrl("https://cdn.discordapp.com/attachments/750696464014901268/1092783310129860618/rick.gif"); + return new EmbedAuthorBuilder().WithName(Client.CurrentUser.Username + " - The Rick version").WithUrl(ConfigService.Get().Moonlight.AppUrl).WithIconUrl("https://cdn.discordapp.com/attachments/750696464014901268/1092783310129860618/rick.gif"); - return new EmbedAuthorBuilder().WithName(Client.CurrentUser.Username).WithUrl(ConfigService.GetSection("Moonlight").GetValue("AppUrl")).WithIconUrl(Client.CurrentUser.GetAvatarUrl()); + return new EmbedAuthorBuilder().WithName(Client.CurrentUser.Username).WithUrl(ConfigService.Get().Moonlight.AppUrl).WithIconUrl(Client.CurrentUser.GetAvatarUrl()); } } diff --git a/Moonlight/App/Services/DiscordBot/Modules/ServerListComponentHandlerModule.cs b/Moonlight/App/Services/DiscordBot/Modules/ServerListComponentHandlerModule.cs index ba22f6e3..6dd5111f 100644 --- a/Moonlight/App/Services/DiscordBot/Modules/ServerListComponentHandlerModule.cs +++ b/Moonlight/App/Services/DiscordBot/Modules/ServerListComponentHandlerModule.cs @@ -72,7 +72,7 @@ public class ServerListComponentHandlerModule : BaseModule // stopping // offline // installing - if (!ConfigService.GetSection("Moonlight").GetSection("DiscordBot").GetValue("PowerActions") && costomId[1] is "Start" or "Restart" or "Stop" or "Kill" or "Update") + if (!ConfigService.Get().Moonlight.DiscordBot.PowerActions && costomId[1] is "Start" or "Restart" or "Stop" or "Kill" or "Update") { embed = dcs.EmbedBuilderModule.StandardEmbed($"This feature is disabled for Security reasons! \n If you believe this is a error please contact the Administrators from this panel.", Color.Red, component.User); await component.RespondAsync(embed: embed.Build(), ephemeral: true); @@ -80,7 +80,7 @@ public class ServerListComponentHandlerModule : BaseModule return; } - if (!ConfigService.GetSection("Moonlight").GetSection("DiscordBot").GetValue("SendCommands") && costomId[1] is "SendCommand") + if (!ConfigService.Get().Moonlight.DiscordBot.SendCommands && costomId[1] is "SendCommand") { embed = dcs.EmbedBuilderModule.StandardEmbed($"This feature is disabled for Security reasons! \n If you believe this is a error please contact the Administrators from this panel.", Color.Red, component.User); await component.RespondAsync(embed: embed.Build(), ephemeral: true); @@ -302,7 +302,7 @@ public class ServerListComponentHandlerModule : BaseModule { embed = dcs.EmbedBuilderModule.StandardEmbed("Sorry ;( \n Please first create and/or link a Account to Discord! \n Press the Button to register/log in.", Color.Red, component.User); components = new ComponentBuilder(); - components.WithButton("Click Here", style: ButtonStyle.Link, url: ConfigService.GetSection("Moonlight").GetValue("AppUrl")); + components.WithButton("Click Here", style: ButtonStyle.Link, url: ConfigService.Get().Moonlight.AppUrl); await component.RespondAsync(embed: embed.Build(), components: components.Build(), ephemeral: true); return; @@ -332,7 +332,7 @@ public class ServerListComponentHandlerModule : BaseModule components.WithButton("Panel", emote: Emote.Parse(""), style: ButtonStyle.Link, - url: $"{ConfigService.GetSection("Moonlight").GetValue("AppUrl")}"); + url: $"{ConfigService.Get().Moonlight.AppUrl}"); components.WithButton("Previous-page", emote: Emote.Parse("<:ArrowLeft:1101547474180649030>"), @@ -378,7 +378,7 @@ public class ServerListComponentHandlerModule : BaseModule var components = new ComponentBuilder(); - if (ConfigService.GetSection("Moonlight").GetSection("DiscordBot").GetValue("PowerActions")) + if (ConfigService.Get().Moonlight.DiscordBot.PowerActions) { components.WithButton("Start", style: ButtonStyle.Success, customId: $"Sm.Start.{server.Id}", disabled: false); components.WithButton("Restart", style: ButtonStyle.Primary, customId: $"Sm.Restart.{server.Id}", disabled: false); @@ -389,14 +389,14 @@ public class ServerListComponentHandlerModule : BaseModule components.WithButton("Way2Server", emote: Emote.Parse(""), style: ButtonStyle.Link, - url: $"{ConfigService.GetSection("Moonlight").GetValue("AppUrl")}/server/{server.Uuid}"); + url: $"{ConfigService.Get().Moonlight.AppUrl}/server/{server.Uuid}"); components.WithButton("Update", emote: Emote.Parse("<:refresh:1101547898803605605>"), style: ButtonStyle.Secondary, customId: $"Sm.Update.{server.Id}"); - if (ConfigService.GetSection("Moonlight").GetSection("DiscordBot").GetValue("SendCommands")) + if (ConfigService.Get().Moonlight.DiscordBot.SendCommands) { components.WithButton("SendCommand", emote: Emote.Parse("<:Console:1101547358157819944>"), diff --git a/Moonlight/App/Services/DomainService.cs b/Moonlight/App/Services/DomainService.cs index 039ddae1..c56855f1 100644 --- a/Moonlight/App/Services/DomainService.cs +++ b/Moonlight/App/Services/DomainService.cs @@ -33,15 +33,15 @@ public class DomainService SharedDomainRepository = sharedDomainRepository; var config = configService - .GetSection("Moonlight") - .GetSection("Domains"); + .Get() + .Moonlight.Domains; - AccountId = config.GetValue("AccountId"); + AccountId = config.AccountId; Client = new( new ApiKeyAuthentication( - config.GetValue("Email"), - config.GetValue("Key") + config.Email, + config.Key ) ); } diff --git a/Moonlight/App/Services/Files/ResourceService.cs b/Moonlight/App/Services/Files/ResourceService.cs index 7dfff856..a113fad7 100644 --- a/Moonlight/App/Services/Files/ResourceService.cs +++ b/Moonlight/App/Services/Files/ResourceService.cs @@ -8,7 +8,7 @@ public class ResourceService public ResourceService(ConfigService configService) { - AppUrl = configService.GetSection("Moonlight").GetValue("AppUrl"); + AppUrl = configService.Get().Moonlight.AppUrl; } public string Image(string name) diff --git a/Moonlight/App/Services/Interop/ReCaptchaService.cs b/Moonlight/App/Services/Interop/ReCaptchaService.cs index 86c9af37..159cce16 100644 --- a/Moonlight/App/Services/Interop/ReCaptchaService.cs +++ b/Moonlight/App/Services/Interop/ReCaptchaService.cs @@ -25,16 +25,15 @@ public class ReCaptchaService ConfigService = configService; var recaptchaConfig = ConfigService - .GetSection("Moonlight") - .GetSection("Security") - .GetSection("ReCaptcha"); + .Get() + .Moonlight.Security.ReCaptcha; - Enable = recaptchaConfig.GetValue("Enable"); + Enable = recaptchaConfig.Enable; if (Enable) { - SiteKey = recaptchaConfig.GetValue("SiteKey"); - SecretKey = recaptchaConfig.GetValue("SecretKey"); + SiteKey = recaptchaConfig.SiteKey; + SecretKey = recaptchaConfig.SecretKey; } } diff --git a/Moonlight/App/Services/Mail/MailService.cs b/Moonlight/App/Services/Mail/MailService.cs index efdfc878..23d425bf 100644 --- a/Moonlight/App/Services/Mail/MailService.cs +++ b/Moonlight/App/Services/Mail/MailService.cs @@ -17,14 +17,14 @@ public class MailService public MailService(ConfigService configService) { var mailConfig = configService - .GetSection("Moonlight") - .GetSection("Mail"); + .Get() + .Moonlight.Mail; - Server = mailConfig.GetValue("Server"); - Password = mailConfig.GetValue("Password"); - Email = mailConfig.GetValue("Email"); - Port = mailConfig.GetValue("Port"); - Ssl = mailConfig.GetValue("Ssl"); + Server = mailConfig.Server; + Password = mailConfig.Password; + Email = mailConfig.Email; + Port = mailConfig.Port; + Ssl = mailConfig.Ssl; } public async Task SendMail( diff --git a/Moonlight/App/Services/OAuth2Service.cs b/Moonlight/App/Services/OAuth2Service.cs index 31f17ff4..87804732 100644 --- a/Moonlight/App/Services/OAuth2Service.cs +++ b/Moonlight/App/Services/OAuth2Service.cs @@ -1,4 +1,5 @@ -using Moonlight.App.Database.Entities; +using Mappy.Net; +using Moonlight.App.Database.Entities; using Moonlight.App.Exceptions; using Moonlight.App.Helpers; using Moonlight.App.Models.Misc; @@ -24,14 +25,17 @@ public class OAuth2Service ConfigService = configService; ServiceScopeFactory = serviceScopeFactory; - var config = ConfigService.GetSection("Moonlight").GetSection("OAuth2"); + var config = ConfigService + .Get() + .Moonlight.OAuth2; - Configs = config.GetSection("Providers").Get() - ?? Array.Empty(); + Configs = config.Providers + .Select(Mapper.Map) + .ToArray(); - OverrideUrl = config.GetValue("OverrideUrl"); - EnableOverrideUrl = config.GetValue("EnableOverrideUrl"); - AppUrl = configService.GetSection("Moonlight").GetValue("AppUrl"); + OverrideUrl = config.OverrideUrl; + EnableOverrideUrl = config.EnableOverrideUrl; + AppUrl = configService.Get().Moonlight.AppUrl; // Register additional providers here RegisterOAuth2("discord"); diff --git a/Moonlight/App/Services/OneTimeJwtService.cs b/Moonlight/App/Services/OneTimeJwtService.cs index b931a278..a4a377eb 100644 --- a/Moonlight/App/Services/OneTimeJwtService.cs +++ b/Moonlight/App/Services/OneTimeJwtService.cs @@ -25,10 +25,9 @@ public class OneTimeJwtService var opt = new Dictionary(); options.Invoke(opt); - string secret = ConfigService - .GetSection("Moonlight") - .GetSection("Security") - .GetValue("Token"); + var secret = ConfigService + .Get() + .Moonlight.Security.Token; var id = StringHelper.GenerateString(16); @@ -55,10 +54,9 @@ public class OneTimeJwtService public async Task?> Validate(string token) { - string secret = ConfigService - .GetSection("Moonlight") - .GetSection("Security") - .GetValue("Token"); + var secret = ConfigService + .Get() + .Moonlight.Security.Token; string json; diff --git a/Moonlight/App/Services/RatingService.cs b/Moonlight/App/Services/RatingService.cs index 3c7d24bf..e0e6bef7 100644 --- a/Moonlight/App/Services/RatingService.cs +++ b/Moonlight/App/Services/RatingService.cs @@ -26,12 +26,12 @@ public class RatingService Event = eventSystem; UserRepository = userRepository; - var config = configService.GetSection("Moonlight").GetSection("Rating"); + var config = configService.Get().Moonlight.Rating; - Enabled = config.GetValue("Enabled"); - Url = config.GetValue("Url"); - MinRating = config.GetValue("MinRating"); - DaysSince = config.GetValue("DaysSince"); + Enabled = config.Enabled; + Url = config.Url; + MinRating = config.MinRating; + DaysSince = config.DaysSince; } public async Task ShouldRate() diff --git a/Moonlight/App/Services/Sessions/IdentityService.cs b/Moonlight/App/Services/Sessions/IdentityService.cs index e231528d..693060ce 100644 --- a/Moonlight/App/Services/Sessions/IdentityService.cs +++ b/Moonlight/App/Services/Sessions/IdentityService.cs @@ -30,9 +30,8 @@ public class IdentityService HttpContextAccessor = httpContextAccessor; Secret = configService - .GetSection("Moonlight") - .GetSection("Security") - .GetValue("Token"); + .Get() + .Moonlight.Security.Token; } public async Task Get() diff --git a/Moonlight/App/Services/SmartDeployService.cs b/Moonlight/App/Services/SmartDeployService.cs index 3aaaf4c1..b26aa7ed 100644 --- a/Moonlight/App/Services/SmartDeployService.cs +++ b/Moonlight/App/Services/SmartDeployService.cs @@ -28,13 +28,12 @@ public class SmartDeployService public async Task GetNode() { var config = ConfigService - .GetSection("Moonlight") - .GetSection("SmartDeploy") - .GetSection("Server"); + .Get() + .Moonlight.SmartDeploy.Server; - if (config.GetValue("EnableOverride")) + if (config.EnableOverride) { - var nodeId = config.GetValue("OverrideNode"); + var nodeId = config.OverrideNode; return NodeRepository.Get().FirstOrDefault(x => x.Id == nodeId); } diff --git a/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs b/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs index 929362b2..39f341e9 100644 --- a/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs +++ b/Moonlight/App/Services/Statistics/StatisticsCaptureService.cs @@ -17,13 +17,13 @@ public class StatisticsCaptureService DateTimeService = dateTimeService; var config = configService - .GetSection("Moonlight") - .GetSection("Statistics"); + .Get() + .Moonlight.Statistics; - if(!config.GetValue("Enabled")) + if(!config.Enabled) return; - var period = TimeSpan.FromMinutes(config.GetValue("Wait")); + var period = TimeSpan.FromMinutes(config.Wait); Timer = new(period); Logger.Info("Starting statistics system"); diff --git a/Moonlight/App/Services/UserService.cs b/Moonlight/App/Services/UserService.cs index ed724d33..3fcb1107 100644 --- a/Moonlight/App/Services/UserService.cs +++ b/Moonlight/App/Services/UserService.cs @@ -38,9 +38,8 @@ public class UserService DateTimeService = dateTimeService; JwtSecret = configService - .GetSection("Moonlight") - .GetSection("Security") - .GetValue("Token"); + .Get() + .Moonlight.Security.Token; } public async Task Register(string email, string password, string firstname, string lastname) diff --git a/Moonlight/Moonlight.csproj b/Moonlight/Moonlight.csproj index 3a697212..1ffe3145 100644 --- a/Moonlight/Moonlight.csproj +++ b/Moonlight/Moonlight.csproj @@ -103,4 +103,12 @@ + + + true + PreserveNewest + PreserveNewest + + + diff --git a/Moonlight/Pages/_Layout.cshtml b/Moonlight/Pages/_Layout.cshtml index 9cb8fdee..1ba6f38c 100644 --- a/Moonlight/Pages/_Layout.cshtml +++ b/Moonlight/Pages/_Layout.cshtml @@ -11,12 +11,12 @@ @{ var headerConfig = ConfigService - .GetSection("Moonlight") - .GetSection("Html") - .GetSection("Headers"); + .Get() + .Moonlight.Html.Headers; var moonlightConfig = ConfigService - .GetSection("Moonlight"); + .Get() + .Moonlight; } @@ -26,16 +26,16 @@ - - - - - + + + + + - - + + - + @@ -75,7 +75,7 @@
- Logo + Logo @{ string loadingMessage; diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index 608c1f75..223e2f26 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -43,9 +43,8 @@ namespace Moonlight // This will also copy all default config files var configService = new ConfigService(new StorageService()); var shouldUseSentry = configService - .GetSection("Moonlight") - .GetSection("Sentry") - .GetValue("Enable"); + .Get() + .Moonlight.Sentry.Enable; if (configService.DebugMode) { @@ -139,9 +138,8 @@ namespace Moonlight builder.WebHost.UseSentry(options => { options.Dsn = configService - .GetSection("Moonlight") - .GetSection("Sentry") - .GetValue("Dsn"); + .Get() + .Moonlight.Sentry.Dsn; options.Debug = configService.DebugMode; options.DiagnosticLevel = SentryLevel.Warning; diff --git a/Moonlight/Shared/Components/Alerts/BannedAlert.razor b/Moonlight/Shared/Components/Alerts/BannedAlert.razor index 264d4bb6..214ee81b 100644 --- a/Moonlight/Shared/Components/Alerts/BannedAlert.razor +++ b/Moonlight/Shared/Components/Alerts/BannedAlert.razor @@ -1,16 +1,12 @@ @using Moonlight.App.Services +@using Moonlight.App.Services.Files -@inject ConfigService ConfigService - -@{ - var moonlightConfig = ConfigService - .GetSection("Moonlight"); -} +@inject ResourceService ResourceService
- Logo + Logo

Your account is banned from moonlight

diff --git a/Moonlight/Shared/Components/Alerts/DisabledAlert.razor b/Moonlight/Shared/Components/Alerts/DisabledAlert.razor index f0d095b9..cecd949b 100644 --- a/Moonlight/Shared/Components/Alerts/DisabledAlert.razor +++ b/Moonlight/Shared/Components/Alerts/DisabledAlert.razor @@ -1,16 +1,12 @@ @using Moonlight.App.Services +@using Moonlight.App.Services.Files -@inject ConfigService ConfigService - -@{ - var moonlightConfig = ConfigService - .GetSection("Moonlight"); -} +@inject ResourceService ResourceService
- Logo + Logo

Your moonlight account is disabled

diff --git a/Moonlight/Shared/Components/Partials/Footer.razor b/Moonlight/Shared/Components/Partials/Footer.razor index e8931f54..e79e6055 100644 --- a/Moonlight/Shared/Components/Partials/Footer.razor +++ b/Moonlight/Shared/Components/Partials/Footer.razor @@ -4,31 +4,31 @@ @{ var marketingConfig = ConfigService - .GetSection("Moonlight") - .GetSection("Marketing"); + .Get() + .Moonlight.Marketing; }