From 4432b88281cbd489ac4a71a8e9635c6325ccd168 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 26 Oct 2017 23:05:29 -0400 Subject: update network detection --- Emby.Server.Implementations/Networking/NetworkManager.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'Emby.Server.Implementations/Networking/NetworkManager.cs') diff --git a/Emby.Server.Implementations/Networking/NetworkManager.cs b/Emby.Server.Implementations/Networking/NetworkManager.cs index 66b2cba39..db2c455fb 100644 --- a/Emby.Server.Implementations/Networking/NetworkManager.cs +++ b/Emby.Server.Implementations/Networking/NetworkManager.cs @@ -103,7 +103,9 @@ namespace Emby.Server.Implementations.Networking } return endpoint.StartsWith("localhost", StringComparison.OrdinalIgnoreCase) || - endpoint.StartsWith("127.0.0.1", StringComparison.OrdinalIgnoreCase) || + endpoint.StartsWith("127.", StringComparison.OrdinalIgnoreCase) || + endpoint.StartsWith("192.168", StringComparison.OrdinalIgnoreCase) || + endpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase) || IsInPrivateAddressSpaceAndLocalSubnet(endpoint); } -- cgit v1.2.3 From 88caeaa783d8d6a7d2d17c98955fa4bc8264d446 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 26 Oct 2017 23:43:06 -0400 Subject: update address detection --- Emby.Server.Implementations/Networking/NetworkManager.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'Emby.Server.Implementations/Networking/NetworkManager.cs') diff --git a/Emby.Server.Implementations/Networking/NetworkManager.cs b/Emby.Server.Implementations/Networking/NetworkManager.cs index db2c455fb..be85db80a 100644 --- a/Emby.Server.Implementations/Networking/NetworkManager.cs +++ b/Emby.Server.Implementations/Networking/NetworkManager.cs @@ -113,7 +113,6 @@ namespace Emby.Server.Implementations.Networking { var endpointFirstPart = endpoint.Split('.')[0]; - string subnet_Match = ""; if ( endpoint.StartsWith("127.", StringComparison.OrdinalIgnoreCase) || endpoint.StartsWith("10.", StringComparison.OrdinalIgnoreCase) || @@ -122,7 +121,9 @@ namespace Emby.Server.Implementations.Networking ) { foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) + { foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses) + { if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork && endpointFirstPart == unicastIPAddressInformation.Address.ToString().Split('.')[0]) { int subnet_Test = 0; @@ -132,11 +133,18 @@ namespace Emby.Server.Implementations.Networking subnet_Test++; } - subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); + var subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); + + if (endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase)) + { + return true; + } } + } + } } - return endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase); + return false; } private Dictionary _subnetLookup = new Dictionary(StringComparer.Ordinal); -- cgit v1.2.3 From 44a270fa6fed9ef806e02a200af0bb14b5cf0a0e Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 27 Oct 2017 00:14:45 -0400 Subject: cache subnet results --- .../Networking/NetworkManager.cs | 58 +++++++++++----------- 1 file changed, 28 insertions(+), 30 deletions(-) (limited to 'Emby.Server.Implementations/Networking/NetworkManager.cs') diff --git a/Emby.Server.Implementations/Networking/NetworkManager.cs b/Emby.Server.Implementations/Networking/NetworkManager.cs index be85db80a..7664ed442 100644 --- a/Emby.Server.Implementations/Networking/NetworkManager.cs +++ b/Emby.Server.Implementations/Networking/NetworkManager.cs @@ -120,26 +120,15 @@ namespace Emby.Server.Implementations.Networking endpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase) ) { - foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) - { - foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses) - { - if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork && endpointFirstPart == unicastIPAddressInformation.Address.ToString().Split('.')[0]) - { - int subnet_Test = 0; - foreach (string part in unicastIPAddressInformation.IPv4Mask.ToString().Split('.')) - { - if (part.Equals("0")) break; - subnet_Test++; - } + var subnets = GetSubnets(endpointFirstPart); - var subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); + foreach (var subnet_Match in subnets) + { + //Logger.Debug("subnet_Match:" + subnet_Match); - if (endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase)) - { - return true; - } - } + if (endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase)) + { + return true; } } } @@ -147,20 +136,24 @@ namespace Emby.Server.Implementations.Networking return false; } - private Dictionary _subnetLookup = new Dictionary(StringComparer.Ordinal); - private string GetSubnet(string endpointFirstPart) + private Dictionary> _subnetLookup = new Dictionary>(StringComparer.Ordinal); + private List GetSubnets(string endpointFirstPart) { - string subnet_Match = ""; + List subnets; lock (_subnetLookup) { - if (_subnetLookup.TryGetValue(endpointFirstPart, out subnet_Match)) + if (_subnetLookup.TryGetValue(endpointFirstPart, out subnets)) { - return subnet_Match; + return subnets; } + subnets = new List(); + foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) + { foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses) + { if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork && endpointFirstPart == unicastIPAddressInformation.Address.ToString().Split('.')[0]) { int subnet_Test = 0; @@ -170,16 +163,21 @@ namespace Emby.Server.Implementations.Networking subnet_Test++; } - subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); - } + var subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); - if (!string.IsNullOrWhiteSpace(subnet_Match)) - { - _subnetLookup[endpointFirstPart] = subnet_Match; + // TODO: Is this check necessary? + if (adapter.OperationalStatus == OperationalStatus.Up) + { + subnets.Add(subnet_Match); + } + } + } } - } - return subnet_Match; + _subnetLookup[endpointFirstPart] = subnets; + + return subnets; + } } private bool Is172AddressPrivate(string endpoint) -- cgit v1.2.3