internal void BackgroundThread() { Thread thread = new Thread(() => { Clipboard.SetText(string.Format("connect 127.0.0.1:{0}", listenPort)); MessageBox.Show("Se ha copiado en tu portapeles el contenido : \n " + Clipboard.GetText()); }); thread.SetApartmentState(ApartmentState.STA); //Hace falta para escribir en el Clipboard thread.IsBackground = true; thread.CurrentCulture = System.Globalization.CultureInfo.CurrentCulture; thread.Priority = ThreadPriority.Lowest; thread.Name = "RustInterceptorNotificadorPortapapeles"; thread.Start(); thread.Join(); Console.WriteLine("Server address copied to Clipboard (F1 -> Paste -> Enter)"); Console.WriteLine("Listening on {0}", listenPort); clientPacket = new Packet(); clientPeer = RakNetPeer.CreateServer("0.0.0.0", listenPort, 1); var emptyPacket = false; var hasClientPacket = false; var hasServerPacket = false; while (isAlive) { hasClientPacket = clientPeer.Receive(); hasServerPacket = (serverPeer == null ? false : serverPeer.Receive()); if (!hasClientPacket && !hasServerPacket) { Thread.Sleep(1); continue; } if (hasClientPacket) { var ticks = DateTime.Now.Ticks; clientPacket.Receive(clientPeer); var packet = clientPacket.Clone(); if (packet.Length == packet.Position) { emptyPacket = true; } if (packet.type == Packet.PacketType.RAKNET) { switch ((Packet.RakNet)packet.packetID) { case Packet.RakNet.NEW_INCOMING_CONNECTION: Console.Write("Starting Proxy: "); serverPeer = RakNetPeer.CreateConnection(serverIP, serverPort, 32, 100, 1000); while (!serverPeer.Receive()) { Thread.Sleep(10); } serverPacket.Receive(serverPeer); serverPacket.Send(clientPeer, clientGUID); serverPacket.Clear(); Console.WriteLine("Started"); break; case Packet.RakNet.CONNECTION_LOST: isAlive = false; Console.WriteLine("Client Disconnected"); break; } if (!isAlive) { break; } } else { bool shouldDrop = false; if (clientPacket.type == Packet.PacketType.RUST && commandCallback != null) { if (clientPacket.rustID == Packet.Rust.ConsoleCommand) { string command = clientPacket.String(); if (command.StartsWith(CommandPrefix, StringComparison.OrdinalIgnoreCase)) { commandCallback(command.Substring(CommandPrefix.Length)); shouldDrop = true; } } } if (!shouldDrop) { if (ClientPackets && !emptyPacket && IsFiltered(packet)) { if (packetHandlerCallback != null) { packetHandlerCallback(packet); } else { lock (packetQueue) { packetQueue.Enqueue(packet); } } } clientPacket.Send(serverPeer, serverGUID); clientPacket.Clear(); packet.delay = (DateTime.Now.Ticks - ticks); if (RememberFilteredOnly && !IsFiltered(packet) && !ClientPackets) { continue; } if (RememberPackets) { lock (remeberedPackets) { remeberedPackets.Add(packet); } } } } } if (hasServerPacket) { var ticks = DateTime.Now.Ticks; serverPacket.Receive(serverPeer); var packet = serverPacket.Clone(); emptyPacket = false; if (packet.Position == packet.Length) { emptyPacket = true; } if (IsFiltered(packet) && !emptyPacket) { if (packetHandlerCallback != null) { packetHandlerCallback(packet); } else { lock (packetQueue) { packetQueue.Enqueue(packet); } } } serverPacket.Send(clientPeer, clientGUID); serverPacket.Clear(); packet.delay = (DateTime.Now.Ticks - ticks); if (RememberFilteredOnly && !IsFiltered(packet)) { continue; } if (RememberPackets) { lock (remeberedPackets) { remeberedPackets.Add(packet); } } } } clientPeer.Close(); clientPacket.Close(); }
internal void BackgroundThread() { Thread thread = new Thread(() => Clipboard.SetText(string.Format("connect 127.0.0.1:{0}", listenPort))); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); Console.WriteLine("Server address copied to Clipboard (F1 -> Paste -> Enter)"); Console.WriteLine("Listening on {0}", listenPort); clientPeer = RakNetPeer.CreateServer("0.0.0.0", listenPort, 1); var emptyPacket = false; var hasClientPacket = false; var hasServerPacket = false; while (isAlive) { hasClientPacket = clientPeer.Receive(); hasServerPacket = (serverPeer == null ? false : serverPeer.Receive()); if (!hasClientPacket && !hasServerPacket) { Thread.Sleep(1); continue; } if (hasClientPacket) { var ticks = DateTime.Now.Ticks; clientPacket.Receive(clientPeer); var packet = clientPacket.Clone(); if (packet.Length == packet.Position) { emptyPacket = true; } if (packet.type == Packet.PacketType.RAKNET) { switch ((Packet.RakNet)packet.packetID) { case Packet.RakNet.NEW_INCOMING_CONNECTION: Console.Write("Starting Proxy: "); serverPeer = RakNetPeer.CreateConnection(serverIP, serverPort, 32, 100, 1000); while (!serverPeer.Receive()) { Thread.Sleep(10); } serverPacket.Receive(serverPeer); serverPacket.Send(clientPeer, clientGUID); serverPacket.Clear(); Console.WriteLine("Started"); break; case Packet.RakNet.CONNECTION_LOST: isAlive = false; Console.WriteLine("Client Disconnected"); break; } if (!isAlive) { break; } } else if (ClientPackets && !emptyPacket && IsFiltered(packet)) { if (packetHandlerCallback != null) { packetHandlerCallback(packet); } else { lock (packetQueue) { packetQueue.Enqueue(packet); } } } clientPacket.Send(serverPeer, serverGUID); clientPacket.Clear(); packet.delay = (DateTime.Now.Ticks - ticks); if (RememberFilteredOnly && !IsFiltered(packet) && !ClientPackets) { continue; } if (RememberPackets) { lock (remeberedPackets) { remeberedPackets.Add(packet); } } } if (hasServerPacket) { var ticks = DateTime.Now.Ticks; serverPacket.Receive(serverPeer); var packet = serverPacket.Clone(); emptyPacket = false; if (packet.Position == packet.Length) { emptyPacket = true; } if (IsFiltered(packet) && !emptyPacket) { if (packetHandlerCallback != null) { packetHandlerCallback(packet); } else { lock (packetQueue) { packetQueue.Enqueue(packet); } } } serverPacket.Send(clientPeer, clientGUID); serverPacket.Clear(); packet.delay = (DateTime.Now.Ticks - ticks); if (RememberFilteredOnly && !IsFiltered(packet)) { continue; } if (RememberPackets) { lock (remeberedPackets) { remeberedPackets.Add(packet); } } } } }