From 666f9c5b3dc97c1a5eff645e4b8df2521f0bd80d Mon Sep 17 00:00:00 2001 From: Alex Macocian Date: Tue, 13 Aug 2024 15:23:53 +0200 Subject: [PATCH] Reorder ip parsing logic --- .../Middleware/IPExtractingMiddleware.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/GuildWarsPartySearch/Middleware/IPExtractingMiddleware.cs b/GuildWarsPartySearch/Middleware/IPExtractingMiddleware.cs index 4746cca..6cf6638 100644 --- a/GuildWarsPartySearch/Middleware/IPExtractingMiddleware.cs +++ b/GuildWarsPartySearch/Middleware/IPExtractingMiddleware.cs @@ -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; }