Selaa lähdekoodia

Merge pull request #5416 from BaronGreenback/SubnetOverlappFix

(cherry picked from commit 19e7ebb27927737ab38499be1f66621cf14f6698)
Signed-off-by: Joshua M. Boniface <joshua@boniface.me>
Joshua M. Boniface 4 vuotta sitten
vanhempi
sitoutus
e78fa8c3ef

+ 30 - 19
Jellyfin.Networking/Manager/NetworkManager.cs

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

+ 7 - 4
MediaBrowser.Common/Net/NetworkExtensions.cs

@@ -32,9 +32,11 @@ namespace MediaBrowser.Common.Net
         /// </summary>
         /// </summary>
         /// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
         /// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
         /// <param name="item">Item to add.</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);
                 source.Add(item);
             }
             }
@@ -195,8 +197,9 @@ namespace MediaBrowser.Common.Net
         /// </summary>
         /// </summary>
         /// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
         /// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
         /// <param name="excludeList">Items to exclude.</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>
         /// <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)
             if (source.Count == 0 || excludeList == null)
             {
             {
@@ -221,7 +224,7 @@ namespace MediaBrowser.Common.Net
 
 
                 if (!found)
                 if (!found)
                 {
                 {
-                    results.AddItem(outer);
+                    results.AddItem(outer, isNetwork);
                 }
                 }
             }
             }
 
 

+ 2 - 0
tests/Jellyfin.Networking.Tests/NetworkTesting/NetworkParseTests.cs

@@ -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", "[]")]
         [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.
         // 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]")]
         [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)
         public void IgnoreVirtualInterfaces(string interfaces, string lan, string value)
         {
         {
             var conf = new NetworkConfiguration()
             var conf = new NetworkConfiguration()