Merge pull request #5416 from BaronGreenback/SubnetOverlappFix

This commit is contained in:
Joshua M. Boniface 2021-04-11 13:29:00 -04:00 committed by GitHub
commit 19e7ebb279
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 23 deletions

View File

@ -165,7 +165,7 @@ namespace Jellyfin.Networking.Manager
{
foreach (var item in source)
{
result.AddItem(item);
result.AddItem(item, false);
}
}
@ -274,7 +274,7 @@ namespace Jellyfin.Networking.Manager
if (_bindExclusions.Count > 0)
{
// Return all the interfaces except the ones specifically excluded.
return _interfaceAddresses.Exclude(_bindExclusions);
return _interfaceAddresses.Exclude(_bindExclusions, false);
}
if (individualInterfaces)
@ -310,7 +310,7 @@ namespace Jellyfin.Networking.Manager
}
// Remove any excluded bind interfaces.
return _bindAddresses.Exclude(_bindExclusions);
return _bindAddresses.Exclude(_bindExclusions, false);
}
/// <inheritdoc/>
@ -397,15 +397,26 @@ namespace Jellyfin.Networking.Manager
}
// Get the first LAN interface address that isn't a loopback.
var interfaces = CreateCollection(_interfaceAddresses
.Exclude(_bindExclusions)
.Where(IsInLocalNetwork)
.OrderBy(p => p.Tag));
var interfaces = CreateCollection(
_interfaceAddresses
.Exclude(_bindExclusions, false)
.Where(IsInLocalNetwork)
.OrderBy(p => p.Tag));
if (interfaces.Count > 0)
{
if (haveSource)
{
foreach (var intf in interfaces)
{
if (intf.Address.Equals(source.Address))
{
result = FormatIP6String(intf.Address);
_logger.LogDebug("{Source}: GetBindInterface: Has found matching interface. {Result}", source, result);
return result;
}
}
// Does the request originate in one of the interface subnets?
// (For systems with multiple internal network cards, and multiple subnets)
foreach (var intf in interfaces)
@ -532,10 +543,10 @@ namespace Jellyfin.Networking.Manager
{
if (filter == null)
{
return _lanSubnets.Exclude(_excludedSubnets).AsNetworks();
return _lanSubnets.Exclude(_excludedSubnets, true).AsNetworks();
}
return _lanSubnets.Exclude(filter);
return _lanSubnets.Exclude(filter, true);
}
/// <inheritdoc/>
@ -566,7 +577,7 @@ namespace Jellyfin.Networking.Manager
&& ((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork)
|| (IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6)))
{
result.AddItem(iface);
result.AddItem(iface, false);
}
}
@ -633,8 +644,8 @@ namespace Jellyfin.Networking.Manager
var address = IPNetAddress.Parse(parts[0]);
var index = int.Parse(parts[1], CultureInfo.InvariantCulture);
address.Tag = index;
_interfaceAddresses.AddItem(address);
_interfaceNames.Add(parts[2], Math.Abs(index));
_interfaceAddresses.AddItem(address, false);
_interfaceNames[parts[2]] = Math.Abs(index);
}
}
@ -1051,7 +1062,7 @@ namespace Jellyfin.Networking.Manager
_logger.LogInformation("Defined LAN addresses : {0}", _lanSubnets.AsString());
_logger.LogInformation("Defined LAN exclusions : {0}", _excludedSubnets.AsString());
_logger.LogInformation("Using LAN addresses: {0}", _lanSubnets.Exclude(_excludedSubnets).AsNetworks().AsString());
_logger.LogInformation("Using LAN addresses: {0}", _lanSubnets.Exclude(_excludedSubnets, true).AsNetworks().AsString());
}
}
@ -1105,7 +1116,7 @@ namespace Jellyfin.Networking.Manager
nw.Tag *= -1;
}
_interfaceAddresses.AddItem(nw);
_interfaceAddresses.AddItem(nw, false);
// Store interface name so we can use the name in Collections.
_interfaceNames[adapter.Description.ToLower(CultureInfo.InvariantCulture)] = tag;
@ -1126,7 +1137,7 @@ namespace Jellyfin.Networking.Manager
nw.Tag *= -1;
}
_interfaceAddresses.AddItem(nw);
_interfaceAddresses.AddItem(nw, false);
// Store interface name so we can use the name in Collections.
_interfaceNames[adapter.Description.ToLower(CultureInfo.InvariantCulture)] = tag;
@ -1160,10 +1171,10 @@ namespace Jellyfin.Networking.Manager
{
_logger.LogWarning("No interfaces information available. Using loopback.");
// Last ditch attempt - use loopback address.
_interfaceAddresses.AddItem(IPNetAddress.IP4Loopback);
_interfaceAddresses.AddItem(IPNetAddress.IP4Loopback, false);
if (IsIP6Enabled)
{
_interfaceAddresses.AddItem(IPNetAddress.IP6Loopback);
_interfaceAddresses.AddItem(IPNetAddress.IP6Loopback, false);
}
}
}
@ -1240,7 +1251,7 @@ namespace Jellyfin.Networking.Manager
private bool MatchesBindInterface(IPObject source, bool isInExternalSubnet, out string result)
{
result = string.Empty;
var addresses = _bindAddresses.Exclude(_bindExclusions);
var addresses = _bindAddresses.Exclude(_bindExclusions, false);
int count = addresses.Count;
if (count == 1 && (_bindAddresses[0].Equals(IPAddress.Any) || _bindAddresses[0].Equals(IPAddress.IPv6Any)))
@ -1325,7 +1336,7 @@ namespace Jellyfin.Networking.Manager
result = string.Empty;
// Get the first WAN interface address that isn't a loopback.
var extResult = _interfaceAddresses
.Exclude(_bindExclusions)
.Exclude(_bindExclusions, false)
.Where(p => !IsInLocalNetwork(p))
.OrderBy(p => p.Tag);

View File

@ -27,9 +27,11 @@ namespace MediaBrowser.Common.Net
/// </summary>
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <param name="item">Item to add.</param>
public static void AddItem(this Collection<IPObject> source, IPObject item)
/// <param name="itemsAreNetworks">If <c>true</c> the values are treated as subnets.
/// If <b>false</b> items are addresses.</param>
public static void AddItem(this Collection<IPObject> source, IPObject item, bool itemsAreNetworks = true)
{
if (!source.ContainsAddress(item))
if (!source.ContainsAddress(item) || !itemsAreNetworks)
{
source.Add(item);
}
@ -190,8 +192,9 @@ namespace MediaBrowser.Common.Net
/// </summary>
/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
/// <param name="excludeList">Items to exclude.</param>
/// <param name="isNetwork">Collection is a network collection.</param>
/// <returns>A new collection, with the items excluded.</returns>
public static Collection<IPObject> Exclude(this Collection<IPObject> source, Collection<IPObject> excludeList)
public static Collection<IPObject> Exclude(this Collection<IPObject> source, Collection<IPObject> excludeList, bool isNetwork)
{
if (source.Count == 0 || excludeList == null)
{
@ -216,7 +219,7 @@ namespace MediaBrowser.Common.Net
if (!found)
{
results.AddItem(outer);
results.AddItem(outer, isNetwork);
}
}

View File

@ -38,6 +38,8 @@ namespace Jellyfin.Networking.Tests
[InlineData("192.168.1.208/24,-16,vEthernet1|192.168.2.208/24,-16,vEthernet212|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[]")]
// vEthernet1 and vEthernet212 should be excluded.
[InlineData("192.168.1.200/24,-20,vEthernet1|192.168.2.208/24,-16,vEthernet212|200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.200/24", "[200.200.200.200/24]")]
// Overlapping interface,
[InlineData("192.168.1.110/24,-20,br0|192.168.1.10/24,-16,br0|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.110/24,192.168.1.10/24]")]
public void IgnoreVirtualInterfaces(string interfaces, string lan, string value)
{
var conf = new NetworkConfiguration()