Explorar el Código

增加爬虫检测接口

懒得勤快 hace 3 años
padre
commit
57b10c58fc

+ 6 - 0
src/Masuit.MyBlogs.Core/Common/CommonHelper.cs

@@ -18,6 +18,7 @@ using System.Drawing;
 using System.Net;
 using System.Net;
 using System.Net.Sockets;
 using System.Net.Sockets;
 using System.Text;
 using System.Text;
+using Masuit.Tools.Systems;
 using TimeZoneConverter;
 using TimeZoneConverter;
 
 
 namespace Masuit.MyBlogs.Core.Common
 namespace Masuit.MyBlogs.Core.Common
@@ -309,6 +310,11 @@ namespace Masuit.MyBlogs.Core.Common
                 a.SetStyle("position: absolute;color: transparent;z-index: -1");
                 a.SetStyle("position: absolute;color: transparent;z-index: -1");
                 a.TextContent = SystemSettings["Title"] + SystemSettings["Domain"];
                 a.TextContent = SystemSettings["Title"] + SystemSettings["Domain"];
                 el.InsertAfter(a);
                 el.InsertAfter(a);
+                var a2 = doc.CreateElement("a");
+                a2.SetAttribute("href", "/craw/" + SnowFlake.NewId);
+                a2.SetStyle("position: absolute;color: transparent;z-index: -1");
+                a2.TextContent = title;
+                a.InsertAfter(a2);
             }
             }
 
 
             return doc.Body.InnerHtml;
             return doc.Body.InnerHtml;

+ 42 - 2
src/Masuit.MyBlogs.Core/Controllers/FirewallController.cs

@@ -1,5 +1,11 @@
-using Masuit.MyBlogs.Core.Configs;
+using System.Net;
+using System.Web;
+using CacheManager.Core;
+using Masuit.MyBlogs.Core.Common;
+using Masuit.MyBlogs.Core.Configs;
+using Masuit.MyBlogs.Core.Extensions.Firewall;
 using Masuit.MyBlogs.Core.Models.ViewModel;
 using Masuit.MyBlogs.Core.Models.ViewModel;
+using Masuit.Tools;
 using Masuit.Tools.AspNetCore.Mime;
 using Masuit.Tools.AspNetCore.Mime;
 using Masuit.Tools.AspNetCore.ResumeFileResults.Extensions;
 using Masuit.Tools.AspNetCore.ResumeFileResults.Extensions;
 using Masuit.Tools.Core.Net;
 using Masuit.Tools.Core.Net;
@@ -77,5 +83,39 @@ namespace Masuit.MyBlogs.Core.Controllers
             var buffer = HttpContext.CreateValidateGraphic(code);
             var buffer = HttpContext.CreateValidateGraphic(code);
             return this.ResumeFile(buffer, ContentType.Jpeg, "验证码.jpg");
             return this.ResumeFile(buffer, ContentType.Jpeg, "验证码.jpg");
         }
         }
+
+        /// <summary>
+        /// 反爬虫检测
+        /// </summary>
+        /// <param name="id"></param>
+        /// <param name="cacheManager"></param>
+        /// <param name="env"></param>
+        /// <returns></returns>
+        [HttpGet("/craw/{id}")]
+        public async Task<IActionResult> AntiCrawler(string id, [FromServices] ICacheManager<int> cacheManager, [FromServices] IWebHostEnvironment env)
+        {
+            if (Request.IsRobot())
+            {
+                return Ok();
+            }
+
+            var ip = HttpContext.Connection.RemoteIpAddress.ToString();
+            await RedisHelper.LPushAsync("intercept", new IpIntercepter()
+            {
+                IP = ip,
+                RequestUrl = HttpUtility.UrlDecode(Request.Scheme + "://" + Request.Host + "/craw/" + id),
+                Time = DateTime.Now,
+                Referer = Request.Headers[HeaderNames.Referer],
+                UserAgent = Request.Headers[HeaderNames.UserAgent],
+                Remark = "检测到异常爬虫行为",
+                Address = Request.Location(),
+                HttpVersion = Request.Protocol,
+                Headers = Request.Headers.ToJsonString()
+            });
+            cacheManager.AddOrUpdate("AntiCrawler:" + ip, 1, i => i + 1, 5);
+            cacheManager.Expire("AntiCrawler:" + ip, ExpirationMode.Sliding, TimeSpan.FromMinutes(10));
+            var sitemap = Path.Combine(env.WebRootPath, "sitemap.txt");
+            return System.IO.File.Exists(sitemap) ? Redirect(System.IO.File.ReadLines(sitemap).OrderByRandom().FirstOrDefault() ?? "/") : Redirect("/");
+        }
     }
     }
-}
+}

+ 16 - 2
src/Masuit.MyBlogs.Core/Extensions/Firewall/FirewallAttribute.cs

@@ -14,6 +14,7 @@ using System.Net;
 using System.Text;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Text.RegularExpressions;
 using System.Web;
 using System.Web;
+using Masuit.MyBlogs.Core.Controllers;
 using HeaderNames = Microsoft.Net.Http.Headers.HeaderNames;
 using HeaderNames = Microsoft.Net.Http.Headers.HeaderNames;
 
 
 namespace Masuit.MyBlogs.Core.Extensions.Firewall
 namespace Masuit.MyBlogs.Core.Extensions.Firewall
@@ -21,6 +22,7 @@ namespace Masuit.MyBlogs.Core.Extensions.Firewall
     public class FirewallAttribute : ActionFilterAttribute
     public class FirewallAttribute : ActionFilterAttribute
     {
     {
         public ICacheManager<int> CacheManager { get; set; }
         public ICacheManager<int> CacheManager { get; set; }
+
         public IFirewallRepoter FirewallRepoter { get; set; }
         public IFirewallRepoter FirewallRepoter { get; set; }
 
 
         /// <inheritdoc />
         /// <inheritdoc />
@@ -55,7 +57,7 @@ namespace Masuit.MyBlogs.Core.Extensions.Firewall
                 context.Result = new ContentResult()
                 context.Result = new ContentResult()
                 {
                 {
                     Content = Template.Create(msg).Set("browser", agent.Browser + " " + agent.BrowserVersion).Set("os", agent.Platform).Render(),
                     Content = Template.Create(msg).Set("browser", agent.Browser + " " + agent.BrowserVersion).Set("os", agent.Platform).Render(),
-                    ContentType = ContentType.Html,
+                    ContentType = ContentType.Html + "; charset=utf-8",
                     StatusCode = 403
                     StatusCode = 403
                 };
                 };
                 return;
                 return;
@@ -67,6 +69,18 @@ namespace Masuit.MyBlogs.Core.Extensions.Firewall
                 return;
                 return;
             }
             }
 
 
+            // 反爬虫
+            if (CacheManager.GetOrAdd(nameof(FirewallController.AntiCrawler) + ":" + ip, 0) > 3)
+            {
+                context.Result = new ContentResult
+                {
+                    ContentType = ContentType.Html + "; charset=utf-8",
+                    StatusCode = 429,
+                    Content = "检测到访问异常,请在10分钟后再试!"
+                };
+                return;
+            }
+
             //白名单地区
             //白名单地区
             var (location, network, pos) = ip.GetIPLocation();
             var (location, network, pos) = ip.GetIPLocation();
             var allowedAreas = CommonHelper.SystemSettings.GetOrAdd("AllowedArea", "").Split(new[] { ',', ',' }, StringSplitOptions.RemoveEmptyEntries);
             var allowedAreas = CommonHelper.SystemSettings.GetOrAdd("AllowedArea", "").Split(new[] { ',', ',' }, StringSplitOptions.RemoveEmptyEntries);
@@ -174,4 +188,4 @@ namespace Masuit.MyBlogs.Core.Extensions.Firewall
             });
             });
         }
         }
     }
     }
-}
+}

+ 5 - 5
src/Masuit.MyBlogs.Core/Masuit.MyBlogs.Core.csproj

@@ -52,10 +52,10 @@
         <PackageReference Include="IP2Region" Version="1.2.0" />
         <PackageReference Include="IP2Region" Version="1.2.0" />
         <PackageReference Include="Karambolo.AspNetCore.Bundling.NUglify" Version="3.5.0" />
         <PackageReference Include="Karambolo.AspNetCore.Bundling.NUglify" Version="3.5.0" />
         <PackageReference Include="MaxMind.GeoIP2" Version="4.1.0" />
         <PackageReference Include="MaxMind.GeoIP2" Version="4.1.0" />
-        <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
-        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
-        <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.0" />
-        <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.0" />
+        <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />
+        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.1" />
+        <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.1" />
+        <PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.1" />
         <PackageReference Include="Microsoft.Graph" Version="4.11.0" />
         <PackageReference Include="Microsoft.Graph" Version="4.11.0" />
         <PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.7" />
         <PackageReference Include="Microsoft.Graph.Auth" Version="1.0.0-preview.7" />
         <PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.22" />
         <PackageReference Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.22" />
@@ -66,7 +66,7 @@
         <PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.14" />
         <PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.14" />
         <PackageReference Include="TimeZoneConverter" Version="3.5.0" />
         <PackageReference Include="TimeZoneConverter" Version="3.5.0" />
         <PackageReference Include="WilderMinds.RssSyndication" Version="1.7.0" />
         <PackageReference Include="WilderMinds.RssSyndication" Version="1.7.0" />
-        <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="6.13.2" />
+        <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="6.13.4" />
     </ItemGroup>
     </ItemGroup>
     <ItemGroup>
     <ItemGroup>
         <Content Update="appsettings.json">
         <Content Update="appsettings.json">

+ 2 - 2
src/Masuit.MyBlogs.Core/wwwroot/Assets/UEditor/ueditor.config.admin.js

@@ -172,7 +172,7 @@
 		//,indentValue:'2em'
 		//,indentValue:'2em'
 
 
 		,initialFrameWidth:null  //初始化编辑器宽度,默认1000
 		,initialFrameWidth:null  //初始化编辑器宽度,默认1000
-		, initialFrameHeight: window.innerHeight*0.8 //初始化编辑器高度,默认320
+		//, initialFrameHeight: window.innerHeight*0.8 //初始化编辑器高度,默认320
 
 
 		//,readonly : false //编辑器初始化结束后,编辑区域是否是只读的,默认是false
 		//,readonly : false //编辑器初始化结束后,编辑区域是否是只读的,默认是false
 
 
@@ -361,7 +361,7 @@
 
 
 		//autoHeightEnabled
 		//autoHeightEnabled
 		// 是否自动长高,默认true
 		// 是否自动长高,默认true
-		,autoHeightEnabled:false
+		//,autoHeightEnabled:false
 
 
 		//scaleEnabled
 		//scaleEnabled
 		//是否可以拉伸长高,默认true(当开启时,自动长高失效)
 		//是否可以拉伸长高,默认true(当开启时,自动长高失效)

+ 1 - 1
src/Masuit.MyBlogs.Core/wwwroot/Assets/UEditor/ueditor.config.front.js

@@ -361,7 +361,7 @@
 
 
         //autoHeightEnabled
         //autoHeightEnabled
         // 是否自动长高,默认true
         // 是否自动长高,默认true
-        ,autoHeightEnabled:false
+        ,autoHeightEnabled:true
 
 
         //scaleEnabled
         //scaleEnabled
         //是否可以拉伸长高,默认true(当开启时,自动长高失效)
         //是否可以拉伸长高,默认true(当开启时,自动长高失效)

+ 3 - 3
src/Masuit.MyBlogs.Core/wwwroot/ng-views/controllers/post.js

@@ -265,8 +265,8 @@
 	}
 	}
 }]);
 }]);
 myApp.controller("writeblog", ["$scope", "$http", "$timeout","$location", function ($scope, $http, $timeout,$location) {
 myApp.controller("writeblog", ["$scope", "$http", "$timeout","$location", function ($scope, $http, $timeout,$location) {
-	UEDITOR_CONFIG.autoHeightEnabled=false;
-	UEDITOR_CONFIG.initialFrameHeight=window.innerHeight*0.8;
+	//UEDITOR_CONFIG.autoHeightEnabled=false;
+	//UEDITOR_CONFIG.initialFrameHeight=window.innerHeight*0.8;
 	clearInterval(window.interval);
 	clearInterval(window.interval);
 	$scope.post = {
 	$scope.post = {
 		Title: "",
 		Title: "",
@@ -500,7 +500,7 @@ myApp.controller("writeblog", ["$scope", "$http", "$timeout","$location", functi
 	});
 	});
 }]);
 }]);
 myApp.controller("postedit", ["$scope", "$http", "$location", "$timeout", function ($scope, $http, $location, $timeout) {
 myApp.controller("postedit", ["$scope", "$http", "$location", "$timeout", function ($scope, $http, $location, $timeout) {
-	UEDITOR_CONFIG.initialFrameHeight=window.innerHeight*0.72;
+	//UEDITOR_CONFIG.initialFrameHeight=window.innerHeight*0.72;
 	$scope.id = $location.search()['id'];
 	$scope.id = $location.search()['id'];
 	
 	
 	$scope.reserve = true;
 	$scope.reserve = true;