From 0e5402c347bdaa9c1b0610e7468fbde9844f97a6 Mon Sep 17 00:00:00 2001 From: ChiaraBm Date: Wed, 14 May 2025 09:15:18 +0200 Subject: [PATCH] Removed unused calls and classes from the old plugin system --- .../Configuration/AppConfiguration.cs | 1 + .../Helpers/BundleAssetFileProvider.cs | 28 ---- .../Http/Controllers/OAuth2/Register.razor | 2 +- Moonlight.ApiServer/Models/PluginManifest.cs | 14 -- Moonlight.ApiServer/Services/PluginService.cs | 139 ------------------ Moonlight.ApiServer/Startup.cs | 53 ++++--- 6 files changed, 31 insertions(+), 206 deletions(-) delete mode 100644 Moonlight.ApiServer/Helpers/BundleAssetFileProvider.cs delete mode 100644 Moonlight.ApiServer/Models/PluginManifest.cs delete mode 100644 Moonlight.ApiServer/Services/PluginService.cs diff --git a/Moonlight.ApiServer/Configuration/AppConfiguration.cs b/Moonlight.ApiServer/Configuration/AppConfiguration.cs index 4c5bd173..004f4c44 100644 --- a/Moonlight.ApiServer/Configuration/AppConfiguration.cs +++ b/Moonlight.ApiServer/Configuration/AppConfiguration.cs @@ -55,5 +55,6 @@ public class AppConfiguration public class KestrelConfig { public int UploadLimit { get; set; } = 100; + public string AllowedOrigins { get; set; } = "*"; } } \ No newline at end of file diff --git a/Moonlight.ApiServer/Helpers/BundleAssetFileProvider.cs b/Moonlight.ApiServer/Helpers/BundleAssetFileProvider.cs deleted file mode 100644 index 06669dc5..00000000 --- a/Moonlight.ApiServer/Helpers/BundleAssetFileProvider.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Microsoft.Extensions.FileProviders; -using Microsoft.Extensions.FileProviders.Physical; -using Microsoft.Extensions.Primitives; -using MoonCore.Helpers; - -namespace Moonlight.ApiServer.Helpers; - -public class BundleAssetFileProvider : IFileProvider -{ - public IDirectoryContents GetDirectoryContents(string subpath) - => NotFoundDirectoryContents.Singleton; - - public IFileInfo GetFileInfo(string subpath) - { - if(subpath != "/css/bundle.css") - return new NotFoundFileInfo(subpath); - - var physicalPath = PathBuilder.File("storage", "tmp", "bundle.css"); - - if(!File.Exists(physicalPath)) - return new NotFoundFileInfo(subpath); - - return new PhysicalFileInfo(new FileInfo(physicalPath)); - } - - public IChangeToken Watch(string filter) - => NullChangeToken.Singleton; -} \ No newline at end of file diff --git a/Moonlight.ApiServer/Http/Controllers/OAuth2/Register.razor b/Moonlight.ApiServer/Http/Controllers/OAuth2/Register.razor index b7bf0953..79c16b2a 100644 --- a/Moonlight.ApiServer/Http/Controllers/OAuth2/Register.razor +++ b/Moonlight.ApiServer/Http/Controllers/OAuth2/Register.razor @@ -25,7 +25,7 @@
- +
diff --git a/Moonlight.ApiServer/Models/PluginManifest.cs b/Moonlight.ApiServer/Models/PluginManifest.cs deleted file mode 100644 index cda0d868..00000000 --- a/Moonlight.ApiServer/Models/PluginManifest.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Moonlight.ApiServer.Models; - -public class PluginManifest -{ - public string Id { get; set; } - public string Name { get; set; } - public string Author { get; set; } - public string[] Dependencies { get; set; } = []; - - public string[] Scripts { get; set; } = []; - public string[] Styles { get; set; } = []; - - public Dictionary Assemblies { get; set; } = new(); -} \ No newline at end of file diff --git a/Moonlight.ApiServer/Services/PluginService.cs b/Moonlight.ApiServer/Services/PluginService.cs deleted file mode 100644 index 94e3c8bd..00000000 --- a/Moonlight.ApiServer/Services/PluginService.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System.Text.Json; -using Microsoft.Extensions.FileProviders; -using MoonCore.Helpers; -using Moonlight.ApiServer.Models; - -namespace Moonlight.ApiServer.Services; - -public class PluginService -{ - private readonly ILogger Logger; - private readonly string PluginRoot; - - public readonly Dictionary LoadedPlugins = new(); - public IFileProvider WwwRootFileProvider; - - public PluginService(ILogger logger) - { - Logger = logger; - - PluginRoot = PathBuilder.Dir("storage", "plugins"); - } - - public async Task Load() - { - var jsonOptions = new JsonSerializerOptions() - { - PropertyNameCaseInsensitive = true - }; - - var pluginDirs = Directory.GetDirectories(PluginRoot); - var pluginMap = new Dictionary(); - - #region Scan plugins/ directory for plugin.json files - - foreach (var dir in pluginDirs) - { - var metaPath = PathBuilder.File(dir, "plugin.json"); - - if (!File.Exists(metaPath)) - { - Logger.LogWarning("Skipped '{dir}' as it is missing a plugin.json", dir); - continue; - } - - var json = await File.ReadAllTextAsync(metaPath); - - try - { - var meta = JsonSerializer.Deserialize(json, jsonOptions); - - if (meta == null) - throw new JsonException("Unable to parse. Return value was null"); - - pluginMap.Add(meta, dir); - } - catch (JsonException e) - { - Logger.LogError("Unable to load plugin.json at '{path}': {e}", metaPath, e); - } - } - - #endregion - - #region Depdenency check - - foreach (var plugin in pluginMap.Keys) - { - var hasMissingDep = false; - - foreach (var dependency in plugin.Dependencies) - { - if (pluginMap.Keys.All(x => x.Id != dependency)) - { - hasMissingDep = true; - Logger.LogWarning("Plugin '{name}' has missing dependency: {dep}", plugin.Name, dependency); - } - } - - if (hasMissingDep) - Logger.LogWarning("Unable to load '{name}' due to missing dependencies", plugin.Name); - else - LoadedPlugins.Add(plugin, pluginMap[plugin]); - } - - #endregion - - #region Create wwwroot file provider - - Logger.LogInformation("Creating wwwroot file provider"); - WwwRootFileProvider = CreateWwwRootProvider(); - - #endregion - - Logger.LogInformation("Loaded {count} plugins", LoadedPlugins.Count); - } - - public Dictionary GetAssemblies(string section) - { - var assemblyMap = new Dictionary(); - - foreach (var loadedPlugin in LoadedPlugins.Keys) - { - // Skip all plugins which haven't defined any assemblies in that section - if (!loadedPlugin.Assemblies.ContainsKey(section)) - continue; - - var pluginPath = LoadedPlugins[loadedPlugin]; - - foreach (var assembly in loadedPlugin.Assemblies[section]) - { - var assemblyFile = Path.GetFileName(assembly); - assemblyMap[assemblyFile] = PathBuilder.File(pluginPath, assembly); - } - } - - return assemblyMap; - } - - private IFileProvider CreateWwwRootProvider() - { - List wwwRootProviders = new(); - - foreach (var pluginFolder in LoadedPlugins.Values) - { - var wwwRootPath = Path.GetFullPath( - PathBuilder.Dir(pluginFolder, "wwwroot") - ); - - if(!Directory.Exists(wwwRootPath)) - continue; - - wwwRootProviders.Add( - new PhysicalFileProvider(wwwRootPath) - ); - } - - return new CompositeFileProvider(wwwRootProviders); - } -} \ No newline at end of file diff --git a/Moonlight.ApiServer/Startup.cs b/Moonlight.ApiServer/Startup.cs index c37f9343..84695cc1 100644 --- a/Moonlight.ApiServer/Startup.cs +++ b/Moonlight.ApiServer/Startup.cs @@ -1,8 +1,8 @@ -using System.Runtime.Loader; using System.Text; using System.Text.Json; using Hangfire; using Hangfire.EntityFrameworkCore; +using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using MoonCore.EnvConfiguration; @@ -15,12 +15,10 @@ using MoonCore.Helpers; using Moonlight.ApiServer.Configuration; using Moonlight.ApiServer.Database; using Moonlight.ApiServer.Database.Entities; -using Moonlight.ApiServer.Helpers; using Moonlight.ApiServer.Implementations; using Moonlight.ApiServer.Implementations.Startup; using Moonlight.ApiServer.Interfaces; using Moonlight.ApiServer.Plugins; -using Moonlight.ApiServer.Services; namespace Moonlight.ApiServer; @@ -79,7 +77,6 @@ public class Startup await PrepareDatabase(); await UseCors(); - await UsePluginAssets(); // We need to move the plugin assets to the top to allow plugins to override content await UseBase(); await UseAuth(); await UseHangfire(); @@ -191,7 +188,7 @@ public class Startup var serviceCollection = new ServiceCollection(); serviceCollection.AddSingleton(Configuration); - + serviceCollection.AddLogging(builder => { builder.ClearProviders(); @@ -199,20 +196,20 @@ public class Startup }); PluginLoadServiceProvider = serviceCollection.BuildServiceProvider(); - + // Collect startups var pluginStartups = new List(); - + pluginStartups.Add(new CoreStartup()); - + pluginStartups.AddRange(AdditionalPlugins); // Used by the development server - + // Do NOT remove the following comment, as its used to place the plugin startup register calls // MLBUILD_PLUGIN_STARTUP_HERE PluginStartups = pluginStartups.ToArray(); - + return Task.CompletedTask; } @@ -221,16 +218,6 @@ public class Startup return Task.CompletedTask; } - private Task UsePluginAssets() - { - WebApplication.UseStaticFiles(new StaticFileOptions() - { - FileProvider = new BundleAssetFileProvider() - }); - - return Task.CompletedTask; - } - #region Hooks private async Task HookPluginBuild() @@ -489,7 +476,7 @@ public class Startup }); WebApplicationBuilder.Services.AddAuthorization(); - + // Add local oauth2 provider if enabled if (Configuration.Authentication.EnableLocalOAuth2) WebApplicationBuilder.Services.AddScoped(); @@ -514,12 +501,30 @@ public class Startup private Task RegisterCors() { + var allowedOrigins = Configuration.Kestrel.AllowedOrigins.Split(";", StringSplitOptions.RemoveEmptyEntries); + WebApplicationBuilder.Services.AddCors(options => { - options.AddDefaultPolicy(builder => + var cors = new CorsPolicyBuilder(); + + if (allowedOrigins.Contains("*")) { - builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().Build(); - }); + cors.SetIsOriginAllowed(_ => true) + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials(); + } + else + { + cors.WithOrigins(allowedOrigins) + .AllowAnyHeader() + .AllowAnyMethod() + .AllowCredentials(); + } + + options.AddDefaultPolicy( + cors.Build() + ); }); return Task.CompletedTask;