private void OnClickStart(object sender, RoutedEventArgs e) { if (Model.NotStarted) { Password = PasswordInput.SecurePassword; NetworkCommunicator.Init(); if (Model.IsServer) { MouseHook.Start(); KeyboardHook.Start(); } Model.NotStarted = false; } else { NetworkCommunicator.Close(); if (Model.IsServer) { MouseHook.Stop(); KeyboardHook.Stop(); } DisplayManager.SetUp(); //Reload DisplayManager Model.NotStarted = true; } }
public MainWindow() { InitializeComponent(); Window = this; Log($"The IP-Address of this machine is { NetworkCommunicator.GetLocalIPAddress() }", false); DisplayManager.SetUp(); Model = (HarmonyViewModel)DataContext; VersionLabel.Content = "Harmony-Version: " + typeof(MainWindow).Assembly.GetName().Version; MediaControl.Reload(); MediaControl.UpdateMediaProperties(); }
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(); } } }