11 changed files with 161 additions and 4 deletions
@ -0,0 +1,56 @@ |
|||
using JetBrains.Annotations; |
|||
using MyCSharp.HttpUserAgentParser.Providers; |
|||
using Volo.Abp.AspNetCore.WebClientInfo; |
|||
|
|||
namespace Lion.AbpPro.AspNetCore; |
|||
|
|||
/// <summary>
|
|||
/// 真实IP地址提供程序,支持代理服务器场景
|
|||
/// </summary>
|
|||
public class RealIpHttpContextWebClientInfoProvider : HttpContextWebClientInfoProvider |
|||
{ |
|||
private const string XForwardedForHeader = "X-Forwarded-For"; |
|||
|
|||
|
|||
public RealIpHttpContextWebClientInfoProvider( |
|||
ILogger<HttpContextWebClientInfoProvider> logger, |
|||
IHttpContextAccessor httpContextAccessor, |
|||
IHttpUserAgentParserProvider httpUserAgentParser) |
|||
: base(logger, httpContextAccessor, httpUserAgentParser) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取客户端IP地址,优先从X-Forwarded-For头部获取
|
|||
/// </summary>
|
|||
/// <returns>客户端IP地址</returns>
|
|||
protected override string GetClientIpAddress() |
|||
{ |
|||
try |
|||
{ |
|||
var httpContext = HttpContextAccessor.HttpContext; |
|||
if (httpContext == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
var headers = httpContext.Request?.Headers; |
|||
if (headers != null && headers.ContainsKey(XForwardedForHeader)) |
|||
{ |
|||
// 从X-Forwarded-For获取真实客户端IP
|
|||
var forwardedIp = headers[XForwardedForHeader].FirstOrDefault(); |
|||
if (!string.IsNullOrEmpty(forwardedIp)) |
|||
{ |
|||
httpContext.Connection.RemoteIpAddress = IPAddress.Parse(forwardedIp); |
|||
} |
|||
} |
|||
|
|||
return httpContext.Connection?.RemoteIpAddress?.ToString(); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
Logger.LogWarning(ex, "获取客户端IP地址时发生异常"); |
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,84 @@ |
|||
using IPTools.Core; |
|||
using Microsoft.Extensions.Options; |
|||
using UAParser; |
|||
using Volo.Abp.AspNetCore.ExceptionHandling; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Guids; |
|||
using Volo.Abp.Json; |
|||
|
|||
namespace Lion.AbpPro.BasicManagement.AuditLogs; |
|||
|
|||
[Dependency(ReplaceServices = true)] |
|||
[ExposeServices(typeof(IAuditLogInfoToAuditLogConverter))] |
|||
public class AbpProAuditLogInfoToAuditLogConverter : AuditLogInfoToAuditLogConverter |
|||
{ |
|||
public AbpProAuditLogInfoToAuditLogConverter( |
|||
IGuidGenerator guidGenerator, |
|||
IExceptionToErrorInfoConverter exceptionToErrorInfoConverter, |
|||
IJsonSerializer jsonSerializer, |
|||
IOptions<AbpExceptionHandlingOptions> exceptionHandlingOptions, |
|||
AuditLogEntityTypeFullNameConverter auditLogEntityTypeFullNameConverter) |
|||
: base( |
|||
guidGenerator, |
|||
exceptionToErrorInfoConverter, |
|||
jsonSerializer, |
|||
exceptionHandlingOptions, |
|||
auditLogEntityTypeFullNameConverter) |
|||
{ |
|||
} |
|||
|
|||
public override Task<AuditLog> ConvertAsync(AuditLogInfo auditLogInfo) |
|||
{ |
|||
auditLogInfo.ClientIpAddress = GetClientIpAddress(auditLogInfo); |
|||
auditLogInfo.BrowserInfo = GetBrowserInfo(auditLogInfo); |
|||
return base.ConvertAsync(auditLogInfo); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取客户端IP地址
|
|||
/// </summary>
|
|||
/// <param name="auditLogInfo">审计日志信息</param>
|
|||
/// <returns>IP地址</returns>
|
|||
protected virtual string GetClientIpAddress(AuditLogInfo auditLogInfo) |
|||
{ |
|||
var ipAddr = auditLogInfo.ClientIpAddress; |
|||
IpInfo location; |
|||
if (auditLogInfo.ClientIpAddress == "127.0.0.1" || auditLogInfo.ClientIpAddress?.ToLower() == "localhost") |
|||
{ |
|||
return "本地-本机"; |
|||
} |
|||
|
|||
try |
|||
{ |
|||
location = IpTool.Search(ipAddr); |
|||
} |
|||
catch |
|||
{ |
|||
location = new IpInfo() { Province = ipAddr, City = "未知地区" }; |
|||
} |
|||
|
|||
return location.Province + "-" + location.City; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取浏览器信息
|
|||
/// </summary>
|
|||
/// <param name="auditLogInfo">审计日志信息</param>
|
|||
/// <returns>浏览器信息</returns>
|
|||
protected virtual string GetBrowserInfo(AuditLogInfo auditLogInfo) |
|||
{ |
|||
try |
|||
{ |
|||
if (auditLogInfo.BrowserInfo.IsNotNullOrWhiteSpace()) |
|||
{ |
|||
var uaParser = Parser.GetDefault(); |
|||
return uaParser.Parse(auditLogInfo.BrowserInfo).UA.ToString(); |
|||
} |
|||
} |
|||
catch |
|||
{ |
|||
return "Other"; |
|||
} |
|||
return auditLogInfo.BrowserInfo; |
|||
} |
|||
} |
|||
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue