1
0
懒得勤快 5 жил өмнө
parent
commit
84a67cb402

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


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


+ 10 - 4
src/Masuit.MyBlogs.Core/Common/CommonHelper.cs

@@ -2,6 +2,7 @@
 using Hangfire;
 using HtmlAgilityPack;
 using IP2Region;
+using Masuit.MyBlogs.Core.Common.Mails;
 using Masuit.Tools;
 using Masuit.Tools.Media;
 using MaxMind.GeoIP2;
@@ -19,7 +20,6 @@ using System.Net;
 using System.Net.Sockets;
 using System.Text;
 using System.Threading;
-using Masuit.MyBlogs.Core.Common.Mails;
 using TimeZoneConverter;
 
 #if !DEBUG
@@ -140,6 +140,7 @@ namespace Masuit.MyBlogs.Core.Common
 
         private static readonly DbSearcher IPSearcher = new DbSearcher(Path.Combine(AppContext.BaseDirectory + "App_Data", "ip2region.db"));
         public static readonly DatabaseReader MaxmindReader = new DatabaseReader(Path.Combine(AppContext.BaseDirectory + "App_Data", "GeoLite2-City.mmdb"));
+        public static readonly DatabaseReader MaxmindAsnReader = new DatabaseReader(Path.Combine(AppContext.BaseDirectory + "App_Data", "GeoLite2-ASN.mmdb"));
 
         public static string GetIPLocation(this string ips)
         {
@@ -157,19 +158,24 @@ namespace Masuit.MyBlogs.Core.Common
                 case AddressFamily.InterNetwork when ip.IsPrivateIP():
                 case AddressFamily.InterNetworkV6 when ip.IsPrivateIP():
                     return ("内网", "内网IP");
+                case AddressFamily.InterNetworkV6 when ip.IsIPv4MappedToIPv6:
+                    ip = ip.MapToIPv4();
+                    goto case AddressFamily.InterNetwork;
                 case AddressFamily.InterNetwork:
                     var parts = IPSearcher.MemorySearch(ip.ToString())?.Region.Split('|');
                     if (parts != null)
                     {
-                        var network = parts[^1] == "0" ? "未知" : parts[^1];
+                        var asn = MaxmindAsnReader.Asn(ip);
+                        var network = parts[^1] == "0" ? asn.AutonomousSystemOrganization : parts[^1];
                         var location = parts[..^1].Where(s => s != "0").Distinct().Join("");
-                        return (location, network);
+                        return (location, network + $"(AS{asn.AutonomousSystemNumber})");
                     }
 
                     goto default;
                 default:
                     var response = MaxmindReader.City(ip);
-                    return (response.Country.Names.GetValueOrDefault("zh-CN") + response.City.Names.GetValueOrDefault("zh-CN"), "未知");
+                    var asnResp = MaxmindAsnReader.Asn(ip);
+                    return (response.Country.Names.GetValueOrDefault("zh-CN") + response.City.Names.GetValueOrDefault("zh-CN"), asnResp.AutonomousSystemOrganization + $"(AS{asnResp.AutonomousSystemNumber})");
             }
         }
 

+ 0 - 2
src/Masuit.MyBlogs.Core/Controllers/SubscribeController.cs

@@ -1,6 +1,5 @@
 using Masuit.MyBlogs.Core.Common;
 using Masuit.MyBlogs.Core.Extensions;
-using Masuit.MyBlogs.Core.Extensions.Firewall;
 using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
 using Masuit.MyBlogs.Core.Models.Enum;
 using Masuit.MyBlogs.Core.Models.ViewModel;
@@ -21,7 +20,6 @@ namespace Masuit.MyBlogs.Core.Controllers
     /// <summary>
     /// 订阅服务
     /// </summary>
-    [ServiceFilter(typeof(FirewallAttribute))]
     public class SubscribeController : Controller
     {
         /// <summary>

+ 7 - 0
src/Masuit.MyBlogs.Core/Extensions/Firewall/FirewallAttribute.cs

@@ -86,7 +86,14 @@ namespace Masuit.MyBlogs.Core.Extensions.Firewall
             if (times > limit * 1.2)
             {
                 CacheManager.Expire("Frequency:" + ip, ExpirationMode.Sliding, TimeSpan.FromMinutes(CommonHelper.SystemSettings.GetOrAdd("BanIPTimespan", "10").ToInt32()));
+                AccessDeny(ip, request, "访问频次限制");
+                return;
+            }
+
+            if (times > limit * 1.5)
+            {
                 FirewallRepoter.ReportAsync(IPAddress.Parse(ip)).ContinueWith(_ => AccessDeny(ip, request, "访问频次限制,已上报至:" + FirewallRepoter.ReporterName)).ConfigureAwait(false);
+                return;
             }
 
             throw new TempDenyException("访问地区限制");

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

@@ -82,6 +82,9 @@
         <None Update="App_Data\DenyIPRange.txt">
             <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
         </None>
+        <None Update="App_Data\GeoLite2-ASN.mmdb">
+          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+        </None>
         <None Update="App_Data\GeoLite2-City.mmdb">
           <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
         </None>

+ 1 - 1
src/Masuit.MyBlogs.Core/wwwroot/ng-views/views/post/edit.html

@@ -112,7 +112,7 @@
             </label>
         </span>
         <div class="fg-line" ng-if="post.LimitMode==1||post.LimitMode==2">
-            <input type="text" class="form-control" placeholder="江苏,移动" ng-model="post.Regions">
+            <input type="text" class="form-control" placeholder="江苏,苏州,移动,AS2333,DMIT,支持地区、运营商、ASN、机房名称" ng-model="post.Regions">
         </div>
     </div>
 </form>

+ 1 - 1
src/Masuit.MyBlogs.Core/wwwroot/ng-views/views/post/writeblog.html

@@ -137,7 +137,7 @@
             </label>
         </span>
         <div class="fg-line" ng-if="post.LimitMode==1||post.LimitMode==2">
-            <input type="text" class="form-control" placeholder="江苏,移动" ng-model="post.Regions">
+            <input type="text" class="form-control" placeholder="江苏,苏州,移动,AS2333,DMIT,支持地区、运营商、ASN、机房名称" ng-model="post.Regions">
         </div>
     </div>
 </form>

+ 1 - 1
src/Masuit.MyBlogs.Core/wwwroot/ng-views/views/system/firewall.html

@@ -53,7 +53,7 @@
                     <div class="input-group">
                         <span class="input-group-addon">受限制的地区或运营商:</span>
                         <div class="fg-line">
-                            <input class="form-control" ng-model="Settings.DenyArea" placeholder="禁止访问的地区,逗号分隔" type="text" />
+                            <input class="form-control" ng-model="Settings.DenyArea" placeholder="禁止访问的地区,逗号分隔,如:江苏,苏州,移动,AS2333,DMIT,支持地区、运营商、ASN、机房名称" type="text" />
                         </div>
                     </div>
                 </div>