diff --git a/MyApp.ServiceInterface/Data/StatUtils.cs b/MyApp.ServiceInterface/Data/StatUtils.cs index 4a134c1..e26bcc2 100644 --- a/MyApp.ServiceInterface/Data/StatUtils.cs +++ b/MyApp.ServiceInterface/Data/StatUtils.cs @@ -1,6 +1,7 @@ using System.Security.Claims; using Microsoft.AspNetCore.Http; using MyApp.ServiceModel; +using ServiceStack; namespace MyApp.Data; @@ -10,11 +11,23 @@ public static class StatUtils public static string? GetUserName(this ClaimsPrincipal? user) => user?.Identity?.Name; public static bool IsAdminOrModerator(this ClaimsPrincipal? user) => Stats.IsAdminOrModerator(user?.Identity?.Name); + public static string? GetRemoteIp(this HttpContext? ctx) + { + var headers = ctx?.Request.Headers; + if (headers == null) + return null; + return string.IsNullOrEmpty(headers[HttpHeaders.XForwardedFor]) + ? headers[HttpHeaders.XForwardedFor].ToString() + : string.IsNullOrEmpty(headers[HttpHeaders.XRealIp]) + ? headers[HttpHeaders.XForwardedFor].ToString() + : ctx?.Connection.RemoteIpAddress?.ToString(); + } + public static T WithRequest(this T stat, HttpContext? ctx) where T : StatBase { var user = ctx?.User; stat.UserName = user?.Identity?.Name; - stat.RemoteIp = ctx?.Connection.RemoteIpAddress?.ToString(); + stat.RemoteIp = ctx.GetRemoteIp(); stat.CreatedDate = DateTime.UtcNow; return stat; }