Forráskód Böngészése

拦截器异常后能出应用程序

陈国伟 3 éve
szülő
commit
b9d8fccd77

+ 3 - 3
FastGithub.Configuration/ReverseProxyPort.cs

@@ -14,17 +14,17 @@ namespace FastGithub.Configuration
         /// <summary>
         /// ssh端口
         /// </summary>
-        public static int Ssh { get; } = GetAvailableTcpPort(22);
+        public static int Ssh { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(22) : GetAvailableTcpPort(3822);
 
         /// <summary>
         /// http端口
         /// </summary>
-        public static int Http { get; } = GetAvailableTcpPort(80);
+        public static int Http { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(80) : GetAvailableTcpPort(3880);
 
         /// <summary>
         /// https端口
         /// </summary>
-        public static int Https { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(48457);
+        public static int Https { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(38443);
 
         /// <summary>
         /// 获取可用的随机Tcp端口

+ 4 - 8
FastGithub.PacketIntercept/Dns/DnsInterceptor.cs

@@ -23,7 +23,6 @@ namespace FastGithub.PacketIntercept.Dns
     [SupportedOSPlatform("windows")]
     sealed class DnsInterceptor : IDnsInterceptor
     {
-        private const int ERROR_INVALID_HANDLE = 0x6;
         private const string DNS_FILTER = "udp.DstPort == 53";
         private readonly FastGithubConfig fastGithubConfig;
         private readonly ILogger<DnsInterceptor> logger;
@@ -56,6 +55,7 @@ namespace FastGithub.PacketIntercept.Dns
         /// DNS拦截
         /// </summary>
         /// <param name="cancellationToken"></param>
+        /// <exception cref="Win32Exception"></exception>
         /// <returns></returns>
         public async Task InterceptAsync(CancellationToken cancellationToken)
         {
@@ -64,8 +64,8 @@ namespace FastGithub.PacketIntercept.Dns
             var handle = WinDivert.WinDivertOpen(DNS_FILTER, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
             if (handle == IntPtr.MaxValue || handle == IntPtr.Zero)
             {
-                this.logger.LogError($"打开驱动失败");
-                return;
+                const int ERROR_INVALID_HANDLE = 0x6;
+                throw new Win32Exception(ERROR_INVALID_HANDLE, "打开驱动失败");
             }
 
             cancellationToken.Register(hwnd =>
@@ -99,11 +99,7 @@ namespace FastGithub.PacketIntercept.Dns
                 else
                 {
                     var errorCode = Marshal.GetLastWin32Error();
-                    this.logger.LogError(new Win32Exception(errorCode).Message);
-                    if (errorCode == ERROR_INVALID_HANDLE)
-                    {
-                        break;
-                    }
+                    throw new Win32Exception(errorCode);
                 }
             }
         }

+ 23 - 4
FastGithub.PacketIntercept/DnsInterceptHostedService.cs

@@ -1,4 +1,6 @@
 using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using System;
 using System.Collections.Generic;
 using System.Runtime.Versioning;
 using System.Threading;
@@ -14,18 +16,26 @@ namespace FastGithub.PacketIntercept
     {
         private readonly IDnsInterceptor dnsInterceptor;
         private readonly IEnumerable<IDnsConflictSolver> conflictSolvers;
+        private readonly ILogger<DnsInterceptHostedService> logger;
+        private readonly IHost host;
 
         /// <summary>
         /// dns拦截后台服务
-        /// </summary> 
+        /// </summary>
         /// <param name="dnsInterceptor"></param>
         /// <param name="conflictSolvers"></param>
+        /// <param name="logger"></param>
+        /// <param name="host"></param>
         public DnsInterceptHostedService(
             IDnsInterceptor dnsInterceptor,
-            IEnumerable<IDnsConflictSolver> conflictSolvers)
+            IEnumerable<IDnsConflictSolver> conflictSolvers,
+            ILogger<DnsInterceptHostedService> logger,
+            IHost host)
         {
             this.dnsInterceptor = dnsInterceptor;
             this.conflictSolvers = conflictSolvers;
+            this.logger = logger;
+            this.host = host;
         }
 
         /// <summary>
@@ -61,9 +71,18 @@ namespace FastGithub.PacketIntercept
         /// </summary>
         /// <param name="stoppingToken"></param>
         /// <returns></returns>
-        protected override Task ExecuteAsync(CancellationToken stoppingToken)
+        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
         {
-            return this.dnsInterceptor.InterceptAsync(stoppingToken);
+            try
+            {
+                await this.dnsInterceptor.InterceptAsync(stoppingToken);
+            }
+            catch (Exception ex)
+            {
+                stoppingToken.ThrowIfCancellationRequested();
+                this.logger.LogError(ex, "dns拦截器异常");
+                await this.host.StopAsync(stoppingToken);
+            }
         }
     }
 }

+ 6 - 10
FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs

@@ -16,8 +16,7 @@ namespace FastGithub.PacketIntercept.Tcp
     /// </summary>   
     [SupportedOSPlatform("windows")]
     abstract class TcpInterceptor : ITcpInterceptor
-    {
-        private const int ERROR_INVALID_HANDLE = 0x6;
+    { 
         private readonly string filter;
         private readonly ushort oldServerPort;
         private readonly ushort newServerPort;
@@ -41,6 +40,7 @@ namespace FastGithub.PacketIntercept.Tcp
         /// 拦截指定端口的数据包
         /// </summary>
         /// <param name="cancellationToken"></param>
+        /// <exception cref="Win32Exception"></exception>
         public async Task InterceptAsync(CancellationToken cancellationToken)
         {
             if (this.oldServerPort == this.newServerPort)
@@ -52,9 +52,9 @@ namespace FastGithub.PacketIntercept.Tcp
 
             var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
             if (handle == IntPtr.MaxValue || handle == IntPtr.Zero)
-            {
-                this.logger.LogError($"打开驱动失败");
-                return;
+            { 
+                const int ERROR_INVALID_HANDLE = 0x6;
+                throw new Win32Exception(ERROR_INVALID_HANDLE, "打开驱动失败");
             }
 
             this.logger.LogInformation($"tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(this.oldServerPort)} => tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(this.newServerPort)}");
@@ -84,11 +84,7 @@ namespace FastGithub.PacketIntercept.Tcp
                 else
                 {
                     var errorCode = Marshal.GetLastWin32Error();
-                    this.logger.LogError(new Win32Exception(errorCode).Message);
-                    if (errorCode == ERROR_INVALID_HANDLE)
-                    {
-                        break;
-                    }
+                    throw new Win32Exception(errorCode);
                 }
             }
         }

+ 26 - 6
FastGithub.PacketIntercept/TcpInterceptHostedService.cs

@@ -1,4 +1,6 @@
 using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Versioning;
@@ -14,14 +16,23 @@ namespace FastGithub.PacketIntercept
     sealed class TcpInterceptHostedService : BackgroundService
     {
         private readonly IEnumerable<ITcpInterceptor> tcpInterceptors;
+        private readonly ILogger<TcpInterceptHostedService> logger;
+        private readonly IHost host;
 
         /// <summary>
         /// tcp拦截后台服务
-        /// </summary> 
-        /// <param name="tcpInterceptors"></param> 
-        public TcpInterceptHostedService(IEnumerable<ITcpInterceptor> tcpInterceptors)
+        /// </summary>
+        /// <param name="tcpInterceptors"></param>
+        /// <param name="logger"></param>
+        /// <param name="host"></param>
+        public TcpInterceptHostedService(
+            IEnumerable<ITcpInterceptor> tcpInterceptors,
+            ILogger<TcpInterceptHostedService> logger,
+            IHost host)
         {
             this.tcpInterceptors = tcpInterceptors;
+            this.logger = logger;
+            this.host = host;
         }
 
         /// <summary>
@@ -29,10 +40,19 @@ namespace FastGithub.PacketIntercept
         /// </summary>
         /// <param name="stoppingToken"></param>
         /// <returns></returns>
-        protected override Task ExecuteAsync(CancellationToken stoppingToken)
+        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
         {
-            var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
-            return Task.WhenAll(tasks);
+            try
+            {
+                var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken));
+                await Task.WhenAll(tasks);
+            }
+            catch (Exception ex)
+            {
+                stoppingToken.ThrowIfCancellationRequested();
+                this.logger.LogError(ex, "tcp拦截器异常");
+                await this.host.StopAsync(stoppingToken);
+            }
         }
     }
 }