Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 46d87bc

Browse files
committed
🎨 WinDivertSharp.NativeAssets.Win32
1 parent 70ce323 commit 46d87bc

8 files changed

Lines changed: 368 additions & 13 deletions

File tree

ref/WinDivert.dll

33.5 KB
Binary file not shown.

ref/WinDivert64.sys

49.4 KB
Binary file not shown.

src/BD.WTTS.Client.Plugins.Accelerator/BD.WTTS.Client.Plugins.Accelerator.csproj

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<AvaloniaResource Include="UI\Assets\defaultBackground.png" />
2020
<AvaloniaResource Include="UI\Assets\script.ico" />
2121
</ItemGroup>
22-
22+
2323
<ItemGroup>
2424
<Compile Include="..\AssemblyInfo.Xaml.cs">
2525
<LinkBase>Properties</LinkBase>
@@ -110,18 +110,35 @@
110110
</ItemGroup>
111111

112112
<ItemGroup>
113-
<Compile Update="Properties\Resources.Designer.cs">
114-
<DesignTime>True</DesignTime>
115-
<AutoGen>True</AutoGen>
116-
<DependentUpon>Resources.resx</DependentUpon>
117-
</Compile>
113+
<Compile Update="Properties\Resources.Designer.cs">
114+
<DesignTime>True</DesignTime>
115+
<AutoGen>True</AutoGen>
116+
<DependentUpon>Resources.resx</DependentUpon>
117+
</Compile>
118118
</ItemGroup>
119119

120120
<ItemGroup>
121-
<EmbeddedResource Update="Properties\Resources.resx">
122-
<Generator>ResXFileCodeGenerator</Generator>
123-
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
124-
</EmbeddedResource>
121+
<EmbeddedResource Update="Properties\Resources.resx">
122+
<Generator>ResXFileCodeGenerator</Generator>
123+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
124+
</EmbeddedResource>
125+
</ItemGroup>
126+
127+
<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
128+
<Compile Update="Properties\WinDivertResource.Designer.cs">
129+
<DesignTime>True</DesignTime>
130+
<AutoGen>True</AutoGen>
131+
<DependentUpon>WinDivertResource.resx</DependentUpon>
132+
</Compile>
133+
<EmbeddedResource Update="Properties\WinDivertResource.resx">
134+
<Generator>ResXFileCodeGenerator</Generator>
135+
<LastGenOutput>WinDivertResource.Designer.cs</LastGenOutput>
136+
</EmbeddedResource>
137+
</ItemGroup>
138+
139+
<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'windows'">
140+
<Compile Remove="Properties\WinDivertResource.Designer.cs" />
141+
<EmbeddedResource Remove="Properties\WinDivertResource.resx" />
125142
</ItemGroup>
126143

127144
<ItemGroup>
@@ -143,7 +160,7 @@
143160
<Import Project="..\TFM_NETX_WITH_DESKTOP.props" />
144161

145162
<ItemGroup>
146-
<UpToDateCheckInput Remove="UI\Views\Pages\AcceleratorPage2.axaml" />
163+
<UpToDateCheckInput Remove="UI\Views\Pages\AcceleratorPage2.axaml" />
147164
</ItemGroup>
148-
165+
149166
</Project>
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#if WINDOWS
2+
using WinDivertBinRes = BD.WTTS.Properties.WinDivertResource;
3+
4+
namespace Mobius.Helpers;
5+
6+
/// <summary>
7+
/// 初始化 WinDivert 助手类
8+
/// </summary>
9+
static class WinDivertInitHelper
10+
{
11+
const string libraryName = "WinDivert.dll";
12+
13+
static readonly string? libraryPath;
14+
static readonly string? libraryDirPath;
15+
//static nint libraryIntPtr;
16+
17+
///// <summary>
18+
///// 配置本机库路径解析
19+
///// </summary>
20+
///// <param name="libraryName"></param>
21+
///// <param name="assembly"></param>
22+
///// <param name="searchPath"></param>
23+
///// <returns></returns>
24+
//public static nint DllImportResolver(string libraryName,
25+
// Assembly assembly,
26+
// DllImportSearchPath? searchPath)
27+
//{
28+
// switch (libraryName)
29+
// {
30+
// case WinDivertInitHelper.libraryName:
31+
// if (libraryIntPtr != default)
32+
// return libraryIntPtr;
33+
// break;
34+
// }
35+
// return default;
36+
//}
37+
38+
static bool isInitialize = false;
39+
40+
/// <summary>
41+
/// 初始化 WinDivert
42+
/// </summary>
43+
public static async Task InitializeAsync()
44+
{
45+
if (libraryPath != null && libraryDirPath != null)
46+
{
47+
bool isWrite;
48+
var libraryFileInfo = new FileInfo(libraryPath);
49+
if (libraryFileInfo.Exists)
50+
{
51+
if (libraryFileInfo.Length > 20000000L) // 文件不可能大于 20 MB
52+
{
53+
libraryFileInfo.Delete();
54+
isWrite = true;
55+
}
56+
else
57+
{
58+
var libraryBytes = await File.ReadAllBytesAsync(libraryPath);
59+
var libraryHash = SHA256.HashData(libraryBytes);
60+
var libraryHashRes = SHA256.HashData(WinDivertBinRes.WinDivert_dll);
61+
if (libraryHash.SequenceEqual(libraryHashRes)) // 哈希值相同,不写入
62+
{
63+
isWrite = false;
64+
}
65+
else
66+
{
67+
libraryFileInfo.Delete();
68+
isWrite = true;
69+
}
70+
}
71+
}
72+
else
73+
{
74+
isWrite = true;
75+
}
76+
77+
if (isWrite) // 检查文件不存在时写入
78+
{
79+
switch (RuntimeInformation.ProcessArchitecture)
80+
{
81+
//case Architecture.X86:
82+
// break;
83+
case Architecture.X64:
84+
IOPath.DirCreateByNotExists(libraryDirPath);
85+
var task1 = File.WriteAllBytesAsync(Path.Combine(libraryDirPath, "WinDivert64.sys"), WinDivertBinRes.WinDivert64_sys);
86+
var task2 = File.WriteAllBytesAsync(libraryPath, WinDivertBinRes.WinDivert_dll);
87+
await Task.WhenAll(task1, task2);
88+
break;
89+
}
90+
}
91+
}
92+
93+
if (isInitialize)
94+
return;
95+
isInitialize = true;
96+
//if (IsSupported && libraryPath != null)
97+
//{
98+
// try
99+
// {
100+
// libraryIntPtr = NativeLibrary.Load(libraryPath);
101+
// }
102+
// catch
103+
// {
104+
// }
105+
// if (libraryIntPtr != default)
106+
// {
107+
// NativeLibrary.SetDllImportResolver(typeof(WinDivertSharp.WinDivert).Assembly, DllImportResolver);
108+
// return;
109+
// }
110+
//}
111+
IsSupported = false;
112+
}
113+
114+
/// <summary>
115+
/// 当前运行环境是否支持使用 WinDivert
116+
/// </summary>
117+
public static bool IsSupported { get; private set; }
118+
119+
static WinDivertInitHelper()
120+
{
121+
switch (RuntimeInformation.OSArchitecture)
122+
{
123+
//case Architecture.X86:
124+
case Architecture.X64:
125+
IsSupported = true;
126+
libraryDirPath = Path.Combine(AppContext.BaseDirectory, "native", "win-x64");
127+
libraryPath = Path.Combine(libraryDirPath, "WinDivert.dll");
128+
break;
129+
}
130+
}
131+
}
132+
#endif

src/BD.WTTS.Client.Plugins.Accelerator/Properties/WinDivertResource.Designer.cs

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 1.3
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">1.3</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1">this is my long string</data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
[base64 mime encoded serialized .NET Framework object]
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
[base64 mime encoded string representing a byte array form of the .NET Framework object]
27+
</data>
28+
29+
There are any number of "resheader" rows that contain simple
30+
name/value pairs.
31+
32+
Each data row contains a name, and value. The row also contains a
33+
type or mimetype. Type corresponds to a .NET class that support
34+
text/value conversion through the TypeConverter architecture.
35+
Classes that don't support this are serialized and stored with the
36+
mimetype set.
37+
38+
The mimetype is used for serialized objects, and tells the
39+
ResXResourceReader how to depersist the object. This is currently not
40+
extensible. For a given mimetype the value must be set accordingly:
41+
42+
Note - application/x-microsoft.net.object.binary.base64 is the format
43+
that the ResXResourceWriter will generate, however the reader can
44+
read any of the formats listed below.
45+
46+
mimetype: application/x-microsoft.net.object.binary.base64
47+
value : The object must be serialized with
48+
: System.Serialization.Formatters.Binary.BinaryFormatter
49+
: and then encoded with base64 encoding.
50+
51+
mimetype: application/x-microsoft.net.object.soap.base64
52+
value : The object must be serialized with
53+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
54+
: and then encoded with base64 encoding.
55+
56+
mimetype: application/x-microsoft.net.object.bytearray.base64
57+
value : The object must be serialized into a byte array
58+
: using a System.ComponentModel.TypeConverter
59+
: and then encoded with base64 encoding.
60+
-->
61+
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:element name="root" msdata:IsDataSet="true">
64+
<xsd:complexType>
65+
<xsd:choice maxOccurs="unbounded">
66+
<xsd:element name="data">
67+
<xsd:complexType>
68+
<xsd:sequence>
69+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
70+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
73+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
74+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
75+
</xsd:complexType>
76+
</xsd:element>
77+
<xsd:element name="resheader">
78+
<xsd:complexType>
79+
<xsd:sequence>
80+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
81+
</xsd:sequence>
82+
<xsd:attribute name="name" type="xsd:string" use="required" />
83+
</xsd:complexType>
84+
</xsd:element>
85+
</xsd:choice>
86+
</xsd:complexType>
87+
</xsd:element>
88+
</xsd:schema>
89+
<resheader name="resmimetype">
90+
<value>text/microsoft-resx</value>
91+
</resheader>
92+
<resheader name="version">
93+
<value>1.3</value>
94+
</resheader>
95+
<resheader name="reader">
96+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
97+
</resheader>
98+
<resheader name="writer">
99+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
100+
</resheader>
101+
<assembly alias="System.Windows.Forms" name="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
102+
<data name="WinDivert64_sys" type="System.Resources.ResXFileRef, System.Windows.Forms">
103+
<value>..\..\..\ref\WinDivert64.sys;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
104+
</data>
105+
<data name="WinDivert_dll" type="System.Resources.ResXFileRef, System.Windows.Forms">
106+
<value>..\..\..\ref\WinDivert.dll;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
107+
</data>
108+
</root>

0 commit comments

Comments
 (0)