diff --git a/EonaCat.Logger.Web/EonaCat.Logger.Web.sln b/EonaCat.Logger.Web/EonaCat.Logger.Web.sln index dad97e6..558621e 100644 --- a/EonaCat.Logger.Web/EonaCat.Logger.Web.sln +++ b/EonaCat.Logger.Web/EonaCat.Logger.Web.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33122.133 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EonaCat.Logger.Web", "EonaCat.Logger.Web\EonaCat.Logger.Web.csproj", "{74BF09D7-E547-4221-9FBE-5F957A4D37D7}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EonaCat.Logger.Web", "EonaCat.Logger.Web\EonaCat.Logger.Web.csproj", "{74BF09D7-E547-4221-9FBE-5F957A4D37D7}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EonaCat.Web.RateLimiter", "..\..\EonaCat.Web.RateLimiter\EonaCat.Web.RateLimiter\EonaCat.Web.RateLimiter.csproj", "{8558EBE9-4F3D-4942-806F-E0251D575AD1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {74BF09D7-E547-4221-9FBE-5F957A4D37D7}.Debug|Any CPU.Build.0 = Debug|Any CPU {74BF09D7-E547-4221-9FBE-5F957A4D37D7}.Release|Any CPU.ActiveCfg = Release|Any CPU {74BF09D7-E547-4221-9FBE-5F957A4D37D7}.Release|Any CPU.Build.0 = Release|Any CPU + {8558EBE9-4F3D-4942-806F-E0251D575AD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8558EBE9-4F3D-4942-806F-E0251D575AD1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8558EBE9-4F3D-4942-806F-E0251D575AD1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8558EBE9-4F3D-4942-806F-E0251D575AD1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/EonaCat.Logger.Web/EonaCat.Logger.Web/EonaCat.Logger.Web.csproj b/EonaCat.Logger.Web/EonaCat.Logger.Web/EonaCat.Logger.Web.csproj index 06b1c97..70a1dc2 100644 --- a/EonaCat.Logger.Web/EonaCat.Logger.Web/EonaCat.Logger.Web.csproj +++ b/EonaCat.Logger.Web/EonaCat.Logger.Web/EonaCat.Logger.Web.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0 enable enable @@ -11,6 +11,10 @@ + + + + ..\..\..\EonaCat.Logger\EonaCat.Logger\bin\Debug\netstandard2.0\EonaCat.Logger.dll diff --git a/EonaCat.Logger.Web/EonaCat.Logger.Web/Pages/Index.cshtml.cs b/EonaCat.Logger.Web/EonaCat.Logger.Web/Pages/Index.cshtml.cs index 226ac36..d23a77d 100644 --- a/EonaCat.Logger.Web/EonaCat.Logger.Web/Pages/Index.cshtml.cs +++ b/EonaCat.Logger.Web/EonaCat.Logger.Web/Pages/Index.cshtml.cs @@ -1,5 +1,4 @@ -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.AspNetCore.Mvc.RazorPages; namespace EonaCat.Logger.Web.Pages { diff --git a/EonaCat.Logger.Web/EonaCat.Logger.Web/Program.cs b/EonaCat.Logger.Web/EonaCat.Logger.Web/Program.cs index 0c066b3..eb03bf0 100644 --- a/EonaCat.Logger.Web/EonaCat.Logger.Web/Program.cs +++ b/EonaCat.Logger.Web/EonaCat.Logger.Web/Program.cs @@ -1,9 +1,8 @@ -using EonaCat.Logger; -using EonaCat.Logger.Extensions; using EonaCat.Logger.Managers; -using EonaCat.Web.Tracer; +using EonaCat.Web.RateLimiter; +using EonaCat.Web.RateLimiter.Endpoints.Extensions; using EonaCat.Web.Tracer.Extensions; -using System.Diagnostics; +using Microsoft.AspNetCore.Builder; var builder = WebApplication.CreateBuilder(args); @@ -15,6 +14,46 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); +//var rateLimitOptions = new RateLimitOptions +//{ +// Capacity = 10, +// Duration = TimeSpan.FromMinutes(1) +//}; + +//builder.Services.AddRateLimiting(rateLimitOptions); +var options = new RateLimiterOptions(); +options.OutputLimitMessageAsHtml = false; +options.OutputLimitMessageAsXml = false; +options.OutputLimitMessageAsJson = true; + +options.AddDefaultConfiguration(config => + config.AddIpResolver().AddRule(3, TimeSpan.FromSeconds(10)) +); + +RateLimiterMiddleware.OnLimitResponseCreated += RateLimiterMiddleware_OnLimitResponseCreatedAsync; +async void RateLimiterMiddleware_OnLimitResponseCreatedAsync(object? sender, HttpContext httpContext) +{ + await httpContext.Response.WriteAsync(" THIS IS MY CUSTOM RATE LIMIT MESSAGE"); +} + +//builder.Services.UseWebRateLimiter(options); +builder.Services.UseWebRateLimiter(options); +builder.Services.UseWebRateLimiter(options => +{ + // Configures the default rateLimitConfiguration + options.AddDefaultConfiguration(rateLimitConfiguration => + { + // throttling is based on request ip + rateLimitConfiguration.AddIpResolver() + // add general rules for all ips + .AddRule(3, TimeSpan.FromSeconds(10)) // 3 requests could be called every 10 seconds + .AddRule(30, TimeSpan.FromMinutes(1)) // 30 requests could be called every 1 minute + .AddRule(500, TimeSpan.FromHours(1)); // 500 requests could be called every 1 hour + }); +}); +builder.Services.AddMemoryCache(); + + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -25,23 +64,31 @@ if (!app.Environment.IsDevelopment()) app.UseHsts(); } +//EonaCat.Web.RateLimiter.RateLimiterOptions options = new EonaCat.Web.RateLimiter.RateLimiterOptions(); +//options.LimitMessage = "FUCKING NOOB!"; +//options.WriteLimitMessageToResponse = true; +//options.WriteLimitMessageStatusCodeToResponse = true; +//app.UseRateLimiter(options); +//RateLimiterMiddleware.OnLimitResponseCreated += RateLimiterMiddleware_OnLimitResponseCreatedAsync; +//async void RateLimiterMiddleware_OnLimitResponseCreatedAsync(object? sender, HttpContext httpContext) +//{ +// await httpContext.Response.WriteAsync("THIS IS MY CUSTOM RATE LIMIT MESSAGE"); +//} WebTracer.OnLog += WebTracer_OnLog; - void WebTracer_OnLog(object? sender, string e) { Console.WriteLine(e); } - -app.UseWebTracer(); +//app.UseWebTracer(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); - app.UseAuthorization(); +app.UseWebRateLimiter(); -app.MapRazorPages(); +app.MapRazorPages().RateLimit(); void RunLoggingExceptionTests() { @@ -56,7 +103,7 @@ void RunLoggingExceptionTests() } catch (Exception exception) { - logger.Write(exception); + logger.Write(exception); Console.WriteLine($"Normal ExceptionLogged: {i}"); } Task.Delay(1); @@ -90,7 +137,7 @@ void RunWebLoggingTests() for (int i = 0; i < 9000000; i++) { app.Logger.LogInformation($"web-test {i}"); - File.AppendAllText("C:\\workdir\\EonaCat.Testers\\EonaCat.Logger.Web\\EonaCat.Logger.Web\\bin\\Debug\\net6.0\\logs\\test.log",$"WebLogged: {i}{Environment.NewLine}"); + File.AppendAllText("C:\\workdir\\EonaCat.Testers\\EonaCat.Logger.Web\\EonaCat.Logger.Web\\bin\\Debug\\net6.0\\logs\\test.log", $"WebLogged: {i}{Environment.NewLine}"); Console.WriteLine($"WebLogged: {i}"); Task.Delay(1); }