Преглед изворни кода

解决WinDivert.sys删除不掉的问题

陈国伟 пре 3 година
родитељ
комит
f193a5077c

+ 4 - 5
FastGithub.PacketIntercept/Dns/DnsInterceptor.cs

@@ -1,10 +1,10 @@
 using DNS.Protocol;
 using DNS.Protocol.ResourceRecords;
 using FastGithub.Configuration;
+using FastGithub.WinDiverts;
 using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Options;
 using System;
-using System.Buffers.Binary;
 using System.ComponentModel;
 using System.Diagnostics.CodeAnalysis;
 using System.Linq;
@@ -13,7 +13,6 @@ using System.Runtime.InteropServices;
 using System.Runtime.Versioning;
 using System.Threading;
 using System.Threading.Tasks;
-using WinDivertSharp;
 
 namespace FastGithub.PacketIntercept.Dns
 {
@@ -146,21 +145,21 @@ namespace FastGithub.PacketIntercept.Dns
                 var destAddress = packet.IPv4Header->DstAddr;
                 packet.IPv4Header->DstAddr = packet.IPv4Header->SrcAddr;
                 packet.IPv4Header->SrcAddr = destAddress;
-                packet.IPv4Header->Length = BinaryPrimitives.ReverseEndianness((ushort)packetLength);
+                packet.IPv4Header->Length = (ushort)packetLength;
             }
             else
             {
                 var destAddress = packet.IPv6Header->DstAddr;
                 packet.IPv6Header->DstAddr = packet.IPv6Header->SrcAddr;
                 packet.IPv6Header->SrcAddr = destAddress;
-                packet.IPv6Header->Length = BinaryPrimitives.ReverseEndianness((ushort)packetLength);
+                packet.IPv6Header->Length = (ushort)packetLength;
             }
 
             // 修改udp包
             var destPort = packet.UdpHeader->DstPort;
             packet.UdpHeader->DstPort = packet.UdpHeader->SrcPort;
             packet.UdpHeader->SrcPort = destPort;
-            packet.UdpHeader->Length = BinaryPrimitives.ReverseEndianness((ushort)(sizeof(UdpHeader) + responsePayload.Length));
+            packet.UdpHeader->Length = (ushort)(sizeof(UdpHeader) + responsePayload.Length);
 
             winDivertAddress.Impostor = true;
             WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);

+ 1 - 1
FastGithub.PacketIntercept/FastGithub.PacketIntercept.csproj

@@ -9,7 +9,7 @@
 		<PackageReference Include="DNS" Version="6.1.0" />
 		<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
 		<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
-		<PackageReference Include="WinDivertSharp" Version="1.4.3.3" />
+    <PackageReference Include="FastGithub.WinDiverts" Version="1.4.0" />
 	</ItemGroup>
 
 	<ItemGroup>

+ 5 - 6
FastGithub.PacketIntercept/Tcp/TcpInterceptor.cs

@@ -1,12 +1,11 @@
-using Microsoft.Extensions.Logging;
+using FastGithub.WinDiverts;
+using Microsoft.Extensions.Logging;
 using System;
-using System.Buffers.Binary;
 using System.ComponentModel;
 using System.Net;
 using System.Runtime.Versioning;
 using System.Threading;
 using System.Threading.Tasks;
-using WinDivertSharp;
 
 namespace FastGithub.PacketIntercept.Tcp
 {
@@ -30,8 +29,8 @@ namespace FastGithub.PacketIntercept.Tcp
         public TcpInterceptor(int oldServerPort, int newServerPort, ILogger logger)
         {
             this.filter = $"loopback and (tcp.DstPort == {oldServerPort} or tcp.SrcPort == {newServerPort})";
-            this.oldServerPort = BinaryPrimitives.ReverseEndianness((ushort)oldServerPort);
-            this.newServerPort = BinaryPrimitives.ReverseEndianness((ushort)newServerPort);
+            this.oldServerPort = (ushort)oldServerPort;
+            this.newServerPort = (ushort)newServerPort;
             this.logger = logger;
         }
 
@@ -55,7 +54,7 @@ namespace FastGithub.PacketIntercept.Tcp
                 throw new Win32Exception();
             }
 
-            this.logger.LogInformation($"tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(this.oldServerPort)} => tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(this.newServerPort)}");
+            this.logger.LogInformation($"tcp://{IPAddress.Loopback}:{this.oldServerPort} => tcp://{IPAddress.Loopback}:{this.newServerPort}");
             cancellationToken.Register(hwnd => WinDivert.WinDivertClose((IntPtr)hwnd!), handle);
 
             var packetLength = 0U;

+ 2 - 2
FastGithub.sln

@@ -1,7 +1,7 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio Version 17
-VisualStudioVersion = 17.0.31612.314
+# Visual Studio Version 16
+VisualStudioVersion = 16.0.31507.150
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub", "FastGithub\FastGithub.csproj", "{C1099390-6103-4917-A740-A3002B542FE0}"
 EndProject

+ 3 - 3
pack.sh

@@ -2,14 +2,14 @@
 cd ./FastGithub/bin/publish
  
 # win-x64
-zip -r fastgithub_win-x64.zip fastgithub_win-x64 -x "./fastgithub_win-x64/x86/*" -x "./fastgithub_win-x64/*.pdb"
+zip -r fastgithub_win-x64.zip fastgithub_win-x64
 
 # linux-x64
 chmod 777 ./fastgithub_linux-x64/fastgithub
 chmod 777 ./fastgithub_linux-x64/dnscrypt-proxy/dnscrypt-proxy
-zip -r fastgithub_linux-x64.zip fastgithub_linux-x64 -x "./fastgithub_linux-x64/x64/*" -x "./fastgithub_linux-x64/x86/*" -x "./fastgithub_linux-x64/*.pdb"
+zip -r fastgithub_linux-x64.zip fastgithub_linux-x64
 
 # osx-x64
 chmod 777 ./fastgithub_osx-x64/fastgithub
 chmod 777 ./fastgithub_osx-x64/dnscrypt-proxy/dnscrypt-proxy
-zip -r fastgithub_osx-x64.zip fastgithub_osx-x64 -x "./fastgithub_osx-x64/x64/*" -x "./fastgithub_osx-x64/x86/*" -x "./fastgithub_osx-x64/*.pdb"
+zip -r fastgithub_osx-x64.zip fastgithub_osx-x64