public static ServerStatus DoPing(IPEndPoint endPoint, string hostname = null) { var client = new TcpClient(); client.Connect(endPoint); var manager = new NetworkManager(client.GetStream()); manager.WritePacket(new HandshakePacket( NetworkManager.ProtocolVersion, hostname ?? endPoint.Address.ToString(), (ushort)endPoint.Port, NetworkMode.Status), PacketDirection.Serverbound); manager.WritePacket(new StatusRequestPacket(), PacketDirection.Serverbound); var _response = manager.ReadPacket(PacketDirection.Clientbound); if (!(_response is StatusResponsePacket)) { client.Close(); throw new InvalidOperationException("Server returned invalid ping response"); } var response = (StatusResponsePacket)_response; var sent = DateTime.Now; manager.WritePacket(new StatusPingPacket(sent.Ticks), PacketDirection.Serverbound); var _pong = manager.ReadPacket(PacketDirection.Clientbound); if (!(_pong is StatusPingPacket)) { client.Close(); throw new InvalidOperationException("Server returned invalid ping response"); } client.Close(); var pong = (StatusPingPacket)_pong; var time = new DateTime(pong.Time); response.Status.Latency = time - sent; return response.Status; }
public Proxy(NetworkStream client, NetworkStream server, Log log, ProxySettings settings) { Client = client; Server = server; Log = log; ClientStream = new NetworkManager(Client); //ClientStream = new MinecraftStream(new BufferedStream(Client)); ServerStream = new NetworkManager(Server); //ServerStream = new MinecraftStream(new BufferedStream(Server)); CryptoServiceProvider = new RSACryptoServiceProvider(1024); ServerKey = CryptoServiceProvider.ExportParameters(true); Settings = settings; }
public void Connect(IPEndPoint endPoint) { if (Client != null && Client.Connected) throw new InvalidOperationException("Already connected to a server!"); EndPoint = endPoint; Client = new TcpClient(); Client.Connect(EndPoint); NetworkStream = Client.GetStream(); NetworkManager = new NetworkManager(NetworkStream); NetworkingReset = new ManualResetEvent(true); NetworkWorkerThread = new Thread(NetworkWorker); NetworkWorkerThread.Start(); var handshake = new HandshakePacket(NetworkManager.ProtocolVersion, EndPoint.Address.ToString(), (ushort)EndPoint.Port, NetworkMode.Login); SendPacket(handshake); var login = new LoginStartPacket(Session.SelectedProfile.Name); SendPacket(login); }
private void FinializeServerEncryption(EncryptionKeyResponsePacket encryptionKeyResponsePacket) { // Here, we have all the details we need to initialize our // proxy<->server crypto stream. This happens *after* we have // already completed the crypto handshake with the client. // Wrap the server stream in a crypto stream ServerStream = new NetworkManager(new AesStream(Server, ServerSharedKey)); Log.Write("Encrypted server connection established."); // Write the response. This is the first encrypted packet // sent to the client. The correct response is to send // an 0xFC EncryptionKeyResponse with both fields as empty // arrays. var response = new EncryptionKeyResponsePacket { SharedSecret = new byte[0], VerificationToken = new byte[0] }; ClientStream.WritePacket(response, Craft.Net.PacketDirection.Clientbound); Client.Flush(); // Wrap the client stream in a crypto stream ClientStream = new NetworkManager(new AesStream(Client, ClientSharedKey)); Log.Write("Encrypted client connection established."); // And now we're done with encryption and everything can // continue normally. }