Пример #1
0
 void client_MessageReceivedEvent(Secure_Stream client, Tcp_Message ms)
 {
     if (ReceiveEvent != null)
     {
         ReceiveEvent(ms);
     }
 }
Пример #2
0
        public static Tcp_Message FromBuffer(byte[] b)
        {
            var m = new Tcp_Message(1);

            m.Blocks.Clear();

            var curbuff    = 0;
            var tempbuffer = new byte[4];

            Buffer.BlockCopy(b, curbuff, tempbuffer, 0, 4);
            curbuff += 4;
            var end = BitConverter.ToInt32(tempbuffer, 0);

            for (var i = 0; i < end; i++)
            {
                Buffer.BlockCopy(b, curbuff, tempbuffer, 0, 4);
                curbuff += 4;
                var tmpbuf = new byte[BitConverter.ToInt32(tempbuffer, 0)];
                Buffer.BlockCopy(b, curbuff, tmpbuf, 0, tmpbuf.Length);
                curbuff += tmpbuf.Length;
                m.Add_Block(tmpbuf);
            }

            return(m);
        }
 void _Desktop_Service_MouseImageChangedEvent(Point tl, byte[] data)
 {
     var ms = new Tcp_Message((int)Desktop_Sharing_Shared.Message_Types.MOUSE_IMAGE_EVENT);
     ms.Add_Block(BitConverter.GetBytes(tl.Y));
     ms.Add_Block(BitConverter.GetBytes(tl.X));
     ms.Add_Block(data);
     _Server.Send(ms);
 }
Пример #4
0
        public void Encrypt_And_Send(Tcp_Message m)
        {
            Write(m);
            var l = m.length;

            _Bytes_Sent_in_Window += l;
            Sent_Total            += l;
            UpdateCounters();
        }
Пример #5
0
        private void ReadMessageCallback(IAsyncResult ar)
        {
            try
            {
                // Debug.WriteLine("ReadMessageCallback");
                var state      = (MessageObject)ar.AsyncState;
                var handler    = state.Client;
                var bytesCount = handler.Client.EndReceive(ar);
                if (bytesCount > 0)
                {
                    state.BytesCounter += bytesCount;
                    if (state.BytesCounter == state.MessageBuffer.Length)
                    {                             //done receiving message
                        _BeginRead(state.Client); //start over again

                        using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
                        {
                            aes.KeySize = handler._MySessionKey.Length * 8;
                            aes.Key     = handler._MySessionKey;
                            var iv = new byte[MessageObject.IVSize];
                            Buffer.BlockCopy(state.HeaderBuffer, 0, iv, 0, MessageObject.IVSize);

                            aes.IV      = iv;
                            aes.Mode    = CipherMode.CBC;
                            aes.Padding = PaddingMode.PKCS7;

                            using (ICryptoTransform decrypt = aes.CreateDecryptor())
                            {
                                var arrybuf = decrypt.TransformFinalBlock(state.MessageBuffer, 0, state.MessageBuffer.Length);
                                Received_Total            += state.MessageBuffer.Length;
                                _Bytes_Received_in_Window += state.MessageBuffer.Length;
                                UpdateCounters();


                                if (handler.MessageReceivedEvent != null)
                                {
                                    handler.MessageReceivedEvent(state.Client, Tcp_Message.FromBuffer(arrybuf));
                                }
                                // Debug.WriteLine("ReadMessageCallback DONE");
                            }
                        }
                    }
                    else
                    {
                        handler.Client.BeginReceive(state.MessageBuffer, state.BytesCounter, state.MessageBuffer.Length - state.BytesCounter, 0, new AsyncCallback(ReadMessageCallback), state);
                    }
                }
            } catch (Exception e)
            {
                if (DisconnectEvent != null)
                {
                    DisconnectEvent(this);
                }
                Debug.WriteLine(e.Message);
            }
        }
Пример #6
0
 public void Send(Tcp_Message m)
 {
     lock (_ClientLock)
     {
         _Clients.RemoveAll(a => a.Client == null);
         foreach (var item in _Clients)
         {
             item.Encrypt_And_Send(m);
         }
     }
 }
Пример #7
0
        protected void Write(Tcp_Message m)
        {
            try
            {
                using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
                {
                    aes.KeySize = _MySessionKey.Length * 8;
                    aes.Key     = _MySessionKey;
                    aes.GenerateIV();
                    aes.Mode    = CipherMode.CBC;
                    aes.Padding = PaddingMode.PKCS7;

                    using (ICryptoTransform encrypt = aes.CreateEncryptor())
                    {
                        var sendbuffer    = Tcp_Message.ToBuffer(m);
                        var headermessage = new MessageObject();
                        headermessage.Client        = this;
                        headermessage.MessageBuffer = headermessage.HeaderBuffer;
                        var datamessage = new MessageObject();
                        datamessage.Client = this;

                        datamessage.MessageBuffer = encrypt.TransformFinalBlock(sendbuffer, 0, sendbuffer.Length);


                        Buffer.BlockCopy(aes.IV, 0, headermessage.MessageBuffer, 0, aes.IV.Length);

                        var len = BitConverter.GetBytes(datamessage.MessageBuffer.Length);
                        Buffer.BlockCopy(len, 0, headermessage.MessageBuffer, aes.IV.Length, len.Length);

                        //  Debug.WriteLine("Sending ");
                        //   _SendResetEvent.WaitOne();

                        Client.BeginSend(headermessage.MessageBuffer, 0, headermessage.MessageBuffer.Length, 0, new AsyncCallback(SendMessageCallback), headermessage);
                        Client.BeginSend(datamessage.MessageBuffer, 0, datamessage.MessageBuffer.Length, 0, new AsyncCallback(SendMessageCallback), datamessage);
                    }
                }
            } catch (Exception e)
            {
                //  _SendResetEvent.Set();//just in case another thread was waiting
                if (DisconnectEvent != null)
                {
                    DisconnectEvent(this);
                }
                Debug.WriteLine(e.Message);
            }
        }
Пример #8
0
 public static byte[] ToBuffer(Tcp_Message m)
 {
     var sizeneeded = 4 + (m.Blocks.Count * 4);
     foreach(var item in m.Blocks)
         sizeneeded += item.Length;
     var inbuffer = new byte[sizeneeded];
     var curbuff = 0;
     Buffer.BlockCopy(BitConverter.GetBytes(m.Blocks.Count), 0, inbuffer, curbuff, 4);
     curbuff += 4;
     foreach(var item in m.Blocks)
     {
         Buffer.BlockCopy(BitConverter.GetBytes(item.Length), 0, inbuffer, curbuff, 4);
         curbuff += 4;
         Buffer.BlockCopy(item, 0, inbuffer, curbuff, item.Length);
         curbuff += item.Length;
     }
     return inbuffer;
 }
Пример #9
0
        public static Tcp_Message FromBuffer(byte[] b)
        {
            var m = new Tcp_Message(1);
            m.Blocks.Clear();

            var curbuff = 0;
            var tempbuffer = new byte[4];
            Buffer.BlockCopy(b, curbuff, tempbuffer, 0, 4);
            curbuff += 4;
            var end = BitConverter.ToInt32(tempbuffer, 0);
            for(var i = 0; i < end; i++)
            {
                Buffer.BlockCopy(b, curbuff, tempbuffer, 0, 4);
                curbuff += 4;
                var tmpbuf = new byte[BitConverter.ToInt32(tempbuffer, 0)];
                Buffer.BlockCopy(b, curbuff, tmpbuf, 0, tmpbuf.Length);
                curbuff += tmpbuf.Length;
                m.Add_Block(tmpbuf);
            }

            return m;
        }
Пример #10
0
        public static byte[] ToBuffer(Tcp_Message m)
        {
            var sizeneeded = 4 + (m.Blocks.Count * 4);

            foreach (var item in m.Blocks)
            {
                sizeneeded += item.Length;
            }
            var inbuffer = new byte[sizeneeded];
            var curbuff  = 0;

            Buffer.BlockCopy(BitConverter.GetBytes(m.Blocks.Count), 0, inbuffer, curbuff, 4);
            curbuff += 4;
            foreach (var item in m.Blocks)
            {
                Buffer.BlockCopy(BitConverter.GetBytes(item.Length), 0, inbuffer, curbuff, 4);
                curbuff += 4;
                Buffer.BlockCopy(item, 0, inbuffer, curbuff, item.Length);
                curbuff += item.Length;
            }
            return(inbuffer);
        }
Пример #11
0
        protected void Write(Tcp_Message m)
        {
            try
            {
                using(AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
                {
                    aes.KeySize = _MySessionKey.Length * 8;
                    aes.Key = _MySessionKey;
                    aes.GenerateIV();
                    aes.Mode = CipherMode.CBC;
                    aes.Padding = PaddingMode.PKCS7;

                    using(ICryptoTransform encrypt = aes.CreateEncryptor())
                    {

                        var sendbuffer = Tcp_Message.ToBuffer(m);
                        var headermessage = new MessageObject();
                        headermessage.Client = this;
                        headermessage.MessageBuffer = headermessage.HeaderBuffer;
                        var datamessage = new MessageObject();
                        datamessage.Client = this;

                        datamessage.MessageBuffer = encrypt.TransformFinalBlock(sendbuffer, 0, sendbuffer.Length);

                        Buffer.BlockCopy(aes.IV, 0, headermessage.MessageBuffer, 0, aes.IV.Length);

                        var len = BitConverter.GetBytes(datamessage.MessageBuffer.Length);
                        Buffer.BlockCopy(len, 0, headermessage.MessageBuffer, aes.IV.Length, len.Length);

                      //  Debug.WriteLine("Sending ");
                     //   _SendResetEvent.WaitOne();

                        Client.BeginSend(headermessage.MessageBuffer, 0, headermessage.MessageBuffer.Length, 0, new AsyncCallback(SendMessageCallback), headermessage);
                        Client.BeginSend(datamessage.MessageBuffer, 0, datamessage.MessageBuffer.Length, 0, new AsyncCallback(SendMessageCallback), datamessage);

                    }
                }
            } catch(Exception e)
            {
              //  _SendResetEvent.Set();//just in case another thread was waiting
                if(DisconnectEvent != null)
                    DisconnectEvent(this);
                Debug.WriteLine(e.Message);
            }
        }
Пример #12
0
 public void Encrypt_And_Send(Tcp_Message m)
 {
     Write(m);
     var l = m.length;
     _Bytes_Sent_in_Window += l;
     Sent_Total += l;
     UpdateCounters();
 }
 void _Desktop_Service_MousePositionChangedEvent(Point tl)
 {
     var ms = new Tcp_Message((int)Desktop_Sharing_Shared.Message_Types.MOUSE_POSITION_EVENT);
     ms.Add_Block(BitConverter.GetBytes(tl.Y));
     ms.Add_Block(BitConverter.GetBytes(tl.X));
     _Server.Send(ms);
 }
        private void _server_ReceiveEvent(Tcp_Message ms)
        {
            try
            {
                if(ms.Type == (int)Desktop_Sharing_Shared.Message_Types.MOUSE_EVENT)
                {
                    int width = Screen.AllScreens.Sum(a => a.Bounds.Width);
                    int height = Screen.AllScreens.Max(a => a.Bounds.Height);
                    _Desktop_Service.MouseEvent(new Desktop_Sharing_Shared.Mouse.MouseEventStruct
                    {
                        msg = (Desktop_Sharing_Shared.Mouse.PInvoke.WinFormMouseEventFlags)BitConverter.ToInt32(ms.Blocks[1], 0),
                        wheel_delta = BitConverter.ToInt32(ms.Blocks[4], 0),
                        x = (int)((double)BitConverter.ToInt32(ms.Blocks[2], 0) / (double)width * (double)65535),
                        y = (int)((double)BitConverter.ToInt32(ms.Blocks[3], 0) / (double)height * (double)65535)
                    });

                } else if(ms.Type == (int)Desktop_Sharing_Shared.Message_Types.KEY_EVENT)
                {
                    _Desktop_Service.KeyEvent(new Desktop_Sharing_Shared.Keyboard.KeyboardEventStruct
                    {
                        bVk = BitConverter.ToInt32(ms.Blocks[1], 0),
                        s = (Desktop_Sharing_Shared.Keyboard.PInvoke.PInvoke_KeyState)BitConverter.ToInt32(ms.Blocks[2], 0)
                    });
                } else if(ms.Type == (int)Desktop_Sharing_Shared.Message_Types.FILE)
                {
                    _Desktop_Service.FileEvent(Desktop_Sharing_Shared.Utilities.Format.GetString(ms.Blocks[1]), ms.Blocks[2]);
                } else if(ms.Type == (int)Desktop_Sharing_Shared.Message_Types.FOLDER)
                {
                    _Desktop_Service.FolderEvent(Desktop_Sharing_Shared.Utilities.Format.GetString(ms.Blocks[1]));
                }
            } catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
        void _Server_NewClientEvent(Secure_Stream client)
        {
            if(!_Desktop_Service.Capturing)
            {
                _Desktop_Service.Capturing = true;
                Thread.Sleep(200);//make sure to sleep long enough for the background service to start up and get an image if needed.
            }
            var ms = new Tcp_Message((int)Desktop_Sharing_Shared.Message_Types.RESOLUTION_CHANGE);
            var tmp = _Desktop_Service._LastImage;//make sure to get a copy
            ms.Add_Block(BitConverter.GetBytes(tmp.Dimensions.Height));
            ms.Add_Block(BitConverter.GetBytes(tmp.Dimensions.Width));

            byte[] imgbytes;
            using(var msout = new MemoryStream())
            {
                unsafe
                {
                    fixed(byte* datb = tmp.Data)
                    {
                        using(Bitmap image = new Bitmap(tmp.Dimensions.Width, tmp.Dimensions.Height, tmp.Dimensions.Width * 4, PixelFormat.Format32bppRgb, new IntPtr(datb)))
                        {
                            image.Save(msout, ImageFormat.Png);
                            imgbytes = msout.ToArray();

                        }
                    }
                }
            }
            ms.Add_Block(imgbytes);
            client.Encrypt_And_Send(ms);
        }
        private void _Desktop_Service_ScreenUpdateEvent(byte[] data, Rectangle r)
        {
            var ms = new Tcp_Message((int)Desktop_Sharing_Shared.Message_Types.UPDATE_REGION);
            ms.Add_Block(BitConverter.GetBytes(r.Top));
            ms.Add_Block(BitConverter.GetBytes(r.Left));
            ms.Add_Block(BitConverter.GetBytes(r.Height));
            ms.Add_Block(BitConverter.GetBytes(r.Width));

            byte[] imgbytes;
            using(var msout = new MemoryStream())
            {
                unsafe
                {
                    fixed(byte* datb = data)
                    {
                        using(Bitmap image = new Bitmap(r.Width, r.Height, r.Width * 4, PixelFormat.Format32bppRgb, new IntPtr(datb)))
                        {
                            image.Save(msout, ImageFormat.Png);
                            imgbytes = msout.ToArray();

                        }
                    }
                }
            }
            ms.Add_Block(imgbytes);
            _Server.Send(ms);
        }