Skip to content

Commit

Permalink
应用cancellationToken
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 16, 2022
1 parent c7384ac commit 81c6ebd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
14 changes: 5 additions & 9 deletions FastGithub.PacketIntercept/Dns/DnsInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,15 @@ public DnsInterceptor(
public async Task InterceptAsync(CancellationToken cancellationToken)
{
using var divert = new WinDivert(filter, WinDivertLayer.Network);
cancellationToken.Register(d =>
{
((WinDivert)d!).Dispose();
DnsFlushResolverCache();
}, divert);

using var packet = new WinDivertPacket();
using var addr = new WinDivertAddress();

DnsFlushResolverCache();
cancellationToken.Register(DnsFlushResolverCache);

while (cancellationToken.IsCancellationRequested == false)
{
await divert.RecvAsync(packet, addr);
await divert.RecvAsync(packet, addr, cancellationToken);
try
{
this.ModifyDnsPacket(packet, addr);
Expand All @@ -84,7 +80,7 @@ public async Task InterceptAsync(CancellationToken cancellationToken)
}
finally
{
await divert.SendAsync(packet, addr);
await divert.SendAsync(packet, addr, cancellationToken);
}
}
}
Expand Down Expand Up @@ -159,7 +155,7 @@ unsafe private void ModifyDnsPacket(WinDivertPacket packet, WinDivertAddress add
}
else
{
addr.Flags ^= WinDivertAddressFlag.Outbound;
addr.Flags &= ~WinDivertAddressFlag.Outbound;
}

packet.CalcChecksums(addr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="DNS" Version="7.0.0" />
<PackageReference Include="WindivertDotnet" Version="1.0.2" />
<PackageReference Include="WindivertDotnet" Version="1.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
21 changes: 6 additions & 15 deletions FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public async Task InterceptAsync(CancellationToken cancellationToken)
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)
{
this.logger.LogInformation($"{IPAddress.Loopback}:{this.oldServerPort} <=> {IPAddress.Loopback}:{this.newServerPort}");
Expand All @@ -59,13 +62,10 @@ public async Task InterceptAsync(CancellationToken cancellationToken)
{
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)
{
await divert.RecvAsync(packet, addr);
await divert.RecvAsync(packet, addr, cancellationToken);
try
{
this.ModifyTcpPacket(packet, addr);
Expand All @@ -76,7 +76,7 @@ public async Task InterceptAsync(CancellationToken cancellationToken)
}
finally
{
await divert.SendAsync(packet, addr);
await divert.SendAsync(packet, addr, cancellationToken);
}
}
}
Expand All @@ -89,15 +89,6 @@ public async Task InterceptAsync(CancellationToken cancellationToken)
unsafe private void ModifyTcpPacket(WinDivertPacket packet, WinDivertAddress addr)
{
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)
{
result.TcpHeader->DstPort = this.newServerPort;
Expand Down

0 comments on commit 81c6ebd

Please sign in to comment.