应用cancellationToken

This commit is contained in:
xljiulang 2022-10-16 18:47:26 +08:00
parent c7384ac5ef
commit 81c6ebd583
3 changed files with 12 additions and 25 deletions

View File

@ -61,19 +61,15 @@ namespace FastGithub.PacketIntercept.Dns
public async Task InterceptAsync(CancellationToken cancellationToken) public async Task InterceptAsync(CancellationToken cancellationToken)
{ {
using var divert = new WinDivert(filter, WinDivertLayer.Network); using var divert = new WinDivert(filter, WinDivertLayer.Network);
cancellationToken.Register(d =>
{
((WinDivert)d!).Dispose();
DnsFlushResolverCache();
}, divert);
using var packet = new WinDivertPacket(); using var packet = new WinDivertPacket();
using var addr = new WinDivertAddress(); using var addr = new WinDivertAddress();
DnsFlushResolverCache(); DnsFlushResolverCache();
cancellationToken.Register(DnsFlushResolverCache);
while (cancellationToken.IsCancellationRequested == false) while (cancellationToken.IsCancellationRequested == false)
{ {
await divert.RecvAsync(packet, addr); await divert.RecvAsync(packet, addr, cancellationToken);
try try
{ {
this.ModifyDnsPacket(packet, addr); this.ModifyDnsPacket(packet, addr);
@ -84,7 +80,7 @@ namespace FastGithub.PacketIntercept.Dns
} }
finally finally
{ {
await divert.SendAsync(packet, addr); await divert.SendAsync(packet, addr, cancellationToken);
} }
} }
} }
@ -159,7 +155,7 @@ namespace FastGithub.PacketIntercept.Dns
} }
else else
{ {
addr.Flags ^= WinDivertAddressFlag.Outbound; addr.Flags &= ~WinDivertAddressFlag.Outbound;
} }
packet.CalcChecksums(addr); packet.CalcChecksums(addr);

View File

@ -7,7 +7,7 @@
<ItemGroup> <ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" /> <FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="DNS" Version="7.0.0" /> <PackageReference Include="DNS" Version="7.0.0" />
<PackageReference Include="WindivertDotnet" Version="1.0.2" /> <PackageReference Include="WindivertDotnet" Version="1.0.4" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -50,7 +50,10 @@ namespace FastGithub.PacketIntercept.Tcp
return; return;
} }
using var divert = new WinDivert(this.filter, WinDivertLayer.Network, 0, WinDivertFlag.None); using var divert = new WinDivert(this.filter, WinDivertLayer.Network);
using var packet = new WinDivertPacket();
using var addr = new WinDivertAddress();
if (Socket.OSSupportsIPv4) if (Socket.OSSupportsIPv4)
{ {
this.logger.LogInformation($"{IPAddress.Loopback}:{this.oldServerPort} <=> {IPAddress.Loopback}:{this.newServerPort}"); this.logger.LogInformation($"{IPAddress.Loopback}:{this.oldServerPort} <=> {IPAddress.Loopback}:{this.newServerPort}");
@ -59,13 +62,10 @@ namespace FastGithub.PacketIntercept.Tcp
{ {
this.logger.LogInformation($"{IPAddress.IPv6Loopback}:{this.oldServerPort} <=> {IPAddress.IPv6Loopback}:{this.newServerPort}"); this.logger.LogInformation($"{IPAddress.IPv6Loopback}:{this.oldServerPort} <=> {IPAddress.IPv6Loopback}:{this.newServerPort}");
} }
cancellationToken.Register(d => ((WinDivert)d!).Dispose(), divert);
using var packet = new WinDivertPacket();
using var addr = new WinDivertAddress();
while (cancellationToken.IsCancellationRequested == false) while (cancellationToken.IsCancellationRequested == false)
{ {
await divert.RecvAsync(packet, addr); await divert.RecvAsync(packet, addr, cancellationToken);
try try
{ {
this.ModifyTcpPacket(packet, addr); this.ModifyTcpPacket(packet, addr);
@ -76,7 +76,7 @@ namespace FastGithub.PacketIntercept.Tcp
} }
finally finally
{ {
await divert.SendAsync(packet, addr); await divert.SendAsync(packet, addr, cancellationToken);
} }
} }
} }
@ -89,15 +89,6 @@ namespace FastGithub.PacketIntercept.Tcp
unsafe private void ModifyTcpPacket(WinDivertPacket packet, WinDivertAddress addr) unsafe private void ModifyTcpPacket(WinDivertPacket packet, WinDivertAddress addr)
{ {
var result = packet.GetParseResult(); var result = packet.GetParseResult();
if (result.IPV4Header != null && result.IPV4Header->SrcAddr.Equals(IPAddress.Loopback) == false)
{
return;
}
if (result.IPV6Header != null && result.IPV6Header->SrcAddr.Equals(IPAddress.IPv6Loopback) == false)
{
return;
}
if (result.TcpHeader->DstPort == oldServerPort) if (result.TcpHeader->DstPort == oldServerPort)
{ {
result.TcpHeader->DstPort = this.newServerPort; result.TcpHeader->DstPort = this.newServerPort;