The Free Software Media System - Server Backend & API
Enables the ability to bind to loopback address. (#5773)
BaronGreenback0b774eac12a8
diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.csindex 2f5a5b6e33..73e8b2cd72 100644--- a/Jellyfin.Networking/Manager/NetworkManager.cs+++ b/Jellyfin.Networking/Manager/NetworkManager.cs@@ -647,6 +647,16 @@ namespace Jellyfin.Networking.Manager_interfaceAddresses.AddItem(address, false);_interfaceNames[parts[2]] = Math.Abs(index);}++ if (IsIP4Enabled)+ {+ _interfaceAddresses.AddItem(IPNetAddress.IP4Loopback);+ }++ if (IsIP6Enabled)+ {+ _interfaceAddresses.AddItem(IPNetAddress.IP6Loopback);+ }}InitialiseLAN(config);@@ -978,8 +988,8 @@ namespace Jellyfin.Networking.Manager}// 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 exclusions: {0}", _bindExclusions.AsString());}@@ -1153,36 +1163,40 @@ namespace Jellyfin.Networking.Manager}#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){- _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());}}diff --git a/MediaBrowser.Common/Net/NetworkExtensions.cs b/MediaBrowser.Common/Net/NetworkExtensions.csindex 93cfb4817b..264bfacb49 100644--- a/MediaBrowser.Common/Net/NetworkExtensions.cs+++ b/MediaBrowser.Common/Net/NetworkExtensions.cs@@ -232,7 +232,7 @@ namespace MediaBrowser.Common.Net/// <param name="source">The <see cref="Collection{IPObject}"/>.</param>/// <param name="target">Collection to compare with.</param>/// <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){diff --git a/tests/Jellyfin.Networking.Tests/NetworkParseTests.cs b/tests/Jellyfin.Networking.Tests/NetworkParseTests.csindex 2d33569980..9b0da2b3c4 100644--- a/tests/Jellyfin.Networking.Tests/NetworkParseTests.cs+++ b/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]")]// 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]")]- // 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.- [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,[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)@@ -241,7 +241,7 @@ namespace Jellyfin.Networking.TestsCollection<IPObject> nc1 = nm.CreateIPCollection(settings.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]@@ -350,7 +350,7 @@ namespace Jellyfin.Networking.Tests// Test included, IP6.Collection<IPObject> ncSource = nm.CreateIPCollection(source.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(','));Assert.True(ncResult.Compare(resultCollection));}