Browse Source

feat: 添加ip解析

main
HanPaoPao 6 months ago
parent
commit
1088ab9d32
  1. 2
      aspnet-core/Directory.Build.targets
  2. 8
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreModule.cs
  3. 56
      aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/RealIpHttpContextWebClientInfoProvider.cs
  4. 84
      aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/AuditLogs/AbpProAuditLogInfoToAuditLogConverter.cs
  5. 2
      aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Lion.AbpPro.BasicManagement.Domain.csproj
  6. 3
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Lion.AbpPro.HttpApi.Host.csproj
  7. 5
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Program.cs
  8. 2
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.Development.json
  9. BIN
      aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/ip2region.db
  10. 3
      templates/pro-nuget/service/host/MyCompanyName.MyProjectName.HttpApi.Host/MyCompanyName.MyProjectName.HttpApi.Host.csproj
  11. BIN
      templates/pro-nuget/service/host/MyCompanyName.MyProjectName.HttpApi.Host/ip2region.db

2
aspnet-core/Directory.Build.targets

@ -118,5 +118,7 @@
<PackageReference Update="nacos-sdk-csharp.AspNetCore" 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>
</Project>

8
aspnet-core/frameworks/src/Lion.AbpPro.AspNetCore/Lion/AbpPro/AspNetCore/AbpProAspNetCoreModule.cs

@ -1,6 +1,8 @@
using Lion.AbpPro.AspNetCore.Options;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.AspNetCore.MultiTenancy;
using Volo.Abp.AspNetCore.Mvc.UI.Theme.Basic;
using Volo.Abp.AspNetCore.WebClientInfo;
namespace Lion.AbpPro.AspNetCore;
@ -22,5 +24,11 @@ public class AbpProAspNetCoreModule : AbpModule
context.Services.Configure<AbpProJwtOptions>(context.Configuration.GetSection(AbpProAspNetCoreConsts.Jwt));
context.Services.Configure<AbpProCookieOptions>(context.Configuration.GetSection(AbpProAspNetCoreConsts.Cookie));
context.Services.Configure<AbpProAntiForgeryOptions>(context.Configuration.GetSection(AbpProAspNetCoreConsts.AntiForgery));
// 替换默认的WebClientInfoProvider为支持代理的实现
context.Services.Replace(new ServiceDescriptor(
typeof(IWebClientInfoProvider),
typeof(RealIpHttpContextWebClientInfoProvider),
ServiceLifetime.Transient));
}
}

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

@ -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;
}
}
}

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

@ -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;
}
}

2
aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Lion.AbpPro.BasicManagement.Domain.csproj

@ -14,6 +14,8 @@
<PackageReference Include="Volo.Abp.TenantManagement.Domain" />
<PackageReference Include="Volo.Abp.FeatureManagement.Domain" />
<PackageReference Include="Volo.Abp.SettingManagement.Domain" />
<PackageReference Include="IPTools.China" />
<PackageReference Include="UAParser" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Lion.AbpPro.BasicManagement.Domain.Shared\Lion.AbpPro.BasicManagement.Domain.Shared.csproj" />

3
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Lion.AbpPro.HttpApi.Host.csproj

@ -37,6 +37,9 @@
<Content Remove="Logs\**"/>
<EmbeddedResource Remove="Logs\**"/>
<None Remove="Logs\**"/>
<None Update="ip2region.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>

5
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/Program.cs

@ -11,7 +11,7 @@ public class Program
try
{
Log.Information("Lion.AbpPro.HttpApi.Host.");
Log.Information("Lion.AbpPro启动成功");
var builder = WebApplication.CreateBuilder(args);
builder.Host
.AddAppSettingsSecretsJson()
@ -30,12 +30,11 @@ public class Program
}
catch (Exception ex)
{
Log.Fatal(ex, "Lion.AbpPro启动失败");
if (ex is HostAbortedException)
{
throw;
}
Log.Fatal(ex, "Host terminated unexpectedly!");
return 1;
}
finally

2
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/appsettings.Development.json

@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"Default": "User ID=postgres;Password=1q2w3E*;Host=localhost;Port=5432;Database=LionAbpProDb;"
"Default": "User ID=abpprovben2;Password=rjrwHpkA3JKyS6S8;Host=139.155.114.244;Port=5432;Database=abpprovben2;"
},
"Hangfire": {
"Redis": {

BIN
aspnet-core/services/host/Lion.AbpPro.HttpApi.Host/ip2region.db

Binary file not shown.

3
templates/pro-nuget/service/host/MyCompanyName.MyProjectName.HttpApi.Host/MyCompanyName.MyProjectName.HttpApi.Host.csproj

@ -38,6 +38,9 @@
<Content Remove="Logs\**"/>
<EmbeddedResource Remove="Logs\**"/>
<None Remove="Logs\**"/>
<None Update="ip2region.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>

BIN
templates/pro-nuget/service/host/MyCompanyName.MyProjectName.HttpApi.Host/ip2region.db

Binary file not shown.
Loading…
Cancel
Save