mirror of
https://github.com/immense/Remotely.git
synced 2025-10-26 11:27:15 +00:00
36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Remotely.Server.Attributes
|
|
{
|
|
[AttributeUsage(AttributeTargets.Method)]
|
|
public class ActionRateLimiterAttribute : ActionFilterAttribute
|
|
{
|
|
public string Action { get; set; }
|
|
public int TimeoutInSeconds { get; set; } = 5;
|
|
private static MemoryCache RequestCache { get; } = new MemoryCache(new MemoryCacheOptions());
|
|
|
|
|
|
public override void OnActionExecuting(ActionExecutingContext context)
|
|
{
|
|
var ip = context.HttpContext.Request.HttpContext.Connection.RemoteIpAddress;
|
|
var key = $"Action-{ip}";
|
|
|
|
if (!RequestCache.TryGetValue(key, out _))
|
|
{
|
|
RequestCache.Set(key, true, TimeSpan.FromSeconds(TimeoutInSeconds));
|
|
}
|
|
else
|
|
{
|
|
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.TooManyRequests;
|
|
}
|
|
base.OnActionExecuting(context);
|
|
}
|
|
}
|
|
}
|