From b441405cb7b81d8dda3c44ed44d231be37bc62d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Wed, 22 Sep 2021 14:29:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => Dns}/DnsInterceptor.cs | 12 ++++++++---- .../{ => Dns}/HostsConflictSolver.cs | 4 ++-- .../{ => Dns}/ProxyConflictSolver.cs | 4 ++-- .../DnsInterceptHostedService.cs | 13 ++++++------- ...ConflictSolver.cs => IDnsConflictSolver.cs} | 2 +- FastGithub.PacketIntercept/IDnsInterceptor.cs | 18 ++++++++++++++++++ FastGithub.PacketIntercept/ITcpInterceptor.cs | 6 ++++-- .../ServiceCollectionExtensions.cs | 8 +++++--- .../{ => Tcp}/HttpInterceptor.cs | 2 +- .../{ => Tcp}/HttpsInterceptor.cs | 2 +- .../{ => Tcp}/SshInterceptor.cs | 2 +- .../{ => Tcp}/TcpInterceptor.cs | 7 +++++-- .../TcpInterceptHostedService.cs | 14 +------------- 13 files changed, 55 insertions(+), 39 deletions(-) rename FastGithub.PacketIntercept/{ => Dns}/DnsInterceptor.cs (96%) rename FastGithub.PacketIntercept/{ => Dns}/HostsConflictSolver.cs (96%) rename FastGithub.PacketIntercept/{ => Dns}/ProxyConflictSolver.cs (98%) rename FastGithub.PacketIntercept/{IConflictSolver.cs => IDnsConflictSolver.cs} (94%) create mode 100644 FastGithub.PacketIntercept/IDnsInterceptor.cs rename FastGithub.PacketIntercept/{ => Tcp}/HttpInterceptor.cs (92%) rename FastGithub.PacketIntercept/{ => Tcp}/HttpsInterceptor.cs (92%) rename FastGithub.PacketIntercept/{ => Tcp}/SshInterceptor.cs (92%) rename FastGithub.PacketIntercept/{ => Tcp}/TcpInterceptor.cs (95%) diff --git a/FastGithub.PacketIntercept/DnsInterceptor.cs b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs similarity index 96% rename from FastGithub.PacketIntercept/DnsInterceptor.cs rename to FastGithub.PacketIntercept/Dns/DnsInterceptor.cs index 8b2081b..2980feb 100644 --- a/FastGithub.PacketIntercept/DnsInterceptor.cs +++ b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs @@ -11,15 +11,16 @@ using System.Net; using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Threading; +using System.Threading.Tasks; using WinDivertSharp; -namespace FastGithub.PacketIntercept +namespace FastGithub.PacketIntercept.Dns { /// /// dns拦截器 /// [SupportedOSPlatform("windows")] - sealed class DnsInterceptor + sealed class DnsInterceptor : IDnsInterceptor { private const string DNS_FILTER = "udp.DstPort == 53"; private readonly FastGithubConfig fastGithubConfig; @@ -52,8 +53,11 @@ namespace FastGithub.PacketIntercept /// DNS拦截 /// /// - public void Intercept(CancellationToken cancellationToken) + /// + public async Task InterceptAsync(CancellationToken cancellationToken) { + await Task.Yield(); + var handle = WinDivert.WinDivertOpen(DNS_FILTER, WinDivertLayer.Network, 0, WinDivertOpenFlags.None); if (handle == IntPtr.Zero) { @@ -163,7 +167,7 @@ namespace FastGithub.PacketIntercept { winDivertAddress.Direction = WinDivertDirection.Inbound; } - + WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All); this.logger.LogInformation($"{domain} => {IPAddress.Loopback}"); } diff --git a/FastGithub.PacketIntercept/HostsConflictSolver.cs b/FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs similarity index 96% rename from FastGithub.PacketIntercept/HostsConflictSolver.cs rename to FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs index eceeaf9..48ae608 100644 --- a/FastGithub.PacketIntercept/HostsConflictSolver.cs +++ b/FastGithub.PacketIntercept/Dns/HostsConflictSolver.cs @@ -6,13 +6,13 @@ using System.Text; using System.Threading; using System.Threading.Tasks; -namespace FastGithub.PacketIntercept +namespace FastGithub.PacketIntercept.Dns { /// /// host文件冲解决者 /// [SupportedOSPlatform("windows")] - sealed class HostsConflictSolver : IConflictSolver + sealed class HostsConflictSolver : IDnsConflictSolver { private readonly FastGithubConfig fastGithubConfig; diff --git a/FastGithub.PacketIntercept/ProxyConflictSolver.cs b/FastGithub.PacketIntercept/Dns/ProxyConflictSolver.cs similarity index 98% rename from FastGithub.PacketIntercept/ProxyConflictSolver.cs rename to FastGithub.PacketIntercept/Dns/ProxyConflictSolver.cs index a61278e..2ad54d7 100644 --- a/FastGithub.PacketIntercept/ProxyConflictSolver.cs +++ b/FastGithub.PacketIntercept/Dns/ProxyConflictSolver.cs @@ -11,13 +11,13 @@ using System.Runtime.Versioning; using System.Threading; using System.Threading.Tasks; -namespace FastGithub.PacketIntercept +namespace FastGithub.PacketIntercept.Dns { /// /// 代理冲突解决者 /// [SupportedOSPlatform("windows")] - sealed class ProxyConflictSolver : IConflictSolver + sealed class ProxyConflictSolver : IDnsConflictSolver { private const int INTERNET_OPTION_REFRESH = 37; private const int INTERNET_OPTION_PROXY_SETTINGS_CHANGED = 95; diff --git a/FastGithub.PacketIntercept/DnsInterceptHostedService.cs b/FastGithub.PacketIntercept/DnsInterceptHostedService.cs index 8583892..7db3f96 100644 --- a/FastGithub.PacketIntercept/DnsInterceptHostedService.cs +++ b/FastGithub.PacketIntercept/DnsInterceptHostedService.cs @@ -12,8 +12,8 @@ namespace FastGithub.PacketIntercept [SupportedOSPlatform("windows")] sealed class DnsInterceptHostedService : BackgroundService { - private readonly DnsInterceptor dnsInterceptor; - private readonly IEnumerable conflictSolvers; + private readonly IDnsInterceptor dnsInterceptor; + private readonly IEnumerable conflictSolvers; /// /// dns拦截后台服务 @@ -21,8 +21,8 @@ namespace FastGithub.PacketIntercept /// /// public DnsInterceptHostedService( - DnsInterceptor dnsInterceptor, - IEnumerable conflictSolvers) + IDnsInterceptor dnsInterceptor, + IEnumerable conflictSolvers) { this.dnsInterceptor = dnsInterceptor; this.conflictSolvers = conflictSolvers; @@ -61,10 +61,9 @@ namespace FastGithub.PacketIntercept /// /// /// - protected override async Task ExecuteAsync(CancellationToken stoppingToken) + protected override Task ExecuteAsync(CancellationToken stoppingToken) { - await Task.Yield(); - this.dnsInterceptor.Intercept(stoppingToken); + return this.dnsInterceptor.InterceptAsync(stoppingToken); } } } diff --git a/FastGithub.PacketIntercept/IConflictSolver.cs b/FastGithub.PacketIntercept/IDnsConflictSolver.cs similarity index 94% rename from FastGithub.PacketIntercept/IConflictSolver.cs rename to FastGithub.PacketIntercept/IDnsConflictSolver.cs index b22dd8e..d41525b 100644 --- a/FastGithub.PacketIntercept/IConflictSolver.cs +++ b/FastGithub.PacketIntercept/IDnsConflictSolver.cs @@ -6,7 +6,7 @@ namespace FastGithub.PacketIntercept /// /// Dns冲突解决者 /// - interface IConflictSolver + interface IDnsConflictSolver { /// /// 解决冲突 diff --git a/FastGithub.PacketIntercept/IDnsInterceptor.cs b/FastGithub.PacketIntercept/IDnsInterceptor.cs new file mode 100644 index 0000000..7244af3 --- /dev/null +++ b/FastGithub.PacketIntercept/IDnsInterceptor.cs @@ -0,0 +1,18 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace FastGithub.PacketIntercept +{ + /// + /// dns拦截器接口 + /// + interface IDnsInterceptor + { + /// + /// 拦截数据包 + /// + /// + /// + Task InterceptAsync(CancellationToken cancellationToken); + } +} \ No newline at end of file diff --git a/FastGithub.PacketIntercept/ITcpInterceptor.cs b/FastGithub.PacketIntercept/ITcpInterceptor.cs index 206a4f7..d1ee0cf 100644 --- a/FastGithub.PacketIntercept/ITcpInterceptor.cs +++ b/FastGithub.PacketIntercept/ITcpInterceptor.cs @@ -1,4 +1,5 @@ using System.Threading; +using System.Threading.Tasks; namespace FastGithub.PacketIntercept { @@ -8,9 +9,10 @@ namespace FastGithub.PacketIntercept interface ITcpInterceptor { /// - /// 拦截tcp + /// 拦截数据包 /// /// - void Intercept(CancellationToken cancellationToken); + /// + Task InterceptAsync(CancellationToken cancellationToken); } } \ No newline at end of file diff --git a/FastGithub.PacketIntercept/ServiceCollectionExtensions.cs b/FastGithub.PacketIntercept/ServiceCollectionExtensions.cs index 1cfbbc4..b3fbe5e 100644 --- a/FastGithub.PacketIntercept/ServiceCollectionExtensions.cs +++ b/FastGithub.PacketIntercept/ServiceCollectionExtensions.cs @@ -1,4 +1,6 @@ using FastGithub.PacketIntercept; +using FastGithub.PacketIntercept.Dns; +using FastGithub.PacketIntercept.Tcp; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using System.Runtime.Versioning; @@ -18,9 +20,9 @@ namespace FastGithub [SupportedOSPlatform("windows")] public static IServiceCollection AddPacketIntercept(this IServiceCollection services) { - services.AddSingleton(); - services.AddSingleton(); - services.TryAddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.TryAddSingleton(); services.AddHostedService(); services.AddSingleton(); diff --git a/FastGithub.PacketIntercept/HttpInterceptor.cs b/FastGithub.PacketIntercept/Tcp/HttpInterceptor.cs similarity index 92% rename from FastGithub.PacketIntercept/HttpInterceptor.cs rename to FastGithub.PacketIntercept/Tcp/HttpInterceptor.cs index c663568..88c99f9 100644 --- a/FastGithub.PacketIntercept/HttpInterceptor.cs +++ b/FastGithub.PacketIntercept/Tcp/HttpInterceptor.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using System.Runtime.Versioning; -namespace FastGithub.PacketIntercept +namespace FastGithub.PacketIntercept.Tcp { /// /// http拦截器 diff --git a/FastGithub.PacketIntercept/HttpsInterceptor.cs b/FastGithub.PacketIntercept/Tcp/HttpsInterceptor.cs similarity index 92% rename from FastGithub.PacketIntercept/HttpsInterceptor.cs rename to FastGithub.PacketIntercept/Tcp/HttpsInterceptor.cs index 090ee50..45157fa 100644 --- a/FastGithub.PacketIntercept/HttpsInterceptor.cs +++ b/FastGithub.PacketIntercept/Tcp/HttpsInterceptor.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using System.Runtime.Versioning; -namespace FastGithub.PacketIntercept +namespace FastGithub.PacketIntercept.Tcp { /// /// https拦截器 diff --git a/FastGithub.PacketIntercept/SshInterceptor.cs b/FastGithub.PacketIntercept/Tcp/SshInterceptor.cs similarity index 92% rename from FastGithub.PacketIntercept/SshInterceptor.cs rename to FastGithub.PacketIntercept/Tcp/SshInterceptor.cs index 5f61817..f74715f 100644 --- a/FastGithub.PacketIntercept/SshInterceptor.cs +++ b/FastGithub.PacketIntercept/Tcp/SshInterceptor.cs @@ -2,7 +2,7 @@ using Microsoft.Extensions.Logging; using System.Runtime.Versioning; -namespace FastGithub.PacketIntercept +namespace FastGithub.PacketIntercept.Tcp { /// /// ssh拦截器 diff --git a/FastGithub.PacketIntercept/TcpInterceptor.cs b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs similarity index 95% rename from FastGithub.PacketIntercept/TcpInterceptor.cs rename to FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs index 4e32dc9..ef83e85 100644 --- a/FastGithub.PacketIntercept/TcpInterceptor.cs +++ b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs @@ -4,9 +4,10 @@ using System.Buffers.Binary; using System.Net; using System.Runtime.Versioning; using System.Threading; +using System.Threading.Tasks; using WinDivertSharp; -namespace FastGithub.PacketIntercept +namespace FastGithub.PacketIntercept.Tcp { /// /// tcp拦截器 @@ -37,13 +38,15 @@ namespace FastGithub.PacketIntercept /// 拦截指定端口的数据包 /// /// - public void Intercept(CancellationToken cancellationToken) + public async Task InterceptAsync(CancellationToken cancellationToken) { if (this.oldServerPort == this.newServerPort) { return; } + await Task.Yield(); + var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None); if (handle == IntPtr.Zero) { diff --git a/FastGithub.PacketIntercept/TcpInterceptHostedService.cs b/FastGithub.PacketIntercept/TcpInterceptHostedService.cs index c639a8c..49d3e1d 100644 --- a/FastGithub.PacketIntercept/TcpInterceptHostedService.cs +++ b/FastGithub.PacketIntercept/TcpInterceptHostedService.cs @@ -31,20 +31,8 @@ namespace FastGithub.PacketIntercept /// protected override Task ExecuteAsync(CancellationToken stoppingToken) { - var tasks = this.tcpInterceptors.Select(item => this.InterceptAsync(item, stoppingToken)); + var tasks = this.tcpInterceptors.Select(item => item.InterceptAsync(stoppingToken)); return Task.WhenAll(tasks); } - - /// - /// 拦截 - /// - /// - /// - /// - private async Task InterceptAsync(ITcpInterceptor interceptor, CancellationToken cancellationToken) - { - await Task.Yield(); - interceptor.Intercept(cancellationToken); - } } }