jellyfin/MediaBrowser.Common/Extensions/HttpContextExtensions.cs

40 lines
1.3 KiB
C#
Raw Normal View History

2020-07-17 17:06:52 +02:00
using Microsoft.AspNetCore.Http;
2020-07-18 17:54:23 +02:00
namespace MediaBrowser.Common.Extensions
2020-07-17 17:06:52 +02:00
{
/// <summary>
2020-07-18 17:54:23 +02:00
/// Static class containing extension methods for <see cref="HttpContext"/>.
2020-07-17 17:06:52 +02:00
/// </summary>
2020-07-18 17:54:23 +02:00
public static class HttpContextExtensions
2020-07-17 17:06:52 +02:00
{
/// <summary>
/// Checks the origin of the HTTP context.
2020-07-17 17:06:52 +02:00
/// </summary>
/// <param name="context">The incoming HTTP context.</param>
2020-09-02 12:22:14 +02:00
/// <returns><c>true</c> if the request is coming from LAN, <c>false</c> otherwise.</returns>
public static bool IsLocal(this HttpContext context)
2020-07-17 17:06:52 +02:00
{
return (context.Connection.LocalIpAddress == null
&& context.Connection.RemoteIpAddress == null)
|| context.Connection.LocalIpAddress.Equals(context.Connection.RemoteIpAddress);
2020-07-17 17:06:52 +02:00
}
/// <summary>
/// Extracts the remote IP address of the caller of the HTTP context.
2020-07-17 17:06:52 +02:00
/// </summary>
/// <param name="context">The HTTP context.</param>
2020-09-02 12:22:14 +02:00
/// <returns>The remote caller IP address.</returns>
public static string GetNormalizedRemoteIp(this HttpContext context)
2020-07-17 17:06:52 +02:00
{
var ip = context.Connection.RemoteIpAddress;
2020-09-02 12:22:14 +02:00
if (ip.IsIPv4MappedToIPv6)
{
ip = ip.MapToIPv4();
}
return ip.ToString();
2020-07-17 17:06:52 +02:00
}
}
}