懒得勤快 5 years ago
parent
commit
e585455612

+ 1 - 1
src/Masuit.MyBlogs.Core/Common/CommonHelper.cs

@@ -173,7 +173,7 @@ namespace Masuit.MyBlogs.Core.Common
 
 
         public static string GetIPLocation(this string ips)
         public static string GetIPLocation(this string ips)
         {
         {
-            return ips.Split(',').Select(s => Searcher.MemorySearch(IPAddress.Parse(s).MapToIPv4().ToString()).Region).Join(" , ");
+            return ips.Split(',').Select(s => Searcher.MemorySearch(s).Region).Join(" , ");
         }
         }
 
 
         /// <summary>
         /// <summary>

+ 1 - 1
src/Masuit.MyBlogs.Core/Controllers/BaseController.cs

@@ -46,7 +46,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <summary>
         /// <summary>
         /// 客户端的真实IP
         /// 客户端的真实IP
         /// </summary>
         /// </summary>
-        public string ClientIP => string.IsNullOrEmpty(Request.Headers["X-Forwarded-For"]) ? HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : Request.Headers["X-Forwarded-For"].ToString();
+        public string ClientIP => HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
 
 
         public IMapper Mapper { get; set; }
         public IMapper Mapper { get; set; }
         public MapperConfiguration MapperConfig { get; set; }
         public MapperConfiguration MapperConfig { get; set; }

+ 1 - 1
src/Masuit.MyBlogs.Core/Controllers/PassportController.cs

@@ -31,7 +31,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <summary>
         /// <summary>
         /// 客户端的真实IP
         /// 客户端的真实IP
         /// </summary>
         /// </summary>
-        public string ClientIP => string.IsNullOrEmpty(Request.Headers["X-Forwarded-For"]) ? HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : Request.Headers["X-Forwarded-For"].ToString();
+        public string ClientIP => HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
 
 
         /// <summary>
         /// <summary>
         /// 
         /// 

+ 1 - 1
src/Masuit.MyBlogs.Core/Extensions/FirewallAttribute.cs

@@ -26,7 +26,7 @@ namespace Masuit.MyBlogs.Core.Extensions
         public override void OnActionExecuting(ActionExecutingContext context)
         public override void OnActionExecuting(ActionExecutingContext context)
         {
         {
             var request = context.HttpContext.Request;
             var request = context.HttpContext.Request;
-            var ip = string.IsNullOrEmpty(context.HttpContext.Request.Headers["X-Forwarded-For"]) ? context.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : context.HttpContext.Request.Headers["X-Forwarded-For"].ToString();
+            var ip = context.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
             var sessionToken = context.HttpContext.Session.Get<string>("FullAccessViewToken");
             var sessionToken = context.HttpContext.Session.Get<string>("FullAccessViewToken");
             if (ip.IsDenyIpAddress() && string.IsNullOrEmpty(sessionToken))
             if (ip.IsDenyIpAddress() && string.IsNullOrEmpty(sessionToken))
             {
             {

+ 1 - 1
src/Masuit.MyBlogs.Core/Extensions/MyExceptionFilter.cs

@@ -15,7 +15,7 @@ namespace Masuit.MyBlogs.Core.Extensions
             base.OnException(context);
             base.OnException(context);
             string err;
             string err;
             var req = context.HttpContext.Request;
             var req = context.HttpContext.Request;
-            var ip = string.IsNullOrEmpty(req.Headers["X-Forwarded-For"]) ? context.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : req.Headers["X-Forwarded-For"].ToString();
+            var ip = context.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
             switch (context.Exception)
             switch (context.Exception)
             {
             {
                 case DbUpdateConcurrencyException ex:
                 case DbUpdateConcurrencyException ex:

+ 9 - 1
src/Masuit.MyBlogs.Core/Startup.cs

@@ -22,6 +22,7 @@ using Masuit.Tools.Systems;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Http.Features;
 using Microsoft.AspNetCore.Http.Features;
+using Microsoft.AspNetCore.HttpOverrides;
 using Microsoft.AspNetCore.Rewrite;
 using Microsoft.AspNetCore.Rewrite;
 using Microsoft.AspNetCore.StaticFiles;
 using Microsoft.AspNetCore.StaticFiles;
 using Microsoft.AspNetCore.WebSockets;
 using Microsoft.AspNetCore.WebSockets;
@@ -108,7 +109,13 @@ namespace Masuit.MyBlogs.Core
             services.AddHttpClient("", c => c.Timeout = TimeSpan.FromSeconds(30)); //注入HttpClient
             services.AddHttpClient("", c => c.Timeout = TimeSpan.FromSeconds(30)); //注入HttpClient
             services.AddTransient<ImagebedClient>();
             services.AddTransient<ImagebedClient>();
             services.AddHttpContextAccessor(); //注入静态HttpContext
             services.AddHttpContextAccessor(); //注入静态HttpContext
-            services.AddMapper().AddAutofac().AddMyMvc();
+            services.AddMapper().AddAutofac().AddMyMvc().Configure<ForwardedHeadersOptions>(options => // X-Forwarded-For
+            {
+                options.ForwardLimit = null;
+                options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
+                options.KnownNetworks.Clear();
+                options.KnownProxies.Clear();
+            });
         }
         }
 
 
         public void ConfigureContainer(ContainerBuilder builder)
         public void ConfigureContainer(ContainerBuilder builder)
@@ -126,6 +133,7 @@ namespace Masuit.MyBlogs.Core
         /// <param name="luceneIndexerOptions"></param>
         /// <param name="luceneIndexerOptions"></param>
         public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext db, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions)
         public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext db, IHangfireBackJob hangfire, LuceneIndexerOptions luceneIndexerOptions)
         {
         {
+            app.UseForwardedHeaders().UseCertificateForwarding(); // X-Forwarded-For
             ServiceProvider = app.ApplicationServices;
             ServiceProvider = app.ApplicationServices;
             if (env.IsDevelopment())
             if (env.IsDevelopment())
             {
             {