From c45c17748e92a911fc29e9e4562dca00a5de071f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Mon, 21 Oct 2024 23:28:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E8=BD=AC=E5=8F=91ip=E5=8A=9F=E8=83=BD=E5=90=8E=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RealIpHttpContextWebClientInfoProvider.cs | 37 +++++++++++++++++++ .../YiFrameworkAspNetCoreModule.cs | 8 +++- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/RealIpHttpContextWebClientInfoProvider.cs diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/RealIpHttpContextWebClientInfoProvider.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/RealIpHttpContextWebClientInfoProvider.cs new file mode 100644 index 00000000..d00fd95c --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/RealIpHttpContextWebClientInfoProvider.cs @@ -0,0 +1,37 @@ +using System.Net; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using Volo.Abp.AspNetCore.WebClientInfo; + +namespace Yi.Framework.AspNetCore; + +public class RealIpHttpContextWebClientInfoProvider : HttpContextWebClientInfoProvider +{ + public RealIpHttpContextWebClientInfoProvider(ILogger logger, + IHttpContextAccessor httpContextAccessor) : base(logger, httpContextAccessor) + { + } + + protected override string? GetClientIpAddress() + { + try + { + var httpContext = HttpContextAccessor.HttpContext; + + var headers = httpContext?.Request?.Headers; + + if (headers != null && headers.ContainsKey("X-Forwarded-For")) + { + httpContext.Connection.RemoteIpAddress = + IPAddress.Parse(headers["X-Forwarded-For"].FirstOrDefault()); + } + + return httpContext?.Connection?.RemoteIpAddress?.ToString(); + } + catch (Exception ex) + { + Logger.LogException(ex, LogLevel.Warning); + return null; + } + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs index ad551e70..006a3763 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.AspNetCore/YiFrameworkAspNetCoreModule.cs @@ -11,6 +11,7 @@ using Newtonsoft.Json.Linq; using Swashbuckle.AspNetCore.SwaggerGen; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.AspNetCore.WebClientInfo; using Volo.Abp.DependencyInjection; using Volo.Abp.Modularity; using Yi.Framework.AspNetCore.Mvc; @@ -22,6 +23,11 @@ namespace Yi.Framework.AspNetCore )] public class YiFrameworkAspNetCoreModule : AbpModule { - + public override void PostConfigureServices(ServiceConfigurationContext context) + { + var services = context.Services; + services.Replace(new ServiceDescriptor(typeof(IWebClientInfoProvider), + typeof(RealIpHttpContextWebClientInfoProvider), ServiceLifetime.Transient)); + } } } \ No newline at end of file