Backport pull request #11766 from jellyfin/release-10.9.z

Filter invalid IPs on external interface matching

Original-merge: 2eece01acc

Merged-by: crobibero <cody@robibe.ro>

Backported-by: Joshua M. Boniface <joshua@boniface.me>
This commit is contained in:
gnattu 2024-05-25 11:46:13 -04:00 committed by Joshua M. Boniface
parent debd9eb8ce
commit e7145acd56
2 changed files with 21 additions and 1 deletions

View File

@ -58,6 +58,11 @@ public static class NetworkConstants
/// </summary>
public static readonly IPNetwork IPv4RFC1918PrivateClassC = new IPNetwork(IPAddress.Parse("192.168.0.0"), 16);
/// <summary>
/// IPv4 Link-Local as defined in RFC 3927.
/// </summary>
public static readonly IPNetwork IPv4RFC3927LinkLocal = new IPNetwork(IPAddress.Parse("169.254.0.0"), 16);
/// <summary>
/// IPv6 loopback as defined in RFC 4291.
/// </summary>

View File

@ -903,6 +903,17 @@ public class NetworkManager : INetworkManager, IDisposable
return false;
}
/// <summary>
/// Get if the IPAddress is Link-local.
/// </summary>
/// <param name="address">The IP Address.</param>
/// <returns>Bool indicates if the address is link-local.</returns>
public bool IsLinkLocalAddress(IPAddress address)
{
ArgumentNullException.ThrowIfNull(address);
return NetworkConstants.IPv4RFC3927LinkLocal.Contains(address) || address.IsIPv6LinkLocal;
}
/// <inheritdoc/>
public bool IsInLocalNetwork(IPAddress address)
{
@ -1084,7 +1095,11 @@ public class NetworkManager : INetworkManager, IDisposable
private bool MatchesExternalInterface(IPAddress source, out string result)
{
// Get the first external interface address that isn't a loopback.
var extResult = _interfaces.Where(p => !IsInLocalNetwork(p.Address)).OrderBy(x => x.Index).ToArray();
var extResult = _interfaces
.Where(p => !IsInLocalNetwork(p.Address))
.Where(p => p.Address.AddressFamily.Equals(source.AddressFamily))
.Where(p => !IsLinkLocalAddress(p.Address))
.OrderBy(x => x.Index).ToArray();
// No external interface found
if (extResult.Length == 0)