From aa99fdafac5533a0c0649d41fb9a14c085467d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 13 Oct 2022 12:28:18 +0800 Subject: [PATCH] =?UTF-8?q?WindivertDotnet=E5=BC=82=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.PacketIntercept/Dns/DnsInterceptor.cs | 16 +++++++--------- .../FastGithub.PacketIntercept.csproj | 2 +- FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs | 16 ++++++---------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs index 6b94103..7ebb1aa 100644 --- a/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs +++ b/FastGithub.PacketIntercept/Dns/DnsInterceptor.cs @@ -60,8 +60,6 @@ namespace FastGithub.PacketIntercept.Dns /// public async Task InterceptAsync(CancellationToken cancellationToken) { - await Task.Yield(); - using var divert = new WinDivert(filter, WinDivertLayer.Network); cancellationToken.Register(d => { @@ -69,16 +67,16 @@ namespace FastGithub.PacketIntercept.Dns DnsFlushResolverCache(); }, divert); - var addr = new WinDivertAddress(); using var packet = new WinDivertPacket(); + using var addr = new WinDivertAddress(); DnsFlushResolverCache(); while (cancellationToken.IsCancellationRequested == false) { - divert.Recv(packet, ref addr); + await divert.RecvAsync(packet, addr); try { - this.ModifyDnsPacket(packet, ref addr); + this.ModifyDnsPacket(packet, addr); } catch (Exception ex) { @@ -86,7 +84,7 @@ namespace FastGithub.PacketIntercept.Dns } finally { - divert.Send(packet, ref addr); + await divert.SendAsync(packet, addr); } } } @@ -96,7 +94,7 @@ namespace FastGithub.PacketIntercept.Dns /// /// /// - unsafe private void ModifyDnsPacket(WinDivertPacket packet, ref WinDivertAddress addr) + unsafe private void ModifyDnsPacket(WinDivertPacket packet, WinDivertAddress addr) { var result = packet.GetParseResult(); var requestPayload = result.DataSpan.ToArray(); @@ -162,9 +160,9 @@ namespace FastGithub.PacketIntercept.Dns else { addr.Flags ^= WinDivertAddressFlag.Outbound; - } + } - packet.CalcChecksums(ref addr); + packet.CalcChecksums(addr); this.logger.LogInformation($"{domain}->{loopback}"); } diff --git a/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj b/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj index 8222b05..951e9c1 100644 --- a/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj +++ b/FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj @@ -7,7 +7,7 @@ - + diff --git a/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs index 8dbb2d9..ed0331a 100644 --- a/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs +++ b/FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs @@ -50,8 +50,6 @@ namespace FastGithub.PacketIntercept.Tcp return; } - await Task.Yield(); - using var divert = new WinDivert(this.filter, WinDivertLayer.Network, 0, WinDivertFlag.None); if (Socket.OSSupportsIPv4) { @@ -63,16 +61,14 @@ namespace FastGithub.PacketIntercept.Tcp } cancellationToken.Register(d => ((WinDivert)d!).Dispose(), divert); - var addr = new WinDivertAddress(); using var packet = new WinDivertPacket(); + using var addr = new WinDivertAddress(); while (cancellationToken.IsCancellationRequested == false) { - addr.Clear(); - divert.Recv(packet, ref addr); - + await divert.RecvAsync(packet, addr); try { - this.ModifyTcpPacket(packet, ref addr); + this.ModifyTcpPacket(packet, addr); } catch (Exception ex) { @@ -80,7 +76,7 @@ namespace FastGithub.PacketIntercept.Tcp } finally { - divert.Send(packet, ref addr); + await divert.SendAsync(packet, addr); } } } @@ -90,7 +86,7 @@ namespace FastGithub.PacketIntercept.Tcp /// /// /// - unsafe private void ModifyTcpPacket(WinDivertPacket packet, ref WinDivertAddress addr) + unsafe private void ModifyTcpPacket(WinDivertPacket packet, WinDivertAddress addr) { var result = packet.GetParseResult(); if (result.IPV4Header != null && result.IPV4Header->SrcAddr.Equals(IPAddress.Loopback) == false) @@ -111,7 +107,7 @@ namespace FastGithub.PacketIntercept.Tcp result.TcpHeader->SrcPort = oldServerPort; } addr.Flags |= WinDivertAddressFlag.Impostor; - packet.CalcChecksums(ref addr); + packet.CalcChecksums(addr); } } }