From 6f52f75304e00b3cf2e2bb6949201a575479fb86 Mon Sep 17 00:00:00 2001 From: Folf Date: Mon, 6 Apr 2026 21:41:24 +0200 Subject: [PATCH] Add Feature_UseForwardedForAsClientIp --- DeveLanCacheUI_Backend/DeveLanCacheConfiguration.cs | 1 + .../LogReading/LanCacheLogReaderHostedService.cs | 12 +++++++++--- DeveLanCacheUI_Backend/appsettings.Development.json | 1 + DeveLanCacheUI_Backend/appsettings.json | 1 + README.md | 1 + 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/DeveLanCacheUI_Backend/DeveLanCacheConfiguration.cs b/DeveLanCacheUI_Backend/DeveLanCacheConfiguration.cs index 28ef3e3..9b68640 100644 --- a/DeveLanCacheUI_Backend/DeveLanCacheConfiguration.cs +++ b/DeveLanCacheUI_Backend/DeveLanCacheConfiguration.cs @@ -6,6 +6,7 @@ public class DeveLanCacheConfiguration public required string LanCacheLogsDirectory { get; set; } public required bool Feature_DirectSteamIntegration { get; set; } public required bool Feature_SkipLinesBasedOnBytesRead { get; set; } + public bool Feature_UseForwardedForAsClientIp { get; set; } // List of client IPs to exclude from statistics public string ExcludedClientIps { get; set; } diff --git a/DeveLanCacheUI_Backend/LogReading/LanCacheLogReaderHostedService.cs b/DeveLanCacheUI_Backend/LogReading/LanCacheLogReaderHostedService.cs index 1c24e89..35f60e3 100644 --- a/DeveLanCacheUI_Backend/LogReading/LanCacheLogReaderHostedService.cs +++ b/DeveLanCacheUI_Backend/LogReading/LanCacheLogReaderHostedService.cs @@ -132,7 +132,13 @@ await retryPolicy.ExecuteAsync(async () => _steamManifestService.TryToDownloadManifest(ttt); } - var cacheKey = $"{lanCacheLogLine.CacheIdentifier}_||_{lanCacheLogLine.DownloadIdentifier}_||_{lanCacheLogLine.RemoteAddress}"; + var clientIp = _deveLanCacheConfiguration.Feature_UseForwardedForAsClientIp + && !string.IsNullOrEmpty(lanCacheLogLine.ForwardedFor) + && lanCacheLogLine.ForwardedFor != "-" + ? lanCacheLogLine.ForwardedFor.Split(',')[0].Trim() + : lanCacheLogLine.RemoteAddress; + + var cacheKey = $"{lanCacheLogLine.CacheIdentifier}_||_{lanCacheLogLine.DownloadIdentifier}_||_{clientIp}"; steamAppDownloadEventsCache.TryGetValue(cacheKey, out var cachedEvent); if (cachedEvent == null) @@ -141,7 +147,7 @@ await retryPolicy.ExecuteAsync(async () => .FirstOrDefaultAsync(t => t.CacheIdentifier == lanCacheLogLine.CacheIdentifier && t.DownloadIdentifierString == lanCacheLogLine.DownloadIdentifier && - t.ClientIp == lanCacheLogLine.RemoteAddress && + t.ClientIp == clientIp && t.LastUpdatedAt > lanCacheLogLine.DateTime.AddMinutes(-5) ); if (cachedEvent != null) @@ -162,7 +168,7 @@ await retryPolicy.ExecuteAsync(async () => DownloadIdentifier = downloadIdentifierInt, CreatedAt = lanCacheLogLine.DateTime, LastUpdatedAt = lanCacheLogLine.DateTime, - ClientIp = lanCacheLogLine.RemoteAddress + ClientIp = clientIp }; steamAppDownloadEventsCache[cacheKey] = cachedEvent; await dbContext.DownloadEvents.AddAsync(cachedEvent); diff --git a/DeveLanCacheUI_Backend/appsettings.Development.json b/DeveLanCacheUI_Backend/appsettings.Development.json index 8739a29..db4dcaf 100644 --- a/DeveLanCacheUI_Backend/appsettings.Development.json +++ b/DeveLanCacheUI_Backend/appsettings.Development.json @@ -8,5 +8,6 @@ "LanCacheLogsDirectory": "\\\\10.88.20.1\\DockerComposers\\lancache\\logs", "Feature_DirectSteamIntegration": true, "Feature_SkipLinesBasedOnBytesRead": true, + "Feature_UseForwardedForAsClientIp": false, "ExcludedClientIps": "" } diff --git a/DeveLanCacheUI_Backend/appsettings.json b/DeveLanCacheUI_Backend/appsettings.json index 492bb5f..0ad229c 100644 --- a/DeveLanCacheUI_Backend/appsettings.json +++ b/DeveLanCacheUI_Backend/appsettings.json @@ -16,5 +16,6 @@ "LanCacheLogsDirectory": "/var/develancacheui/lancachelogs", "Feature_DirectSteamIntegration": false, "Feature_SkipLinesBasedOnBytesRead": false, + "Feature_UseForwardedForAsClientIp": false, "ExcludedClientIps": "" } diff --git a/README.md b/README.md index e145153..5c5351b 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,7 @@ This should now all works quite neatly, if it doesn't, let me know in a github i | LANG | Set this to your language | ?? | | Feature_DirectSteamIntegration | When false, the backend will download a .CSV file with all depot => steam game mappings (from: https://github.com/devedse/DeveLanCacheUI_SteamDepotFinder_Runner/releases). When true, the tool wil generate this itself / keep it up to date. I would suggest turning this on. | false (for now) | | Feature_SkipLinesBasedOnBytesRead | When false, it will re-read through the whole file on startup. When true, it tries to be smart and start reading from where it last left off. I would suggest turning this on. | false (for now) | +| Feature_UseForwardedForAsClientIp | When true, the X-Forwarded-For header is used as the client IP instead of the direct remote address. Useful when LanCache sits behind a reverse proxy. The first IP in the header is used. Falls back to RemoteAddress if the header is absent or `-`. | false | | ExcludedClientIps | A comma separated list of IP addresses to exclude in the downloads pages | | **Volume Mounts Backend**