Browse Source

feat: 添加ip解析

main
HanPaoPao 6 months ago
parent
commit
7f2c339b05
  1. 31
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/RealIpHttpContextWebClientInfoProvider.cs
  2. 5
      aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/AuditLogs/AbpProAuditLogInfoToAuditLogConverter.cs
  3. 3
      templates/pro-nuget/service/Directory.Build.targets

31
aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/RealIpHttpContextWebClientInfoProvider.cs

@ -1,5 +1,6 @@
using JetBrains.Annotations; using JetBrains.Annotations;
using MyCSharp.HttpUserAgentParser.Providers; using MyCSharp.HttpUserAgentParser.Providers;
using System.Net;
using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.AspNetCore.WebClientInfo;
namespace Lion.AbpPro.AspNetCore; namespace Lion.AbpPro.AspNetCore;
@ -11,7 +12,6 @@ public class RealIpHttpContextWebClientInfoProvider : HttpContextWebClientInfoPr
{ {
private const string XForwardedForHeader = "X-Forwarded-For"; private const string XForwardedForHeader = "X-Forwarded-For";
public RealIpHttpContextWebClientInfoProvider( public RealIpHttpContextWebClientInfoProvider(
ILogger<HttpContextWebClientInfoProvider> logger, ILogger<HttpContextWebClientInfoProvider> logger,
IHttpContextAccessor httpContextAccessor, IHttpContextAccessor httpContextAccessor,
@ -30,22 +30,35 @@ public class RealIpHttpContextWebClientInfoProvider : HttpContextWebClientInfoPr
{ {
var httpContext = HttpContextAccessor.HttpContext; var httpContext = HttpContextAccessor.HttpContext;
if (httpContext == null) if (httpContext == null)
{
return 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; // 2. 取不到就用默认 RemoteIpAddress
if (headers != null && headers.ContainsKey(XForwardedForHeader)) 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 if (ipAddress.IsIPv4MappedToIPv6)
var forwardedIp = headers[XForwardedForHeader].FirstOrDefault();
if (!string.IsNullOrEmpty(forwardedIp))
{ {
httpContext.Connection.RemoteIpAddress = IPAddress.Parse(forwardedIp); realIp = ipAddress.MapToIPv4().ToString();
} }
} }
return httpContext.Connection?.RemoteIpAddress?.ToString(); return realIp;
} }
catch (Exception ex) catch (Exception ex)
{ {

5
aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/AuditLogs/AbpProAuditLogInfoToAuditLogConverter.cs

@ -54,10 +54,11 @@ public class AbpProAuditLogInfoToAuditLogConverter : AuditLogInfoToAuditLogConve
} }
catch 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})");
} }
/// <summary> /// <summary>

3
templates/pro-nuget/service/Directory.Build.targets

@ -118,5 +118,8 @@
<PackageReference Update="nacos-sdk-csharp.AspNetCore" Version="1.3.10" /> <PackageReference Update="nacos-sdk-csharp.AspNetCore" Version="1.3.10" />
<PackageReference Update="nacos-sdk-csharp.Extensions.Configuration" Version="1.3.10" /> <PackageReference Update="nacos-sdk-csharp.Extensions.Configuration" Version="1.3.10" />
<PackageReference Update="IPTools.China" Version="1.6.0" />
<PackageReference Update="UAParser" Version="3.1.47" />
</ItemGroup> </ItemGroup>
</Project> </Project>
Loading…
Cancel
Save