This commit is contained in:
EonaCat 2023-01-15 21:16:25 +01:00
parent 4050af9902
commit bedd41f829
4 changed files with 71 additions and 15 deletions

View File

@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17 # Visual Studio Version 17
VisualStudioVersion = 17.4.33122.133 VisualStudioVersion = 17.4.33122.133
MinimumVisualStudioVersion = 10.0.40219.1 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{74BF09D7-E547-4221-9FBE-5F957A4D37D7}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
@ -11,6 +11,10 @@
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" /> <PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\EonaCat.Web.RateLimiter\EonaCat.Web.RateLimiter\EonaCat.Web.RateLimiter.csproj" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="EonaCat.Logger"> <Reference Include="EonaCat.Logger">
<HintPath>..\..\..\EonaCat.Logger\EonaCat.Logger\bin\Debug\netstandard2.0\EonaCat.Logger.dll</HintPath> <HintPath>..\..\..\EonaCat.Logger\EonaCat.Logger\bin\Debug\netstandard2.0\EonaCat.Logger.dll</HintPath>

View File

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace EonaCat.Logger.Web.Pages namespace EonaCat.Logger.Web.Pages
{ {

View File

@ -1,9 +1,8 @@
using EonaCat.Logger;
using EonaCat.Logger.Extensions;
using EonaCat.Logger.Managers; using EonaCat.Logger.Managers;
using EonaCat.Web.Tracer; using EonaCat.Web.RateLimiter;
using EonaCat.Web.RateLimiter.Endpoints.Extensions;
using EonaCat.Web.Tracer.Extensions; using EonaCat.Web.Tracer.Extensions;
using System.Diagnostics; using Microsoft.AspNetCore.Builder;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -15,6 +14,46 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages(); 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(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
@ -25,23 +64,31 @@ if (!app.Environment.IsDevelopment())
app.UseHsts(); 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; WebTracer.OnLog += WebTracer_OnLog;
void WebTracer_OnLog(object? sender, string e) void WebTracer_OnLog(object? sender, string e)
{ {
Console.WriteLine(e); Console.WriteLine(e);
} }
//app.UseWebTracer();
app.UseWebTracer();
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseStaticFiles(); app.UseStaticFiles();
app.UseRouting(); app.UseRouting();
app.UseAuthorization(); app.UseAuthorization();
app.UseWebRateLimiter();
app.MapRazorPages(); app.MapRazorPages().RateLimit();
void RunLoggingExceptionTests() void RunLoggingExceptionTests()
{ {
@ -90,7 +137,7 @@ void RunWebLoggingTests()
for (int i = 0; i < 9000000; i++) for (int i = 0; i < 9000000; i++)
{ {
app.Logger.LogInformation($"web-test {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}"); Console.WriteLine($"WebLogged: {i}");
Task.Delay(1); Task.Delay(1);
} }