mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-21 01:24:44 +01:00
Remove ServiceStack and related stuff
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using MediaBrowser.Model.Services;
|
||||
using System.Net;
|
||||
using MediaBrowser.Common.Net;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace MediaBrowser.Common.Extensions
|
||||
@@ -8,26 +9,54 @@ namespace MediaBrowser.Common.Extensions
|
||||
/// </summary>
|
||||
public static class HttpContextExtensions
|
||||
{
|
||||
private const string ServiceStackRequest = "ServiceStackRequest";
|
||||
|
||||
/// <summary>
|
||||
/// Set the ServiceStack request.
|
||||
/// Checks the origin of the HTTP request.
|
||||
/// </summary>
|
||||
/// <param name="httpContext">The HttpContext instance.</param>
|
||||
/// <param name="request">The service stack request instance.</param>
|
||||
public static void SetServiceStackRequest(this HttpContext httpContext, IRequest request)
|
||||
/// <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)
|
||||
{
|
||||
httpContext.Items[ServiceStackRequest] = request;
|
||||
return (request.HttpContext.Connection.LocalIpAddress == null
|
||||
&& request.HttpContext.Connection.RemoteIpAddress == null)
|
||||
|| request.HttpContext.Connection.LocalIpAddress.Equals(request.HttpContext.Connection.RemoteIpAddress);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the ServiceStack request.
|
||||
/// Extracts the remote IP address of the caller of the HTTP request.
|
||||
/// </summary>
|
||||
/// <param name="httpContext">The HttpContext instance.</param>
|
||||
/// <returns>The service stack request instance.</returns>
|
||||
public static IRequest GetServiceStackRequest(this HttpContext httpContext)
|
||||
/// <param name="request">The HTTP request.</param>
|
||||
/// <returns>The remote caller IP address.</returns>
|
||||
public static string RemoteIp(this HttpRequest request)
|
||||
{
|
||||
return (IRequest)httpContext.Items[ServiceStackRequest];
|
||||
if (string.IsNullOrEmpty(request.HttpContext.Items["RemoteIp"].ToString()))
|
||||
{
|
||||
return request.HttpContext.Items["RemoteIp"].ToString();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user