2020-11-21 13:59:14 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2023-07-02 12:34:50 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2020-11-21 13:59:14 +01:00
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Net.NetworkInformation;
|
|
|
|
using System.Net.Sockets;
|
2022-10-14 10:25:57 +02:00
|
|
|
using System.Threading;
|
2020-11-21 13:59:14 +01:00
|
|
|
using Jellyfin.Networking.Configuration;
|
2023-07-03 21:51:36 +02:00
|
|
|
using Jellyfin.Networking.Constants;
|
|
|
|
using Jellyfin.Networking.Extensions;
|
2020-11-21 13:59:14 +01:00
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
using MediaBrowser.Common.Net;
|
2023-02-16 18:15:12 +01:00
|
|
|
using MediaBrowser.Model.Net;
|
2020-11-21 13:59:14 +01:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2022-07-19 21:28:04 +02:00
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
2020-11-21 13:59:14 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace Jellyfin.Networking.Manager
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class to take care of network interface management.
|
|
|
|
/// </summary>
|
|
|
|
public class NetworkManager : INetworkManager, IDisposable
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Threading lock for network properties.
|
|
|
|
/// </summary>
|
2022-07-19 21:28:04 +02:00
|
|
|
private readonly object _initLock;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
private readonly ILogger<NetworkManager> _logger;
|
|
|
|
|
|
|
|
private readonly IConfigurationManager _configurationManager;
|
|
|
|
|
2022-11-17 11:34:48 +01:00
|
|
|
private readonly object _networkEventLock;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
/// <summary>
|
2022-07-19 21:28:04 +02:00
|
|
|
/// Holds the published server URLs and the IPs to use them on.
|
2020-11-21 13:59:14 +01:00
|
|
|
/// </summary>
|
2023-02-20 21:51:15 +01:00
|
|
|
private IReadOnlyDictionary<IPData, string> _publishedServerUrls;
|
2022-07-19 21:28:04 +02:00
|
|
|
|
2023-02-20 21:51:15 +01:00
|
|
|
private IReadOnlyList<IPNetwork> _remoteAddressFilter;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Used to stop "event-racing conditions".
|
|
|
|
/// </summary>
|
|
|
|
private bool _eventfire;
|
|
|
|
|
2023-02-20 21:51:15 +01:00
|
|
|
/// <summary>
|
|
|
|
/// List of all interface MAC addresses.
|
|
|
|
/// </summary>
|
|
|
|
private IReadOnlyList<PhysicalAddress> _macAddresses;
|
|
|
|
|
2022-07-20 21:19:35 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Dictionary containing interface addresses and their subnets.
|
|
|
|
/// </summary>
|
2023-02-20 21:51:15 +01:00
|
|
|
private IReadOnlyList<IPData> _interfaces;
|
2022-07-20 21:19:35 +02:00
|
|
|
|
2020-11-21 13:59:14 +01:00
|
|
|
/// <summary>
|
2022-07-19 21:28:04 +02:00
|
|
|
/// Unfiltered user defined LAN subnets (<see cref="NetworkConfiguration.LocalNetworkSubnets"/>)
|
2020-11-21 13:59:14 +01:00
|
|
|
/// or internal interface network subnets if undefined by user.
|
|
|
|
/// </summary>
|
2023-02-20 21:51:15 +01:00
|
|
|
private IReadOnlyList<IPNetwork> _lanSubnets;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// User defined list of subnets to excluded from the LAN.
|
|
|
|
/// </summary>
|
2023-02-20 21:51:15 +01:00
|
|
|
private IReadOnlyList<IPNetwork> _excludedSubnets;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// True if this object is disposed.
|
|
|
|
/// </summary>
|
|
|
|
private bool _disposed;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="NetworkManager"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="configurationManager">IServerConfigurationManager instance.</param>
|
|
|
|
/// <param name="logger">Logger to use for messages.</param>
|
|
|
|
#pragma warning disable CS8618 // Non-nullable field is uninitialized. : Values are set in UpdateSettings function. Compiler doesn't yet recognise this.
|
|
|
|
public NetworkManager(IConfigurationManager configurationManager, ILogger<NetworkManager> logger)
|
|
|
|
{
|
2022-10-07 09:04:41 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(logger);
|
|
|
|
ArgumentNullException.ThrowIfNull(configurationManager);
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-10-07 09:04:41 +02:00
|
|
|
_logger = logger;
|
|
|
|
_configurationManager = configurationManager;
|
2022-07-19 21:28:04 +02:00
|
|
|
_initLock = new();
|
|
|
|
_interfaces = new List<IPData>();
|
2020-11-21 13:59:14 +01:00
|
|
|
_macAddresses = new List<PhysicalAddress>();
|
2022-07-19 21:28:04 +02:00
|
|
|
_publishedServerUrls = new Dictionary<IPData, string>();
|
2022-11-17 11:34:48 +01:00
|
|
|
_networkEventLock = new object();
|
2022-07-20 14:29:30 +02:00
|
|
|
_remoteAddressFilter = new List<IPNetwork>();
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
UpdateSettings(_configurationManager.GetNetworkConfiguration());
|
|
|
|
|
|
|
|
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
|
|
|
|
NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
|
|
|
|
|
|
|
|
_configurationManager.NamedConfigurationUpdated += ConfigurationUpdated;
|
|
|
|
}
|
|
|
|
#pragma warning restore CS8618 // Non-nullable field is uninitialized.
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Event triggered on network changes.
|
|
|
|
/// </summary>
|
|
|
|
public event EventHandler? NetworkChanged;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a value indicating whether testing is taking place.
|
|
|
|
/// </summary>
|
|
|
|
public static string MockNetworkSettings { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
/// <summary>
|
2022-07-19 21:28:04 +02:00
|
|
|
/// Gets a value indicating whether IP4 is enabled.
|
2020-11-21 13:59:14 +01:00
|
|
|
/// </summary>
|
2023-02-15 22:34:44 +01:00
|
|
|
public bool IsIPv4Enabled => _configurationManager.GetNetworkConfiguration().EnableIPv4;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
/// <summary>
|
2022-07-19 21:28:04 +02:00
|
|
|
/// Gets a value indicating whether IP6 is enabled.
|
2020-11-21 13:59:14 +01:00
|
|
|
/// </summary>
|
2023-02-15 22:34:44 +01:00
|
|
|
public bool IsIPv6Enabled => _configurationManager.GetNetworkConfiguration().EnableIPv6;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating whether is all IPv6 interfaces are trusted as internal.
|
|
|
|
/// </summary>
|
2023-02-17 19:27:36 +01:00
|
|
|
public bool TrustAllIPv6Interfaces { get; private set; }
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the Published server override list.
|
|
|
|
/// </summary>
|
2023-02-20 21:51:15 +01:00
|
|
|
public IReadOnlyDictionary<IPData, string> PublishedServerUrls => _publishedServerUrls;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Handler for network change events.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">Sender.</param>
|
|
|
|
/// <param name="e">A <see cref="NetworkAvailabilityEventArgs"/> containing network availability information.</param>
|
|
|
|
private void OnNetworkAvailabilityChanged(object? sender, NetworkAvailabilityEventArgs e)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
_logger.LogDebug("Network availability changed.");
|
2022-10-14 10:25:57 +02:00
|
|
|
HandleNetworkChange();
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Handler for network change events.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">Sender.</param>
|
|
|
|
/// <param name="e">An <see cref="EventArgs"/>.</param>
|
|
|
|
private void OnNetworkAddressChanged(object? sender, EventArgs e)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
_logger.LogDebug("Network address change detected.");
|
2022-10-14 10:25:57 +02:00
|
|
|
HandleNetworkChange();
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Triggers our event, and re-loads interface information.
|
|
|
|
/// </summary>
|
2022-10-14 10:25:57 +02:00
|
|
|
private void HandleNetworkChange()
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-12-03 12:44:59 +01:00
|
|
|
lock (_networkEventLock)
|
|
|
|
{
|
2022-11-17 11:34:48 +01:00
|
|
|
if (!_eventfire)
|
|
|
|
{
|
|
|
|
_logger.LogDebug("Network Address Change Event.");
|
|
|
|
// As network events tend to fire one after the other only fire once every second.
|
|
|
|
_eventfire = true;
|
|
|
|
OnNetworkChange();
|
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <summary>
|
2022-10-14 10:25:57 +02:00
|
|
|
/// Waits for 2 seconds before re-initialising the settings, as typically these events fire multiple times in succession.
|
2022-07-19 21:28:04 +02:00
|
|
|
/// </summary>
|
2022-10-14 10:25:57 +02:00
|
|
|
private void OnNetworkChange()
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
try
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-10-14 10:25:57 +02:00
|
|
|
Thread.Sleep(2000);
|
2022-07-20 21:19:35 +02:00
|
|
|
var networkConfig = _configurationManager.GetNetworkConfiguration();
|
2023-05-11 15:41:39 +02:00
|
|
|
if (IsIPv6Enabled && !Socket.OSSupportsIPv6)
|
|
|
|
{
|
|
|
|
UpdateSettings(networkConfig);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
InitialiseInterfaces();
|
|
|
|
InitialiseLan(networkConfig);
|
|
|
|
EnforceBindSettings(networkConfig);
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
NetworkChanged?.Invoke(this, EventArgs.Empty);
|
|
|
|
}
|
|
|
|
finally
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
_eventfire = false;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Generate a list of all the interface ip addresses and submasks where that are in the active/unknown state.
|
|
|
|
/// Generate a list of all active mac addresses that aren't loopback addresses.
|
|
|
|
/// </summary>
|
|
|
|
private void InitialiseInterfaces()
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
lock (_initLock)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
_logger.LogDebug("Refreshing interfaces.");
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-02-20 21:51:15 +01:00
|
|
|
var interfaces = new List<IPData>();
|
|
|
|
var macAddresses = new List<PhysicalAddress>();
|
2020-11-21 13:59:14 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-07-20 21:19:35 +02:00
|
|
|
var nics = NetworkInterface.GetAllNetworkInterfaces()
|
2022-07-19 21:28:04 +02:00
|
|
|
.Where(i => i.SupportsMulticast && i.OperationalStatus == OperationalStatus.Up);
|
|
|
|
|
|
|
|
foreach (NetworkInterface adapter in nics)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
try
|
|
|
|
{
|
2022-07-20 14:29:30 +02:00
|
|
|
var ipProperties = adapter.GetIPProperties();
|
|
|
|
var mac = adapter.GetPhysicalAddress();
|
2022-07-19 21:28:04 +02:00
|
|
|
|
|
|
|
// Populate MAC list
|
|
|
|
if (adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback && PhysicalAddress.None.Equals(mac))
|
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
macAddresses.Add(mac);
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate interface list
|
2022-07-20 14:29:30 +02:00
|
|
|
foreach (var info in ipProperties.UnicastAddresses)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2023-02-15 22:34:44 +01:00
|
|
|
if (IsIPv4Enabled && info.Address.AddressFamily == AddressFamily.InterNetwork)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
var interfaceObject = new IPData(info.Address, new IPNetwork(info.Address, info.PrefixLength), adapter.Name);
|
|
|
|
interfaceObject.Index = ipProperties.GetIPv4Properties().Index;
|
2022-10-14 10:25:57 +02:00
|
|
|
interfaceObject.Name = adapter.Name;
|
2022-07-19 21:28:04 +02:00
|
|
|
|
2023-02-20 21:51:15 +01:00
|
|
|
interfaces.Add(interfaceObject);
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2023-02-15 22:34:44 +01:00
|
|
|
else if (IsIPv6Enabled && info.Address.AddressFamily == AddressFamily.InterNetworkV6)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
var interfaceObject = new IPData(info.Address, new IPNetwork(info.Address, info.PrefixLength), adapter.Name);
|
|
|
|
interfaceObject.Index = ipProperties.GetIPv6Properties().Index;
|
2022-10-14 10:25:57 +02:00
|
|
|
interfaceObject.Name = adapter.Name;
|
2022-07-19 21:28:04 +02:00
|
|
|
|
2023-02-20 21:51:15 +01:00
|
|
|
interfaces.Add(interfaceObject);
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#pragma warning disable CA1031 // Do not catch general exception types
|
|
|
|
catch (Exception ex)
|
|
|
|
#pragma warning restore CA1031 // Do not catch general exception types
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
// Ignore error, and attempt to continue.
|
|
|
|
_logger.LogError(ex, "Error encountered parsing interfaces.");
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
#pragma warning disable CA1031 // Do not catch general exception types
|
|
|
|
catch (Exception ex)
|
|
|
|
#pragma warning restore CA1031 // Do not catch general exception types
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
_logger.LogError(ex, "Error obtaining interfaces.");
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2023-02-15 11:31:47 +01:00
|
|
|
// If no interfaces are found, fallback to loopback interfaces.
|
2023-02-20 21:51:15 +01:00
|
|
|
if (interfaces.Count == 0)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-21 22:09:54 +02:00
|
|
|
_logger.LogWarning("No interface information available. Using loopback interface(s).");
|
2022-07-19 21:28:04 +02:00
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (IsIPv4Enabled && !IsIPv6Enabled)
|
2022-07-20 21:51:12 +02:00
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
interfaces.Add(new IPData(IPAddress.Loopback, new IPNetwork(IPAddress.Loopback, 8), "lo"));
|
2022-07-20 21:51:12 +02:00
|
|
|
}
|
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (!IsIPv4Enabled && IsIPv6Enabled)
|
2022-07-20 21:51:12 +02:00
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
interfaces.Add(new IPData(IPAddress.IPv6Loopback, new IPNetwork(IPAddress.IPv6Loopback, 128), "lo"));
|
2022-07-20 21:51:12 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2023-02-20 21:51:15 +01:00
|
|
|
_logger.LogDebug("Discovered {NumberOfInterfaces} interfaces.", interfaces.Count);
|
|
|
|
_logger.LogDebug("Interfaces addresses: {Addresses}", interfaces.OrderByDescending(s => s.AddressFamily == AddressFamily.InterNetwork).Select(s => s.Address.ToString()));
|
|
|
|
|
|
|
|
_macAddresses = macAddresses;
|
|
|
|
_interfaces = interfaces;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Initialises internal LAN cache.
|
|
|
|
/// </summary>
|
|
|
|
private void InitialiseLan(NetworkConfiguration config)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
lock (_initLock)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
_logger.LogDebug("Refreshing LAN information.");
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
// Get configuration options
|
2023-02-15 11:31:47 +01:00
|
|
|
var subnets = config.LocalNetworkSubnets;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-02-15 11:31:47 +01:00
|
|
|
// If no LAN addresses are specified, all private subnets and Loopback are deemed to be the LAN
|
2023-02-20 21:51:15 +01:00
|
|
|
if (!NetworkExtensions.TryParseToSubnets(subnets, out var lanSubnets, false) || lanSubnets.Count == 0)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
_logger.LogDebug("Using LAN interface addresses as user provided no LAN details.");
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-02-20 21:51:15 +01:00
|
|
|
var fallbackLanSubnets = new List<IPNetwork>();
|
2023-02-15 22:34:44 +01:00
|
|
|
if (IsIPv6Enabled)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2023-07-03 21:51:36 +02:00
|
|
|
fallbackLanSubnets.Add(Network.IPv6RFC4291Loopback); // RFC 4291 (Loopback)
|
|
|
|
fallbackLanSubnets.Add(Network.IPv6RFC4291SiteLocal); // RFC 4291 (Site local)
|
|
|
|
fallbackLanSubnets.Add(Network.IPv6RFC4193UniqueLocal); // RFC 4193 (Unique local)
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (IsIPv4Enabled)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2023-07-03 21:51:36 +02:00
|
|
|
fallbackLanSubnets.Add(Network.IPv4RFC5735Loopback); // RFC 5735 (Loopback)
|
|
|
|
fallbackLanSubnets.Add(Network.IPv4RFC1918PrivateClassA); // RFC 1918 (private Class A)
|
|
|
|
fallbackLanSubnets.Add(Network.IPv4RFC1918PrivateClassB); // RFC 1918 (private Class B)
|
|
|
|
fallbackLanSubnets.Add(Network.IPv4RFC1918PrivateClassC); // RFC 1918 (private Class C)
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2023-02-20 21:51:15 +01:00
|
|
|
|
|
|
|
_lanSubnets = fallbackLanSubnets;
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2023-02-20 21:51:15 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_lanSubnets = lanSubnets;
|
|
|
|
}
|
|
|
|
|
|
|
|
_excludedSubnets = NetworkExtensions.TryParseToSubnets(subnets, out var excludedSubnets, true)
|
|
|
|
? excludedSubnets
|
|
|
|
: new List<IPNetwork>();
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-10-15 20:57:02 +02:00
|
|
|
_logger.LogInformation("Defined LAN addresses: {0}", _lanSubnets.Select(s => s.Prefix + "/" + s.PrefixLength));
|
|
|
|
_logger.LogInformation("Defined LAN exclusions: {0}", _excludedSubnets.Select(s => s.Prefix + "/" + s.PrefixLength));
|
2022-07-19 21:28:04 +02:00
|
|
|
_logger.LogInformation("Using LAN addresses: {0}", _lanSubnets.Where(s => !_excludedSubnets.Contains(s)).Select(s => s.Prefix + "/" + s.PrefixLength));
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <summary>
|
2022-07-20 21:19:35 +02:00
|
|
|
/// Enforce bind addresses and exclusions on available interfaces.
|
2022-07-19 21:28:04 +02:00
|
|
|
/// </summary>
|
2022-07-20 21:51:12 +02:00
|
|
|
private void EnforceBindSettings(NetworkConfiguration config)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
lock (_initLock)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-20 21:19:35 +02:00
|
|
|
// Respect explicit bind addresses
|
2023-02-20 21:51:15 +01:00
|
|
|
var interfaces = _interfaces.ToList();
|
2022-07-20 21:19:35 +02:00
|
|
|
var localNetworkAddresses = config.LocalNetworkAddresses;
|
2023-02-15 11:31:47 +01:00
|
|
|
if (localNetworkAddresses.Length > 0 && !string.IsNullOrWhiteSpace(localNetworkAddresses[0]))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-10-17 15:38:42 +02:00
|
|
|
var bindAddresses = localNetworkAddresses.Select(p => NetworkExtensions.TryParseToSubnet(p, out var network)
|
|
|
|
? network.Prefix
|
2023-02-20 21:51:15 +01:00
|
|
|
: (interfaces.Where(x => x.Name.Equals(p, StringComparison.OrdinalIgnoreCase))
|
2022-07-20 21:19:35 +02:00
|
|
|
.Select(x => x.Address)
|
|
|
|
.FirstOrDefault() ?? IPAddress.None))
|
2023-02-15 11:31:47 +01:00
|
|
|
.Where(x => x != IPAddress.None)
|
|
|
|
.ToHashSet();
|
2023-02-20 21:51:15 +01:00
|
|
|
interfaces = interfaces.Where(x => bindAddresses.Contains(x.Address)).ToList();
|
2022-07-20 21:51:12 +02:00
|
|
|
|
|
|
|
if (bindAddresses.Contains(IPAddress.Loopback))
|
|
|
|
{
|
2023-07-03 21:51:36 +02:00
|
|
|
interfaces.Add(new IPData(IPAddress.Loopback, Network.IPv4RFC5735Loopback, "lo"));
|
2022-07-20 21:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bindAddresses.Contains(IPAddress.IPv6Loopback))
|
|
|
|
{
|
2023-07-03 21:51:36 +02:00
|
|
|
interfaces.Add(new IPData(IPAddress.IPv6Loopback, Network.IPv6RFC4291Loopback, "lo"));
|
2022-07-20 21:51:12 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-20 21:19:35 +02:00
|
|
|
// Remove all interfaces matching any virtual machine interface prefix
|
2022-07-19 21:28:04 +02:00
|
|
|
if (config.IgnoreVirtualInterfaces)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-07-02 12:34:50 +02:00
|
|
|
// Remove potentially existing * and split config string into prefixes
|
2022-07-20 21:19:35 +02:00
|
|
|
var virtualInterfacePrefixes = config.VirtualInterfaceNames
|
2022-10-14 10:25:57 +02:00
|
|
|
.Select(i => i.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase));
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-20 21:19:35 +02:00
|
|
|
// Check all interfaces for matches against the prefixes and remove them
|
2022-10-14 10:25:57 +02:00
|
|
|
if (_interfaces.Count > 0)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
foreach (var virtualInterfacePrefix in virtualInterfacePrefixes)
|
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
interfaces.RemoveAll(x => x.Name.StartsWith(virtualInterfacePrefix, StringComparison.OrdinalIgnoreCase));
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-21 09:26:18 +02:00
|
|
|
// Remove all IPv4 interfaces if IPv4 is disabled
|
2023-02-15 22:34:44 +01:00
|
|
|
if (!IsIPv4Enabled)
|
2022-07-20 21:51:12 +02:00
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetwork);
|
2022-07-20 21:51:12 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 09:26:18 +02:00
|
|
|
// Remove all IPv6 interfaces if IPv6 is disabled
|
2023-02-15 22:34:44 +01:00
|
|
|
if (!IsIPv6Enabled)
|
2022-07-20 21:51:12 +02:00
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
interfaces.RemoveAll(x => x.AddressFamily == AddressFamily.InterNetworkV6);
|
2022-07-20 21:51:12 +02:00
|
|
|
}
|
|
|
|
|
2023-02-20 21:51:15 +01:00
|
|
|
_logger.LogInformation("Using bind addresses: {0}", interfaces.OrderByDescending(x => x.AddressFamily == AddressFamily.InterNetwork).Select(x => x.Address));
|
|
|
|
_interfaces = interfaces;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Initialises the remote address values.
|
|
|
|
/// </summary>
|
|
|
|
private void InitialiseRemote(NetworkConfiguration config)
|
|
|
|
{
|
|
|
|
lock (_initLock)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
// Parse config values into filter collection
|
|
|
|
var remoteIPFilter = config.RemoteIPFilter;
|
|
|
|
if (remoteIPFilter.Any() && !string.IsNullOrWhiteSpace(remoteIPFilter.First()))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
// Parse all IPs with netmask to a subnet
|
2023-02-20 21:51:15 +01:00
|
|
|
var remoteAddressFilter = new List<IPNetwork>();
|
2023-02-15 11:31:47 +01:00
|
|
|
var remoteFilteredSubnets = remoteIPFilter.Where(x => x.Contains('/', StringComparison.OrdinalIgnoreCase)).ToArray();
|
2023-02-20 21:51:15 +01:00
|
|
|
if (NetworkExtensions.TryParseToSubnets(remoteFilteredSubnets, out var remoteAddressFilterResult, false))
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
remoteAddressFilter = remoteAddressFilterResult.ToList();
|
2023-02-15 11:31:47 +01:00
|
|
|
}
|
2021-03-14 18:30:25 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
// Parse everything else as an IP and construct subnet with a single IP
|
2023-02-15 11:31:47 +01:00
|
|
|
var remoteFilteredIPs = remoteIPFilter.Where(x => !x.Contains('/', StringComparison.OrdinalIgnoreCase));
|
|
|
|
foreach (var ip in remoteFilteredIPs)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
if (IPAddress.TryParse(ip, out var ipp))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-07-03 21:51:36 +02:00
|
|
|
remoteAddressFilter.Add(new IPNetwork(ipp, ipp.AddressFamily == AddressFamily.InterNetwork ? Network.MinimumIPv4PrefixSize : Network.MinimumIPv6PrefixSize));
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-20 21:51:15 +01:00
|
|
|
|
|
|
|
_remoteAddressFilter = remoteAddressFilter;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Parses the user defined overrides into the dictionary object.
|
|
|
|
/// Overrides are the equivalent of localised publishedServerUrl, enabling
|
|
|
|
/// different addresses to be advertised over different subnets.
|
|
|
|
/// format is subnet=ipaddress|host|uri
|
|
|
|
/// when subnet = 0.0.0.0, any external address matches.
|
|
|
|
/// </summary>
|
|
|
|
private void InitialiseOverrides(NetworkConfiguration config)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
lock (_initLock)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
var publishedServerUrls = new Dictionary<IPData, string>();
|
|
|
|
var overrides = config.PublishedServerUriBySubnet;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
foreach (var entry in overrides)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
var parts = entry.Split('=');
|
|
|
|
if (parts.Length != 2)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
_logger.LogError("Unable to parse bind override: {Entry}", entry);
|
2023-02-15 11:31:47 +01:00
|
|
|
return;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2023-02-15 11:31:47 +01:00
|
|
|
|
|
|
|
var replacement = parts[1].Trim();
|
|
|
|
var identifier = parts[0];
|
|
|
|
if (string.Equals(identifier, "all", StringComparison.OrdinalIgnoreCase))
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
publishedServerUrls[new IPData(IPAddress.Broadcast, null)] = replacement;
|
2023-02-15 11:31:47 +01:00
|
|
|
}
|
|
|
|
else if (string.Equals(identifier, "external", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2023-07-03 21:51:36 +02:00
|
|
|
publishedServerUrls[new IPData(IPAddress.Any, Network.IPv4Any)] = replacement;
|
|
|
|
publishedServerUrls[new IPData(IPAddress.IPv6Any, Network.IPv6Any)] = replacement;
|
2023-02-15 11:31:47 +01:00
|
|
|
}
|
|
|
|
else if (string.Equals(identifier, "internal", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
foreach (var lan in _lanSubnets)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2023-02-15 11:31:47 +01:00
|
|
|
var lanPrefix = lan.Prefix;
|
2023-02-20 21:51:15 +01:00
|
|
|
publishedServerUrls[new IPData(lanPrefix, new IPNetwork(lanPrefix, lan.PrefixLength))] = replacement;
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2023-02-15 11:31:47 +01:00
|
|
|
}
|
2023-02-15 22:34:44 +01:00
|
|
|
else if (NetworkExtensions.TryParseToSubnet(identifier, out var result) && result is not null)
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
|
|
|
var data = new IPData(result.Prefix, result);
|
2023-02-20 21:51:15 +01:00
|
|
|
publishedServerUrls[data] = replacement;
|
2023-02-15 11:31:47 +01:00
|
|
|
}
|
|
|
|
else if (TryParseInterface(identifier, out var ifaces))
|
|
|
|
{
|
|
|
|
foreach (var iface in ifaces)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2023-02-20 21:51:15 +01:00
|
|
|
publishedServerUrls[iface] = replacement;
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-15 11:31:47 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.LogError("Unable to parse bind override: {Entry}", entry);
|
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2023-02-20 21:51:15 +01:00
|
|
|
|
|
|
|
_publishedServerUrls = publishedServerUrls;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
private void ConfigurationUpdated(object? sender, ConfigurationUpdateEventArgs evt)
|
2021-03-11 22:36:58 +01:00
|
|
|
{
|
2023-02-15 22:34:44 +01:00
|
|
|
if (evt.Key.Equals(NetworkConfigurationStore.StoreKey, StringComparison.Ordinal))
|
2021-03-11 22:36:58 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
UpdateSettings((NetworkConfiguration)evt.NewConfiguration);
|
2021-03-11 22:36:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 13:59:14 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Reloads all settings and re-initialises the instance.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="configuration">The <see cref="NetworkConfiguration"/> to use.</param>
|
|
|
|
public void UpdateSettings(object configuration)
|
|
|
|
{
|
2022-10-07 09:04:41 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-10-07 09:04:41 +02:00
|
|
|
var config = (NetworkConfiguration)configuration;
|
2023-05-11 15:41:39 +02:00
|
|
|
HappyEyeballs.HttpClientExtension.UseIPv6 = config.EnableIPv6;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-20 21:19:35 +02:00
|
|
|
InitialiseLan(config);
|
|
|
|
InitialiseRemote(config);
|
|
|
|
|
2020-11-21 13:59:14 +01:00
|
|
|
if (string.IsNullOrEmpty(MockNetworkSettings))
|
|
|
|
{
|
|
|
|
InitialiseInterfaces();
|
|
|
|
}
|
|
|
|
else // Used in testing only.
|
|
|
|
{
|
|
|
|
// Format is <IPAddress>,<Index>,<Name>: <next interface>. Set index to -ve to simulate a gateway.
|
2021-01-06 21:39:25 +01:00
|
|
|
var interfaceList = MockNetworkSettings.Split('|');
|
2023-02-20 21:51:15 +01:00
|
|
|
var interfaces = new List<IPData>();
|
2020-11-21 13:59:14 +01:00
|
|
|
foreach (var details in interfaceList)
|
|
|
|
{
|
|
|
|
var parts = details.Split(',');
|
2022-10-17 15:38:42 +02:00
|
|
|
if (NetworkExtensions.TryParseToSubnet(parts[0], out var subnet))
|
|
|
|
{
|
|
|
|
var address = subnet.Prefix;
|
|
|
|
var index = int.Parse(parts[1], CultureInfo.InvariantCulture);
|
|
|
|
if (address.AddressFamily == AddressFamily.InterNetwork || address.AddressFamily == AddressFamily.InterNetworkV6)
|
|
|
|
{
|
|
|
|
var data = new IPData(address, subnet, parts[2]);
|
|
|
|
data.Index = index;
|
2023-02-20 21:51:15 +01:00
|
|
|
interfaces.Add(data);
|
2022-10-17 15:38:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2022-10-17 15:38:42 +02:00
|
|
|
_logger.LogWarning("Could not parse mock interface settings: {Part}", details);
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2023-02-20 21:51:15 +01:00
|
|
|
|
|
|
|
_interfaces = interfaces;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-20 21:51:12 +02:00
|
|
|
EnforceBindSettings(config);
|
2020-11-21 13:59:14 +01:00
|
|
|
InitialiseOverrides(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Protected implementation of Dispose pattern.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="disposing"><c>True</c> to dispose the managed state.</param>
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
if (!_disposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
_configurationManager.NamedConfigurationUpdated -= ConfigurationUpdated;
|
|
|
|
NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged;
|
|
|
|
NetworkChange.NetworkAvailabilityChanged -= OnNetworkAvailabilityChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
_disposed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
2023-07-02 12:34:50 +02:00
|
|
|
public bool TryParseInterface(string intf, [NotNullWhen(true)] out IReadOnlyList<IPData>? result)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
if (string.IsNullOrEmpty(intf)
|
|
|
|
|| _interfaces is null
|
|
|
|
|| _interfaces.Count == 0)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
result = null;
|
2022-07-19 21:28:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-02-15 11:31:47 +01:00
|
|
|
// Match all interfaces starting with names starting with token
|
2023-07-03 10:03:39 +02:00
|
|
|
result = _interfaces
|
|
|
|
.Where(i => i.Name.Equals(intf, StringComparison.OrdinalIgnoreCase)
|
|
|
|
&& ((IsIPv4Enabled && i.Address.AddressFamily == AddressFamily.InterNetwork)
|
|
|
|
|| (IsIPv6Enabled && i.Address.AddressFamily == AddressFamily.InterNetworkV6)))
|
|
|
|
.OrderBy(x => x.Index)
|
|
|
|
.ToArray();
|
|
|
|
return result.Count > 0;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
2023-02-17 19:27:36 +01:00
|
|
|
public bool HasRemoteAccess(IPAddress remoteIP)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
var config = _configurationManager.GetNetworkConfiguration();
|
|
|
|
if (config.EnableRemoteAccess)
|
|
|
|
{
|
|
|
|
// Comma separated list of IP addresses or IP/netmask entries for networks that will be allowed to connect remotely.
|
|
|
|
// If left blank, all remote addresses will be allowed.
|
2023-02-17 19:27:36 +01:00
|
|
|
if (_remoteAddressFilter.Any() && !_lanSubnets.Any(x => x.Contains(remoteIP)))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
// remoteAddressFilter is a whitelist or blacklist.
|
2023-02-17 19:27:36 +01:00
|
|
|
var matches = _remoteAddressFilter.Count(remoteNetwork => remoteNetwork.Contains(remoteIP));
|
2022-07-19 21:28:04 +02:00
|
|
|
if ((!config.IsRemoteIPFilterBlacklist && matches > 0)
|
|
|
|
|| (config.IsRemoteIPFilterBlacklist && matches == 0))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
return true;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
|
|
|
|
return false;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-17 19:27:36 +01:00
|
|
|
else if (!_lanSubnets.Any(x => x.Contains(remoteIP)))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
// Remote not enabled. So everyone should be LAN.
|
|
|
|
return false;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
return true;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
2022-07-20 14:29:30 +02:00
|
|
|
public IReadOnlyList<PhysicalAddress> GetMacAddresses()
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
// Populated in construction - so always has values.
|
|
|
|
return _macAddresses;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
2022-07-20 14:29:30 +02:00
|
|
|
public IReadOnlyList<IPData> GetLoopbacks()
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-07-02 12:34:50 +02:00
|
|
|
if (!IsIPv4Enabled && !IsIPv6Enabled)
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
|
|
|
return Array.Empty<IPData>();
|
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
var loopbackNetworks = new List<IPData>();
|
2023-02-15 22:34:44 +01:00
|
|
|
if (IsIPv4Enabled)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-07-03 21:51:36 +02:00
|
|
|
loopbackNetworks.Add(new IPData(IPAddress.Loopback, Network.IPv4RFC5735Loopback, "lo"));
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (IsIPv6Enabled)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-07-03 21:51:36 +02:00
|
|
|
loopbackNetworks.Add(new IPData(IPAddress.IPv6Loopback, Network.IPv6RFC4291Loopback, "lo"));
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
|
|
|
|
return loopbackNetworks;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
2022-07-20 14:29:30 +02:00
|
|
|
public IReadOnlyList<IPData> GetAllBindInterfaces(bool individualInterfaces = false)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-02-15 11:31:47 +01:00
|
|
|
if (_interfaces.Count != 0)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-02-15 11:31:47 +01:00
|
|
|
return _interfaces;
|
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
|
2023-02-15 11:31:47 +01:00
|
|
|
// No bind address and no exclusions, so listen on all interfaces.
|
|
|
|
var result = new List<IPData>();
|
2022-07-19 21:28:04 +02:00
|
|
|
|
2023-02-15 11:31:47 +01:00
|
|
|
if (individualInterfaces)
|
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
result.AddRange(_interfaces);
|
2023-02-15 11:31:47 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (IsIPv4Enabled && IsIPv6Enabled)
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
|
|
|
// Kestrel source code shows it uses Sockets.DualMode - so this also covers IPAddress.Any by default
|
2023-07-03 21:51:36 +02:00
|
|
|
result.Add(new IPData(IPAddress.IPv6Any, Network.IPv6Any));
|
2023-02-15 11:31:47 +01:00
|
|
|
}
|
2023-02-15 22:34:44 +01:00
|
|
|
else if (IsIPv4Enabled)
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
2023-07-03 21:51:36 +02:00
|
|
|
result.Add(new IPData(IPAddress.Any, Network.IPv4Any));
|
2023-02-15 11:31:47 +01:00
|
|
|
}
|
2023-02-15 22:34:44 +01:00
|
|
|
else if (IsIPv6Enabled)
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
|
|
|
// Cannot use IPv6Any as Kestrel will bind to IPv4 addresses too.
|
|
|
|
foreach (var iface in _interfaces)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2023-02-15 11:31:47 +01:00
|
|
|
if (iface.AddressFamily == AddressFamily.InterNetworkV6)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-02-15 11:31:47 +01:00
|
|
|
result.Add(iface);
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 11:31:47 +01:00
|
|
|
return result;
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
2023-02-16 18:15:12 +01:00
|
|
|
public string GetBindAddress(string source, out int? port)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2023-02-15 22:34:44 +01:00
|
|
|
if (!NetworkExtensions.TryParseHost(source, out var addresses, IsIPv4Enabled, IsIPv6Enabled))
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
|
|
|
addresses = Array.Empty<IPAddress>();
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = GetBindAddress(addresses.FirstOrDefault(), out port);
|
2022-07-19 21:28:04 +02:00
|
|
|
return result;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
2023-02-16 18:15:12 +01:00
|
|
|
public string GetBindAddress(HttpRequest source, out int? port)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-02-16 18:15:12 +01:00
|
|
|
var result = GetBindAddress(source.Host.Host, out port);
|
2022-07-19 21:28:04 +02:00
|
|
|
port ??= source.Host.Port;
|
|
|
|
|
|
|
|
return result;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
2023-02-16 18:15:12 +01:00
|
|
|
public string GetBindAddress(IPAddress? source, out int? port, bool skipOverrides = false)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
port = null;
|
|
|
|
|
|
|
|
string result;
|
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (source is not null)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-02-15 22:34:44 +01:00
|
|
|
if (IsIPv4Enabled && !IsIPv6Enabled && source.AddressFamily == AddressFamily.InterNetworkV6)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
_logger.LogWarning("IPv6 is disabled in Jellyfin, but enabled in the OS. This may affect how the interface is selected.");
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (!IsIPv4Enabled && IsIPv6Enabled && source.AddressFamily == AddressFamily.InterNetwork)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
_logger.LogWarning("IPv4 is disabled in Jellyfin, but enabled in the OS. This may affect how the interface is selected.");
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
bool isExternal = !_lanSubnets.Any(network => network.Contains(source));
|
2022-10-15 20:57:02 +02:00
|
|
|
_logger.LogDebug("Trying to get bind address for source {Source} - External: {IsExternal}", source, isExternal);
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-02-16 18:15:12 +01:00
|
|
|
if (!skipOverrides && MatchesPublishedServerUrl(source, isExternal, out result))
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2022-10-15 17:27:37 +02:00
|
|
|
return result;
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
// No preference given, so move on to bind addresses.
|
|
|
|
if (MatchesBindInterface(source, isExternal, out result))
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
if (isExternal && MatchesExternalInterface(source, out result))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-19 21:28:04 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
// Get the first LAN interface address that's not excluded and not a loopback address.
|
2023-02-15 22:34:44 +01:00
|
|
|
// Get all available interfaces, prefer local interfaces
|
2022-07-19 21:28:04 +02:00
|
|
|
var availableInterfaces = _interfaces.Where(x => !IPAddress.IsLoopback(x.Address))
|
2022-07-20 21:19:35 +02:00
|
|
|
.OrderByDescending(x => IsInLocalNetwork(x.Address))
|
2023-01-19 10:19:53 +01:00
|
|
|
.ThenBy(x => x.Index)
|
|
|
|
.ToList();
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-07-03 10:03:39 +02:00
|
|
|
if (availableInterfaces.Count == 0)
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
// There isn't any others, so we'll use the loopback.
|
|
|
|
result = IsIPv4Enabled && !IsIPv6Enabled ? "127.0.0.1" : "::1";
|
|
|
|
_logger.LogWarning("{Source}: Only loopback {Result} returned, using that as bind address.", source, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no source address is given, use the preferred (first) interface
|
|
|
|
if (source is null)
|
|
|
|
{
|
|
|
|
result = NetworkExtensions.FormatIPString(availableInterfaces.First().Address);
|
|
|
|
_logger.LogDebug("{Source}: Using first internal interface as bind address: {Result}", source, result);
|
|
|
|
return result;
|
|
|
|
}
|
2023-02-15 11:31:47 +01:00
|
|
|
|
2023-07-03 10:03:39 +02:00
|
|
|
// Does the request originate in one of the interface subnets?
|
|
|
|
// (For systems with multiple internal network cards, and multiple subnets)
|
|
|
|
foreach (var intf in availableInterfaces)
|
|
|
|
{
|
|
|
|
if (intf.Subnet.Contains(source))
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
result = NetworkExtensions.FormatIPString(intf.Address);
|
|
|
|
_logger.LogDebug("{Source}: Found interface with matching subnet, using it as bind address: {Result}", source, result);
|
|
|
|
return result;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
|
|
|
|
2023-07-03 10:03:39 +02:00
|
|
|
// Fallback to first available interface
|
|
|
|
result = NetworkExtensions.FormatIPString(availableInterfaces[0].Address);
|
|
|
|
_logger.LogDebug("{Source}: No matching interfaces found, using preferred interface as bind address: {Result}", source, result);
|
2022-07-19 21:28:04 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2022-07-20 14:29:30 +02:00
|
|
|
public IReadOnlyList<IPData> GetInternalBindAddresses()
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
// Select all local bind addresses
|
2022-07-20 21:19:35 +02:00
|
|
|
return _interfaces.Where(x => IsInLocalNetwork(x.Address))
|
2022-07-20 14:29:30 +02:00
|
|
|
.OrderBy(x => x.Index)
|
|
|
|
.ToList();
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
|
|
|
public bool IsInLocalNetwork(string address)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-10-17 15:38:42 +02:00
|
|
|
if (NetworkExtensions.TryParseToSubnet(address, out var subnet))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-10-17 15:38:42 +02:00
|
|
|
return IPAddress.IsLoopback(subnet.Prefix) || (_lanSubnets.Any(x => x.Contains(subnet.Prefix)) && !_excludedSubnets.Any(x => x.Contains(subnet.Prefix)));
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (NetworkExtensions.TryParseHost(address, out var addresses, IsIPv4Enabled, IsIPv6Enabled))
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
foreach (var ept in addresses)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
if (IPAddress.IsLoopback(ept) || (_lanSubnets.Any(x => x.Contains(ept)) && !_excludedSubnets.Any(x => x.Contains(ept))))
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
return true;
|
2023-02-15 11:31:47 +01:00
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <inheritdoc/>
|
|
|
|
public bool IsInLocalNetwork(IPAddress address)
|
|
|
|
{
|
2022-10-07 09:04:41 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(address);
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
// See conversation at https://github.com/jellyfin/jellyfin/pull/3515.
|
2023-02-17 19:27:36 +01:00
|
|
|
if ((TrustAllIPv6Interfaces && address.AddressFamily == AddressFamily.InterNetworkV6)
|
2023-02-15 11:31:47 +01:00
|
|
|
|| address.Equals(IPAddress.Loopback)
|
|
|
|
|| address.Equals(IPAddress.IPv6Loopback))
|
2022-07-19 21:28:04 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
// As private addresses can be redefined by Configuration.LocalNetworkAddresses
|
2023-02-15 11:31:47 +01:00
|
|
|
return CheckIfLanAndNotExcluded(address);
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private bool CheckIfLanAndNotExcluded(IPAddress address)
|
|
|
|
{
|
|
|
|
foreach (var lanSubnet in _lanSubnets)
|
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
if (lanSubnet.Contains(address))
|
2023-02-15 22:34:44 +01:00
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
foreach (var excludedSubnet in _excludedSubnets)
|
|
|
|
{
|
|
|
|
if (excludedSubnet.Contains(address))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-02-15 22:34:44 +01:00
|
|
|
|
2023-07-03 10:03:39 +02:00
|
|
|
return true;
|
2023-02-15 22:34:44 +01:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
|
2023-07-03 10:03:39 +02:00
|
|
|
return false;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-07-19 21:28:04 +02:00
|
|
|
/// Attempts to match the source against the published server URL overrides.
|
2020-11-21 13:59:14 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="source">IP source address to use.</param>
|
2022-07-19 21:28:04 +02:00
|
|
|
/// <param name="isInExternalSubnet">True if the source is in an external subnet.</param>
|
|
|
|
/// <param name="bindPreference">The published server URL that matches the source address.</param>
|
2020-11-21 13:59:14 +01:00
|
|
|
/// <returns><c>true</c> if a match is found, <c>false</c> otherwise.</returns>
|
2022-10-15 17:27:37 +02:00
|
|
|
private bool MatchesPublishedServerUrl(IPAddress source, bool isInExternalSubnet, out string bindPreference)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
|
|
|
bindPreference = string.Empty;
|
2022-10-15 17:27:37 +02:00
|
|
|
int? port = null;
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-10-14 10:25:57 +02:00
|
|
|
var validPublishedServerUrls = _publishedServerUrls.Where(x => x.Key.Address.Equals(IPAddress.Any)
|
2023-02-15 11:31:47 +01:00
|
|
|
|| x.Key.Address.Equals(IPAddress.IPv6Any)
|
|
|
|
|| x.Key.Subnet.Contains(source))
|
|
|
|
.DistinctBy(x => x.Key)
|
|
|
|
.OrderBy(x => x.Key.Address.Equals(IPAddress.Any)
|
|
|
|
|| x.Key.Address.Equals(IPAddress.IPv6Any))
|
|
|
|
.ToList();
|
2022-07-19 21:28:04 +02:00
|
|
|
|
2020-11-21 13:59:14 +01:00
|
|
|
// Check for user override.
|
2022-07-19 21:28:04 +02:00
|
|
|
foreach (var data in validPublishedServerUrls)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-10-15 17:27:37 +02:00
|
|
|
if (isInExternalSubnet && (data.Key.Address.Equals(IPAddress.Any) || data.Key.Address.Equals(IPAddress.IPv6Any)))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
|
|
|
// External.
|
2022-07-19 21:28:04 +02:00
|
|
|
bindPreference = data.Value;
|
2020-11-21 13:59:14 +01:00
|
|
|
break;
|
|
|
|
}
|
2022-10-15 17:27:37 +02:00
|
|
|
|
2023-02-15 11:31:47 +01:00
|
|
|
// Get address interface.
|
|
|
|
var intf = _interfaces.OrderBy(x => x.Index).FirstOrDefault(x => data.Key.Subnet.Contains(x.Address));
|
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (intf?.Address is not null)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-10-15 17:27:37 +02:00
|
|
|
// Match IP address.
|
2022-07-19 21:28:04 +02:00
|
|
|
bindPreference = data.Value;
|
2020-11-21 13:59:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(bindPreference))
|
|
|
|
{
|
2023-02-15 11:31:47 +01:00
|
|
|
_logger.LogDebug("{Source}: No matching bind address override found", source);
|
2020-11-21 13:59:14 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has it got a port defined?
|
|
|
|
var parts = bindPreference.Split(':');
|
|
|
|
if (parts.Length > 1)
|
|
|
|
{
|
|
|
|
if (int.TryParse(parts[1], out int p))
|
|
|
|
{
|
|
|
|
bindPreference = parts[0];
|
|
|
|
port = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (port is not null)
|
2022-10-15 17:27:37 +02:00
|
|
|
{
|
2022-10-15 20:57:02 +02:00
|
|
|
_logger.LogDebug("{Source}: Matching bind address override found: {Address}:{Port}", source, bindPreference, port);
|
2022-10-15 17:27:37 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-10-15 20:57:02 +02:00
|
|
|
_logger.LogDebug("{Source}: Matching bind address override found: {Address}", source, bindPreference);
|
2022-10-15 17:27:37 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 13:59:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-10-15 20:57:02 +02:00
|
|
|
/// Attempts to match the source against the user defined bind interfaces.
|
2020-11-21 13:59:14 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="source">IP source address to use.</param>
|
|
|
|
/// <param name="isInExternalSubnet">True if the source is in the external subnet.</param>
|
|
|
|
/// <param name="result">The result, if a match is found.</param>
|
|
|
|
/// <returns><c>true</c> if a match is found, <c>false</c> otherwise.</returns>
|
2022-07-19 21:28:04 +02:00
|
|
|
private bool MatchesBindInterface(IPAddress source, bool isInExternalSubnet, out string result)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
|
|
|
result = string.Empty;
|
|
|
|
|
2022-07-20 21:51:12 +02:00
|
|
|
int count = _interfaces.Count;
|
2022-07-20 21:19:35 +02:00
|
|
|
if (count == 1 && (_interfaces[0].Equals(IPAddress.Any) || _interfaces[0].Equals(IPAddress.IPv6Any)))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
|
|
|
// Ignore IPAny addresses.
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
2023-02-15 11:31:47 +01:00
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress? bindAddress = null;
|
|
|
|
if (isInExternalSubnet)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2022-07-20 21:19:35 +02:00
|
|
|
var externalInterfaces = _interfaces.Where(x => !IsInLocalNetwork(x.Address))
|
|
|
|
.OrderBy(x => x.Index)
|
|
|
|
.ToList();
|
2023-02-15 11:31:47 +01:00
|
|
|
if (externalInterfaces.Count > 0)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-02-15 11:31:47 +01:00
|
|
|
// Check to see if any of the external bind interfaces are in the same subnet as the source.
|
|
|
|
// If none exists, this will select the first external interface if there is one.
|
|
|
|
bindAddress = externalInterfaces
|
2022-07-20 21:19:35 +02:00
|
|
|
.OrderByDescending(x => x.Subnet.Contains(source))
|
|
|
|
.ThenBy(x => x.Index)
|
|
|
|
.Select(x => x.Address)
|
2023-07-03 10:03:39 +02:00
|
|
|
.First();
|
2022-07-20 21:19:35 +02:00
|
|
|
|
2023-07-03 10:03:39 +02:00
|
|
|
result = NetworkExtensions.FormatIPString(bindAddress);
|
|
|
|
_logger.LogDebug("{Source}: External request received, matching external bind address found: {Result}", source, result);
|
|
|
|
return true;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2023-02-15 11:31:47 +01:00
|
|
|
|
|
|
|
_logger.LogWarning("{Source}: External request received, no matching external bind address found, trying internal addresses.", source);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Check to see if any of the internal bind interfaces are in the same subnet as the source.
|
|
|
|
// If none exists, this will select the first internal interface if there is one.
|
|
|
|
bindAddress = _interfaces.Where(x => IsInLocalNetwork(x.Address))
|
|
|
|
.OrderByDescending(x => x.Subnet.Contains(source))
|
|
|
|
.ThenBy(x => x.Index)
|
|
|
|
.Select(x => x.Address)
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
2023-02-15 22:34:44 +01:00
|
|
|
if (bindAddress is not null)
|
2023-02-15 11:31:47 +01:00
|
|
|
{
|
2023-02-17 19:27:36 +01:00
|
|
|
result = NetworkExtensions.FormatIPString(bindAddress);
|
2023-02-15 11:31:47 +01:00
|
|
|
_logger.LogDebug("{Source}: Internal request received, matching internal bind address found: {Result}", source, result);
|
|
|
|
return true;
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2022-10-15 20:57:02 +02:00
|
|
|
/// Attempts to match the source against external interfaces.
|
2020-11-21 13:59:14 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="source">IP source address to use.</param>
|
|
|
|
/// <param name="result">The result, if a match is found.</param>
|
|
|
|
/// <returns><c>true</c> if a match is found, <c>false</c> otherwise.</returns>
|
2022-07-19 21:28:04 +02:00
|
|
|
private bool MatchesExternalInterface(IPAddress source, out string result)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-02-15 11:31:47 +01:00
|
|
|
// Get the first external interface address that isn't a loopback.
|
|
|
|
var extResult = _interfaces.Where(p => !IsInLocalNetwork(p.Address)).OrderBy(x => x.Index).ToArray();
|
|
|
|
|
|
|
|
// No external interface found
|
|
|
|
if (extResult.Length == 0)
|
|
|
|
{
|
|
|
|
result = string.Empty;
|
|
|
|
_logger.LogWarning("{Source}: External request received, but no external interface found. Need to route through internal network.", source);
|
|
|
|
return false;
|
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2022-07-19 21:28:04 +02:00
|
|
|
// Does the request originate in one of the interface subnets?
|
2023-02-15 11:31:47 +01:00
|
|
|
// (For systems with multiple network cards and/or multiple subnets)
|
2022-07-19 21:28:04 +02:00
|
|
|
foreach (var intf in extResult)
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-07-03 10:03:39 +02:00
|
|
|
if (intf.Subnet.Contains(source))
|
2020-11-21 13:59:14 +01:00
|
|
|
{
|
2023-02-17 19:27:36 +01:00
|
|
|
result = NetworkExtensions.FormatIPString(intf.Address);
|
2022-10-15 20:57:02 +02:00
|
|
|
_logger.LogDebug("{Source}: Found external interface with matching subnet, using it as bind address: {Result}", source, result);
|
2022-07-19 21:28:04 +02:00
|
|
|
return true;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
2022-07-19 21:28:04 +02:00
|
|
|
}
|
2020-11-21 13:59:14 +01:00
|
|
|
|
2023-02-15 11:31:47 +01:00
|
|
|
// Fallback to first external interface.
|
2023-07-03 21:51:36 +02:00
|
|
|
result = NetworkExtensions.FormatIPString(extResult[0].Address);
|
2023-02-15 11:31:47 +01:00
|
|
|
_logger.LogDebug("{Source}: Using first external interface as bind address: {Result}", source, result);
|
|
|
|
return true;
|
2020-11-21 13:59:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|