Skip to content

Commit 5275c57

Browse files
authored
fix rate limits (#57)
* ensure Fly proxy headers are correctly processed * apply rate limits after forwarded headers
1 parent bcacb32 commit 5275c57

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

fly.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ kill_signal = 'SIGTERM'
1919
auto_stop_machines = true
2020
auto_start_machines = true
2121
min_machines_running = 0
22+
max_machines_running = 1
2223
processes = ['app']
2324

2425
[http_service.concurrency]
@@ -27,8 +28,8 @@ kill_signal = 'SIGTERM'
2728
soft_limit = 1000
2829

2930
[[http_service.checks]]
30-
grace_period = "2s"
31-
interval = "120s"
31+
grace_period = "10s"
32+
interval = "60s"
3233
method = "GET"
3334
timeout = "10s"
3435
path = "/raw/api/WarSeason/current/WarID"

src/Helldivers-2-API/Program.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
using Helldivers.Sync.Configuration;
88
using Helldivers.Sync.Extensions;
99
using Microsoft.AspNetCore.Http.Timeouts;
10+
using Microsoft.AspNetCore.HttpOverrides;
1011
using Microsoft.AspNetCore.Localization;
1112
using System.Globalization;
13+
using System.Net;
1214
using System.Text.Json.Serialization;
15+
using IPNetwork = Microsoft.AspNetCore.HttpOverrides.IPNetwork;
1316

1417
#if DEBUG
1518
// When generating an OpenAPI document, get-document runs with the "--applicationName" flag.
@@ -66,7 +69,14 @@
6669
});
6770

6871
// Add and configure forwarded headers middleware
69-
builder.Services.Configure<ForwardedHeadersOptions>(_ => { });
72+
builder.Services.Configure<ForwardedHeadersOptions>(options =>
73+
{
74+
options.ForwardLimit = 999;
75+
options.OriginalForHeaderName = "Fly-Client-IP";
76+
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor;
77+
options.KnownNetworks.Add(new IPNetwork(IPAddress.Any, 0));
78+
options.KnownNetworks.Add(new IPNetwork(IPAddress.IPv6Any, 0));
79+
});
7080

7181
// This configuration is bound here so that source generators kick in.
7282
builder.Services.Configure<HelldiversSyncConfiguration>(builder.Configuration.GetSection("Helldivers:Synchronization"));
@@ -144,12 +154,12 @@
144154
// Ensure web applications can access the API by setting CORS headers.
145155
app.UseCors();
146156

147-
// Handles rate limiting so everyone plays nice
148-
app.UseMiddleware<RateLimitMiddleware>();
149-
150157
// Make sure ASP.NET Core uses the correct addresses internally rather than Fly's proxy
151158
app.UseForwardedHeaders();
152159

160+
// Handles rate limiting so everyone plays nice
161+
app.UseMiddleware<RateLimitMiddleware>();
162+
153163
// Add middleware to timeout requests if they take too long.
154164
app.UseRequestTimeouts();
155165

0 commit comments

Comments
 (0)