From 8f1b57eb88610f10259b0a7821baf2b5bf08e9de Mon Sep 17 00:00:00 2001 From: Jason Dove <1695733+jasongdove@users.noreply.github.com> Date: Mon, 14 Apr 2025 18:53:25 +0000 Subject: [PATCH] another attempt at fixing separate ports behind reverse proxy (#1994) --- CHANGELOG.md | 2 - ErsatzTV.Core/Settings.cs | 3 - ErsatzTV/Program.cs | 16 ---- ErsatzTV/Properties/launchSettings.json | 2 - ErsatzTV/Startup.cs | 105 ++++++++---------------- 5 files changed, 34 insertions(+), 94 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bdf0110..19746635 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,8 +12,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Add environment variables to allow ETV to run UI and streaming on separate ports - `ETV_STREAMING_PORT`: port used for streaming requests, defaults to 8409 - `ETV_UI_PORT`: port used for admin UI, defaults to 8409 - - `ETV_PUBLIC_STREAMING_PORT`: port used for streaming requests at reverse proxy, defaults to 8409 - - `ETV_PUBLIC_UI_PORT`: port used for admin UI at reverse proxy, defaults to 8409 ### Fixed - Fix error message about synchronizing Plex collections from a Plex server that has zero collections diff --git a/ErsatzTV.Core/Settings.cs b/ErsatzTV.Core/Settings.cs index 2cae0b98..519cc720 100644 --- a/ErsatzTV.Core/Settings.cs +++ b/ErsatzTV.Core/Settings.cs @@ -4,7 +4,4 @@ public static class Settings { public static int UiPort { get; set; } public static int StreamingPort { get; set; } - - public static int PublicUiPort { get; set; } - public static int PublicStreamingPort { get; set; } } diff --git a/ErsatzTV/Program.cs b/ErsatzTV/Program.cs index 30b77d43..1d6ca156 100644 --- a/ErsatzTV/Program.cs +++ b/ErsatzTV/Program.cs @@ -141,14 +141,6 @@ public class Program Settings.UiPort = uiPort; - string publicUiPortVariable = Environment.GetEnvironmentVariable("ETV_PUBLIC_UI_PORT"); - if (!int.TryParse(publicUiPortVariable, out int publicUiPort)) - { - publicUiPort = 8409; - } - - Settings.PublicUiPort = publicUiPort; - string streamingPortVariable = Environment.GetEnvironmentVariable("ETV_STREAMING_PORT"); if (!int.TryParse(streamingPortVariable, out int streamingPort)) { @@ -157,14 +149,6 @@ public class Program Settings.StreamingPort = streamingPort; - string publicStreamingPortVariable = Environment.GetEnvironmentVariable("ETV_PUBLIC_STREAMING_PORT"); - if (!int.TryParse(publicStreamingPortVariable, out int publicStreamingPort)) - { - publicStreamingPort = 8409; - } - - Settings.PublicStreamingPort = publicStreamingPort; - return Host.CreateDefaultBuilder(args) .ConfigureServices(services => services.AddSingleton(LoggingLevelSwitches)) .ConfigureWebHostDefaults( diff --git a/ErsatzTV/Properties/launchSettings.json b/ErsatzTV/Properties/launchSettings.json index 0a3a74d9..a3d86df0 100644 --- a/ErsatzTV/Properties/launchSettings.json +++ b/ErsatzTV/Properties/launchSettings.json @@ -8,8 +8,6 @@ "DOTNET_ENVIRONMENT": "Development", "ETV_STREAMING_PORT": "8409", "ETV_UI_PORT": "8410", - "ETV_PUBLIC_STREAMING_PORT": "8409", - "ETV_PUBLIC_UI_PORT": "8410" } } } diff --git a/ErsatzTV/Startup.cs b/ErsatzTV/Startup.cs index ff0c3b68..f5b8b73b 100644 --- a/ErsatzTV/Startup.cs +++ b/ErsatzTV/Startup.cs @@ -556,86 +556,49 @@ public class Startup app.UseResponseCompression(); - if (Settings.StreamingPort == Settings.UiPort) - { - app.MapWhen( - ctx => !ctx.Request.Path.StartsWithSegments("/iptv"), - blazor => + app.Use( + async (context, next) => + { + if (!context.Request.Path.StartsWithSegments("/iptv") && + context.Connection.LocalPort != Settings.UiPort) { - blazor.UseRouting(); + context.Response.StatusCode = 404; + return; + } - if (OidcHelper.IsEnabled) - { - blazor.UseAuthentication(); -#pragma warning disable ASP0001 - blazor.UseAuthorization(); -#pragma warning restore ASP0001 - } + await next(context); + }); - blazor.UseEndpoints( - endpoints => - { - endpoints.MapControllers(); - endpoints.MapBlazorHub(); - endpoints.MapFallbackToPage("/_Host"); - }); - }); + app.MapWhen( + ctx => !ctx.Request.Path.StartsWithSegments("/iptv"), + blazor => + { + blazor.UseRouting(); - app.MapWhen( - ctx => ctx.Request.Path.StartsWithSegments("/iptv"), - iptv => - { - iptv.UseRouting(); - iptv.UseEndpoints(endpoints => endpoints.MapControllers()); - }); - } - else - { - app.MapWhen( - ctx => ctx.Request.Host.Port == Settings.UiPort || ctx.Request.Host.Port == Settings.PublicUiPort, - uiApp => + if (OidcHelper.IsEnabled) { - uiApp.UseRouting(); - - if (OidcHelper.IsEnabled) - { - uiApp.UseAuthentication(); + blazor.UseAuthentication(); #pragma warning disable ASP0001 - uiApp.UseAuthorization(); + blazor.UseAuthorization(); #pragma warning restore ASP0001 - } - - uiApp.UseEndpoints( - endpoints => - { - endpoints.MapControllers(); - endpoints.MapBlazorHub(); - endpoints.MapFallbackToPage("/_Host"); - }); - }); - - app.MapWhen( - ctx => ctx.Request.Host.Port == Settings.StreamingPort || ctx.Request.Host.Port == Settings.PublicStreamingPort, - streamingApp => - { - streamingApp.UseRouting(); - - streamingApp.UseWhen( - c => !c.Request.Path.StartsWithSegments("/iptv"), - a => - { - a.Run( - c => - { - c.Response.StatusCode = 404; - return Task.CompletedTask; - }); - }); + } - streamingApp.UseEndpoints(endpoints => endpoints.MapControllers()); + blazor.UseEndpoints( + endpoints => + { + endpoints.MapControllers(); + endpoints.MapBlazorHub(); + endpoints.MapFallbackToPage("/_Host"); + }); + }); - }); - } + app.MapWhen( + ctx => ctx.Request.Path.StartsWithSegments("/iptv"), + iptv => + { + iptv.UseRouting(); + iptv.UseEndpoints(endpoints => endpoints.MapControllers()); + }); } private static void CustomServices(IServiceCollection services)