diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index c9f900660b..34b1f0daaf 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -129,6 +129,16 @@ namespace Jellyfin.Networking.Manager /// public static string MockNetworkSettings { get; set; } = string.Empty; + /// + /// Gets or sets a value indicating whether the system has IP4 is enabled. + /// + public static bool SystemIP4Enabled { get; set; } + + /// + /// Gets or sets a value indicating whether the system has IP6 is enabled. + /// + public static bool SystemIP6Enabled { get; set; } + /// /// Gets or sets a value indicating whether IP6 is enabled. /// @@ -139,16 +149,6 @@ namespace Jellyfin.Networking.Manager /// public bool IsIP4Enabled { get; set; } - /// - /// Gets or sets a value indicating whether the system has IP4 is enabled. - /// - public bool SystemIP4Enabled { get; set; } - - /// - /// Gets or sets a value indicating whether the system has IP6 is enabled. - /// - public bool SystemIP6Enabled { get; set; } - /// public Collection RemoteAddressFilter { get; private set; } diff --git a/Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs b/Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs index 9b4e53fc09..0a7ae9a410 100644 --- a/Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs +++ b/Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs @@ -25,6 +25,7 @@ using Jellyfin.Api.Controllers; using Jellyfin.Api.ModelBinders; using Jellyfin.Data.Enums; using Jellyfin.Networking.Configuration; +using Jellyfin.Networking.Manager; using Jellyfin.Server.Configuration; using Jellyfin.Server.Filters; using Jellyfin.Server.Formatters; @@ -174,64 +175,14 @@ namespace Jellyfin.Server.Extensions .AddScheme(AuthenticationSchemes.CustomAuthentication, null); } - private static void AddIpAddress(NetworkConfiguration config, ForwardedHeadersOptions options, IPAddress addr, int prefixLength, bool systemIP6Enabled) - { - if ((!config.EnableIPV4 && addr.AddressFamily == AddressFamily.InterNetwork) || (!config.EnableIPV6 && addr.AddressFamily == AddressFamily.InterNetworkV6)) - { - return; - } - - if (systemIP6Enabled && addr.AddressFamily == AddressFamily.InterNetwork) - { - // If the server is using dual-mode sockets, IPv4 addresses are supplied in an IPv6 format. - // https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-5.0 . - addr = addr.MapToIPv6(); - } - - if (prefixLength == 32) - { - options.KnownProxies.Add(addr); - } - else - { - options.KnownNetworks.Add(new IPNetwork(addr, prefixLength)); - } - } - - /// - /// Sets up the proxy configuration based on the addresses in . - /// - /// The instance. - /// The containing the config settings. - /// The string array to parse. - /// The instance. - internal static void ParseList(INetworkManager networkManager, NetworkConfiguration config, string[] userList, ForwardedHeadersOptions options) - { - for (var i = 0; i < userList.Length; i++) - { - if (IPNetAddress.TryParse(userList[i], out var addr)) - { - AddIpAddress(config, options, addr.Address, addr.PrefixLength, networkManager.SystemIP6Enabled); - } - else if (IPHost.TryParse(userList[i], out var host)) - { - foreach (var address in host.GetAddresses()) - { - AddIpAddress(config, options, addr.Address, addr.PrefixLength, networkManager.SystemIP6Enabled); - } - } - } - } - /// /// Extension method for adding the jellyfin API to the service collection. /// /// The service collection. /// An IEnumerable containing all plugin assemblies with API controllers. /// The . - /// The instance. /// The MVC builder. - public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, IEnumerable pluginAssemblies, NetworkConfiguration config, INetworkManager networkManager) + public static IMvcBuilder AddJellyfinApi(this IServiceCollection serviceCollection, IEnumerable pluginAssemblies, NetworkConfiguration config) { IMvcBuilder mvcBuilder = serviceCollection .AddCors() @@ -249,7 +200,7 @@ namespace Jellyfin.Server.Extensions } else { - ParseList(networkManager, config, config.KnownProxies, options); + ParseList(config, config.KnownProxies, options); } // Only set forward limit if we have some known proxies or some known networks. @@ -370,6 +321,54 @@ namespace Jellyfin.Server.Extensions }); } + /// + /// Sets up the proxy configuration based on the addresses in . + /// + /// The containing the config settings. + /// The string array to parse. + /// The instance. + internal static void ParseList(NetworkConfiguration config, string[] userList, ForwardedHeadersOptions options) + { + for (var i = 0; i < userList.Length; i++) + { + if (IPNetAddress.TryParse(userList[i], out var addr)) + { + AddIpAddress(config, options, addr.Address, addr.PrefixLength); + } + else if (IPHost.TryParse(userList[i], out var host)) + { + foreach (var address in host.GetAddresses()) + { + AddIpAddress(config, options, addr.Address, addr.PrefixLength); + } + } + } + } + + private static void AddIpAddress(NetworkConfiguration config, ForwardedHeadersOptions options, IPAddress addr, int prefixLength) + { + if ((!config.EnableIPV4 && addr.AddressFamily == AddressFamily.InterNetwork) || (!config.EnableIPV6 && addr.AddressFamily == AddressFamily.InterNetworkV6)) + { + return; + } + + if (NetworkManager.SystemIP6Enabled && addr.AddressFamily == AddressFamily.InterNetwork) + { + // If the server is using dual-mode sockets, IPv4 addresses are supplied in an IPv6 format. + // https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer?view=aspnetcore-5.0 . + addr = addr.MapToIPv6(); + } + + if (prefixLength == 32) + { + options.KnownProxies.Add(addr); + } + else + { + options.KnownNetworks.Add(new IPNetwork(addr, prefixLength)); + } + } + private static void AddSwaggerTypeMappings(this SwaggerGenOptions options) { /* diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index 05228ccaab..e56e61092b 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -26,22 +26,18 @@ namespace Jellyfin.Server { private readonly IServerConfigurationManager _serverConfigurationManager; private readonly IServerApplicationHost _serverApplicationHost; - private readonly INetworkManager _networkManager; /// /// Initializes a new instance of the class. /// /// The server configuration manager. /// The server application host. - /// The network manager. public Startup( IServerConfigurationManager serverConfigurationManager, - IServerApplicationHost serverApplicationHost, - INetworkManager networkManager) + IServerApplicationHost serverApplicationHost) { _serverConfigurationManager = serverConfigurationManager; _serverApplicationHost = serverApplicationHost; - _networkManager = networkManager; } /// @@ -56,7 +52,7 @@ namespace Jellyfin.Server { options.HttpsPort = _serverApplicationHost.HttpsPort; }); - services.AddJellyfinApi(_serverApplicationHost.GetApiPluginAssemblies(), _serverConfigurationManager.GetNetworkConfiguration(), _networkManager); + services.AddJellyfinApi(_serverApplicationHost.GetApiPluginAssemblies(), _serverConfigurationManager.GetNetworkConfiguration()); services.AddJellyfinApiSwagger(); diff --git a/MediaBrowser.Common/Net/INetworkManager.cs b/MediaBrowser.Common/Net/INetworkManager.cs index 7ee76107bc..b6c390d239 100644 --- a/MediaBrowser.Common/Net/INetworkManager.cs +++ b/MediaBrowser.Common/Net/INetworkManager.cs @@ -44,16 +44,6 @@ namespace MediaBrowser.Common.Net /// bool IsIP4Enabled { get; set; } - /// - /// Gets or sets a value indicating whether the system has IP4 is enabled. - /// - bool SystemIP4Enabled { get; set; } - - /// - /// Gets or sets a value indicating whether the system has IP6 is enabled. - /// - bool SystemIP6Enabled { get; set; } - /// /// Calculates the list of interfaces to use for Kestrel. /// diff --git a/tests/Jellyfin.Api.Tests/ParseNetworkTests.cs b/tests/Jellyfin.Api.Tests/ParseNetworkTests.cs index 6ee66d2a8f..a76b8b2439 100644 --- a/tests/Jellyfin.Api.Tests/ParseNetworkTests.cs +++ b/tests/Jellyfin.Api.Tests/ParseNetworkTests.cs @@ -30,7 +30,7 @@ namespace Jellyfin.Api.Tests public void TestNetworks(bool ip4, bool ip6, string hostList, string match) { using var nm = CreateNetworkManager(); - nm.SystemIP6Enabled = ip6; + NetworkManager.SystemIP6Enabled = ip6; var settings = new NetworkConfiguration { @@ -45,7 +45,7 @@ namespace Jellyfin.Api.Tests options.KnownProxies.Clear(); options.KnownNetworks.Clear(); - ApiServiceCollectionExtensions.ParseList(nm, settings, hostList.Split(","), options); + ApiServiceCollectionExtensions.ParseList(settings, hostList.Split(","), options); var sb = new StringBuilder(); foreach (var item in options.KnownProxies)