|
@@ -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
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|