fixes #1769 - IsInLocalNetwork returns wrong value

This commit is contained in:
Luke Pulverenti 2017-10-11 00:45:21 -04:00
parent 0d21476944
commit 3e0cfd3af3

View File

@ -81,8 +81,18 @@ namespace Emby.Server.Implementations.Networking
return true; return true;
} }
// Handle ipv4 mapped to ipv6 // ipv6
endpoint = endpoint.Replace("::ffff:", string.Empty); if (endpoint.Split('.').Length > 4)
{
// Handle ipv4 mapped to ipv6
var originalEndpoint = endpoint;
endpoint = endpoint.Replace("::ffff:", string.Empty);
if (string.Equals(endpoint, originalEndpoint, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}
// Private address space: // Private address space:
// http://en.wikipedia.org/wiki/Private_network // http://en.wikipedia.org/wiki/Private_network
@ -92,13 +102,15 @@ namespace Emby.Server.Implementations.Networking
return Is172AddressPrivate(endpoint); return Is172AddressPrivate(endpoint);
} }
return return endpoint.StartsWith("localhost", StringComparison.OrdinalIgnoreCase) ||
endpoint.StartsWith("localhost", StringComparison.OrdinalIgnoreCase) || endpoint.StartsWith("127.0.0.1", StringComparison.OrdinalIgnoreCase) ||
IsInPrivateAddressSpaceAndLocalSubnet(endpoint); IsInPrivateAddressSpaceAndLocalSubnet(endpoint);
} }
public bool IsInPrivateAddressSpaceAndLocalSubnet(string endpoint) public bool IsInPrivateAddressSpaceAndLocalSubnet(string endpoint)
{ {
var endpointFirstPart = endpoint.Split('.')[0];
string subnet_Match = ""; string subnet_Match = "";
if ( if (
endpoint.StartsWith("127.", StringComparison.OrdinalIgnoreCase) || endpoint.StartsWith("127.", StringComparison.OrdinalIgnoreCase) ||
@ -109,7 +121,7 @@ namespace Emby.Server.Implementations.Networking
{ {
foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses) foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses)
if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork && endpoint.Split('.')[0] == unicastIPAddressInformation.Address.ToString().Split('.')[0]) if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork && endpointFirstPart == unicastIPAddressInformation.Address.ToString().Split('.')[0])
{ {
int subnet_Test = 0; int subnet_Test = 0;
foreach (string part in unicastIPAddressInformation.IPv4Mask.ToString().Split('.')) foreach (string part in unicastIPAddressInformation.IPv4Mask.ToString().Split('.'))
@ -125,6 +137,41 @@ namespace Emby.Server.Implementations.Networking
return endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase); return endpoint.StartsWith(subnet_Match + ".", StringComparison.OrdinalIgnoreCase);
} }
private Dictionary<string, string> _subnetLookup = new Dictionary<string, string>(StringComparer.Ordinal);
private string GetSubnet(string endpointFirstPart)
{
string subnet_Match = "";
lock (_subnetLookup)
{
if (_subnetLookup.TryGetValue(endpointFirstPart, out subnet_Match))
{
return subnet_Match;
}
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++;
}
subnet_Match = String.Join(".", unicastIPAddressInformation.Address.ToString().Split('.').Take(subnet_Test).ToArray());
}
if (!string.IsNullOrWhiteSpace(subnet_Match))
{
_subnetLookup[endpointFirstPart] = subnet_Match;
}
}
return subnet_Match;
}
private bool Is172AddressPrivate(string endpoint) private bool Is172AddressPrivate(string endpoint)
{ {
for (var i = 16; i <= 31; i++) for (var i = 16; i <= 31; i++)