fix: use stdlib IPAddress.IsLoopback

pull/7549/head
cvium 3 years ago
parent 1d585146d6
commit e299adc819

@ -464,35 +464,13 @@ namespace Jellyfin.Networking.Manager
/// <inheritdoc/> /// <inheritdoc/>
public bool IsInLocalNetwork(IPObject address) public bool IsInLocalNetwork(IPObject address)
{ {
if (address == null) return IsInLocalNetwork(address.Address);
{
throw new ArgumentNullException(nameof(address));
}
if (address.Equals(IPAddress.None))
{
return false;
}
// See conversation at https://github.com/jellyfin/jellyfin/pull/3515.
if (TrustAllIP6Interfaces && address.AddressFamily == AddressFamily.InterNetworkV6)
{
return true;
}
// As private addresses can be redefined by Configuration.LocalNetworkAddresses
return address.IsLoopback() || (_lanSubnets.ContainsAddress(address) && !_excludedSubnets.ContainsAddress(address));
} }
/// <inheritdoc/> /// <inheritdoc/>
public bool IsInLocalNetwork(string address) public bool IsInLocalNetwork(string address)
{ {
if (IPHost.TryParse(address, out IPHost ep)) return IPHost.TryParse(address, out IPHost ipHost) && IsInLocalNetwork(ipHost);
{
return _lanSubnets.ContainsAddress(ep) && !_excludedSubnets.ContainsAddress(ep);
}
return false;
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -503,6 +481,11 @@ namespace Jellyfin.Networking.Manager
throw new ArgumentNullException(nameof(address)); throw new ArgumentNullException(nameof(address));
} }
if (address.Equals(IPAddress.None))
{
return false;
}
// See conversation at https://github.com/jellyfin/jellyfin/pull/3515. // See conversation at https://github.com/jellyfin/jellyfin/pull/3515.
if (TrustAllIP6Interfaces && address.AddressFamily == AddressFamily.InterNetworkV6) if (TrustAllIP6Interfaces && address.AddressFamily == AddressFamily.InterNetworkV6)
{ {
@ -510,7 +493,7 @@ namespace Jellyfin.Networking.Manager
} }
// As private addresses can be redefined by Configuration.LocalNetworkAddresses // As private addresses can be redefined by Configuration.LocalNetworkAddresses
return _lanSubnets.ContainsAddress(address) && !_excludedSubnets.ContainsAddress(address); return IPAddress.IsLoopback(address) || (_lanSubnets.ContainsAddress(address) && !_excludedSubnets.ContainsAddress(address));
} }
/// <inheritdoc/> /// <inheritdoc/>

@ -65,7 +65,7 @@ namespace MediaBrowser.Common.Net
address = address.MapToIPv4(); address = address.MapToIPv4();
} }
if (IsLoopback(address)) if (IPAddress.IsLoopback(address))
{ {
return (address, prefixLength); return (address, prefixLength);
} }
@ -102,31 +102,6 @@ namespace MediaBrowser.Common.Net
return (new IPAddress(addressBytes), prefixLength); return (new IPAddress(addressBytes), prefixLength);
} }
/// <summary>
/// Tests to see if the ip address is a Loopback address.
/// </summary>
/// <param name="address">Value to test.</param>
/// <returns>True if it is.</returns>
public static bool IsLoopback(IPAddress address)
{
if (address == null)
{
throw new ArgumentNullException(nameof(address));
}
if (!address.Equals(IPAddress.None))
{
if (address.IsIPv4MappedToIPv6)
{
address = address.MapToIPv4();
}
return address.Equals(IPAddress.Loopback) || address.Equals(IPAddress.IPv6Loopback);
}
return false;
}
/// <summary> /// <summary>
/// Tests to see if the ip address is an IP6 address. /// Tests to see if the ip address is an IP6 address.
/// </summary> /// </summary>
@ -295,7 +270,7 @@ namespace MediaBrowser.Common.Net
/// <returns>True if it is.</returns> /// <returns>True if it is.</returns>
public virtual bool IsLoopback() public virtual bool IsLoopback()
{ {
return IsLoopback(Address); return IPAddress.IsLoopback(Address);
} }
/// <summary> /// <summary>

Loading…
Cancel
Save