From 0d7cd8009fd14effad1a74d3f687a798697a3c2e Mon Sep 17 00:00:00 2001 From: Qstick Date: Fri, 27 Apr 2018 23:29:25 -0400 Subject: [PATCH] Added: Make Lidarr CSP compatible Co-Authored-By: Mark McDowall --- frontend/src/index.html | 13 +---- .../Frontend/InitializeJsModule.cs | 54 +++++++++++++++++++ .../Frontend/Mappers/HtmlMapperBase.cs | 7 --- .../Frontend/Mappers/IndexHtmlMapper.cs | 20 ------- .../Frontend/Mappers/StaticResourceMapper.cs | 2 +- .../Frontend/StaticResourceModule.cs | 5 +- src/Lidarr.Http/Lidarr.Http.csproj | 1 + 7 files changed, 58 insertions(+), 44 deletions(-) create mode 100644 src/Lidarr.Http/Frontend/InitializeJsModule.cs diff --git a/frontend/src/index.html b/frontend/src/index.html index 5cab2268f..2012790de 100644 --- a/frontend/src/index.html +++ b/frontend/src/index.html @@ -52,18 +52,7 @@
- + diff --git a/src/Lidarr.Http/Frontend/InitializeJsModule.cs b/src/Lidarr.Http/Frontend/InitializeJsModule.cs new file mode 100644 index 000000000..279b12d6b --- /dev/null +++ b/src/Lidarr.Http/Frontend/InitializeJsModule.cs @@ -0,0 +1,54 @@ +using System.IO; +using Nancy; +using Nancy.Responses; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Core.Analytics; +using NzbDrone.Core.Configuration; + +namespace Lidarr.Http.Frontend +{ + public class InitializeJsModule : NancyModule + { + private readonly IConfigFileProvider _configFileProvider; + private readonly IAnalyticsService _analyticsService; + + + public InitializeJsModule(IConfigFileProvider configFileProvider, + IAnalyticsService analyticsService) + { + _configFileProvider = configFileProvider; + _analyticsService = analyticsService; + + Get["/initialize.js"] = x => Index(); + } + + private Response Index() + { + // TODO: Move away from window.Lidarr and prefetch the information returned here when starting the UI + return new StreamResponse(GetContentStream, "application/javascript"); + } + + private Stream GetContentStream() + { + var urlBase = _configFileProvider.UrlBase; + var stream = new MemoryStream(); + var writer = new StreamWriter(stream); + + writer.WriteLine("window.Lidarr = {"); + writer.WriteLine($" apiRoot: '{urlBase}/api/v1',"); + writer.WriteLine($" apiKey: '{_configFileProvider.ApiKey}',"); + writer.WriteLine($" release: '{BuildInfo.Release}',"); + writer.WriteLine($" version: '{BuildInfo.Version.ToString()}',"); + writer.WriteLine($" branch: '{_configFileProvider.Branch.ToLower()}',"); + writer.WriteLine($" analytics: {_analyticsService.IsEnabled.ToString().ToLowerInvariant()},"); + writer.WriteLine($" urlBase: '{urlBase}',"); + writer.WriteLine($" isProduction: {RuntimeInfo.IsProduction.ToString().ToLowerInvariant()}"); + writer.WriteLine("};"); + + writer.Flush(); + stream.Position = 0; + + return stream; + } + } +} diff --git a/src/Lidarr.Http/Frontend/Mappers/HtmlMapperBase.cs b/src/Lidarr.Http/Frontend/Mappers/HtmlMapperBase.cs index 66a1c62f5..3b5a30cc2 100644 --- a/src/Lidarr.Http/Frontend/Mappers/HtmlMapperBase.cs +++ b/src/Lidarr.Http/Frontend/Mappers/HtmlMapperBase.cs @@ -74,16 +74,9 @@ namespace Lidarr.Http.Frontend.Mappers return string.Format("{0}=\"{1}{2}\"", match.Groups["attribute"].Value, UrlBase, url); }); - text = ReplaceText(text); - _generatedContent = text; return _generatedContent; } - - protected virtual string ReplaceText(string text) - { - return text; - } } } diff --git a/src/Lidarr.Http/Frontend/Mappers/IndexHtmlMapper.cs b/src/Lidarr.Http/Frontend/Mappers/IndexHtmlMapper.cs index d2e64d8b0..62b041acc 100644 --- a/src/Lidarr.Http/Frontend/Mappers/IndexHtmlMapper.cs +++ b/src/Lidarr.Http/Frontend/Mappers/IndexHtmlMapper.cs @@ -3,7 +3,6 @@ using System.IO; using NLog; using NzbDrone.Common.Disk; using NzbDrone.Common.EnvironmentInfo; -using NzbDrone.Core.Analytics; using NzbDrone.Core.Configuration; namespace Lidarr.Http.Frontend.Mappers @@ -11,25 +10,20 @@ namespace Lidarr.Http.Frontend.Mappers public class IndexHtmlMapper : HtmlMapperBase { private readonly IConfigFileProvider _configFileProvider; - private readonly IAnalyticsService _analyticsService; private static string API_KEY; public IndexHtmlMapper(IAppFolderInfo appFolderInfo, IDiskProvider diskProvider, IConfigFileProvider configFileProvider, - IAnalyticsService analyticsService, Func cacheBreakProviderFactory, Logger logger) : base(diskProvider, cacheBreakProviderFactory, logger) { _configFileProvider = configFileProvider; - _analyticsService = analyticsService; HtmlPath = Path.Combine(appFolderInfo.StartUpFolder, _configFileProvider.UiFolder, "index.html"); UrlBase = configFileProvider.UrlBase; - - API_KEY = configFileProvider.ApiKey; } public override string Map(string resourceUrl) @@ -47,19 +41,5 @@ namespace Lidarr.Http.Frontend.Mappers !resourceUrl.StartsWith("/login"); } - - protected override string ReplaceText(string text) - { - text = text.Replace("API_ROOT", UrlBase + "/api/v1"); - text = text.Replace("API_KEY", API_KEY); - text = text.Replace("RELEASE", BuildInfo.Release); - text = text.Replace("APP_VERSION", BuildInfo.Version.ToString()); - text = text.Replace("APP_BRANCH", _configFileProvider.Branch.ToLower()); - text = text.Replace("APP_ANALYTICS", _analyticsService.IsEnabled.ToString().ToLowerInvariant()); - text = text.Replace("URL_BASE", UrlBase); - text = text.Replace("IS_PRODUCTION", RuntimeInfo.IsProduction.ToString().ToLowerInvariant()); - - return text; - } } } diff --git a/src/Lidarr.Http/Frontend/Mappers/StaticResourceMapper.cs b/src/Lidarr.Http/Frontend/Mappers/StaticResourceMapper.cs index 0799ef5d4..98f282961 100644 --- a/src/Lidarr.Http/Frontend/Mappers/StaticResourceMapper.cs +++ b/src/Lidarr.Http/Frontend/Mappers/StaticResourceMapper.cs @@ -37,7 +37,7 @@ namespace Lidarr.Http.Frontend.Mappers } return resourceUrl.StartsWith("/content") || - resourceUrl.EndsWith(".js") || + (resourceUrl.EndsWith(".js") && !resourceUrl.EndsWith("initialize.js")) || resourceUrl.EndsWith(".map") || resourceUrl.EndsWith(".css") || (resourceUrl.EndsWith(".ico") && !resourceUrl.Equals("/favicon.ico")) || diff --git a/src/Lidarr.Http/Frontend/StaticResourceModule.cs b/src/Lidarr.Http/Frontend/StaticResourceModule.cs index 6b10e1125..7d4975d55 100644 --- a/src/Lidarr.Http/Frontend/StaticResourceModule.cs +++ b/src/Lidarr.Http/Frontend/StaticResourceModule.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Linq; using Nancy; using NLog; -using NzbDrone.Core.Configuration; using Lidarr.Http.Frontend.Mappers; namespace Lidarr.Http.Frontend @@ -11,14 +10,12 @@ namespace Lidarr.Http.Frontend public class StaticResourceModule : NancyModule { private readonly IEnumerable _requestMappers; - private readonly IConfigFileProvider _configFileProvider; private readonly Logger _logger; - public StaticResourceModule(IEnumerable requestMappers, IConfigFileProvider configFileProvider, Logger logger) + public StaticResourceModule(IEnumerable requestMappers, Logger logger) { _requestMappers = requestMappers; - _configFileProvider = configFileProvider; _logger = logger; Get["/{resource*}"] = x => Index(); diff --git a/src/Lidarr.Http/Lidarr.Http.csproj b/src/Lidarr.Http/Lidarr.Http.csproj index d6af75307..ab1827725 100644 --- a/src/Lidarr.Http/Lidarr.Http.csproj +++ b/src/Lidarr.Http/Lidarr.Http.csproj @@ -91,6 +91,7 @@ +