From b9d8fccd775c1779c7c03107a9202e02e0331c57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Fri, 24 Sep 2021 11:43:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=90=8E=E8=83=BD=E5=87=BA=E5=BA=94=E7=94=A8=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Configuration/ReverseProxyPort.cs | 6 ++-- .../Dns/DnsInterceptor.cs | 12 +++---- .../DnsInterceptHostedService.cs | 27 +++++++++++++--- .../Tcp/TcpInterceptor.cs | 16 ++++------ .../TcpInterceptHostedService.cs | 32 +++++++++++++++---- 5 files changed, 62 insertions(+), 31 deletions(-) diff --git a/FastGithub.Configuration/ReverseProxyPort.cs b/FastGithub.Configuration/ReverseProxyPort.cs index e14e140..4f66816 100644 --- a/FastGithub.Configuration/ReverseProxyPort.cs +++ b/FastGithub.Configuration/ReverseProxyPort.cs @@ -14,17 +14,17 @@ namespace FastGithub.Configuration /// /// ssh端口 /// - public static int Ssh { get; } = GetAvailableTcpPort(22); + public static int Ssh { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(22) : GetAvailableTcpPort(3822); /// /// http端口 /// - public static int Http { get; } = GetAvailableTcpPort(80); + public static int Http { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(80) : GetAvailableTcpPort(3880); /// /// https端口 /// - public static int Https { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(48457); + public static int Https { get; } = OperatingSystem.IsWindows() ? GetAvailableTcpPort(443) : GetAvailableTcpPort(38443); /// /// 获取可用的随机Tcp端口 diff --git a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs index 7015b75..ad8ad1e 100644 --- a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs +++ b/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 logger; @@ -56,6 +55,7 @@ namespace FastGithub.PacketIntercept.Dns /// DNS拦截 /// /// + /// /// 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); } } } diff --git a/FastGithub.PacketIntercept/DnsInterceptHostedService.cs b/FastGithub.PacketIntercept/DnsInterceptHostedService.cs index 7db3f96..1ac3b12 100644 --- a/FastGithub.PacketIntercept/DnsInterceptHostedService.cs +++ b/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 conflictSolvers; + private readonly ILogger logger; + private readonly IHost host; /// /// dns拦截后台服务 - /// + /// /// /// + /// + /// public DnsInterceptHostedService( IDnsInterceptor dnsInterceptor, - IEnumerable conflictSolvers) + IEnumerable conflictSolvers, + ILogger logger, + IHost host) { this.dnsInterceptor = dnsInterceptor; this.conflictSolvers = conflictSolvers; + this.logger = logger; + this.host = host; } /// @@ -61,9 +71,18 @@ namespace FastGithub.PacketIntercept /// /// /// - 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); + } } } } diff --git a/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs index 2fd52ae..4b49299 100644 --- a/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs +++ b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs @@ -16,8 +16,7 @@ namespace FastGithub.PacketIntercept.Tcp /// [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 /// 拦截指定端口的数据包 /// /// + /// 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); } } } diff --git a/FastGithub.PacketIntercept/TcpInterceptHostedService.cs b/FastGithub.PacketIntercept/TcpInterceptHostedService.cs index 49d3e1d..04c1a53 100644 --- a/FastGithub.PacketIntercept/TcpInterceptHostedService.cs +++ b/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 tcpInterceptors; + private readonly ILogger logger; + private readonly IHost host; /// /// tcp拦截后台服务 - /// - /// - public TcpInterceptHostedService(IEnumerable tcpInterceptors) + /// + /// + /// + /// + public TcpInterceptHostedService( + IEnumerable tcpInterceptors, + ILogger logger, + IHost host) { this.tcpInterceptors = tcpInterceptors; + this.logger = logger; + this.host = host; } /// @@ -29,10 +40,19 @@ namespace FastGithub.PacketIntercept /// /// /// - 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); + } } } }