2020-09-02 10:22:14 +00:00
|
|
|
using System.Net;
|
|
|
|
using MediaBrowser.Common.Net;
|
2020-07-17 15:06:52 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
2020-07-18 15:54:23 +00:00
|
|
|
namespace MediaBrowser.Common.Extensions
|
2020-07-17 15:06:52 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-07-18 15:54:23 +00:00
|
|
|
/// Static class containing extension methods for <see cref="HttpContext"/>.
|
2020-07-17 15:06:52 +00:00
|
|
|
/// </summary>
|
2020-07-18 15:54:23 +00:00
|
|
|
public static class HttpContextExtensions
|
2020-07-17 15:06:52 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-09-02 10:22:14 +00:00
|
|
|
/// Checks the origin of the HTTP request.
|
2020-07-17 15:06:52 +00:00
|
|
|
/// </summary>
|
2020-09-02 10:22:14 +00:00
|
|
|
/// <param name="request">The incoming HTTP request.</param>
|
|
|
|
/// <returns><c>true</c> if the request is coming from LAN, <c>false</c> otherwise.</returns>
|
|
|
|
public static bool IsLocal(this HttpRequest request)
|
2020-07-17 15:06:52 +00:00
|
|
|
{
|
2020-09-02 10:22:14 +00:00
|
|
|
return (request.HttpContext.Connection.LocalIpAddress == null
|
|
|
|
&& request.HttpContext.Connection.RemoteIpAddress == null)
|
|
|
|
|| request.HttpContext.Connection.LocalIpAddress.Equals(request.HttpContext.Connection.RemoteIpAddress);
|
2020-07-17 15:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-09-02 10:22:14 +00:00
|
|
|
/// Extracts the remote IP address of the caller of the HTTP request.
|
2020-07-17 15:06:52 +00:00
|
|
|
/// </summary>
|
2020-09-02 10:22:14 +00:00
|
|
|
/// <param name="request">The HTTP request.</param>
|
|
|
|
/// <returns>The remote caller IP address.</returns>
|
|
|
|
public static string RemoteIp(this HttpRequest request)
|
2020-07-17 15:06:52 +00:00
|
|
|
{
|
2020-09-02 22:31:42 +00:00
|
|
|
var cachedRemoteIp = request.HttpContext.Items["RemoteIp"]?.ToString();
|
2020-09-02 13:27:54 +00:00
|
|
|
if (!string.IsNullOrEmpty(cachedRemoteIp))
|
2020-09-02 10:22:14 +00:00
|
|
|
{
|
2020-09-02 10:49:48 +00:00
|
|
|
return cachedRemoteIp;
|
2020-09-02 10:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IPAddress ip;
|
|
|
|
|
|
|
|
// "Real" remote ip might be in X-Forwarded-For of X-Real-Ip
|
|
|
|
// (if the server is behind a reverse proxy for example)
|
|
|
|
if (!IPAddress.TryParse(request.Headers[CustomHeaderNames.XForwardedFor].ToString(), out ip))
|
|
|
|
{
|
|
|
|
if (!IPAddress.TryParse(request.Headers[CustomHeaderNames.XRealIP].ToString(), out ip))
|
|
|
|
{
|
|
|
|
ip = request.HttpContext.Connection.RemoteIpAddress;
|
|
|
|
|
|
|
|
// Default to the loopback address if no RemoteIpAddress is specified (i.e. during integration tests)
|
|
|
|
ip ??= IPAddress.Loopback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ip.IsIPv4MappedToIPv6)
|
|
|
|
{
|
|
|
|
ip = ip.MapToIPv4();
|
|
|
|
}
|
|
|
|
|
|
|
|
var normalizedIp = ip.ToString();
|
|
|
|
|
|
|
|
request.HttpContext.Items["RemoteIp"] = normalizedIp;
|
|
|
|
return normalizedIp;
|
2020-07-17 15:06:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|