Example #1
0
        private static void ClientSend(object obj)
        {
            if (!_connections.TryGetValue((EndPoint)obj, out var client))
            {
                return;
            }
            var stream = client.GetStream();

            while (stream.CanWrite)
            {
                var hp = _toSend.Take();
                stream.Write(HarmonyPacket.Encode(hp));
            }
        }
Example #2
0
        //This is the Method that handles the Client->Server Communication on the Server
        private static void ServerReceive(object obj)
        {
            if (!_connections.TryGetValue((EndPoint)obj, out var client))
            {
                return;
            }
            var stream = client.GetStream();

            while (stream.CanRead)
            {
                var hp = HarmonyPacket.ReadPacket(stream);
                _toProcess.Add(hp);
            }
        }
Example #3
0
 public static void SendAsync(HarmonyPacket p)
 {
     _toSend?.Add(p);
 }
Example #4
0
        private static void ClientConnect()
        {
            if (!int.TryParse(MainWindow.Model.Port, out var port))
            {
                return;
            }
            var tcpClient = new TcpClient(MainWindow.Model.IpAddress, port);

            var stream = tcpClient.GetStream();

            MainWindow.Log($"Connected to {tcpClient.Client.RemoteEndPoint} as Client!", false);

            var saltPacket = HarmonyPacket.ReadPacket(stream);

            if (saltPacket.Type != HarmonyPacket.PacketType.SaltPacket)
            {
                MainWindow.Log($"Received unexpected Packet-Type! Expected: {HarmonyPacket.PacketType.SaltPacket}; Actual: {saltPacket.Type}", true);
                tcpClient.Close();
                return;
            }

            _salt = saltPacket.Pack;
            Crypto.Init(MainWindow.Password, _salt);
            MainWindow.Log("Successfully obtained Salt-Packet!", false);

            if (!_connections.TryAdd(tcpClient.Client.RemoteEndPoint, tcpClient))
            {
                return;
            }
            var sendthread = new Thread(ClientSend);

            _threads.Add(sendthread);
            sendthread.Start(tcpClient.Client.RemoteEndPoint);

            {
                var displayPacket = HarmonyPacket.Encode(new HarmonyPacket
                {
                    Type = HarmonyPacket.PacketType.DisplayPacket,
                    Pack = new HarmonyPacket.DisplayPacket
                    {
                        screens = DisplayManager.Displays
                    }
                });
                stream.Write(displayPacket, 0, displayPacket.Length);
                stream.Flush();
                MainWindow.Log("Send Display-Packet!", false);
            }

            {
                var displayPacket = HarmonyPacket.ReadPacket(stream);
                if (displayPacket.Type != HarmonyPacket.PacketType.DisplayPacket)
                {
                    MainWindow.Log($"Received unexpected Packet-Type! Expected: {HarmonyPacket.PacketType.DisplayPacket}; Actual: {displayPacket.Type}", true);
                    stream.Close();
                    tcpClient.Close();
                    return;
                }
                DisplayManager.SetUp(((HarmonyPacket.DisplayPacket)displayPacket.Pack).screens);
                MainWindow.Log("Finished Handshake with Server!", false);
            }


            while (stream.CanRead)
            {
                var packet = HarmonyPacket.ReadPacket(stream);

                switch (packet.Type)
                {
                case HarmonyPacket.PacketType.MousePacket:
                    Mouse.SendInput(packet.Pack);
                    break;

                case HarmonyPacket.PacketType.KeyBoardPacket:
                    var kp = (HarmonyPacket.KeyboardPacket)packet.Pack;
                    Keyboard.SendInput(kp);
                    break;

                case HarmonyPacket.PacketType.DisplayPacket:
                    var dp = ((JObject)packet.Pack).ToObject <HarmonyPacket.DisplayPacket>();
                    DisplayManager.SetUp(dp.screens);
                    break;

                case HarmonyPacket.PacketType.MouseMovePacket:
                    var mmp = (HarmonyPacket.MouseMovePacket)packet.Pack;
                    NativeMethods.SetCursorPos(mmp.PosX - DisplayManager.SlaveMain.Location.X, mmp.PosY - DisplayManager.SlaveMain.Location.Y);
                    break;

                case HarmonyPacket.PacketType.SaltPacket:
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
Example #5
0
        private static void ServerClientHandler(object obj)
        {
            if (!_connections.TryGetValue((EndPoint)obj, out var client))
            {
                return;
            }
            MainWindow.Log($"Connected to {obj} as Server!", false);
            var stream = client.GetStream();
            {
                var saltPacket = HarmonyPacket.Encode(new HarmonyPacket()
                {
                    Type = HarmonyPacket.PacketType.SaltPacket,
                    Pack = _salt
                });

                stream.Write(saltPacket, 0, saltPacket.Length);
                stream.Flush();
                MainWindow.Log("Send Salt-Packet!", false);
            }

            var keyMap = new Dictionary <Keys, bool>();

            foreach (Keys key in Enum.GetValues(typeof(Keys)))
            {
                keyMap[key] = false;
            }

            {
                var displayPacket = HarmonyPacket.ReadPacket(stream);
                if (displayPacket.Type != HarmonyPacket.PacketType.DisplayPacket)
                {
                    MainWindow.Log(
                        $"Received unexpected Packet-Type! Expected: {HarmonyPacket.PacketType.SaltPacket}; Actual: {displayPacket.Type}",
                        true);
                    client.Close();
                    _ = _connections.TryRemove((EndPoint)obj, out _);
                    return;
                }

                foreach (var d in ((HarmonyPacket.DisplayPacket)displayPacket.Pack).screens)
                {
                    if (d.Screen.Location.X == 0 && d.Screen.Location.Y == 0)
                    {
                        DisplayManager.SlaveMain = d;
                    }
                    d.OwnDisplay = false;
                    DisplayManager.AddRight(d);
                }
            }

            {
                var returnPacket = HarmonyPacket.Encode(new HarmonyPacket
                {
                    Type = HarmonyPacket.PacketType.DisplayPacket,
                    Pack = new HarmonyPacket.DisplayPacket
                    {
                        screens = DisplayManager.Displays
                    }
                });
                stream.Write(returnPacket, 0, returnPacket.Length);
                stream.Flush();
                MainWindow.Log($"Finished Handshake with Client {obj}!", false);
            }

            var receive = new Thread(ServerReceive);

            receive.Start(obj);
            _threads.Add(receive);

            var mouseX = 0;
            var mouseY = 0;

            while (stream.CanWrite)
            {
                var hp = _toSend.Take();
                if (hp.Type == HarmonyPacket.PacketType.MouseMovePacket)
                {
                    var mp = (HarmonyPacket.MouseMovePacket)hp.Pack;

                    if (OnSlave == 0)
                    {
                        var onScreen = DisplayManager.GetDisplayFromPoint(mp.PosX, mp.PosY);
                        if (onScreen == null)
                        {
                            continue;
                        }
                        mouseX = mp.PosX;
                        mouseY = mp.PosY;
                        if (onScreen.OwnDisplay)
                        {
                            continue;
                        }

                        OnSlave = 1;
                    }
                    else
                    {
                        var pos = NativeMethods.GetCursorPosition();
                        mouseX += mp.PosX - pos.X;
                        mouseY += mp.PosY - pos.Y;
                        var onScreen = DisplayManager.GetDisplayFromPoint(mouseX, mouseY);
                        if (onScreen == null)
                        {
                            mouseX -= mp.PosX - pos.X;
                            mouseY -= mp.PosY - pos.Y;
                            continue;
                        }

                        if (onScreen.OwnDisplay)
                        {
                            NativeMethods.SetCursorPos(mouseX, mouseY);
                            OnSlave = 0;
                        }

                        mp.PosX = mouseX;
                        mp.PosY = mouseY;
                    }
                }

                if (OnSlave == 0 && hp.Type != HarmonyPacket.PacketType.DisplayPacket)
                {
                    continue;
                }
                var packet = HarmonyPacket.Encode(hp);
                stream.Write(packet, 0, packet.Length);
                stream.Flush();
            }
        }
Example #6
0
        internal static byte[] Encode(HarmonyPacket hp)
        {
            var bytes   = new List <byte>();
            var type    = new [] { (byte)hp.Type };
            var encrypt = true;

            switch (hp.Type)
            {
            case PacketType.MouseMovePacket:
                var mmp  = (MouseMovePacket)hp.Pack;
                var PosX = BitConverter.GetBytes(mmp.PosX);
                var PosY = BitConverter.GetBytes(mmp.PosY);
                bytes.AddRange(PosX);
                bytes.AddRange(PosY);
                break;

            case PacketType.MousePacket:
                var mp        = (MousePacket)hp.Pack;
                var Action    = BitConverter.GetBytes(mp.Action);
                var MouseData = BitConverter.GetBytes(mp.MouseData);
                var Flags     = BitConverter.GetBytes(mp.Flags);
                bytes.AddRange(Action);
                bytes.AddRange(MouseData);
                bytes.AddRange(Flags);
                break;

            case PacketType.KeyBoardPacket:
                var kp     = (KeyboardPacket)hp.Pack;
                var Key    = BitConverter.GetBytes((int)kp.Key);
                var wParam = BitConverter.GetBytes(kp.wParam);
                bytes.AddRange(Key);
                bytes.AddRange(wParam);
                break;

            case PacketType.DisplayPacket:
                var dp      = (DisplayPacket)hp.Pack;
                var screens = JsonConvert.SerializeObject(dp.screens);
                foreach (var c in screens)
                {
                    bytes.AddRange(BitConverter.GetBytes(c));
                }
                break;

            case PacketType.SaltPacket:
                bytes.AddRange((byte[])hp.Pack);
                encrypt = false;
                break;

            case PacketType.MediaControl:
                bytes.Add((byte)((MediaControlPacket)hp.Pack).Action);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            var arr = bytes.ToArray();

            if (encrypt)
            {
                arr = Crypto.Encrypt(arr);
            }
            var length = BitConverter.GetBytes(arr.Length + 1);

            return(length.Concat(type).Concat(arr).ToArray());
        }