feat: add docker as localhost

This commit is contained in:
Polianin Nikita 2024-10-27 04:09:31 +03:00
parent fc5ec1fd54
commit 6c20713d81

View File

@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Net; using System.Net;
namespace Mirea.Api.Endpoint.Common.Attributes; namespace Mirea.Api.Endpoint.Common.Attributes;
@ -9,11 +10,21 @@ public class LocalhostAttribute : ActionFilterAttribute
public override void OnActionExecuting(ActionExecutingContext context) public override void OnActionExecuting(ActionExecutingContext context)
{ {
var ip = context.HttpContext.Connection.RemoteIpAddress; var ip = context.HttpContext.Connection.RemoteIpAddress;
if (ip == null || !IPAddress.IsLoopback(ip))
if (ip == null)
{ {
context.Result = new UnauthorizedResult(); context.Result = new UnauthorizedResult();
return; return;
} }
var isRunningInContainer = Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER")?.ToLower() == "true";
if (IPAddress.IsLoopback(ip) || (isRunningInContainer && ip.ToString().StartsWith("172.")))
{
base.OnActionExecuting(context); base.OnActionExecuting(context);
return;
}
context.Result = new UnauthorizedResult();
} }
} }