(),
+ builderContext,
+ options);
+ })
.Configure(app =>
{
app.UseHealthChecks("/health");
@@ -57,14 +104,14 @@ public sealed class SetupServer : IDisposable
{
loggerRoute.Run(async context =>
{
- var networkManager = networkManagerFactory();
+ var networkManager = _networkManagerFactory();
if (context.Connection.RemoteIpAddress is null || networkManager is null || !networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
{
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return;
}
- var logFilePath = new DirectoryInfo(applicationPaths.LogDirectoryPath)
+ var logFilePath = new DirectoryInfo(_applicationPaths.LogDirectoryPath)
.EnumerateFiles()
.OrderBy(f => f.CreationTimeUtc)
.FirstOrDefault()
@@ -80,20 +127,20 @@ public sealed class SetupServer : IDisposable
{
systemRoute.Run(async context =>
{
- var jfApplicationHost = serverApplicationHost();
+ var jfApplicationHost = _serverFactory();
var retryCounter = 0;
while (jfApplicationHost is null && retryCounter < 5)
{
await Task.Delay(500).ConfigureAwait(false);
- jfApplicationHost = serverApplicationHost();
+ jfApplicationHost = _serverFactory();
retryCounter++;
}
if (jfApplicationHost is null)
{
context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
- context.Response.Headers.RetryAfter = new Microsoft.Extensions.Primitives.StringValues("60");
+ context.Response.Headers.RetryAfter = new StringValues("5");
return;
}
@@ -114,9 +161,10 @@ public sealed class SetupServer : IDisposable
app.Run((context) =>
{
context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
- context.Response.Headers.RetryAfter = new Microsoft.Extensions.Primitives.StringValues("60");
+ context.Response.Headers.RetryAfter = new StringValues("5");
+ context.Response.Headers.ContentType = new StringValues("text/html");
context.Response.WriteAsync("Jellyfin Server still starting. Please wait.
");
- var networkManager = networkManagerFactory();
+ var networkManager = _networkManagerFactory();
if (networkManager is not null && context.Connection.RemoteIpAddress is not null && networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
{
context.Response.WriteAsync("You can download the current logfiles here.
");
diff --git a/src/Jellyfin.Networking/Manager/NetworkManager.cs b/src/Jellyfin.Networking/Manager/NetworkManager.cs
index 6f6ee51461..80a5741df9 100644
--- a/src/Jellyfin.Networking/Manager/NetworkManager.cs
+++ b/src/Jellyfin.Networking/Manager/NetworkManager.cs
@@ -7,6 +7,7 @@ using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
+using J2N.Collections.Generic.Extensions;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Net;
@@ -214,81 +215,92 @@ public class NetworkManager : INetworkManager, IDisposable
{
lock (_initLock)
{
- _logger.LogDebug("Refreshing interfaces.");
+ _interfaces = GetInterfacesCore(_logger, IsIPv4Enabled, IsIPv6Enabled).ToList();
+ }
+ }
- var interfaces = new List();
+ ///
+ /// Generate a list of all the interface ip addresses and submasks where that are in the active/unknown state.
+ ///
+ /// The logger.
+ /// If true evaluates IPV4 type ip addresses.
+ /// If true evaluates IPV6 type ip addresses.
+ /// A list of all locally known up addresses and submasks that are to be considered usable.
+ public static IReadOnlyList GetInterfacesCore(ILogger logger, bool isIPv4Enabled, bool isIPv6Enabled)
+ {
+ logger.LogDebug("Refreshing interfaces.");
- try
+ var interfaces = new List();
+
+ try
+ {
+ var nics = NetworkInterface.GetAllNetworkInterfaces()
+ .Where(i => i.OperationalStatus == OperationalStatus.Up);
+
+ foreach (NetworkInterface adapter in nics)
{
- var nics = NetworkInterface.GetAllNetworkInterfaces()
- .Where(i => i.OperationalStatus == OperationalStatus.Up);
-
- foreach (NetworkInterface adapter in nics)
+ try
{
- try
+ var ipProperties = adapter.GetIPProperties();
+
+ // Populate interface list
+ foreach (var info in ipProperties.UnicastAddresses)
{
- var ipProperties = adapter.GetIPProperties();
-
- // Populate interface list
- foreach (var info in ipProperties.UnicastAddresses)
+ if (isIPv4Enabled && info.Address.AddressFamily == AddressFamily.InterNetwork)
{
- if (IsIPv4Enabled && info.Address.AddressFamily == AddressFamily.InterNetwork)
+ var interfaceObject = new IPData(info.Address, new IPNetwork(info.Address, info.PrefixLength), adapter.Name)
{
- var interfaceObject = new IPData(info.Address, new IPNetwork(info.Address, info.PrefixLength), adapter.Name)
- {
- Index = ipProperties.GetIPv4Properties().Index,
- Name = adapter.Name,
- SupportsMulticast = adapter.SupportsMulticast
- };
+ Index = ipProperties.GetIPv4Properties().Index,
+ Name = adapter.Name,
+ SupportsMulticast = adapter.SupportsMulticast
+ };
- interfaces.Add(interfaceObject);
- }
- else if (IsIPv6Enabled && info.Address.AddressFamily == AddressFamily.InterNetworkV6)
+ interfaces.Add(interfaceObject);
+ }
+ else if (isIPv6Enabled && info.Address.AddressFamily == AddressFamily.InterNetworkV6)
+ {
+ var interfaceObject = new IPData(info.Address, new IPNetwork(info.Address, info.PrefixLength), adapter.Name)
{
- var interfaceObject = new IPData(info.Address, new IPNetwork(info.Address, info.PrefixLength), adapter.Name)
- {
- Index = ipProperties.GetIPv6Properties().Index,
- Name = adapter.Name,
- SupportsMulticast = adapter.SupportsMulticast
- };
+ Index = ipProperties.GetIPv6Properties().Index,
+ Name = adapter.Name,
+ SupportsMulticast = adapter.SupportsMulticast
+ };
- interfaces.Add(interfaceObject);
- }
+ interfaces.Add(interfaceObject);
}
}
- catch (Exception ex)
- {
- // Ignore error, and attempt to continue.
- _logger.LogError(ex, "Error encountered parsing interfaces.");
- }
}
- }
- catch (Exception ex)
- {
- _logger.LogError(ex, "Error obtaining interfaces.");
- }
-
- // If no interfaces are found, fallback to loopback interfaces.
- if (interfaces.Count == 0)
- {
- _logger.LogWarning("No interface information available. Using loopback interface(s).");
-
- if (IsIPv4Enabled)
+ catch (Exception ex)
{
- interfaces.Add(new IPData(IPAddress.Loopback, NetworkConstants.IPv4RFC5735Loopback, "lo"));
- }
-
- if (IsIPv6Enabled)
- {
- interfaces.Add(new IPData(IPAddress.IPv6Loopback, NetworkConstants.IPv6RFC4291Loopback, "lo"));
+ // Ignore error, and attempt to continue.
+ logger.LogError(ex, "Error encountered parsing interfaces.");
}
}
-
- _logger.LogDebug("Discovered {NumberOfInterfaces} interfaces.", interfaces.Count);
- _logger.LogDebug("Interfaces addresses: {Addresses}", interfaces.OrderByDescending(s => s.AddressFamily == AddressFamily.InterNetwork).Select(s => s.Address.ToString()));
-
- _interfaces = interfaces;
}
+ catch (Exception ex)
+ {
+ logger.LogError(ex, "Error obtaining interfaces.");
+ }
+
+ // If no interfaces are found, fallback to loopback interfaces.
+ if (interfaces.Count == 0)
+ {
+ logger.LogWarning("No interface information available. Using loopback interface(s).");
+
+ if (isIPv4Enabled)
+ {
+ interfaces.Add(new IPData(IPAddress.Loopback, NetworkConstants.IPv4RFC5735Loopback, "lo"));
+ }
+
+ if (isIPv6Enabled)
+ {
+ interfaces.Add(new IPData(IPAddress.IPv6Loopback, NetworkConstants.IPv6RFC4291Loopback, "lo"));
+ }
+ }
+
+ logger.LogDebug("Discovered {NumberOfInterfaces} interfaces.", interfaces.Count);
+ logger.LogDebug("Interfaces addresses: {Addresses}", interfaces.OrderByDescending(s => s.AddressFamily == AddressFamily.InterNetwork).Select(s => s.Address.ToString()));
+ return interfaces;
}
///
@@ -345,66 +357,78 @@ public class NetworkManager : INetworkManager, IDisposable
{
lock (_initLock)
{
- // Respect explicit bind addresses
- var interfaces = _interfaces.ToList();
- var localNetworkAddresses = config.LocalNetworkAddresses;
- if (localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]))
- {
- var bindAddresses = localNetworkAddresses.Select(p => NetworkUtils.TryParseToSubnet(p, out var network)
- ? network.Prefix
- : (interfaces.Where(x => x.Name.Equals(p, StringComparison.OrdinalIgnoreCase))
- .Select(x => x.Address)
- .FirstOrDefault() ?? IPAddress.None))
- .Where(x => x != IPAddress.None)
- .ToHashSet();
- interfaces = interfaces.Where(x => bindAddresses.Contains(x.Address)).ToList();
-
- if (bindAddresses.Contains(IPAddress.Loopback) && !interfaces.Any(i => i.Address.Equals(IPAddress.Loopback)))
- {
- interfaces.Add(new IPData(IPAddress.Loopback, NetworkConstants.IPv4RFC5735Loopback, "lo"));
- }
-
- if (bindAddresses.Contains(IPAddress.IPv6Loopback) && !interfaces.Any(i => i.Address.Equals(IPAddress.IPv6Loopback)))
- {
- interfaces.Add(new IPData(IPAddress.IPv6Loopback, NetworkConstants.IPv6RFC4291Loopback, "lo"));
- }
- }
-
- // Remove all interfaces matching any virtual machine interface prefix
- if (config.IgnoreVirtualInterfaces)
- {
- // Remove potentially existing * and split config string into prefixes
- var virtualInterfacePrefixes = config.VirtualInterfaceNames
- .Select(i => i.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase));
-
- // Check all interfaces for matches against the prefixes and remove them
- if (_interfaces.Count > 0)
- {
- foreach (var virtualInterfacePrefix in virtualInterfacePrefixes)
- {
- interfaces.RemoveAll(x => x.Name.StartsWith(virtualInterfacePrefix, StringComparison.OrdinalIgnoreCase));
- }
- }
- }
-
- // Remove all IPv4 interfaces if IPv4 is disabled
- if (!IsIPv4Enabled)
- {
- interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetwork);
- }
-
- // Remove all IPv6 interfaces if IPv6 is disabled
- if (!IsIPv6Enabled)
- {
- interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetworkV6);
- }
-
- // Users may have complex networking configuration that multiple interfaces sharing the same IP address
- // Only return one IP for binding, and let the OS handle the rest
- _interfaces = interfaces.DistinctBy(iface => iface.Address).ToList();
+ _interfaces = FilterBindSettings(config, _interfaces, IsIPv4Enabled, IsIPv6Enabled).ToList();
}
}
+ ///
+ /// Filteres a list of bind addresses and exclusions on available interfaces.
+ ///
+ /// The network config to be filtered by.
+ /// A list of possible interfaces to be filtered.
+ /// If true evaluates IPV4 type ip addresses.
+ /// If true evaluates IPV6 type ip addresses.
+ /// A list of all locally known up addresses and submasks that are to be considered usable.
+ public static IReadOnlyList FilterBindSettings(NetworkConfiguration config, IList interfaces, bool isIPv4Enabled, bool isIPv6Enabled)
+ {
+ // Respect explicit bind addresses
+ var localNetworkAddresses = config.LocalNetworkAddresses;
+ if (localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]))
+ {
+ var bindAddresses = localNetworkAddresses.Select(p => NetworkUtils.TryParseToSubnet(p, out var network)
+ ? network.Prefix
+ : (interfaces.Where(x => x.Name.Equals(p, StringComparison.OrdinalIgnoreCase))
+ .Select(x => x.Address)
+ .FirstOrDefault() ?? IPAddress.None))
+ .Where(x => x != IPAddress.None)
+ .ToHashSet();
+ interfaces = interfaces.Where(x => bindAddresses.Contains(x.Address)).ToList();
+
+ if (bindAddresses.Contains(IPAddress.Loopback) && !interfaces.Any(i => i.Address.Equals(IPAddress.Loopback)))
+ {
+ interfaces.Add(new IPData(IPAddress.Loopback, NetworkConstants.IPv4RFC5735Loopback, "lo"));
+ }
+
+ if (bindAddresses.Contains(IPAddress.IPv6Loopback) && !interfaces.Any(i => i.Address.Equals(IPAddress.IPv6Loopback)))
+ {
+ interfaces.Add(new IPData(IPAddress.IPv6Loopback, NetworkConstants.IPv6RFC4291Loopback, "lo"));
+ }
+ }
+
+ // Remove all interfaces matching any virtual machine interface prefix
+ if (config.IgnoreVirtualInterfaces)
+ {
+ // Remove potentially existing * and split config string into prefixes
+ var virtualInterfacePrefixes = config.VirtualInterfaceNames
+ .Select(i => i.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase));
+
+ // Check all interfaces for matches against the prefixes and remove them
+ if (interfaces.Count > 0)
+ {
+ foreach (var virtualInterfacePrefix in virtualInterfacePrefixes)
+ {
+ interfaces.RemoveAll(x => x.Name.StartsWith(virtualInterfacePrefix, StringComparison.OrdinalIgnoreCase));
+ }
+ }
+ }
+
+ // Remove all IPv4 interfaces if IPv4 is disabled
+ if (!isIPv4Enabled)
+ {
+ interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetwork);
+ }
+
+ // Remove all IPv6 interfaces if IPv6 is disabled
+ if (!isIPv6Enabled)
+ {
+ interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetworkV6);
+ }
+
+ // Users may have complex networking configuration that multiple interfaces sharing the same IP address
+ // Only return one IP for binding, and let the OS handle the rest
+ return interfaces.DistinctBy(iface => iface.Address).ToList();
+ }
+
///
/// Initializes the remote address values.
///
@@ -720,28 +744,47 @@ public class NetworkManager : INetworkManager, IDisposable
///
public IReadOnlyList GetAllBindInterfaces(bool individualInterfaces = false)
{
- var config = _configurationManager.GetNetworkConfiguration();
+ return NetworkManager.GetAllBindInterfaces(individualInterfaces, _configurationManager, _interfaces, IsIPv4Enabled, IsIPv6Enabled);
+ }
+
+ ///
+ /// Reads the jellyfin configuration of the configuration manager and produces a list of interfaces that should be bound.
+ ///
+ /// Defines that only known interfaces should be used.
+ /// The ConfigurationManager.
+ /// The known interfaces that gets returned if possible or instructed.
+ /// Include IPV4 type interfaces.
+ /// Include IPV6 type interfaces.
+ /// A list of ip address of which jellyfin should bind to.
+ public static IReadOnlyList GetAllBindInterfaces(
+ bool individualInterfaces,
+ IConfigurationManager configurationManager,
+ IReadOnlyList knownInterfaces,
+ bool readIpv4,
+ bool readIpv6)
+ {
+ var config = configurationManager.GetNetworkConfiguration();
var localNetworkAddresses = config.LocalNetworkAddresses;
- if ((localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]) && _interfaces.Count > 0) || individualInterfaces)
+ if ((localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]) && knownInterfaces.Count > 0) || individualInterfaces)
{
- return _interfaces;
+ return knownInterfaces;
}
// No bind address and no exclusions, so listen on all interfaces.
var result = new List();
- if (IsIPv4Enabled && IsIPv6Enabled)
+ if (readIpv4 && readIpv6)
{
// Kestrel source code shows it uses Sockets.DualMode - so this also covers IPAddress.Any by default
result.Add(new IPData(IPAddress.IPv6Any, NetworkConstants.IPv6Any));
}
- else if (IsIPv4Enabled)
+ else if (readIpv4)
{
result.Add(new IPData(IPAddress.Any, NetworkConstants.IPv4Any));
}
- else if (IsIPv6Enabled)
+ else if (readIpv6)
{
// Cannot use IPv6Any as Kestrel will bind to IPv4 addresses too.
- foreach (var iface in _interfaces)
+ foreach (var iface in knownInterfaces)
{
if (iface.AddressFamily == AddressFamily.InterNetworkV6)
{