2024-05-29 03:30:00 +03:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
2024-10-27 04:09:31 +03:00
|
|
|
|
using System;
|
2024-05-29 03:30:00 +03:00
|
|
|
|
using System.Net;
|
|
|
|
|
|
|
|
|
|
namespace Mirea.Api.Endpoint.Common.Attributes;
|
|
|
|
|
|
|
|
|
|
public class LocalhostAttribute : ActionFilterAttribute
|
|
|
|
|
{
|
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext context)
|
|
|
|
|
{
|
|
|
|
|
var ip = context.HttpContext.Connection.RemoteIpAddress;
|
2024-10-27 04:09:31 +03:00
|
|
|
|
|
|
|
|
|
if (ip == null)
|
2024-05-29 03:30:00 +03:00
|
|
|
|
{
|
|
|
|
|
context.Result = new UnauthorizedResult();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-27 04:09:31 +03:00
|
|
|
|
|
|
|
|
|
var isRunningInContainer = Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER")?.ToLower() == "true";
|
|
|
|
|
|
|
|
|
|
if (IPAddress.IsLoopback(ip) || (isRunningInContainer && ip.ToString().StartsWith("172.")))
|
|
|
|
|
{
|
|
|
|
|
base.OnActionExecuting(context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Result = new UnauthorizedResult();
|
2024-05-29 03:30:00 +03:00
|
|
|
|
}
|
|
|
|
|
}
|