From 329305230eb0b0f12e5de4cdb04188dcb89e4a0c Mon Sep 17 00:00:00 2001 From: randomevents Date: Tue, 10 Oct 2017 23:35:30 -0400 Subject: [PATCH] Test for matching subnet of IP's (VPN request) test available ip4 host address against the endpoint (only local addresses, 10 127 192.68 169), if there's a preliminary match, it finds the subnet of the host ip (255.255.255.0) and makes sure's there's a match between host (255.255.255) and endpoint --- .../Networking/NetworkManager.cs | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Emby.Server.Implementations/Networking/NetworkManager.cs b/Emby.Server.Implementations/Networking/NetworkManager.cs index 5fb8f7fc9..7f0eef0b9 100644 --- a/Emby.Server.Implementations/Networking/NetworkManager.cs +++ b/Emby.Server.Implementations/Networking/NetworkManager.cs @@ -93,12 +93,36 @@ namespace Emby.Server.Implementations.Networking } return - endpoint.StartsWith("localhost", StringComparison.OrdinalIgnoreCase) || + IsInPrivateAddressSpaceAndLocalSubnet(endpoint); + } + + public bool IsInPrivateAddressSpaceAndLocalSubnet(string endpoint) + { + string subnet_Match = ""; + if ( endpoint.StartsWith("127.", StringComparison.OrdinalIgnoreCase) || endpoint.StartsWith("10.", StringComparison.OrdinalIgnoreCase) || endpoint.StartsWith("192.168", StringComparison.OrdinalIgnoreCase) || - endpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase); + endpoint.StartsWith("169.", StringComparison.OrdinalIgnoreCase) + ) + { + foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) + foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses) + if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork && endpoint.Split('.')[0] == unicastIPAddressInformation.Address.ToString().Split('.')[0]) + { + int subnet_Test = 0; + foreach (string part in unicastIPAddressInformation.IPv4Mask.ToString().Split('.')) + { + if (part.Equals("0")) break; + subnet_Test++; + } + + subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray()); + } + } + + return endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase); } private bool Is172AddressPrivate(string endpoint)