Browse Source

URL重写

懒得勤快 6 years ago
parent
commit
10d45a4b12

+ 21 - 0
src/Masuit.MyBlogs.Core/App_Data/rewrite.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<rewrite>
+    <rules>
+        <rule name="重定向到https" enabled="true" stopProcessing="true">
+            <match url="(.*)" />
+            <conditions>
+                <add input="{HTTPS}" pattern="^OFF$" />
+                <add input="{HTTPS_HOST}" pattern="^(localhost)" negate="true" />
+            </conditions>
+            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
+        </rule>
+        <rule name="强制重定向到顶级域名" enabled="true" stopProcessing="true">
+            <match url="(.*)" />
+            <conditions logicalGrouping="MatchAny">
+                <add input="{HTTP_HOST}" pattern="^.+\.(.+\..+)$" />
+            </conditions>
+            <action type="Redirect" url="https://{C:1}/{R:1}" appendQueryString="true" redirectType="Permanent" />
+        </rule>
+    </rules>
+</rewrite>

+ 1 - 4
src/Masuit.MyBlogs.Core/Extensions/FirewallMiddleware.cs

@@ -1,7 +1,4 @@
-using Common;
-using Hangfire;
-using Masuit.MyBlogs.Core.Extensions.Hangfire;
-using Masuit.Tools;
+using Masuit.Tools;
 using Masuit.Tools.NoSQL;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Net.Http.Headers;

+ 4 - 0
src/Masuit.MyBlogs.Core/Extensions/IApplicationBuilderExtensions.cs

@@ -12,5 +12,9 @@ namespace Masuit.MyBlogs.Core.Extensions
         {
             return builder.UseMiddleware<ExceptionMiddleware>();
         }
+        public static IApplicationBuilder UseRequestIntercept(this IApplicationBuilder builder)
+        {
+            return builder.UseMiddleware<RequestInterceptMiddleware>();
+        }
     }
 }

+ 37 - 0
src/Masuit.MyBlogs.Core/Extensions/RequestInterceptMiddleware.cs

@@ -0,0 +1,37 @@
+using Masuit.Tools.Core.Net;
+using Masuit.Tools.NoSQL;
+using Microsoft.AspNetCore.Http;
+using System.Threading.Tasks;
+
+namespace Masuit.MyBlogs.Core.Extensions
+{
+    /// <summary>
+    /// 请求拦截器
+    /// </summary>
+    public class RequestInterceptMiddleware
+    {
+        private readonly RequestDelegate _next;
+        private readonly RedisHelper _redisHelper;
+
+        /// <summary>
+        /// 构造函数
+        /// </summary>
+        /// <param name="next"></param>
+        /// <param name="redisHelper"></param>
+        public RequestInterceptMiddleware(RequestDelegate next, RedisHelper redisHelper)
+        {
+            _next = next;
+            _redisHelper = redisHelper;
+        }
+
+        public async Task Invoke(HttpContext context)
+        {
+            if (!context.Session.TryGetValue(context.Connection.Id, out _))
+            {
+                context.Session.Set(context.Connection.Id, context.Connection.Id);
+                _redisHelper.StringIncrement("Interview:ViewCount");
+            }
+            await _next.Invoke(context);
+        }
+    }
+}

+ 1 - 1
src/Masuit.MyBlogs.Core/Hubs/MyHub.cs

@@ -65,7 +65,7 @@ namespace Masuit.MyBlogs.Core.Hubs
         /// <summary>
         /// 性能计数器缓存
         /// </summary>
-        public static ConcurrentLimitedQueue<PerformanceCounter> PerformanceCounter { get; set; } = new ConcurrentLimitedQueue<PerformanceCounter>(10000);
+        public static ConcurrentLimitedQueue<PerformanceCounter> PerformanceCounter { get; set; } = new ConcurrentLimitedQueue<PerformanceCounter>(2000);
 
         static MyHub()
         {

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

@@ -70,16 +70,17 @@
     <PackageReference Include="Microsoft.AspNetCore.App" />
     <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
     <PackageReference Include="Microsoft.AspNetCore.ResponseCaching.Abstractions" Version="2.2.0" />
+    <PackageReference Include="Microsoft.AspNetCore.Rewrite" Version="2.2.0" />
     <PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
     <PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="2.2.1" />
     <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.1" />
-    <PackageReference Include="NinjaNye.SearchExtensions" Version="2.2.0" />
+    <PackageReference Include="NinjaNye.SearchExtensions" Version="3.0.0" />
     <PackageReference Include="PanGu.HighLight" Version="1.0.0" />
-    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.4" />
+    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.2.0" />
     <PackageReference Include="Quartz" Version="3.0.7" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
     <PackageReference Include="WilderMinds.RssSyndication" Version="1.4.0" />
-    <PackageReference Include="Z.EntityFramework.Extensions.EFCore" Version="2.1.47" />
+    <PackageReference Include="Z.EntityFramework.Extensions.EFCore" Version="2.1.48" />
     <PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="1.8.18" />
     <PackageReference Include="Z.ExtensionMethods" Version="2.1.1" />
   </ItemGroup>
@@ -120,6 +121,9 @@
     <None Update="App_Data\mod.txt">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
+    <None Update="App_Data\rewrite.xml">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
     <None Update="App_Data\whitelist.txt">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>

+ 11 - 6
src/Masuit.MyBlogs.Core/Startup.cs

@@ -26,6 +26,7 @@ using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Http.Features;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Rewrite;
 using Microsoft.AspNetCore.StaticFiles;
 using Microsoft.AspNetCore.WebSockets;
 using Microsoft.EntityFrameworkCore;
@@ -52,6 +53,11 @@ namespace Masuit.MyBlogs.Core
     /// </summary>
     public class Startup
     {
+        /// <summary>
+        /// 依赖注入容器
+        /// </summary>
+        public static IServiceProvider AutofacContainer { get; set; }
+
         /// <summary>
         /// asp.net core核心配置
         /// </summary>
@@ -151,11 +157,6 @@ namespace Masuit.MyBlogs.Core
             return AutofacContainer;
         }
 
-        /// <summary>
-        /// 依赖注入容器
-        /// </summary>
-        public static IServiceProvider AutofacContainer { get; set; }
-
         /// <summary>
         /// Configure
         /// </summary>
@@ -175,6 +176,10 @@ namespace Masuit.MyBlogs.Core
                 app.UseExceptionHandler("/Home/Error");
                 app.UseHsts();
                 app.UseException();
+                using (var fs = File.OpenText(Path.Combine(env.ContentRootPath, "App_Data", "rewrite.xml")))
+                {
+                    app.UseRewriter(new RewriteOptions().AddIISUrlRewrite(fs));
+                }
             }
 
             app.UseHttpsRedirection().UseStaticFiles(new StaticFileOptions //静态资源缓存策略
@@ -187,7 +192,7 @@ namespace Masuit.MyBlogs.Core
                 ContentTypeProvider = new FileExtensionContentTypeProvider(MimeMapper.MimeTypes)
             }).UseCookiePolicy();
 
-            app.UseFirewall(); //启用网站防火墙
+            app.UseFirewall().UseRequestIntercept(); //启用网站防火墙
             //db.Database.Migrate();
             CommonHelper.SystemSettings = db.SystemSetting.ToDictionary(s => s.Name, s => s.Value); //初始化系统设置参数