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

This commit is contained in:
BaronGreenback 2021-04-14 10:26:05 +01:00 committed by GitHub
parent 51af569159
commit 0b774eac12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 34 deletions

View file

@ -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
}
_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 (_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);
}
}
}
}
catch (NetworkInformationException ex)
catch (Exception ex)
{
_logger.LogError(ex, "Error in InitialiseInterfaces.");
}
// 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.");
}
}
if (IsIP4Enabled)
{
_interfaceAddresses.AddItem(IPNetAddress.IP4Loopback);
}
if (IsIP6Enabled)
{
_interfaceAddresses.AddItem(IPNetAddress.IP6Loopback);
}
_logger.LogDebug("Discovered {0} interfaces.", _interfaceAddresses.Count);
_logger.LogDebug("Interfaces addresses : {0}", _interfaceAddresses.AsString());
}
}

View file

@ -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)
{

View file

@ -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.Tests
Collection<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));
}