diff options
| author | WWWesten <4700006+WWWesten@users.noreply.github.com> | 2021-11-01 23:43:29 +0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-01 23:43:29 +0500 |
| commit | 0a14279e2a21bcb9654a06a2d49e1e4f0cc5329c (patch) | |
| tree | e1b1bd603b011ca98e5793e356326bf4a35a7050 /MediaBrowser.Common/Extensions/HttpContextExtensions.cs | |
| parent | f2817fef743eeb75a00782ceea363b2d3e7dc9f2 (diff) | |
| parent | 76eeb8f655424d295e73ced8349c6fefee6ddb12 (diff) | |
Merge branch 'jellyfin:master' into master
Diffstat (limited to 'MediaBrowser.Common/Extensions/HttpContextExtensions.cs')
| -rw-r--r-- | MediaBrowser.Common/Extensions/HttpContextExtensions.cs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/MediaBrowser.Common/Extensions/HttpContextExtensions.cs b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs new file mode 100644 index 000000000..1e5877c84 --- /dev/null +++ b/MediaBrowser.Common/Extensions/HttpContextExtensions.cs @@ -0,0 +1,41 @@ +using System.Net; +using Microsoft.AspNetCore.Http; + +namespace MediaBrowser.Common.Extensions +{ + /// <summary> + /// Static class containing extension methods for <see cref="HttpContext"/>. + /// </summary> + public static class HttpContextExtensions + { + /// <summary> + /// Checks the origin of the HTTP context. + /// </summary> + /// <param name="context">The incoming HTTP context.</param> + /// <returns><c>true</c> if the request is coming from LAN, <c>false</c> otherwise.</returns> + public static bool IsLocal(this HttpContext context) + { + return (context.Connection.LocalIpAddress == null + && context.Connection.RemoteIpAddress == null) + || Equals(context.Connection.LocalIpAddress, context.Connection.RemoteIpAddress); + } + + /// <summary> + /// Extracts the remote IP address of the caller of the HTTP context. + /// </summary> + /// <param name="context">The HTTP context.</param> + /// <returns>The remote caller IP address.</returns> + public static IPAddress GetNormalizedRemoteIp(this HttpContext context) + { + // Default to the loopback address if no RemoteIpAddress is specified (i.e. during integration tests) + var ip = context.Connection.RemoteIpAddress ?? IPAddress.Loopback; + + if (ip.IsIPv4MappedToIPv6) + { + ip = ip.MapToIPv4(); + } + + return ip; + } + } +} |
