| | 1 | | using System.Net; |
| | 2 | | using Microsoft.AspNetCore.Http; |
| | 3 | |
|
| | 4 | | namespace MediaBrowser.Common.Extensions |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Static class containing extension methods for <see cref="HttpContext"/>. |
| | 8 | | /// </summary> |
| | 9 | | public static class HttpContextExtensions |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Checks the origin of the HTTP context. |
| | 13 | | /// </summary> |
| | 14 | | /// <param name="context">The incoming HTTP context.</param> |
| | 15 | | /// <returns><c>true</c> if the request is coming from LAN, <c>false</c> otherwise.</returns> |
| | 16 | | public static bool IsLocal(this HttpContext context) |
| | 17 | | { |
| 165 | 18 | | return (context.Connection.LocalIpAddress is null |
| 165 | 19 | | && context.Connection.RemoteIpAddress is null) |
| 165 | 20 | | || Equals(context.Connection.LocalIpAddress, context.Connection.RemoteIpAddress); |
| | 21 | | } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Extracts the remote IP address of the caller of the HTTP context. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="context">The HTTP context.</param> |
| | 27 | | /// <returns>The remote caller IP address.</returns> |
| | 28 | | public static IPAddress GetNormalizedRemoteIP(this HttpContext context) |
| | 29 | | { |
| | 30 | | // Default to the loopback address if no RemoteIpAddress is specified (i.e. during integration tests) |
| 122 | 31 | | var ip = context.Connection.RemoteIpAddress ?? IPAddress.Loopback; |
| | 32 | |
|
| 122 | 33 | | if (ip.IsIPv4MappedToIPv6) |
| | 34 | | { |
| 0 | 35 | | ip = ip.MapToIPv4(); |
| | 36 | | } |
| | 37 | |
|
| 122 | 38 | | return ip; |
| | 39 | | } |
| | 40 | | } |
| | 41 | | } |