2
0
Эх сурвалжийг харах

Enables the ability to bind to loopback address. (#5773)

BaronGreenback 4 жил өмнө
parent
commit
0b774eac12

+ 38 - 24
Jellyfin.Networking/Manager/NetworkManager.cs

@@ -647,6 +647,16 @@ namespace Jellyfin.Networking.Manager
                     _interfaceAddresses.AddItem(address, false);
                     _interfaceAddresses.AddItem(address, false);
                     _interfaceNames[parts[2]] = Math.Abs(index);
                     _interfaceNames[parts[2]] = Math.Abs(index);
                 }
                 }
+
+                if (IsIP4Enabled)
+                {
+                    _interfaceAddresses.AddItem(IPNetAddress.IP4Loopback);
+                }
+
+                if (IsIP6Enabled)
+                {
+                    _interfaceAddresses.AddItem(IPNetAddress.IP6Loopback);
+                }
             }
             }
 
 
             InitialiseLAN(config);
             InitialiseLAN(config);
@@ -978,8 +988,8 @@ namespace Jellyfin.Networking.Manager
                 }
                 }
 
 
                 // Read and parse bind addresses and exclusions, removing ones that don't exist.
                 // Read and parse bind addresses and exclusions, removing ones that don't exist.
-                _bindAddresses = CreateIPCollection(lanAddresses).Union(_interfaceAddresses);
-                _bindExclusions = CreateIPCollection(lanAddresses, true).Union(_interfaceAddresses);
+                _bindAddresses = CreateIPCollection(lanAddresses).ThatAreContainedInNetworks(_interfaceAddresses);
+                _bindExclusions = CreateIPCollection(lanAddresses, true).ThatAreContainedInNetworks(_interfaceAddresses);
                 _logger.LogInformation("Using bind addresses: {0}", _bindAddresses.AsString());
                 _logger.LogInformation("Using bind addresses: {0}", _bindAddresses.AsString());
                 _logger.LogInformation("Using bind exclusions: {0}", _bindExclusions.AsString());
                 _logger.LogInformation("Using bind exclusions: {0}", _bindExclusions.AsString());
             }
             }
@@ -1153,36 +1163,40 @@ namespace Jellyfin.Networking.Manager
                         }
                         }
 #pragma warning restore CA1031 // Do not catch general exception types
 #pragma warning restore CA1031 // Do not catch general exception types
                     }
                     }
+                }
+                catch (Exception ex)
+                {
+                    _logger.LogError(ex, "Error in InitialiseInterfaces.");
+                }
 
 
-                    _logger.LogDebug("Discovered {0} interfaces.", _interfaceAddresses.Count);
-                    _logger.LogDebug("Interfaces addresses : {0}", _interfaceAddresses.AsString());
+                // If for some reason we don't have an interface info, resolve our DNS name.
+                if (_interfaceAddresses.Count == 0)
+                {
+                    _logger.LogError("No interfaces information available. Resolving DNS name.");
+                    IPHost host = new IPHost(Dns.GetHostName());
+                    foreach (var a in host.GetAddresses())
+                    {
+                        _interfaceAddresses.AddItem(a);
+                    }
 
 
-                    // If for some reason we don't have an interface info, resolve our DNS name.
                     if (_interfaceAddresses.Count == 0)
                     if (_interfaceAddresses.Count == 0)
                     {
                     {
-                        _logger.LogError("No interfaces information available. Resolving DNS name.");
-                        IPHost host = new IPHost(Dns.GetHostName());
-                        foreach (var a in host.GetAddresses())
-                        {
-                            _interfaceAddresses.AddItem(a);
-                        }
-
-                        if (_interfaceAddresses.Count == 0)
-                        {
-                            _logger.LogWarning("No interfaces information available. Using loopback.");
-                            // Last ditch attempt - use loopback address.
-                            _interfaceAddresses.AddItem(IPNetAddress.IP4Loopback, false);
-                            if (IsIP6Enabled)
-                            {
-                                _interfaceAddresses.AddItem(IPNetAddress.IP6Loopback, false);
-                            }
-                        }
+                        _logger.LogWarning("No interfaces information available. Using loopback.");
                     }
                     }
                 }
                 }
-                catch (NetworkInformationException ex)
+
+                if (IsIP4Enabled)
                 {
                 {
-                    _logger.LogError(ex, "Error in InitialiseInterfaces.");
+                    _interfaceAddresses.AddItem(IPNetAddress.IP4Loopback);
+                }
+
+                if (IsIP6Enabled)
+                {
+                    _interfaceAddresses.AddItem(IPNetAddress.IP6Loopback);
                 }
                 }
+
+                _logger.LogDebug("Discovered {0} interfaces.", _interfaceAddresses.Count);
+                _logger.LogDebug("Interfaces addresses : {0}", _interfaceAddresses.AsString());
             }
             }
         }
         }
 
 

+ 1 - 1
MediaBrowser.Common/Net/NetworkExtensions.cs

@@ -232,7 +232,7 @@ namespace MediaBrowser.Common.Net
         /// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
         /// <param name="source">The <see cref="Collection{IPObject}"/>.</param>
         /// <param name="target">Collection to compare with.</param>
         /// <param name="target">Collection to compare with.</param>
         /// <returns>A collection containing all the matches.</returns>
         /// <returns>A collection containing all the matches.</returns>
-        public static Collection<IPObject> Union(this Collection<IPObject> source, Collection<IPObject> target)
+        public static Collection<IPObject> ThatAreContainedInNetworks(this Collection<IPObject> source, Collection<IPObject> target)
         {
         {
             if (source.Count == 0)
             if (source.Count == 0)
             {
             {

+ 5 - 5
tests/Jellyfin.Networking.Tests/NetworkParseTests.cs

@@ -34,10 +34,10 @@ namespace Jellyfin.Networking.Tests
         [InlineData("192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.0/24", "[192.168.1.208/24,200.200.200.200/24]")]
         [InlineData("192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11", "192.168.1.0/24;200.200.200.0/24", "[192.168.1.208/24,200.200.200.200/24]")]
         // eth16 only
         // eth16 only
         [InlineData("192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.208/24]")]
         [InlineData("192.168.1.208/24,-16,eth16|200.200.200.200/24,11,eth11", "192.168.1.0/24", "[192.168.1.208/24]")]
-        // All interfaces excluded.
-        [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", "[]")]
+        // All interfaces excluded. (including loopbacks)
+        [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", "[127.0.0.1/8,::1/128]")]
         // 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,127.0.0.1/8,::1/128]")]
         // Overlapping interface,
         // 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]")]
         [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)
@@ -241,7 +241,7 @@ namespace Jellyfin.Networking.Tests
             Collection<IPObject> nc1 = nm.CreateIPCollection(settings.Split(','), false);
             Collection<IPObject> nc1 = nm.CreateIPCollection(settings.Split(','), false);
             Collection<IPObject> nc2 = nm.CreateIPCollection(compare.Split(','), false);
             Collection<IPObject> nc2 = nm.CreateIPCollection(compare.Split(','), false);
 
 
-            Assert.Equal(nc1.Union(nc2).AsString(), result);
+            Assert.Equal(nc1.ThatAreContainedInNetworks(nc2).AsString(), result);
         }
         }
 
 
         [Theory]
         [Theory]
@@ -350,7 +350,7 @@ namespace Jellyfin.Networking.Tests
             // Test included, IP6.
             // Test included, IP6.
             Collection<IPObject> ncSource = nm.CreateIPCollection(source.Split(','));
             Collection<IPObject> ncSource = nm.CreateIPCollection(source.Split(','));
             Collection<IPObject> ncDest = nm.CreateIPCollection(dest.Split(','));
             Collection<IPObject> ncDest = nm.CreateIPCollection(dest.Split(','));
-            Collection<IPObject> ncResult = ncSource.Union(ncDest);
+            Collection<IPObject> ncResult = ncSource.ThatAreContainedInNetworks(ncDest);
             Collection<IPObject> resultCollection = nm.CreateIPCollection(result.Split(','));
             Collection<IPObject> resultCollection = nm.CreateIPCollection(result.Split(','));
             Assert.True(ncResult.Compare(resultCollection));
             Assert.True(ncResult.Compare(resultCollection));
         }
         }