Skip to content

Commit d5e86c0

Browse files
committed
1.0.0.38
1 parent 5f3a88f commit d5e86c0

File tree

6 files changed

+614
-575
lines changed

6 files changed

+614
-575
lines changed

WPELibrary/Lib/Socket_Cache.cs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public struct in_addr
3939
public S_un _S_un;
4040

4141
[StructLayout(LayoutKind.Explicit)]
42+
4243
public struct S_un
4344
{
4445
[FieldOffset(0)]
@@ -80,13 +81,15 @@ public struct sockaddr
8081
}
8182

8283
[StructLayout(LayoutKind.Sequential)]
83-
public unsafe struct WSABUF
84+
85+
public struct WSABUF
8486
{
8587
public int len;
8688
public IntPtr buf;
8789
}
8890

8991
[StructLayout(LayoutKind.Sequential)]
92+
9093
public struct OVERLAPPED
9194
{
9295
public IntPtr InternalLow;
@@ -131,7 +134,7 @@ public enum EncodingFormat
131134
UTF7,
132135
}
133136

134-
#endregion
137+
#endregion
135138
}
136139

137140
#endregion
@@ -742,6 +745,43 @@ public static int GetFilterNum_ByFilterIndex(int FIndex)
742745

743746
#region//执行滤镜
744747

748+
public static void DoFilter_WSABUF(IntPtr lpBuffers, int dwBufferCount, int BytesCNT)
749+
{
750+
try
751+
{
752+
int BytesLeft = BytesCNT;
753+
754+
for (int i = 0; i < dwBufferCount; i++)
755+
{
756+
if (BytesLeft > 0)
757+
{
758+
IntPtr lpNewBuffer = IntPtr.Add(lpBuffers, Marshal.SizeOf(typeof(Socket_Cache.SocketPacket.WSABUF)) * i);
759+
Socket_Cache.SocketPacket.WSABUF wsBuffer = Marshal.PtrToStructure<Socket_Cache.SocketPacket.WSABUF>(lpNewBuffer);
760+
761+
int iBuffLen = 0;
762+
763+
if (wsBuffer.len >= BytesLeft)
764+
{
765+
iBuffLen = BytesLeft;
766+
}
767+
else
768+
{
769+
iBuffLen = wsBuffer.len;
770+
}
771+
772+
BytesLeft -= iBuffLen;
773+
774+
Socket_Cache.FilterList.DoFilter(wsBuffer.buf, iBuffLen);
775+
}
776+
777+
}
778+
}
779+
catch (Exception ex)
780+
{
781+
Socket_Operation.DoLog(MethodBase.GetCurrentMethod().Name, ex.Message);
782+
}
783+
}
784+
745785
public static void DoFilter(IntPtr ipBuff, int iLen)
746786
{
747787
string sFName = string.Empty;

WPELibrary/Lib/Socket_Operation.cs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,11 @@ public static string GetIPString_BySocketAddr(int pSocket, Socket_Cache.SocketPa
344344
sIP_From = Socket_Operation.GetIP_BySocket(pSocket, Socket_Cache.SocketPacket.IPType.From);
345345

346346
if (pType == Socket_Cache.SocketPacket.PacketType.Send || pType == Socket_Cache.SocketPacket.PacketType.Recv || pType == Socket_Cache.SocketPacket.PacketType.WSASend || pType == Socket_Cache.SocketPacket.PacketType.WSARecv)
347-
{
347+
{
348348
sIP_To = Socket_Operation.GetIP_BySocket(pSocket, Socket_Cache.SocketPacket.IPType.To);
349349
}
350350
else if (pType == Socket_Cache.SocketPacket.PacketType.SendTo || pType == Socket_Cache.SocketPacket.PacketType.RecvFrom || pType == Socket_Cache.SocketPacket.PacketType.WSASendTo || pType == Socket_Cache.SocketPacket.PacketType.WSARecvFrom)
351-
{
351+
{
352352
sIP_To = Socket_Operation.GetIP_ByAddr(pAddr);
353353
}
354354

@@ -492,38 +492,47 @@ public static string GetPacketData_Hex(byte[] bBuff, int Max_DataLen)
492492

493493
#region//获取WSABUF数组的字节数组
494494

495-
public static unsafe byte[] GetByteFromWSABUF(IntPtr lpBuffers, Int32 dwBufferCount, int BytesCNT)
495+
public static byte[] GetByteFromWSABUF(IntPtr lpBuffers, Int32 dwBufferCount, int BytesCNT)
496496
{
497-
byte[] bByteBuff = new byte[0];
497+
byte[] bReturn = new byte[0];
498498

499-
int BytesLeft = BytesCNT;
500-
501-
for (int i = 0; i < dwBufferCount; i++)
499+
try
502500
{
503-
IntPtr lpNewBuffer = IntPtr.Add(lpBuffers, sizeof(Socket_Cache.SocketPacket.WSABUF) * i);
504-
Socket_Cache.SocketPacket.WSABUF wsBuffer = Marshal.PtrToStructure<Socket_Cache.SocketPacket.WSABUF>(lpNewBuffer);
505-
506-
if (wsBuffer.len >= BytesLeft)
501+
int BytesLeft = BytesCNT;
502+
503+
for (int i = 0; i < dwBufferCount; i++)
507504
{
508-
byte[] bBuffer = new byte[BytesLeft];
509-
Marshal.Copy(wsBuffer.buf, bBuffer, 0, bBuffer.Length);
505+
if (BytesLeft > 0)
506+
{
507+
IntPtr lpNewBuffer = IntPtr.Add(lpBuffers, Marshal.SizeOf(typeof(Socket_Cache.SocketPacket.WSABUF)) * i);
508+
Socket_Cache.SocketPacket.WSABUF wsBuffer = Marshal.PtrToStructure<Socket_Cache.SocketPacket.WSABUF>(lpNewBuffer);
510509

511-
bByteBuff = bByteBuff.Concat(bBuffer).ToArray();
510+
int iBuffLen = 0;
512511

513-
break;
514-
}
515-
else
516-
{
517-
byte[] bBuffer = new byte[wsBuffer.len];
518-
Marshal.Copy(wsBuffer.buf, bBuffer, 0, bBuffer.Length);
512+
if (wsBuffer.len >= BytesLeft)
513+
{
514+
iBuffLen = BytesLeft;
515+
}
516+
else
517+
{
518+
iBuffLen = wsBuffer.len;
519+
}
519520

520-
bByteBuff = bByteBuff.Concat(bBuffer).ToArray();
521+
BytesLeft -= iBuffLen;
521522

522-
BytesLeft -= wsBuffer.len;
523+
byte[] bBuff = new byte[iBuffLen];
524+
Marshal.Copy(wsBuffer.buf, bBuff, 0, iBuffLen);
525+
526+
bReturn = bReturn.Concat(bBuff).ToArray();
527+
}
523528
}
524529
}
530+
catch (Exception ex)
531+
{
532+
Socket_Operation.DoLog(MethodBase.GetCurrentMethod().Name, ex.Message);
533+
}
525534

526-
return bByteBuff;
535+
return bReturn;
527536
}
528537

529538
#endregion
@@ -1459,7 +1468,7 @@ public static List<int> CheckFilter_IsMatch_Adcanced(Socket_FilterInfo sfi, IntP
14591468
}
14601469
catch (Exception ex)
14611470
{
1462-
Socket_Operation.DoLog(MethodBase.GetCurrentMethod().Name, ex.Message);
1471+
Socket_Operation.DoLog(MethodBase.GetCurrentMethod().Name, MultiLanguage.GetDefaultLanguage(MultiLanguage.MutiLan_53) + ex.Message);
14631472
}
14641473

14651474
return lReturn;

WPELibrary/Lib/WS2_32.cs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,15 @@ public static unsafe Int32 RecvFrom_Hook(Int32 socket, IntPtr buffer, Int32 leng
146146

147147
public static unsafe SocketError WSASend_Hook(Int32 Socket, IntPtr lpBuffers, Int32 dwBufferCount, IntPtr lpNumberOfBytesSent, ref SocketFlags dwFlags, IntPtr lpOverlapped, IntPtr lpCompletionRoutine)
148148
{
149-
for (int i = 0; i < dwBufferCount; i++)
150-
{
151-
IntPtr lpNewBuffer = IntPtr.Add(lpBuffers, sizeof(Socket_Cache.SocketPacket.WSABUF) * i);
149+
int BytesSent = Marshal.ReadInt32(lpNumberOfBytesSent);
152150

153-
Socket_Cache.SocketPacket.WSABUF wsBuffer = Marshal.PtrToStructure<Socket_Cache.SocketPacket.WSABUF>(lpNewBuffer);
154-
Socket_Cache.FilterList.DoFilter(wsBuffer.buf, wsBuffer.len);
155-
}
151+
Socket_Cache.FilterList.DoFilter_WSABUF(lpBuffers, dwBufferCount, BytesSent);
156152

157-
SocketError res = WSASend(Socket, lpBuffers, dwBufferCount, lpNumberOfBytesSent, ref dwFlags, lpOverlapped, lpCompletionRoutine);
158-
int BytesSent = Marshal.ReadInt32(lpNumberOfBytesSent);
153+
SocketError res = WSASend(Socket, lpBuffers, dwBufferCount, lpNumberOfBytesSent, ref dwFlags, lpOverlapped, lpCompletionRoutine);
159154

160-
if (res == SocketError.Success && BytesSent > 0)
155+
if (res == SocketError.Success)
161156
{
157+
BytesSent = Marshal.ReadInt32(lpNumberOfBytesSent);
162158
byte[] bBuff = Socket_Operation.GetByteFromWSABUF(lpBuffers, dwBufferCount, BytesSent);
163159

164160
Task.Run(() =>
@@ -184,18 +180,13 @@ public static unsafe SocketError WSASend_Hook(Int32 Socket, IntPtr lpBuffers, In
184180

185181
public static unsafe SocketError WSARecv_Hook(Int32 Socket, IntPtr lpBuffers, Int32 dwBufferCount, IntPtr lpNumberOfBytesRecvd, ref SocketFlags flags, IntPtr overlapped, IntPtr completionRoutine)
186182
{
187-
SocketError res = WSARecv(Socket, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, ref flags, overlapped, completionRoutine);
188-
int BytesRecvd = Marshal.ReadInt32(lpNumberOfBytesRecvd);
183+
SocketError res = WSARecv(Socket, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, ref flags, overlapped, completionRoutine);
189184

190-
if (res == SocketError.Success && BytesRecvd > 0)
185+
if (res == SocketError.Success)
191186
{
192-
for (int i = 0; i < dwBufferCount; i++)
193-
{
194-
IntPtr lpNewBuffer = IntPtr.Add(lpBuffers, sizeof(Socket_Cache.SocketPacket.WSABUF) * i);
187+
int BytesRecvd = Marshal.ReadInt32(lpNumberOfBytesRecvd);
195188

196-
Socket_Cache.SocketPacket.WSABUF wsBuffer = Marshal.PtrToStructure<Socket_Cache.SocketPacket.WSABUF>(lpNewBuffer);
197-
Socket_Cache.FilterList.DoFilter(wsBuffer.buf, wsBuffer.len);
198-
}
189+
Socket_Cache.FilterList.DoFilter_WSABUF(lpBuffers, dwBufferCount, BytesRecvd);
199190

200191
byte[] bBuff = Socket_Operation.GetByteFromWSABUF(lpBuffers, dwBufferCount, BytesRecvd);
201192

@@ -222,19 +213,15 @@ public static unsafe SocketError WSARecv_Hook(Int32 Socket, IntPtr lpBuffers, In
222213

223214
public static unsafe SocketError WSASendTo_Hook(Int32 Socket, IntPtr lpBuffers, Int32 dwBufferCount, IntPtr lpNumberOfBytesSent, ref SocketFlags dwFlags, IntPtr To, Int32 toLenth, IntPtr lpOverlapped, IntPtr lpCompletionRoutine)
224215
{
225-
for (int i = 0; i < dwBufferCount; i++)
226-
{
227-
IntPtr lpNewBuffer = IntPtr.Add(lpBuffers, sizeof(Socket_Cache.SocketPacket.WSABUF) * i);
216+
int BytesSent = Marshal.ReadInt32(lpNumberOfBytesSent);
228217

229-
Socket_Cache.SocketPacket.WSABUF wsBuffer = Marshal.PtrToStructure<Socket_Cache.SocketPacket.WSABUF>(lpNewBuffer);
230-
Socket_Cache.FilterList.DoFilter(wsBuffer.buf, wsBuffer.len);
231-
}
218+
Socket_Cache.FilterList.DoFilter_WSABUF(lpBuffers, dwBufferCount, BytesSent);
232219

233-
SocketError res = WSASendTo(Socket, lpBuffers, dwBufferCount, lpNumberOfBytesSent, ref dwFlags, To, toLenth, lpOverlapped, lpCompletionRoutine);
234-
int BytesSent = Marshal.ReadInt32(lpNumberOfBytesSent);
220+
SocketError res = WSASendTo(Socket, lpBuffers, dwBufferCount, lpNumberOfBytesSent, ref dwFlags, To, toLenth, lpOverlapped, lpCompletionRoutine);
235221

236-
if (res == SocketError.Success && BytesSent > 0)
222+
if (res == SocketError.Success)
237223
{
224+
BytesSent = Marshal.ReadInt32(lpNumberOfBytesSent);
238225
byte[] bBuff = Socket_Operation.GetByteFromWSABUF(lpBuffers, dwBufferCount, BytesSent);
239226

240227
Task.Run(() =>
@@ -261,18 +248,13 @@ public static unsafe SocketError WSASendTo_Hook(Int32 Socket, IntPtr lpBuffers,
261248

262249
public static unsafe SocketError WSARecvFrom_Hook(Int32 Socket, IntPtr lpBuffers, Int32 dwBufferCount, IntPtr lpNumberOfBytesRecvd, ref SocketFlags flags, IntPtr from, Int32 fromLen, IntPtr overlapped, IntPtr completionRoutine)
263250
{
264-
SocketError res = WSARecvFrom(Socket, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, ref flags, from, fromLen, overlapped, completionRoutine);
265-
int BytesRecvd = Marshal.ReadInt32(lpNumberOfBytesRecvd);
251+
SocketError res = WSARecvFrom(Socket, lpBuffers, dwBufferCount, lpNumberOfBytesRecvd, ref flags, from, fromLen, overlapped, completionRoutine);
266252

267-
if (res == SocketError.Success && BytesRecvd > 0)
253+
if (res == SocketError.Success)
268254
{
269-
for (int i = 0; i < dwBufferCount; i++)
270-
{
271-
IntPtr lpNewBuffer = IntPtr.Add(lpBuffers, sizeof(Socket_Cache.SocketPacket.WSABUF) * i);
255+
int BytesRecvd = Marshal.ReadInt32(lpNumberOfBytesRecvd);
272256

273-
Socket_Cache.SocketPacket.WSABUF wsBuffer = Marshal.PtrToStructure<Socket_Cache.SocketPacket.WSABUF>(lpNewBuffer);
274-
Socket_Cache.FilterList.DoFilter(wsBuffer.buf, wsBuffer.len);
275-
}
257+
Socket_Cache.FilterList.DoFilter_WSABUF(lpBuffers, dwBufferCount, BytesRecvd);
276258

277259
byte[] bBuff = Socket_Operation.GetByteFromWSABUF(lpBuffers, dwBufferCount, BytesRecvd);
278260

0 commit comments

Comments
 (0)