From 605bd80251368a88f7ffc0d0442bd52a7575329a Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Sat, 20 Feb 2021 22:46:16 +0000 Subject: [PATCH 01/10] Fix for ignoreVirtualInterfaces --- Jellyfin.Networking/Manager/NetworkManager.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index 60b899519e..c08406003a 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -916,7 +916,8 @@ namespace Jellyfin.Networking.Manager // Add virtual machine interface names to the list of bind exclusions, so that they are auto-excluded. 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(); var newList = new string[lanAddresses.Length + virtualInterfaceNames.Length]; Array.Copy(lanAddresses, newList, lanAddresses.Length); Array.Copy(virtualInterfaceNames, 0, newList, lanAddresses.Length, virtualInterfaceNames.Length); From b03bd7a29935895c7d11507e59008df0eb5ba58c Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Sun, 21 Feb 2021 00:41:14 +0000 Subject: [PATCH 02/10] Fix testing --- Jellyfin.Networking/Manager/NetworkManager.cs | 37 ++++++++++++------- .../NetworkTesting/NetworkParseTests.cs | 3 +- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index c08406003a..b60e4e23ba 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -691,11 +691,11 @@ namespace Jellyfin.Networking.Manager /// Checks the string to see if it matches any interface names. /// /// String to check. - /// Interface index number. + /// Interface index numbers that match. /// true if an interface name matches the token, False otherwise. - private bool IsInterface(string token, out int index) + private bool IsInterface(string token, out List? index) { - index = -1; + index = null; // Is it the name of an interface (windows) eg, Wireless LAN adapter Wireless Network Connection 1. // Null check required here for automated testing. @@ -712,13 +712,17 @@ namespace Jellyfin.Networking.Manager if ((!partial && string.Equals(interfc, token, StringComparison.OrdinalIgnoreCase)) || (partial && interfc.StartsWith(token, true, CultureInfo.InvariantCulture))) { - index = interfcIndex; - return true; + if (index == null) + { + index = new List(); + } + + index.Add(interfcIndex); } } } - return false; + return index != null; } /// @@ -730,14 +734,14 @@ namespace Jellyfin.Networking.Manager { // Is it the name of an interface (windows) eg, Wireless LAN adapter Wireless Network Connection 1. // Null check required here for automated testing. - if (IsInterface(token, out int index)) + if (IsInterface(token, out var index)) { _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) { - if (Math.Abs(iface.Tag) == index + if (index!.Contains(Math.Abs(iface.Tag)) && ((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork) || (IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6))) { @@ -918,10 +922,17 @@ namespace Jellyfin.Networking.Manager { // each virtual interface name must be pre-pended with the exclusion symbol ! var virtualInterfaceNames = config.VirtualInterfaceNames.Split(',').Select(p => '!' + p).ToArray(); - var newList = new string[lanAddresses.Length + virtualInterfaceNames.Length]; - Array.Copy(lanAddresses, newList, lanAddresses.Length); - Array.Copy(virtualInterfaceNames, 0, newList, lanAddresses.Length, virtualInterfaceNames.Length); - lanAddresses = newList; + if (lanAddresses.Length > 0) + { + var newList = new string[lanAddresses.Length + virtualInterfaceNames.Length]; + Array.Copy(lanAddresses, newList, lanAddresses.Length); + Array.Copy(virtualInterfaceNames, 0, newList, lanAddresses.Length, virtualInterfaceNames.Length); + lanAddresses = newList; + } + else + { + lanAddresses = virtualInterfaceNames; + } } // Read and parse bind addresses and exclusions, removing ones that don't exist. diff --git a/tests/Jellyfin.Networking.Tests/NetworkTesting/NetworkParseTests.cs b/tests/Jellyfin.Networking.Tests/NetworkTesting/NetworkParseTests.cs index c350685af0..e41ea3cca2 100644 --- a/tests/Jellyfin.Networking.Tests/NetworkTesting/NetworkParseTests.cs +++ b/tests/Jellyfin.Networking.Tests/NetworkTesting/NetworkParseTests.cs @@ -60,7 +60,8 @@ namespace Jellyfin.Networking.Tests [Theory] [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", "[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]")] + [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", "[]")] + [InlineData("192.168.1.200/24,-20,vEthernet1:192.168.1.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) { var conf = new NetworkConfiguration() From cb09096a59c0df9b02f4c057db93d8479678af8f Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Sun, 21 Feb 2021 00:42:06 +0000 Subject: [PATCH 03/10] optimized --- Jellyfin.Networking/Manager/NetworkManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index b60e4e23ba..9b9897e76b 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -921,7 +921,7 @@ namespace Jellyfin.Networking.Manager if (config.IgnoreVirtualInterfaces) { // each virtual interface name must be pre-pended with the exclusion symbol ! - var virtualInterfaceNames = config.VirtualInterfaceNames.Split(',').Select(p => '!' + p).ToArray(); + var virtualInterfaceNames = config.VirtualInterfaceNames.Split(',').Select(p => "!" + p).ToArray(); if (lanAddresses.Length > 0) { var newList = new string[lanAddresses.Length + virtualInterfaceNames.Length]; From 7bfc59b562b2a266e184d76c92a2af56377dfb5c Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Sun, 21 Feb 2021 13:31:59 +0000 Subject: [PATCH 04/10] Fixed test data. --- .../NetworkTesting/NetworkParseTests.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/Jellyfin.Networking.Tests/NetworkTesting/NetworkParseTests.cs b/tests/Jellyfin.Networking.Tests/NetworkTesting/NetworkParseTests.cs index 6bff52e73b..8d5d4884a6 100644 --- a/tests/Jellyfin.Networking.Tests/NetworkTesting/NetworkParseTests.cs +++ b/tests/Jellyfin.Networking.Tests/NetworkTesting/NetworkParseTests.cs @@ -52,16 +52,20 @@ namespace Jellyfin.Networking.Tests } /// - /// Checks the ability to ignore interfaces + /// Checks the ability to ignore virtual interfaces. /// /// Mock network setup, in the format (IP address, interface index, interface name) | .... /// LAN addresses. /// Bind addresses that are excluded. [Theory] - [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", "[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", "[]")] - [InlineData("192.168.1.200/24,-20,vEthernet1:192.168.1.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]")] + // 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]")] + // 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", "[]")] + // 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) { var conf = new NetworkConfiguration() From ff7cae8a13ce4bd5f158da840907a46ef2ad7d86 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Tue, 23 Feb 2021 17:06:40 +0000 Subject: [PATCH 05/10] renamed method --- Jellyfin.Networking/Manager/NetworkManager.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index bd4b165b5d..9cb49ce685 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; using System.Net; @@ -693,7 +694,7 @@ namespace Jellyfin.Networking.Manager /// String to check. /// Interface index numbers that match. /// true if an interface name matches the token, False otherwise. - private bool IsInterface(string token, out List? index) + private bool TryIsInterface(string token, [NotNullWhen(true)] out List? index) { index = null; @@ -734,14 +735,14 @@ namespace Jellyfin.Networking.Manager { // Is it the name of an interface (windows) eg, Wireless LAN adapter Wireless Network Connection 1. // Null check required here for automated testing. - if (IsInterface(token, out var index)) + if (TryIsInterface(token, out var index)) { _logger.LogInformation("Interface {Token} used in settings. Using its interface addresses.", token); // Replace all the interface tags with the interface IP's. foreach (IPNetAddress iface in _interfaceAddresses) { - if (index!.Contains(Math.Abs(iface.Tag)) + if (index.Contains(Math.Abs(iface.Tag)) && ((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork) || (IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6))) { From f67137004c16fc8bf2478e9898e786599469c465 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Tue, 23 Feb 2021 20:34:32 +0000 Subject: [PATCH 06/10] Update Jellyfin.Networking/Manager/NetworkManager.cs Co-authored-by: Bond-009 --- Jellyfin.Networking/Manager/NetworkManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index 9cb49ce685..e4623cd62b 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -694,7 +694,7 @@ namespace Jellyfin.Networking.Manager /// String to check. /// Interface index numbers that match. /// true if an interface name matches the token, False otherwise. - private bool TryIsInterface(string token, [NotNullWhen(true)] out List? index) + private bool TryIsInterface(string token, [MaybeNullWhen(false)] out List index) { index = null; From b5c6e5fb97d06533583f8273227f9215906c2ed1 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Tue, 23 Feb 2021 20:34:42 +0000 Subject: [PATCH 07/10] Update Jellyfin.Networking/Manager/NetworkManager.cs Co-authored-by: Bond-009 --- Jellyfin.Networking/Manager/NetworkManager.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index e4623cd62b..3f209277f4 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -713,11 +713,7 @@ namespace Jellyfin.Networking.Manager if ((!partial && string.Equals(interfc, token, StringComparison.OrdinalIgnoreCase)) || (partial && interfc.StartsWith(token, true, CultureInfo.InvariantCulture))) { - if (index == null) - { - index = new List(); - } - + index ??= new List(); index.Add(interfcIndex); } } From 039a4fb22d52d840019ecdb39c62bcd7f76e3b84 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Wed, 24 Feb 2021 11:40:50 +0000 Subject: [PATCH 08/10] renamed method --- Jellyfin.Networking/Manager/NetworkManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index 3f209277f4..1d5eb1826e 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -694,7 +694,7 @@ namespace Jellyfin.Networking.Manager /// String to check. /// Interface index numbers that match. /// true if an interface name matches the token, False otherwise. - private bool TryIsInterface(string token, [MaybeNullWhen(false)] out List index) + private bool TryGetInterfaces(string token, [NotNullWhen(true)] out List? index) { index = null; @@ -731,7 +731,7 @@ namespace Jellyfin.Networking.Manager { // Is it the name of an interface (windows) eg, Wireless LAN adapter Wireless Network Connection 1. // Null check required here for automated testing. - if (TryIsInterface(token, out var index)) + if (TryGetInterfaces(token, out var index)) { _logger.LogInformation("Interface {Token} used in settings. Using its interface addresses.", token); From d99d95422ed52bd3a7dbf9f7bd947feb809a2616 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Sat, 27 Feb 2021 19:10:53 +0000 Subject: [PATCH 09/10] Update Jellyfin.Networking/Manager/NetworkManager.cs Co-authored-by: Claus Vium --- Jellyfin.Networking/Manager/NetworkManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index 1d5eb1826e..05796d6a38 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -731,7 +731,7 @@ namespace Jellyfin.Networking.Manager { // Is it the name of an interface (windows) eg, Wireless LAN adapter Wireless Network Connection 1. // Null check required here for automated testing. - if (TryGetInterfaces(token, out var index)) + if (TryGetInterfaces(token, out var indices)) { _logger.LogInformation("Interface {Token} used in settings. Using its interface addresses.", token); From cc19d281e7363748af0b42a063859df169bf71e3 Mon Sep 17 00:00:00 2001 From: BaronGreenback Date: Sat, 27 Feb 2021 19:10:58 +0000 Subject: [PATCH 10/10] Update Jellyfin.Networking/Manager/NetworkManager.cs Co-authored-by: Claus Vium --- Jellyfin.Networking/Manager/NetworkManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jellyfin.Networking/Manager/NetworkManager.cs b/Jellyfin.Networking/Manager/NetworkManager.cs index 05796d6a38..51fcb6d9a0 100644 --- a/Jellyfin.Networking/Manager/NetworkManager.cs +++ b/Jellyfin.Networking/Manager/NetworkManager.cs @@ -738,7 +738,7 @@ namespace Jellyfin.Networking.Manager // Replace all the interface tags with the interface IP's. foreach (IPNetAddress iface in _interfaceAddresses) { - if (index.Contains(Math.Abs(iface.Tag)) + if (indices.Contains(Math.Abs(iface.Tag)) && ((IsIP4Enabled && iface.Address.AddressFamily == AddressFamily.InterNetwork) || (IsIP6Enabled && iface.Address.AddressFamily == AddressFamily.InterNetworkV6))) {