Jelajahi Sumber

异常短路

陈国伟 3 tahun lalu
induk
melakukan
23c2e4f40f

+ 13 - 16
FastGithub.PacketIntercept/Dns/DnsInterceptor.cs

@@ -81,25 +81,22 @@ namespace FastGithub.PacketIntercept.Dns
             DnsFlushResolverCache();
             DnsFlushResolverCache();
             while (cancellationToken.IsCancellationRequested == false)
             while (cancellationToken.IsCancellationRequested == false)
             {
             {
-                if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength))
+                if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength) == false)
                 {
                 {
-                    try
-                    {
-                        this.ModifyDnsPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
-                    }
-                    catch (Exception ex)
-                    {
-                        this.logger.LogWarning(ex.Message);
-                    }
-                    finally
-                    {
-                        WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
-                    }
+                    throw new Win32Exception();
                 }
                 }
-                else
+
+                try
+                {
+                    this.ModifyDnsPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
+                }
+                catch (Exception ex)
+                {
+                    this.logger.LogWarning(ex.Message);
+                }
+                finally
                 {
                 {
-                    var errorCode = Marshal.GetLastWin32Error();
-                    throw new Win32Exception(errorCode);
+                    WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
                 }
                 }
             }
             }
         }
         }

+ 15 - 19
FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs

@@ -3,7 +3,6 @@ using System;
 using System.Buffers.Binary;
 using System.Buffers.Binary;
 using System.ComponentModel;
 using System.ComponentModel;
 using System.Net;
 using System.Net;
-using System.Runtime.InteropServices;
 using System.Runtime.Versioning;
 using System.Runtime.Versioning;
 using System.Threading;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
@@ -16,7 +15,7 @@ namespace FastGithub.PacketIntercept.Tcp
     /// </summary>   
     /// </summary>   
     [SupportedOSPlatform("windows")]
     [SupportedOSPlatform("windows")]
     abstract class TcpInterceptor : ITcpInterceptor
     abstract class TcpInterceptor : ITcpInterceptor
-    { 
+    {
         private readonly string filter;
         private readonly string filter;
         private readonly ushort oldServerPort;
         private readonly ushort oldServerPort;
         private readonly ushort newServerPort;
         private readonly ushort newServerPort;
@@ -52,7 +51,7 @@ namespace FastGithub.PacketIntercept.Tcp
 
 
             var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
             var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
             if (handle == IntPtr.MaxValue || handle == IntPtr.Zero)
             if (handle == IntPtr.MaxValue || handle == IntPtr.Zero)
-            { 
+            {
                 const int ERROR_INVALID_HANDLE = 0x6;
                 const int ERROR_INVALID_HANDLE = 0x6;
                 throw new Win32Exception(ERROR_INVALID_HANDLE, "打开驱动失败");
                 throw new Win32Exception(ERROR_INVALID_HANDLE, "打开驱动失败");
             }
             }
@@ -66,25 +65,22 @@ namespace FastGithub.PacketIntercept.Tcp
 
 
             while (cancellationToken.IsCancellationRequested == false)
             while (cancellationToken.IsCancellationRequested == false)
             {
             {
-                if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength))
+                if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength) == false)
+                {
+                    throw new Win32Exception();
+                }
+
+                try
+                {
+                    this.ModifyTcpPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
+                }
+                catch (Exception ex)
                 {
                 {
-                    try
-                    {
-                        this.ModifyTcpPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
-                    }
-                    catch (Exception ex)
-                    {
-                        this.logger.LogWarning(ex.Message);
-                    }
-                    finally
-                    {
-                        WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
-                    }
+                    this.logger.LogWarning(ex.Message);
                 }
                 }
-                else
+                finally
                 {
                 {
-                    var errorCode = Marshal.GetLastWin32Error();
-                    throw new Win32Exception(errorCode);
+                    WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
                 }
                 }
             }
             }
         }
         }

+ 10 - 19
FastGithub/AppHostedService.cs

@@ -39,17 +39,6 @@ namespace FastGithub
             return base.StartAsync(cancellationToken);
             return base.StartAsync(cancellationToken);
         }
         }
 
 
-        /// <summary>
-        /// 停止完成
-        /// </summary>
-        /// <param name="cancellationToken"></param>
-        /// <returns></returns>
-        public override Task StopAsync(CancellationToken cancellationToken)
-        {
-            this.logger.LogInformation($"{nameof(FastGithub)}已停止运行");
-            return base.StopAsync(cancellationToken);
-        }
-
         /// <summary>
         /// <summary>
         /// 后台任务
         /// 后台任务
         /// </summary>
         /// </summary>
@@ -57,14 +46,16 @@ namespace FastGithub
         /// <returns></returns>
         /// <returns></returns>
         protected override async Task ExecuteAsync(CancellationToken stoppingToken)
         protected override async Task ExecuteAsync(CancellationToken stoppingToken)
         {
         {
-            if (OperatingSystem.IsWindows() == false)
+            if (OperatingSystem.IsWindows())
+            {
+                return;
+            }
+
+            await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken);
+            if (await this.UseFastGithubProxyAsync() == false)
             {
             {
-                await Task.Delay(TimeSpan.FromSeconds(1d), stoppingToken);
-                if (await this.UseFastGithubProxyAsync() == false)
-                {
-                    var httpProxyPort = this.options.Value.HttpProxyPort;
-                    this.logger.LogWarning($"请设置系统自动代理为http://{IPAddress.Loopback}:{httpProxyPort},或手动代理http/https为{IPAddress.Loopback}:{httpProxyPort}");
-                }
+                var httpProxyPort = this.options.Value.HttpProxyPort;
+                this.logger.LogWarning($"请设置系统自动代理为http://{IPAddress.Loopback}:{httpProxyPort},或手动代理http/https为{IPAddress.Loopback}:{httpProxyPort}");
             }
             }
         }
         }
 
 
@@ -108,7 +99,7 @@ namespace FastGithub
 
 
             try
             try
             {
             {
-                var addresses = await System.Net.Dns.GetHostAddressesAsync(proxyServer.Host);
+                var addresses = await Dns.GetHostAddressesAsync(proxyServer.Host);
                 return addresses.Contains(IPAddress.Loopback);
                 return addresses.Contains(IPAddress.Loopback);
             }
             }
             catch (Exception)
             catch (Exception)