Skip to content

Commit

Permalink
v2.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
msasanmh committed May 30, 2023
1 parent f2b1426 commit b52b8b9
Show file tree
Hide file tree
Showing 12 changed files with 877 additions and 371 deletions.
506 changes: 378 additions & 128 deletions SecureDNSClient/Forms/FormMain.Designer.cs

Large diffs are not rendered by default.

185 changes: 143 additions & 42 deletions SecureDNSClient/Forms/FormMain.cs

Large diffs are not rendered by default.

150 changes: 110 additions & 40 deletions SecureDNSClient/MsmhTools/HTTPProxyServer/DPIBypass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,78 @@ namespace MsmhTools.HTTPProxyServer
public class DPIBypass
{
// Max Data Length = 65536
private readonly int MaxDataLength = 65536;
public enum Mode
{
Program,
Random,
Disable
}

public class ProgramMode : DPIBypass
{
private byte[] Data;
private Socket Socket;

/// <summary>
/// Don't chunk the request when size is 65536.
/// </summary>
public bool DontChunkTheBiggestRequest { get; set; } = false;

public bool SendInRandom { get; set; } = false;

public event EventHandler<EventArgs>? OnChunkDetailsReceived;

public ProgramMode(byte[] data, Socket socket)
{
Data = data;
Socket = socket;
}

public void Send(int firstPartOfDataLength, int programFragmentSize, int divideBy, int fragmentDelay = 0)
public void Send(int firstPartOfDataLength, int fragmentSize, int fragmentChunks, int fragmentDelay = 0)
{
int fragmentSize;
if (Data.Length < firstPartOfDataLength)
fragmentSize = programFragmentSize;
else
fragmentSize = Data.Length / divideBy;
if (!SendInRandom)
{
int fragmentSizeOut;
if (Data.Length <= firstPartOfDataLength)
fragmentSizeOut = Math.Min(fragmentSize, Data.Length);
else
fragmentSizeOut = Data.Length / fragmentChunks;

SendDataInNormalFragment1(Data, Socket, fragmentSize, fragmentDelay);
}
}
if (fragmentSizeOut == 0) fragmentSizeOut = 1;
if (fragmentSizeOut > MaxDataLength) fragmentSizeOut = MaxDataLength;

public class RandomMode : DPIBypass
{
private byte[] Data;
private Socket Socket;
public RandomMode(byte[] data, Socket socket)
{
Data = data;
Socket = socket;
}
if (DontChunkTheBiggestRequest)
if (Data.Length == MaxDataLength) fragmentSizeOut = MaxDataLength;

public void Send(int fragmentLength, int fragmentDelay = 0)
{
SendDataInRandomFragment(Data, Socket, fragmentLength, fragmentDelay);
SendDataInNormalFragment1(Data, Socket, fragmentSizeOut, fragmentDelay, OnChunkDetailsReceived);
}
else
{
if (Data.Length <= firstPartOfDataLength)
{
int fragmentSizeOut = Math.Min(fragmentSize, Data.Length);
SendDataInNormalFragment1(Data, Socket, fragmentSizeOut, fragmentDelay, OnChunkDetailsReceived);
}
else
{
int offset = 5;
Random random = new();
fragmentChunks = random.Next(fragmentChunks - offset, fragmentChunks + offset);

if (fragmentChunks == 0) fragmentChunks = 1;
if (fragmentChunks > Data.Length) fragmentChunks = Data.Length;

SendDataInRandomFragment(Data, Socket, fragmentChunks, fragmentDelay, OnChunkDetailsReceived);
}
}
}
}

private static void SendDataInNormalFragment1(byte[] data, Socket socket, int fragmentSize, int fragmentDelay)
private static void SendDataInNormalFragment1(byte[] data, Socket socket, int fragmentSize, int fragmentDelay, EventHandler<EventArgs>? onChunkDetailsReceived)
{
// Send data
// Create packets
List<byte[]> packets = new();
packets.Clear();
int prevIndex = 0;
for (int n = 0; n < data.Length; n += fragmentSize)
{
Expand All @@ -65,6 +89,23 @@ private static void SendDataInNormalFragment1(byte[] data, Socket socket, int fr
byte[] fragmentData = new byte[fragmentSize];
prevIndex = n;
Buffer.BlockCopy(data, n, fragmentData, 0, fragmentSize);
packets.Add(fragmentData);
}
catch (Exception ex)
{
packets.Clear();
string msgEvent = $"Error, Creating normal packets: {ex.Message}";
Console.WriteLine(msgEvent);
return;
}
}

// Send packets
for (int i = 0; i < packets.Count; i++)
{
try
{
byte[] fragmentData = packets[i];
if (socket == null) return;
socket.Send(fragmentData);
if (fragmentDelay > 0)
Expand All @@ -77,9 +118,12 @@ private static void SendDataInNormalFragment1(byte[] data, Socket socket, int fr
return;
}
}


string chunkDetailsEvent = $"Length: {data.Length}, Chunks: {packets.Count}";
onChunkDetailsReceived?.Invoke(chunkDetailsEvent, EventArgs.Empty);

int outLength = prevIndex + fragmentSize;
Debug.WriteLine($"{outLength} = {data.Length}");
Debug.WriteLine($"{outLength} = {data.Length}, Chunks: {packets.Count}");
}

private static void SendDataInNormalFragment2(byte[] data, Socket socket, int fragmentSize, int fragmentDelay)
Expand Down Expand Up @@ -138,13 +182,19 @@ private static void SendDataInNormalFragment3(byte[] data, Socket socket, int fr
Debug.WriteLine($"{outLength} = {data.Length}");
}

private static void SendDataInRandomFragment(byte[] data, Socket socket, int fragmentLength, int fragmentDelay)
private static void SendDataInRandomFragment(byte[] data, Socket socket, int fragmentChunks, int fragmentDelay, EventHandler<EventArgs>? onChunkDetailsReceived)
{
// Send data
fragmentLength = Math.Min(fragmentLength, data.Length);
List<int> indices = GenerateRandomIndices(1, data.Length - 1, fragmentLength - 1);
// Create packets
List<byte[]> packets = new();
packets.Clear();
fragmentChunks = Math.Min(fragmentChunks, data.Length);
List<int> indices;
if (fragmentChunks < data.Length)
indices = GenerateRandomIndices(1, data.Length - 1, fragmentChunks - 1);
else
indices = Enumerable.Range(0, data.Length - 1).ToList();
indices.Sort();

int prevIndex = 0;
for (int n = 0; n < indices.Count; n++)
{
Expand All @@ -153,15 +203,13 @@ private static void SendDataInRandomFragment(byte[] data, Socket socket, int fra
int index = indices[n];
byte[] fragmentData = new byte[index - prevIndex];
Buffer.BlockCopy(data, prevIndex, fragmentData, 0, fragmentData.Length);
packets.Add(fragmentData);
prevIndex = index;
if (socket == null) return;
socket.Send(fragmentData);
if (fragmentDelay > 0)
Task.Delay(fragmentDelay).Wait();
}
catch (Exception ex)
{
string msgEvent = $"Error, SendDataInRandomFragment: {ex.Message}";
packets.Clear();
string msgEvent = $"Error, Creating random packets: {ex.Message}";
Console.WriteLine(msgEvent);
return;
}
Expand All @@ -171,17 +219,39 @@ private static void SendDataInRandomFragment(byte[] data, Socket socket, int fra
{
byte[] lastFragmentData = new byte[data.Length - prevIndex];
Buffer.BlockCopy(data, prevIndex, lastFragmentData, 0, lastFragmentData.Length);
if (socket == null) return;
socket.Send(lastFragmentData);
packets.Add(lastFragmentData);

int outLength = prevIndex + lastFragmentData.Length;
Debug.WriteLine($"{outLength} = {data.Length}");
Debug.WriteLine($"{outLength} = {data.Length}, Chunks: {packets.Count}");
}
catch (Exception ex)
{
string msgEvent = $"Error, SendDataInRandomFragment Last Fragment: {ex.Message}";
packets.Clear();
string msgEvent = $"Error, Creating last random packet: {ex.Message}";
Console.WriteLine(msgEvent);
}

// Send packets
for (int i = 0; i < packets.Count; i++)
{
try
{
byte[] fragmentData = packets[i];
if (socket == null) return;
socket.Send(fragmentData);
if (fragmentDelay > 0)
Task.Delay(fragmentDelay).Wait();
}
catch (Exception ex)
{
string msgEvent = $"Error, SendDataInRandomFragment: {ex.Message}";
Console.WriteLine(msgEvent);
return;
}
}

string chunkDetailsEvent = $"Length: {data.Length}, Chunks: {packets.Count}";
onChunkDetailsReceived?.Invoke(chunkDetailsEvent, EventArgs.Empty);
}

private static List<int> GenerateRandomIndices(int minValue, int maxValue, int count)
Expand Down
Loading

0 comments on commit b52b8b9

Please sign in to comment.