mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-09 03:04:24 -04:00
Add port awareness to startup server (#13913)
This commit is contained in:
parent
269508be9f
commit
7df6e0b16f
@ -1,10 +1,14 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using Jellyfin.Server.Helpers;
|
using Jellyfin.Server.Helpers;
|
||||||
using MediaBrowser.Common.Configuration;
|
using MediaBrowser.Common.Configuration;
|
||||||
using MediaBrowser.Controller.Extensions;
|
using MediaBrowser.Controller.Extensions;
|
||||||
|
using MediaBrowser.Model.Net;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@ -35,28 +39,59 @@ public static class WebHostBuilderExtensions
|
|||||||
return builder
|
return builder
|
||||||
.UseKestrel((builderContext, options) =>
|
.UseKestrel((builderContext, options) =>
|
||||||
{
|
{
|
||||||
var addresses = appHost.NetManager.GetAllBindInterfaces(false);
|
SetupJellyfinWebServer(
|
||||||
|
appHost.NetManager.GetAllBindInterfaces(false),
|
||||||
|
appHost.HttpPort,
|
||||||
|
appHost.ListenWithHttps ? appHost.HttpsPort : null,
|
||||||
|
appHost.Certificate,
|
||||||
|
startupConfig,
|
||||||
|
appPaths,
|
||||||
|
logger,
|
||||||
|
builderContext,
|
||||||
|
options);
|
||||||
|
})
|
||||||
|
.UseStartup(context => new Startup(appHost, context.Configuration));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configures a Kestrel type webServer to bind to the specific arguments.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="addresses">The IP addresses that should be listend to.</param>
|
||||||
|
/// <param name="httpPort">The http port.</param>
|
||||||
|
/// <param name="httpsPort">If set the https port. If set you must also set the certificate.</param>
|
||||||
|
/// <param name="certificate">The certificate used for https port.</param>
|
||||||
|
/// <param name="startupConfig">The startup config.</param>
|
||||||
|
/// <param name="appPaths">The app paths.</param>
|
||||||
|
/// <param name="logger">A logger.</param>
|
||||||
|
/// <param name="builderContext">The kestrel build pipeline context.</param>
|
||||||
|
/// <param name="options">The kestrel server options.</param>
|
||||||
|
/// <exception cref="InvalidOperationException">Will be thrown when a https port is set but no or an invalid certificate is provided.</exception>
|
||||||
|
public static void SetupJellyfinWebServer(
|
||||||
|
IReadOnlyList<IPData> addresses,
|
||||||
|
int httpPort,
|
||||||
|
int? httpsPort,
|
||||||
|
X509Certificate2? certificate,
|
||||||
|
IConfiguration startupConfig,
|
||||||
|
IApplicationPaths appPaths,
|
||||||
|
ILogger logger,
|
||||||
|
WebHostBuilderContext builderContext,
|
||||||
|
KestrelServerOptions options)
|
||||||
|
{
|
||||||
bool flagged = false;
|
bool flagged = false;
|
||||||
foreach (var netAdd in addresses)
|
foreach (var netAdd in addresses)
|
||||||
{
|
{
|
||||||
var address = netAdd.Address;
|
var address = netAdd.Address;
|
||||||
logger.LogInformation("Kestrel is listening on {Address}", address.Equals(IPAddress.IPv6Any) ? "all interfaces" : address);
|
logger.LogInformation("Kestrel is listening on {Address}", address.Equals(IPAddress.IPv6Any) ? "all interfaces" : address);
|
||||||
options.Listen(netAdd.Address, appHost.HttpPort);
|
options.Listen(netAdd.Address, httpPort);
|
||||||
if (appHost.ListenWithHttps)
|
if (httpsPort.HasValue)
|
||||||
{
|
{
|
||||||
options.Listen(
|
if (builderContext.HostingEnvironment.IsDevelopment())
|
||||||
address,
|
|
||||||
appHost.HttpsPort,
|
|
||||||
listenOptions => listenOptions.UseHttps(appHost.Certificate));
|
|
||||||
}
|
|
||||||
else if (builderContext.HostingEnvironment.IsDevelopment())
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
options.Listen(
|
options.Listen(
|
||||||
address,
|
address,
|
||||||
appHost.HttpsPort,
|
httpsPort.Value,
|
||||||
listenOptions => listenOptions.UseHttps());
|
listenOptions => listenOptions.UseHttps());
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException)
|
catch (InvalidOperationException)
|
||||||
@ -68,6 +103,19 @@ public static class WebHostBuilderExtensions
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (certificate is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Cannot run jellyfin with https without setting a valid certificate.");
|
||||||
|
}
|
||||||
|
|
||||||
|
options.Listen(
|
||||||
|
address,
|
||||||
|
httpsPort.Value,
|
||||||
|
listenOptions => listenOptions.UseHttps(certificate));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bind to unix socket (only on unix systems)
|
// Bind to unix socket (only on unix systems)
|
||||||
@ -84,7 +132,5 @@ public static class WebHostBuilderExtensions
|
|||||||
options.ListenUnixSocket(socketPath);
|
options.ListenUnixSocket(socketPath);
|
||||||
logger.LogInformation("Kestrel listening to unix socket {SocketPath}", socketPath);
|
logger.LogInformation("Kestrel listening to unix socket {SocketPath}", socketPath);
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.UseStartup(context => new Startup(appHost, context.Configuration));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ namespace Jellyfin.Server
|
|||||||
public const string LoggingConfigFileSystem = "logging.json";
|
public const string LoggingConfigFileSystem = "logging.json";
|
||||||
|
|
||||||
private static readonly SerilogLoggerFactory _loggerFactory = new SerilogLoggerFactory();
|
private static readonly SerilogLoggerFactory _loggerFactory = new SerilogLoggerFactory();
|
||||||
private static SetupServer _setupServer = new();
|
private static SetupServer? _setupServer;
|
||||||
private static CoreAppHost? _appHost;
|
private static CoreAppHost? _appHost;
|
||||||
private static IHost? _jellyfinHost = null;
|
private static IHost? _jellyfinHost = null;
|
||||||
private static long _startTimestamp;
|
private static long _startTimestamp;
|
||||||
@ -75,7 +75,6 @@ namespace Jellyfin.Server
|
|||||||
{
|
{
|
||||||
_startTimestamp = Stopwatch.GetTimestamp();
|
_startTimestamp = Stopwatch.GetTimestamp();
|
||||||
ServerApplicationPaths appPaths = StartupHelpers.CreateApplicationPaths(options);
|
ServerApplicationPaths appPaths = StartupHelpers.CreateApplicationPaths(options);
|
||||||
await _setupServer.RunAsync(static () => _jellyfinHost?.Services?.GetService<INetworkManager>(), appPaths, static () => _appHost).ConfigureAwait(false);
|
|
||||||
|
|
||||||
// $JELLYFIN_LOG_DIR needs to be set for the logger configuration manager
|
// $JELLYFIN_LOG_DIR needs to be set for the logger configuration manager
|
||||||
Environment.SetEnvironmentVariable("JELLYFIN_LOG_DIR", appPaths.LogDirectoryPath);
|
Environment.SetEnvironmentVariable("JELLYFIN_LOG_DIR", appPaths.LogDirectoryPath);
|
||||||
@ -88,7 +87,8 @@ namespace Jellyfin.Server
|
|||||||
|
|
||||||
// Create an instance of the application configuration to use for application startup
|
// Create an instance of the application configuration to use for application startup
|
||||||
IConfiguration startupConfig = CreateAppConfiguration(options, appPaths);
|
IConfiguration startupConfig = CreateAppConfiguration(options, appPaths);
|
||||||
|
_setupServer = new SetupServer(static () => _jellyfinHost?.Services?.GetService<INetworkManager>(), appPaths, static () => _appHost, _loggerFactory, startupConfig);
|
||||||
|
await _setupServer.RunAsync().ConfigureAwait(false);
|
||||||
StartupHelpers.InitializeLoggingFramework(startupConfig, appPaths);
|
StartupHelpers.InitializeLoggingFramework(startupConfig, appPaths);
|
||||||
_logger = _loggerFactory.CreateLogger("Main");
|
_logger = _loggerFactory.CreateLogger("Main");
|
||||||
|
|
||||||
@ -130,10 +130,12 @@ namespace Jellyfin.Server
|
|||||||
if (_restartOnShutdown)
|
if (_restartOnShutdown)
|
||||||
{
|
{
|
||||||
_startTimestamp = Stopwatch.GetTimestamp();
|
_startTimestamp = Stopwatch.GetTimestamp();
|
||||||
_setupServer = new SetupServer();
|
await _setupServer.StopAsync().ConfigureAwait(false);
|
||||||
await _setupServer.RunAsync(static () => _jellyfinHost?.Services?.GetService<INetworkManager>(), appPaths, static () => _appHost).ConfigureAwait(false);
|
await _setupServer.RunAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
} while (_restartOnShutdown);
|
} while (_restartOnShutdown);
|
||||||
|
|
||||||
|
_setupServer.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task StartServer(IServerApplicationPaths appPaths, StartupOptions options, IConfiguration startupConfig)
|
private static async Task StartServer(IServerApplicationPaths appPaths, StartupOptions options, IConfiguration startupConfig)
|
||||||
@ -170,9 +172,7 @@ namespace Jellyfin.Server
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _setupServer.StopAsync().ConfigureAwait(false);
|
await _setupServer!.StopAsync().ConfigureAwait(false);
|
||||||
_setupServer.Dispose();
|
|
||||||
_setupServer = null!;
|
|
||||||
await _jellyfinHost.StartAsync().ConfigureAwait(false);
|
await _jellyfinHost.StartAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
if (!OperatingSystem.IsWindows() && startupConfig.UseUnixSocket())
|
if (!OperatingSystem.IsWindows() && startupConfig.UseUnixSocket())
|
||||||
|
@ -4,6 +4,9 @@ using System.Linq;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Emby.Server.Implementations.Configuration;
|
||||||
|
using Emby.Server.Implementations.Serialization;
|
||||||
|
using Jellyfin.Networking.Manager;
|
||||||
using MediaBrowser.Common.Configuration;
|
using MediaBrowser.Common.Configuration;
|
||||||
using MediaBrowser.Common.Net;
|
using MediaBrowser.Common.Net;
|
||||||
using MediaBrowser.Controller;
|
using MediaBrowser.Controller;
|
||||||
@ -11,9 +14,12 @@ using MediaBrowser.Model.System;
|
|||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Microsoft.Extensions.Primitives;
|
||||||
|
|
||||||
namespace Jellyfin.Server.ServerSetupApp;
|
namespace Jellyfin.Server.ServerSetupApp;
|
||||||
|
|
||||||
@ -22,20 +28,45 @@ namespace Jellyfin.Server.ServerSetupApp;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class SetupServer : IDisposable
|
public sealed class SetupServer : IDisposable
|
||||||
{
|
{
|
||||||
|
private readonly Func<INetworkManager?> _networkManagerFactory;
|
||||||
|
private readonly IApplicationPaths _applicationPaths;
|
||||||
|
private readonly Func<IServerApplicationHost?> _serverFactory;
|
||||||
|
private readonly ILoggerFactory _loggerFactory;
|
||||||
|
private readonly IConfiguration _startupConfiguration;
|
||||||
|
private readonly ServerConfigurationManager _configurationManager;
|
||||||
private IHost? _startupServer;
|
private IHost? _startupServer;
|
||||||
private bool _disposed;
|
private bool _disposed;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Starts the Bind-All Setup aspcore server to provide a reflection on the current core setup.
|
/// Initializes a new instance of the <see cref="SetupServer"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="networkManagerFactory">The networkmanager.</param>
|
/// <param name="networkManagerFactory">The networkmanager.</param>
|
||||||
/// <param name="applicationPaths">The application paths.</param>
|
/// <param name="applicationPaths">The application paths.</param>
|
||||||
/// <param name="serverApplicationHost">The servers application host.</param>
|
/// <param name="serverApplicationHostFactory">The servers application host.</param>
|
||||||
/// <returns>A Task.</returns>
|
/// <param name="loggerFactory">The logger factory.</param>
|
||||||
public async Task RunAsync(
|
/// <param name="startupConfiguration">The startup configuration.</param>
|
||||||
|
public SetupServer(
|
||||||
Func<INetworkManager?> networkManagerFactory,
|
Func<INetworkManager?> networkManagerFactory,
|
||||||
IApplicationPaths applicationPaths,
|
IApplicationPaths applicationPaths,
|
||||||
Func<IServerApplicationHost?> serverApplicationHost)
|
Func<IServerApplicationHost?> serverApplicationHostFactory,
|
||||||
|
ILoggerFactory loggerFactory,
|
||||||
|
IConfiguration startupConfiguration)
|
||||||
|
{
|
||||||
|
_networkManagerFactory = networkManagerFactory;
|
||||||
|
_applicationPaths = applicationPaths;
|
||||||
|
_serverFactory = serverApplicationHostFactory;
|
||||||
|
_loggerFactory = loggerFactory;
|
||||||
|
_startupConfiguration = startupConfiguration;
|
||||||
|
var xmlSerializer = new MyXmlSerializer();
|
||||||
|
_configurationManager = new ServerConfigurationManager(_applicationPaths, loggerFactory, xmlSerializer);
|
||||||
|
_configurationManager.RegisterConfiguration<NetworkConfigurationFactory>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Starts the Bind-All Setup aspcore server to provide a reflection on the current core setup.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A Task.</returns>
|
||||||
|
public async Task RunAsync()
|
||||||
{
|
{
|
||||||
ThrowIfDisposed();
|
ThrowIfDisposed();
|
||||||
_startupServer = Host.CreateDefaultBuilder()
|
_startupServer = Host.CreateDefaultBuilder()
|
||||||
@ -48,7 +79,23 @@ public sealed class SetupServer : IDisposable
|
|||||||
.ConfigureWebHostDefaults(webHostBuilder =>
|
.ConfigureWebHostDefaults(webHostBuilder =>
|
||||||
{
|
{
|
||||||
webHostBuilder
|
webHostBuilder
|
||||||
.UseKestrel()
|
.UseKestrel((builderContext, options) =>
|
||||||
|
{
|
||||||
|
var config = _configurationManager.GetNetworkConfiguration()!;
|
||||||
|
var knownBindInterfaces = NetworkManager.GetInterfacesCore(_loggerFactory.CreateLogger<SetupServer>(), config.EnableIPv4, config.EnableIPv6);
|
||||||
|
knownBindInterfaces = NetworkManager.FilterBindSettings(config, knownBindInterfaces.ToList(), config.EnableIPv4, config.EnableIPv6);
|
||||||
|
var bindInterfaces = NetworkManager.GetAllBindInterfaces(false, _configurationManager, knownBindInterfaces, config.EnableIPv4, config.EnableIPv6);
|
||||||
|
Extensions.WebHostBuilderExtensions.SetupJellyfinWebServer(
|
||||||
|
bindInterfaces,
|
||||||
|
config.InternalHttpPort,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
_startupConfiguration,
|
||||||
|
_applicationPaths,
|
||||||
|
_loggerFactory.CreateLogger<SetupServer>(),
|
||||||
|
builderContext,
|
||||||
|
options);
|
||||||
|
})
|
||||||
.Configure(app =>
|
.Configure(app =>
|
||||||
{
|
{
|
||||||
app.UseHealthChecks("/health");
|
app.UseHealthChecks("/health");
|
||||||
@ -57,14 +104,14 @@ public sealed class SetupServer : IDisposable
|
|||||||
{
|
{
|
||||||
loggerRoute.Run(async context =>
|
loggerRoute.Run(async context =>
|
||||||
{
|
{
|
||||||
var networkManager = networkManagerFactory();
|
var networkManager = _networkManagerFactory();
|
||||||
if (context.Connection.RemoteIpAddress is null || networkManager is null || !networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
|
if (context.Connection.RemoteIpAddress is null || networkManager is null || !networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var logFilePath = new DirectoryInfo(applicationPaths.LogDirectoryPath)
|
var logFilePath = new DirectoryInfo(_applicationPaths.LogDirectoryPath)
|
||||||
.EnumerateFiles()
|
.EnumerateFiles()
|
||||||
.OrderBy(f => f.CreationTimeUtc)
|
.OrderBy(f => f.CreationTimeUtc)
|
||||||
.FirstOrDefault()
|
.FirstOrDefault()
|
||||||
@ -80,20 +127,20 @@ public sealed class SetupServer : IDisposable
|
|||||||
{
|
{
|
||||||
systemRoute.Run(async context =>
|
systemRoute.Run(async context =>
|
||||||
{
|
{
|
||||||
var jfApplicationHost = serverApplicationHost();
|
var jfApplicationHost = _serverFactory();
|
||||||
|
|
||||||
var retryCounter = 0;
|
var retryCounter = 0;
|
||||||
while (jfApplicationHost is null && retryCounter < 5)
|
while (jfApplicationHost is null && retryCounter < 5)
|
||||||
{
|
{
|
||||||
await Task.Delay(500).ConfigureAwait(false);
|
await Task.Delay(500).ConfigureAwait(false);
|
||||||
jfApplicationHost = serverApplicationHost();
|
jfApplicationHost = _serverFactory();
|
||||||
retryCounter++;
|
retryCounter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (jfApplicationHost is null)
|
if (jfApplicationHost is null)
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
|
context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
|
||||||
context.Response.Headers.RetryAfter = new Microsoft.Extensions.Primitives.StringValues("60");
|
context.Response.Headers.RetryAfter = new StringValues("5");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,9 +161,10 @@ public sealed class SetupServer : IDisposable
|
|||||||
app.Run((context) =>
|
app.Run((context) =>
|
||||||
{
|
{
|
||||||
context.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
|
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("<p>Jellyfin Server still starting. Please wait.</p>");
|
context.Response.WriteAsync("<p>Jellyfin Server still starting. Please wait.</p>");
|
||||||
var networkManager = networkManagerFactory();
|
var networkManager = _networkManagerFactory();
|
||||||
if (networkManager is not null && context.Connection.RemoteIpAddress is not null && networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
|
if (networkManager is not null && context.Connection.RemoteIpAddress is not null && networkManager.IsInLocalNetwork(context.Connection.RemoteIpAddress))
|
||||||
{
|
{
|
||||||
context.Response.WriteAsync("<p>You can download the current logfiles <a href='/startup/logger'>here</a>.</p>");
|
context.Response.WriteAsync("<p>You can download the current logfiles <a href='/startup/logger'>here</a>.</p>");
|
||||||
|
@ -7,6 +7,7 @@ using System.Net;
|
|||||||
using System.Net.NetworkInformation;
|
using System.Net.NetworkInformation;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using J2N.Collections.Generic.Extensions;
|
||||||
using MediaBrowser.Common.Configuration;
|
using MediaBrowser.Common.Configuration;
|
||||||
using MediaBrowser.Common.Net;
|
using MediaBrowser.Common.Net;
|
||||||
using MediaBrowser.Model.Net;
|
using MediaBrowser.Model.Net;
|
||||||
@ -214,7 +215,20 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
{
|
{
|
||||||
lock (_initLock)
|
lock (_initLock)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("Refreshing interfaces.");
|
_interfaces = GetInterfacesCore(_logger, IsIPv4Enabled, IsIPv6Enabled).ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generate a list of all the interface ip addresses and submasks where that are in the active/unknown state.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">The logger.</param>
|
||||||
|
/// <param name="isIPv4Enabled">If true evaluates IPV4 type ip addresses.</param>
|
||||||
|
/// <param name="isIPv6Enabled">If true evaluates IPV6 type ip addresses.</param>
|
||||||
|
/// <returns>A list of all locally known up addresses and submasks that are to be considered usable.</returns>
|
||||||
|
public static IReadOnlyList<IPData> GetInterfacesCore(ILogger logger, bool isIPv4Enabled, bool isIPv6Enabled)
|
||||||
|
{
|
||||||
|
logger.LogDebug("Refreshing interfaces.");
|
||||||
|
|
||||||
var interfaces = new List<IPData>();
|
var interfaces = new List<IPData>();
|
||||||
|
|
||||||
@ -232,7 +246,7 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
// Populate interface list
|
// Populate interface list
|
||||||
foreach (var info in ipProperties.UnicastAddresses)
|
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)
|
||||||
{
|
{
|
||||||
@ -243,7 +257,7 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
|
|
||||||
interfaces.Add(interfaceObject);
|
interfaces.Add(interfaceObject);
|
||||||
}
|
}
|
||||||
else if (IsIPv6Enabled && info.Address.AddressFamily == AddressFamily.InterNetworkV6)
|
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)
|
||||||
{
|
{
|
||||||
@ -259,36 +273,34 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
// Ignore error, and attempt to continue.
|
// Ignore error, and attempt to continue.
|
||||||
_logger.LogError(ex, "Error encountered parsing interfaces.");
|
logger.LogError(ex, "Error encountered parsing interfaces.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Error obtaining interfaces.");
|
logger.LogError(ex, "Error obtaining interfaces.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no interfaces are found, fallback to loopback interfaces.
|
// If no interfaces are found, fallback to loopback interfaces.
|
||||||
if (interfaces.Count == 0)
|
if (interfaces.Count == 0)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("No interface information available. Using loopback interface(s).");
|
logger.LogWarning("No interface information available. Using loopback interface(s).");
|
||||||
|
|
||||||
if (IsIPv4Enabled)
|
if (isIPv4Enabled)
|
||||||
{
|
{
|
||||||
interfaces.Add(new IPData(IPAddress.Loopback, NetworkConstants.IPv4RFC5735Loopback, "lo"));
|
interfaces.Add(new IPData(IPAddress.Loopback, NetworkConstants.IPv4RFC5735Loopback, "lo"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (IsIPv6Enabled)
|
if (isIPv6Enabled)
|
||||||
{
|
{
|
||||||
interfaces.Add(new IPData(IPAddress.IPv6Loopback, NetworkConstants.IPv6RFC4291Loopback, "lo"));
|
interfaces.Add(new IPData(IPAddress.IPv6Loopback, NetworkConstants.IPv6RFC4291Loopback, "lo"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.LogDebug("Discovered {NumberOfInterfaces} interfaces.", interfaces.Count);
|
logger.LogDebug("Discovered {NumberOfInterfaces} interfaces.", interfaces.Count);
|
||||||
_logger.LogDebug("Interfaces addresses: {Addresses}", interfaces.OrderByDescending(s => s.AddressFamily == AddressFamily.InterNetwork).Select(s => s.Address.ToString()));
|
logger.LogDebug("Interfaces addresses: {Addresses}", interfaces.OrderByDescending(s => s.AddressFamily == AddressFamily.InterNetwork).Select(s => s.Address.ToString()));
|
||||||
|
return interfaces;
|
||||||
_interfaces = interfaces;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -344,9 +356,22 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
private void EnforceBindSettings(NetworkConfiguration config)
|
private void EnforceBindSettings(NetworkConfiguration config)
|
||||||
{
|
{
|
||||||
lock (_initLock)
|
lock (_initLock)
|
||||||
|
{
|
||||||
|
_interfaces = FilterBindSettings(config, _interfaces, IsIPv4Enabled, IsIPv6Enabled).ToList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Filteres a list of bind addresses and exclusions on available interfaces.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="config">The network config to be filtered by.</param>
|
||||||
|
/// <param name="interfaces">A list of possible interfaces to be filtered.</param>
|
||||||
|
/// <param name="isIPv4Enabled">If true evaluates IPV4 type ip addresses.</param>
|
||||||
|
/// <param name="isIPv6Enabled">If true evaluates IPV6 type ip addresses.</param>
|
||||||
|
/// <returns>A list of all locally known up addresses and submasks that are to be considered usable.</returns>
|
||||||
|
public static IReadOnlyList<IPData> FilterBindSettings(NetworkConfiguration config, IList<IPData> interfaces, bool isIPv4Enabled, bool isIPv6Enabled)
|
||||||
{
|
{
|
||||||
// Respect explicit bind addresses
|
// Respect explicit bind addresses
|
||||||
var interfaces = _interfaces.ToList();
|
|
||||||
var localNetworkAddresses = config.LocalNetworkAddresses;
|
var localNetworkAddresses = config.LocalNetworkAddresses;
|
||||||
if (localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]))
|
if (localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]))
|
||||||
{
|
{
|
||||||
@ -378,7 +403,7 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
.Select(i => i.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase));
|
.Select(i => i.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
// Check all interfaces for matches against the prefixes and remove them
|
// Check all interfaces for matches against the prefixes and remove them
|
||||||
if (_interfaces.Count > 0)
|
if (interfaces.Count > 0)
|
||||||
{
|
{
|
||||||
foreach (var virtualInterfacePrefix in virtualInterfacePrefixes)
|
foreach (var virtualInterfacePrefix in virtualInterfacePrefixes)
|
||||||
{
|
{
|
||||||
@ -388,21 +413,20 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove all IPv4 interfaces if IPv4 is disabled
|
// Remove all IPv4 interfaces if IPv4 is disabled
|
||||||
if (!IsIPv4Enabled)
|
if (!isIPv4Enabled)
|
||||||
{
|
{
|
||||||
interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetwork);
|
interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetwork);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove all IPv6 interfaces if IPv6 is disabled
|
// Remove all IPv6 interfaces if IPv6 is disabled
|
||||||
if (!IsIPv6Enabled)
|
if (!isIPv6Enabled)
|
||||||
{
|
{
|
||||||
interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetworkV6);
|
interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetworkV6);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Users may have complex networking configuration that multiple interfaces sharing the same IP address
|
// 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
|
// Only return one IP for binding, and let the OS handle the rest
|
||||||
_interfaces = interfaces.DistinctBy(iface => iface.Address).ToList();
|
return interfaces.DistinctBy(iface => iface.Address).ToList();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -720,28 +744,47 @@ public class NetworkManager : INetworkManager, IDisposable
|
|||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public IReadOnlyList<IPData> GetAllBindInterfaces(bool individualInterfaces = false)
|
public IReadOnlyList<IPData> GetAllBindInterfaces(bool individualInterfaces = false)
|
||||||
{
|
{
|
||||||
var config = _configurationManager.GetNetworkConfiguration();
|
return NetworkManager.GetAllBindInterfaces(individualInterfaces, _configurationManager, _interfaces, IsIPv4Enabled, IsIPv6Enabled);
|
||||||
var localNetworkAddresses = config.LocalNetworkAddresses;
|
}
|
||||||
if ((localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]) && _interfaces.Count > 0) || individualInterfaces)
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads the jellyfin configuration of the configuration manager and produces a list of interfaces that should be bound.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="individualInterfaces">Defines that only known interfaces should be used.</param>
|
||||||
|
/// <param name="configurationManager">The ConfigurationManager.</param>
|
||||||
|
/// <param name="knownInterfaces">The known interfaces that gets returned if possible or instructed.</param>
|
||||||
|
/// <param name="readIpv4">Include IPV4 type interfaces.</param>
|
||||||
|
/// <param name="readIpv6">Include IPV6 type interfaces.</param>
|
||||||
|
/// <returns>A list of ip address of which jellyfin should bind to.</returns>
|
||||||
|
public static IReadOnlyList<IPData> GetAllBindInterfaces(
|
||||||
|
bool individualInterfaces,
|
||||||
|
IConfigurationManager configurationManager,
|
||||||
|
IReadOnlyList<IPData> knownInterfaces,
|
||||||
|
bool readIpv4,
|
||||||
|
bool readIpv6)
|
||||||
{
|
{
|
||||||
return _interfaces;
|
var config = configurationManager.GetNetworkConfiguration();
|
||||||
|
var localNetworkAddresses = config.LocalNetworkAddresses;
|
||||||
|
if ((localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]) && knownInterfaces.Count > 0) || individualInterfaces)
|
||||||
|
{
|
||||||
|
return knownInterfaces;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No bind address and no exclusions, so listen on all interfaces.
|
// No bind address and no exclusions, so listen on all interfaces.
|
||||||
var result = new List<IPData>();
|
var result = new List<IPData>();
|
||||||
if (IsIPv4Enabled && IsIPv6Enabled)
|
if (readIpv4 && readIpv6)
|
||||||
{
|
{
|
||||||
// Kestrel source code shows it uses Sockets.DualMode - so this also covers IPAddress.Any by default
|
// Kestrel source code shows it uses Sockets.DualMode - so this also covers IPAddress.Any by default
|
||||||
result.Add(new IPData(IPAddress.IPv6Any, NetworkConstants.IPv6Any));
|
result.Add(new IPData(IPAddress.IPv6Any, NetworkConstants.IPv6Any));
|
||||||
}
|
}
|
||||||
else if (IsIPv4Enabled)
|
else if (readIpv4)
|
||||||
{
|
{
|
||||||
result.Add(new IPData(IPAddress.Any, NetworkConstants.IPv4Any));
|
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.
|
// 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)
|
if (iface.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user