Merge pull request #5274 from BaronGreenback/bindfix

(cherry picked from commit 14605280a0)
Signed-off-by: Joshua M. Boniface <joshua@boniface.me>
pull/5640/head
Claus Vium 4 years ago committed by Joshua M. Boniface
parent 0f139e8857
commit 25c19f79d4

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -691,11 +692,11 @@ namespace Jellyfin.Networking.Manager
/// Checks the string to see if it matches any interface names. /// Checks the string to see if it matches any interface names.
/// </summary> /// </summary>
/// <param name="token">String to check.</param> /// <param name="token">String to check.</param>
/// <param name="index">Interface index number.</param> /// <param name="index">Interface index numbers that match.</param>
/// <returns><c>true</c> if an interface name matches the token, <c>False</c> otherwise.</returns> /// <returns><c>true</c> if an interface name matches the token, <c>False</c> otherwise.</returns>
private bool IsInterface(string token, out int index) private bool TryGetInterfaces(string token, [NotNullWhen(true)] out List<int>? index)
{ {
index = -1; index = null;
// Is it the name of an interface (windows) eg, Wireless LAN adapter Wireless Network Connection 1. // Is it the name of an interface (windows) eg, Wireless LAN adapter Wireless Network Connection 1.
// Null check required here for automated testing. // Null check required here for automated testing.
@ -712,13 +713,13 @@ namespace Jellyfin.Networking.Manager
if ((!partial && string.Equals(interfc, token, StringComparison.OrdinalIgnoreCase)) if ((!partial && string.Equals(interfc, token, StringComparison.OrdinalIgnoreCase))
|| (partial && interfc.StartsWith(token, true, CultureInfo.InvariantCulture))) || (partial && interfc.StartsWith(token, true, CultureInfo.InvariantCulture)))
{ {
index = interfcIndex; index ??= new List<int>();
return true; index.Add(interfcIndex);
} }
} }
} }
return false; return index != null;
} }
/// <summary> /// <summary>
@ -730,14 +731,14 @@ namespace Jellyfin.Networking.Manager
{ {
// Is it the name of an interface (windows) eg, Wireless LAN adapter Wireless Network Connection 1. // Is it the name of an interface (windows) eg, Wireless LAN adapter Wireless Network Connection 1.
// Null check required here for automated testing. // Null check required here for automated testing.
if (IsInterface(token, out int index)) if (TryGetInterfaces(token, out var indices))
{ {
_logger.LogInformation("Interface {Token} used in settings. Using its interface addresses.", token); _logger.LogInformation("Interface {Token} used in settings. Using its interface addresses.", token);
// Replace interface tags with the interface IP's. // Replace all the interface tags with the interface IP's.
foreach (IPNetAddress iface in _interfaceAddresses) foreach (IPNetAddress iface in _interfaceAddresses)
{ {
if (Math.Abs(iface.Tag) == index if (indices.Contains(Math.Abs(iface.Tag))
&& ((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork) && ((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork)
|| (IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6))) || (IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6)))
{ {
@ -916,12 +917,20 @@ namespace Jellyfin.Networking.Manager
// Add virtual machine interface names to the list of bind exclusions, so that they are auto-excluded. // Add virtual machine interface names to the list of bind exclusions, so that they are auto-excluded.
if (config.IgnoreVirtualInterfaces) if (config.IgnoreVirtualInterfaces)
{ {
var virtualInterfaceNames = config.VirtualInterfaceNames.Split(','); // each virtual interface name must be pre-pended with the exclusion symbol !
var virtualInterfaceNames = config.VirtualInterfaceNames.Split(',').Select(p => "!" + p).ToArray();
if (lanAddresses.Length > 0)
{
var newList = new string[lanAddresses.Length + virtualInterfaceNames.Length]; var newList = new string[lanAddresses.Length + virtualInterfaceNames.Length];
Array.Copy(lanAddresses, newList, lanAddresses.Length); Array.Copy(lanAddresses, newList, lanAddresses.Length);
Array.Copy(virtualInterfaceNames, 0, newList, lanAddresses.Length, virtualInterfaceNames.Length); Array.Copy(virtualInterfaceNames, 0, newList, lanAddresses.Length, virtualInterfaceNames.Length);
lanAddresses = newList; lanAddresses = newList;
} }
else
{
lanAddresses = virtualInterfaceNames;
}
}
// 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); _bindAddresses = CreateIPCollection(lanAddresses).Union(_interfaceAddresses);

@ -24,15 +24,20 @@ namespace Jellyfin.Networking.Tests
} }
/// <summary> /// <summary>
/// Checks the ability to ignore interfaces /// Checks the ability to ignore virtual interfaces.
/// </summary> /// </summary>
/// <param name="interfaces">Mock network setup, in the format (IP address, interface index, interface name) | .... </param> /// <param name="interfaces">Mock network setup, in the format (IP address, interface index, interface name) | .... </param>
/// <param name="lan">LAN addresses.</param> /// <param name="lan">LAN addresses.</param>
/// <param name="value">Bind addresses that are excluded.</param> /// <param name="value">Bind addresses that are excluded.</param>
[Theory] [Theory]
// All valid
[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
[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]")]
[InlineData("192.168.1.208/24,-16,vEthernet1|192.168.1.208/24,-16,vEthernet212|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", "[]")]
// 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]")]
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()

Loading…
Cancel
Save