Skip to content

Commit

Permalink
Reorder ip parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMacocian committed Aug 13, 2024
1 parent f94df5d commit 666f9c5
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions GuildWarsPartySearch/Middleware/IPExtractingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,27 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
var address = context.Connection.RemoteIpAddress?.ToString();
var scopedLogger = this.logger.CreateScopedLogger(nameof(this.InvokeAsync), address ?? string.Empty);
scopedLogger.LogDebug($"Received request");
if (context.Request.Headers.TryGetValue(XForwardedForHeaderKey, out var xForwardedForValues))

if (context.Request.Headers.TryGetValue(CFConnectingIPHeaderKey, out var cfConnectingIpValues))
{
scopedLogger.LogDebug($"X-Forwarded-For {string.Join(',', xForwardedForValues.Select(s => s))}");
scopedLogger.LogDebug($"CF-Connecting-IP {string.Join(',', cfConnectingIpValues.Select(s => s))}");
}

if (xForwardedForValues.FirstOrDefault() is string xForwardedIpAddress)
if (cfConnectingIpValues.FirstOrDefault() is string xCfIpAddress)
{
context.SetClientIP(xForwardedIpAddress);
context.SetClientIP(xCfIpAddress);
await next(context);
return;
}

if (context.Request.Headers.TryGetValue(CFConnectingIPHeaderKey, out var cfConnectingIpValues))
if (context.Request.Headers.TryGetValue(XForwardedForHeaderKey, out var xForwardedForValues))
{
scopedLogger.LogDebug($"CF-Connecting-IP {string.Join(',', cfConnectingIpValues.Select(s => s))}");
scopedLogger.LogDebug($"X-Forwarded-For {string.Join(',', xForwardedForValues.Select(s => s))}");
}

if (cfConnectingIpValues.FirstOrDefault() is string xCfIpAddress)
if (xForwardedForValues.FirstOrDefault() is string xForwardedIpAddress)
{
context.SetClientIP(xCfIpAddress);
context.SetClientIP(xForwardedIpAddress);
await next(context);
return;
}
Expand Down

0 comments on commit 666f9c5

Please sign in to comment.