Ver código fonte

优化防火墙

懒得勤快 5 anos atrás
pai
commit
1d87ec4004

+ 2 - 24
src/Masuit.MyBlogs.Core/Common/CommonHelper.cs

@@ -199,22 +199,10 @@ namespace Masuit.MyBlogs.Core.Common
         /// <param name="title">标题</param>
         /// <param name="content">内容</param>
         /// <param name="tos">收件人</param>
+        /// <param name="clientip"></param>
         [AutomaticRetry(Attempts = 1, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
         public static void SendMail(string title, string content, string tos, string clientip)
         {
-            //#if !DEBUG
-            //            new Email()
-            //            {
-            //                EnableSsl = bool.Parse(SystemSettings.GetOrAdd("EnableSsl", "true")),
-            //                Body = content,
-            //                SmtpServer = SystemSettings["SMTP"],
-            //                Username = SystemSettings["EmailFrom"],
-            //                Password = SystemSettings["EmailPwd"],
-            //                SmtpPort = SystemSettings["SmtpPort"].ToInt32(),
-            //                Subject = title,
-            //                Tos = tos
-            //            }.Send();
-            //#endif
             Startup.ServiceProvider.GetRequiredService<IMailSender>().Send(title, content, tos);
             RedisHelper.SAdd($"Email:{DateTime.Now:yyyyMMdd}", new { title, content, tos, time = DateTime.Now, clientip });
             RedisHelper.Expire($"Email:{DateTime.Now:yyyyMMdd}", 86400);
@@ -225,17 +213,7 @@ namespace Masuit.MyBlogs.Core.Common
         /// </summary>
         /// <param name="req"></param>
         /// <returns></returns>
-        public static bool IsRobot(this HttpRequest req)
-        {
-            return req.Headers[HeaderNames.UserAgent].ToString().Contains(new[]
-            {
-                "DNSPod",
-                "Baidu",
-                "spider",
-                "Python",
-                "bot"
-            });
-        }
+        public static bool IsRobot(this HttpRequest req) => UserAgent.Parse(req.Headers[HeaderNames.UserAgent].ToString()).IsRobot;
 
         /// <summary>
         /// 清理html的img标签的除src之外的其他属性

+ 4 - 0
src/Masuit.MyBlogs.Core/Common/UA.cs

@@ -197,6 +197,10 @@ namespace Masuit.MyBlogs.Core.Common
             {"MJ12bot", "Majestic-12"},
             {"Uptimebot", "Uptimebot"},
             {"Sogou web spider", "Sogou Web Spider"},
+            {"TelegramBot", "Telegram Bot"},
+            {"DNSPod", "DNSPod"},
+            {"spider", "other spider"},
+            {"bot", "other bot"}
         };
 
         protected string agent;

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

@@ -48,10 +48,10 @@ namespace Masuit.MyBlogs.Core.Extensions
             }
 
             var ua = request.Headers[HeaderNames.UserAgent] + "";
+            var agent = UserAgent.Parse(ua);
             var blocked = CommonHelper.SystemSettings.GetOrAdd("UserAgentBlocked", "").Split(new[] { ',', '|' }, StringSplitOptions.RemoveEmptyEntries);
             if (ua.Contains(blocked))
             {
-                var agent = UserAgent.Parse(ua);
                 AccessDeny(ip, request, $"UA黑名单({agent.Browser} {agent.BrowserVersion}/{agent.Platform})");
                 var msg = CommonHelper.SystemSettings.GetOrAdd("UserAgentBlockedMsg", "当前浏览器不支持访问本站");
                 context.Result = new ContentResult()
@@ -68,7 +68,7 @@ namespace Masuit.MyBlogs.Core.Extensions
                 throw new AccessDenyException("访问地区限制");
             }
 
-            if (Regex.IsMatch(request.Method, "OPTIONS|HEAD", RegexOptions.IgnoreCase) || request.IsRobot())
+            if (Regex.IsMatch(request.Method, "OPTIONS|HEAD", RegexOptions.IgnoreCase) || agent.IsRobot)
             {
                 return;
             }

+ 0 - 1
src/Masuit.MyBlogs.Core/Extensions/Hangfire/HangfireBackJob.cs

@@ -151,7 +151,6 @@ namespace Masuit.MyBlogs.Core.Extensions.Hangfire
         public void EverydayJob()
         {
             CommonHelper.IPErrorTimes.RemoveWhere(kv => kv.Value < 100); //将访客访问出错次数少于100的移开
-            RedisHelper.IncrBy("Interview:RunningDays");
             DateTime time = DateTime.Now.AddMonths(-1);
             _searchDetailsService.DeleteEntitySaved(s => s.SearchTime < time);
             TrackData.DumpLog();

+ 1 - 2
src/Masuit.MyBlogs.Core/Views/Shared/_Layout.cshtml

@@ -1,5 +1,4 @@
-@using System.Diagnostics
-@using Masuit.MyBlogs.Core.Common
+@using Masuit.MyBlogs.Core.Common
 @using Masuit.MyBlogs.Core.Models.DTO
 @using Masuit.MyBlogs.Core.Models.Enum
 @using Masuit.MyBlogs.Core.Models.ViewModel