Selaa lähdekoodia

IP地址信息支持IPv6检测

懒得勤快 5 vuotta sitten
vanhempi
sitoutus
9d4d11edc9

BIN
src/Masuit.MyBlogs.Core/App_Data/GeoLite2-City.mmdb


+ 14 - 3
src/Masuit.MyBlogs.Core/Common/CommonHelper.cs

@@ -5,6 +5,7 @@ using IP2Region;
 using Masuit.Tools;
 using Masuit.Tools.Media;
 using Masuit.Tools.Models;
+using MaxMind.GeoIP2;
 using Microsoft.AspNetCore.Http;
 using Microsoft.EntityFrameworkCore.Internal;
 using Microsoft.Extensions.DependencyInjection;
@@ -16,6 +17,7 @@ using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Net;
+using System.Net.Sockets;
 using System.Text;
 using System.Threading;
 
@@ -134,12 +136,21 @@ namespace Masuit.MyBlogs.Core.Common
             return false;
         }
 
-        private static readonly DbSearcher Searcher = new DbSearcher(Path.Combine(AppContext.BaseDirectory + "App_Data", "ip2region.db"));
-        public static string GetIPLocation(this IPAddress ip) => GetIPLocation(ip.MapToIPv4().ToString());
+        private static readonly DbSearcher IPSearcher = new DbSearcher(Path.Combine(AppContext.BaseDirectory + "App_Data", "ip2region.db"));
+        private static readonly DatabaseReader MaxmindReader = new DatabaseReader(Path.Combine(AppContext.BaseDirectory + "App_Data", "GeoLite2-City.mmdb"));
 
         public static string GetIPLocation(this string ips)
         {
-            return ips.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(s => Searcher.MemorySearch(s.Trim())?.Region).Join(" , ");
+            return ips.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(s =>
+            {
+                var ip = IPAddress.Parse(s.Trim());
+                if (ip.AddressFamily == AddressFamily.InterNetwork)
+                {
+                    return IPSearcher.MemorySearch(ip.ToString())?.Region;
+                }
+                var response = MaxmindReader.City(ip);
+                return response.Country.Names.GetValueOrDefault("zh-CN") + response.City.Names.GetValueOrDefault("zh-CN");
+            }).Join(" , ");
         }
 
         /// <summary>

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

@@ -49,7 +49,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <summary>
         /// 客户端的真实IP
         /// </summary>
-        public string ClientIP => HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
+        public string ClientIP => HttpContext.Connection.RemoteIpAddress.ToString();
 
         public IMapper Mapper { get; set; }
         public MapperConfiguration MapperConfig { get; set; }

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

@@ -55,7 +55,7 @@ namespace Masuit.MyBlogs.Core.Controllers
             {
                 string err;
                 var req = HttpContext.Request;
-                var ip = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
+                var ip = HttpContext.Connection.RemoteIpAddress.ToString();
                 switch (feature.Error)
                 {
                     case DbUpdateConcurrencyException ex:

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

@@ -31,7 +31,7 @@ namespace Masuit.MyBlogs.Core.Controllers
         /// <summary>
         /// 客户端的真实IP
         /// </summary>
-        public string ClientIP => HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
+        public string ClientIP => HttpContext.Connection.RemoteIpAddress.ToString();
 
         /// <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)
         {
             var request = context.HttpContext.Request;
-            var ip = context.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
+            var ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
             var trueip = request.Headers[AppConfig.TrueClientIPHeader].ToString();
             if (!string.IsNullOrEmpty(trueip) && ip != trueip)
             {

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

@@ -38,7 +38,7 @@ namespace Masuit.MyBlogs.Core.Extensions
             {
                 BackgroundJob.Enqueue(() => HangfireBackJob.InterceptLog(new IpIntercepter()
                 {
-                    IP = context.Connection.RemoteIpAddress.MapToIPv4().ToString(),
+                    IP = context.Connection.RemoteIpAddress.ToString(),
                     RequestUrl = requestUrl,
                     Time = DateTime.Now,
                     UserAgent = request.Headers[HeaderNames.UserAgent],

+ 4 - 0
src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj

@@ -35,6 +35,7 @@
         <PackageReference Include="htmldiff.net-core" Version="1.3.6" />
         <PackageReference Include="IP2Region" Version="1.2.0" />
         <PackageReference Include="Karambolo.AspNetCore.Bundling.NUglify" Version="3.1.0" />
+        <PackageReference Include="MaxMind.GeoIP2" Version="3.2.0" />
         <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.5" />
         <PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson" Version="3.1.5" />
         <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="3.1.5" />
@@ -70,6 +71,9 @@
         <None Update="App_Data\DenyIPRange.txt">
             <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
         </None>
+        <None Update="App_Data\GeoLite2-City.mmdb">
+          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+        </None>
         <None Update="App_Data\ip2region.db">
             <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
         </None>

+ 1 - 1
src/Masuit.MyBlogs.Core/Views/Error/TempDeny.cshtml

@@ -55,7 +55,7 @@
             <h3 class="margintop20">
                 <em>
                     <span class="STYLE1">Sorry!访问被拒绝&nbsp;</span>
-                </em>:检测到您的IP(@Context.Connection.RemoteIpAddress.MapToIPv4())访问过于频繁,已被本站暂时禁止访问,请稍后再试,如有疑问,请联系站长!
+                </em>:检测到您的IP(@Context.Connection.RemoteIpAddress)访问过于频繁,已被本站暂时禁止访问,请稍后再试,如有疑问,请联系站长!
             </h3>
         </div>
     </div>