adding messages rate limit, form, controller
This commit is contained in:
+46
-2
@@ -1,11 +1,15 @@
|
||||
using Microsoft.AspNetCore.Antiforgery;
|
||||
using System.Security.Claims;
|
||||
using System.Threading.RateLimiting;
|
||||
using Microsoft.AspNetCore.Antiforgery;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Npgsql;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
using StatiKosev.Data;
|
||||
using StatiKosev.Logging;
|
||||
|
||||
namespace StatiKosev;
|
||||
|
||||
@@ -15,12 +19,14 @@ public class Program
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(arguments);
|
||||
var databaseConnectionString = builder.Configuration.GetConnectionString("DefaultConnection");
|
||||
var messageRateLimitSeconds = builder.Configuration.GetValue("MessageRateLimitSeconds", 10);
|
||||
|
||||
builder.Services.AddLocalization(o => o.ResourcesPath = "LanguagesValues");
|
||||
builder.Services.AddRazorPages();
|
||||
builder.Services.AddNpgsqlDataSource(databaseConnectionString);
|
||||
builder.Services.AddDbContext<AppDbContext>((sp, o) =>
|
||||
o.UseNpgsql(sp.GetRequiredService<NpgsqlDataSource>())); // EF shares the same connection pool
|
||||
|
||||
//builder.Services.AddLogging();
|
||||
builder.Services.AddDefaultIdentity<IdentityUser>(o =>
|
||||
{
|
||||
o.SignIn.RequireConfirmedAccount = false; // no email sending configured yet
|
||||
@@ -33,7 +39,43 @@ public class Program
|
||||
|
||||
builder.Services.AddAuthorizationBuilder()
|
||||
.AddPolicy("AdminOnly", p => p.RequireRole("Admin"));
|
||||
//rate limiting for messages
|
||||
builder.Services.AddRateLimiter(o =>
|
||||
{
|
||||
o.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
|
||||
|
||||
o.OnRejected = (ctx, _) =>
|
||||
{
|
||||
var http = ctx.HttpContext;
|
||||
if (http.Request.HasFormContentType)
|
||||
{
|
||||
var referer = http.Request.Headers.Referer.ToString();
|
||||
var path = Uri.TryCreate(referer, UriKind.Absolute, out var uri)
|
||||
&& uri.Host == http.Request.Host.Host ? uri.AbsolutePath : "/";
|
||||
http.Response.Redirect(path + "?wait=1");
|
||||
}
|
||||
return ValueTask.CompletedTask;
|
||||
};
|
||||
|
||||
o.AddPolicy("contact", ctx => // ← the policy [EnableRateLimiting("contact")] uses
|
||||
{
|
||||
var userId = ctx.User.FindFirstValue(ClaimTypes.NameIdentifier);
|
||||
var key = userId is not null ? "user:" + userId : "ip:" + ctx.Connection.RemoteIpAddress;
|
||||
|
||||
|
||||
return RateLimitPartition.GetFixedWindowLimiter(key, _ => new FixedWindowRateLimiterOptions
|
||||
{
|
||||
PermitLimit = 1,
|
||||
Window = TimeSpan.FromSeconds(messageRateLimitSeconds),
|
||||
QueueLimit = 0
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddConsole(o => o.FormatterName = "color")
|
||||
.AddConsoleFormatter<Logger, ConsoleFormatterOptions>();
|
||||
|
||||
Console.WriteLine($"Database connection string: {databaseConnectionString}");
|
||||
|
||||
var app = builder.Build();
|
||||
@@ -104,6 +146,8 @@ public class Program
|
||||
app.MapStaticAssets();
|
||||
app.MapGet("/_res", () => string.Join("\n",
|
||||
typeof(StatiKosev.StringValues).Assembly.GetManifestResourceNames()));
|
||||
app.Logger.LogInformation("Configured message rate limit secs==" + messageRateLimitSeconds);
|
||||
app.Logger.LogInformation("Application started!");
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user