diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/RealIpHttpContextWebClientInfoProvider.cs b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/RealIpHttpContextWebClientInfoProvider.cs index 572caedb..7453b4a4 100644 --- a/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/RealIpHttpContextWebClientInfoProvider.cs +++ b/aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/RealIpHttpContextWebClientInfoProvider.cs @@ -1,5 +1,6 @@ using JetBrains.Annotations; using MyCSharp.HttpUserAgentParser.Providers; +using System.Net; using Volo.Abp.AspNetCore.WebClientInfo; namespace Lion.AbpPro.AspNetCore; @@ -11,7 +12,6 @@ public class RealIpHttpContextWebClientInfoProvider : HttpContextWebClientInfoPr { private const string XForwardedForHeader = "X-Forwarded-For"; - public RealIpHttpContextWebClientInfoProvider( ILogger logger, IHttpContextAccessor httpContextAccessor, @@ -30,22 +30,35 @@ public class RealIpHttpContextWebClientInfoProvider : HttpContextWebClientInfoPr { var httpContext = HttpContextAccessor.HttpContext; if (httpContext == null) - { return null; + + string realIp = null; + + // 1. 优先从 X-Forwarded-For 获取真实IP + if (httpContext.Request.Headers.TryGetValue(XForwardedForHeader, out var forwardedIps)) + { + realIp = forwardedIps.FirstOrDefault()?.Split(',', StringSplitOptions.RemoveEmptyEntries).FirstOrDefault()?.Trim(); } - var headers = httpContext.Request?.Headers; - if (headers != null && headers.ContainsKey(XForwardedForHeader)) + // 2. 取不到就用默认 RemoteIpAddress + if (string.IsNullOrEmpty(realIp)) + { + realIp = httpContext.Connection.RemoteIpAddress?.ToString(); + } + + if (string.IsNullOrEmpty(realIp)) + return null; + + // 3. 关键:处理 ::ffff:192.168.1.1 这种格式 + if (IPAddress.TryParse(realIp, out var ipAddress)) { - // 从X-Forwarded-For获取真实客户端IP - var forwardedIp = headers[XForwardedForHeader].FirstOrDefault(); - if (!string.IsNullOrEmpty(forwardedIp)) + if (ipAddress.IsIPv4MappedToIPv6) { - httpContext.Connection.RemoteIpAddress = IPAddress.Parse(forwardedIp); + realIp = ipAddress.MapToIPv4().ToString(); } } - return httpContext.Connection?.RemoteIpAddress?.ToString(); + return realIp; } catch (Exception ex) { diff --git a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/AuditLogs/AbpProAuditLogInfoToAuditLogConverter.cs b/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/AuditLogs/AbpProAuditLogInfoToAuditLogConverter.cs index 12f491b6..ba314423 100644 --- a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/AuditLogs/AbpProAuditLogInfoToAuditLogConverter.cs +++ b/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/AuditLogs/AbpProAuditLogInfoToAuditLogConverter.cs @@ -54,10 +54,11 @@ public class AbpProAuditLogInfoToAuditLogConverter : AuditLogInfoToAuditLogConve } catch { - location = new IpInfo() { Province = ipAddr, City = "未知地区" }; + location = new IpInfo() { IpAddress = ipAddr, Province = string.Empty, City = "未知地区" }; } - return location.Province + "-" + location.City; + + return string.Format($"{location.IpAddress}({location.Province}{location.City})"); } /// diff --git a/templates/pro-nuget/service/Directory.Build.targets b/templates/pro-nuget/service/Directory.Build.targets index a6aa15d3..b859d527 100644 --- a/templates/pro-nuget/service/Directory.Build.targets +++ b/templates/pro-nuget/service/Directory.Build.targets @@ -118,5 +118,8 @@ + + + \ No newline at end of file