Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements a blacklist for clients that violate ToS #100

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Helldivers-2-API/Configuration/ApiConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public sealed class ApiConfiguration
/// </summary>
public int RateLimitWindow { get; set; }

/// <summary>
/// A comma separated list of clients that are (temporarily) blacklisted from making requests.
/// </summary>
public string Blacklist { get; set; } = string.Empty;

/// <summary>
/// Contains the <see cref="AuthenticationConfiguration" /> for the API.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions src/Helldivers-2-API/Middlewares/BlacklistMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Helldivers.API.Configuration;
using Helldivers.API.Metrics;
using Microsoft.Extensions.Options;

namespace Helldivers.API.Middlewares;

/// <summary>
/// Handles closing connections from blacklisted clients that violate ToS.
/// </summary>
public sealed class BlacklistMiddleware(IOptions<ApiConfiguration> options) : IMiddleware
{
/// <inheritdoc />
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
var client = ClientMetric.GetClientName(context);
if (options.Value.Blacklist.Contains(client, StringComparison.InvariantCultureIgnoreCase))
{
// don't send response, only wastes more bytes.
context.Abort();
return;
}

await next(context);
}
}
2 changes: 2 additions & 0 deletions src/Helldivers-2-API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
// Register the rate limiting middleware.
builder.Services.AddTransient<RateLimitMiddleware>();
builder.Services.AddTransient<RedirectFlyDomainMiddleware>();
builder.Services.AddTransient<BlacklistMiddleware>();

// Register the memory cache, used in the rate limiting middleware.
builder.Services.AddMemoryCache();
Expand Down Expand Up @@ -186,6 +187,7 @@
var app = builder.Build();

app.UseMiddleware<RedirectFlyDomainMiddleware>();
app.UseMiddleware<BlacklistMiddleware>();

// Track telemetry for Prometheus (Fly.io metrics)
app.UseHttpMetrics(options =>
Expand Down
1 change: 1 addition & 0 deletions src/Helldivers-2-API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"API": {
"RateLimit": 5,
"RateLimitWindow": 10,
"Blacklist": "",
"Authentication": {
"Enabled": true,
"ValidIssuers": ["dealloc"],
Expand Down