public void Read(PacketReader reader)
 {
     Locale = reader.ReadString16(10);
     Logger.LogToConsole( Locale.ToString() );
     ViewDistance = reader.ReadByte();
     ChatFlags = reader.ReadByte();
     Difficulty = reader.ReadByte();
     ShowCapes = reader.ReadBoolean();
 }
        public override void Read(PacketReader stream)
        {
            int posX = stream.ReadInt();
            short posY = stream.ReadShort();
            int posZ = stream.ReadInt();
            byte sizeX = (byte)(stream.ReadByte() + 1);
            byte sizeY = (byte)(stream.ReadByte() + 1);
            byte sizeZ = (byte)(stream.ReadByte() + 1);

            int o = sizeX * sizeY * sizeZ;
            Chunk = new Chunk(null, new Vector3I(posX, posY, posZ));

            int len = stream.ReadInt();
            stream.ReadBytes(len);
        }
 public void Read(PacketReader reader)
 {
     PacketType type = (PacketType)reader.ReadByte();
     Logger.LogToConsole( type.ToString() );
     SharedSecretLength = reader.ReadShort();
     SharedSecret = reader.ReadBytes(SharedSecretLength);
     VerifyTokenLength = reader.ReadShort();
     VerifyTokenResponse = reader.ReadBytes(VerifyTokenLength);
 }
 public void Read(PacketReader reader)
 {
     ProtocolVersion = (int)reader.ReadByte();
     Logger.LogToConsole( "Protocol version " + ProtocolVersion.ToString() );
     Username = reader.ReadString16(64);
     Logger.LogToConsole( "Username " + Username );
     ServerHost = reader.ReadString16(64);
     ServerPort = reader.ReadInt();
     Logger.LogToConsole( "Server " + ServerHost + ":" + ServerPort );
 }
 public void Read( PacketReader reader )
 {
     reader.ReadByte();
     X = reader.ReadDouble();
     Y = reader.ReadDouble();
     Stance = reader.ReadDouble();
     Z = reader.ReadDouble();
     R = reader.ReadSingle();
     L = reader.ReadSingle();
     OnGround = reader.ReadBoolean();
 }
 public void Read(PacketReader reader)
 {
     Payload = reader.ReadByte();
     Logger.LogToConsole( Payload.ToString() );
 }
 public void Read(PacketReader reader)
 {
     Type = (PacketType)reader.ReadByte();
     Logger.LogToConsole( Type.ToString() );
 }
        public void SessionLoop( byte firstPacket )
        {
            try {
                smpReader = new PacketReader( stream );
                smpWriter = new SMPPacketWriter( stream );
                // See if we have a packet where no real loop should be created
                PacketType packet = ( PacketType )firstPacket;// smpReader.ReadByte();

                switch ( packet ) {
                    case PacketType.SERVER_LIST_PING:
                        // Kick player with server list ping message
                        new DisconnectPacket() {
                            Message = ""//Server.ServerListPingMessage
                        }.Write( smpWriter );

                        // Close connection
                        stream.Close();

                        break;
                    case PacketType.HANDSHAKE:
                        Logger.LogToConsole( "Handshake packet received" );

                        // Initialize handshake packet to be read
                        HandshakePacket handshakePacket = new HandshakePacket();

                        // Read the packet
                        handshakePacket.Read( smpReader );

                        string Name = handshakePacket.Username;
                        IP = IPAddress.Parse( handshakePacket.ServerHost );
                        // Send encryption request
                        KeyPair keyPair = GenerateRSAKeyPair();
                        byte[] publicKey = keyPair.getPublic().getEncoded();
                        byte[] verifyToken = GetRandomBytes( 4 );

                        EncryptionKeyRequestPacket encRequestPacket = new EncryptionKeyRequestPacket() {
                            ServerID = "-",
                            PublicKey = publicKey,
                            PublicKeyLength = ( short )publicKey.Length,
                            VerifyToken = verifyToken,
                            VerifyTokenLength = ( short )verifyToken.Length
                        };

                        // Send the packet
                        Logger.LogToConsole( "Sending encryption request packet" );
                        encRequestPacket.Write( smpWriter );

                        // Initialize encrytion response packet
                        EncryptionKeyResponsePacket encResponsePacket = new EncryptionKeyResponsePacket();

                        // Read the packet
                        Logger.LogToConsole( "Reading encryption response packet" );
                        encResponsePacket.Read( smpReader );

                        // Verify token
                        byte[] decryptedVerifyToken = RSADecrypt( encResponsePacket.VerifyTokenResponse, keyPair.getPrivate() );

                        if ( BitConverter.ToInt32( verifyToken, 0 ).Equals( BitConverter.ToInt32( decryptedVerifyToken, 0 ) ) ) {
                            // Send encryption response with empty payload
                            Logger.LogToConsole( "Sending encryption response packet" );

                            new EncryptionKeyResponsePacket() {
                                SharedSecret = new byte[0],
                                SharedSecretLength = 0,
                                VerifyTokenResponse = new byte[0],
                                VerifyTokenLength = 0
                            }.Write( smpWriter );

                            // Decrypt client's shared key
                            byte[] decryptedSharedKey = RSADecrypt( encResponsePacket.SharedSecret, keyPair.getPrivate() );

                            // Switch to encrypted stream
                            Logger.LogToConsole( "Switching to an encrypted stream" );
                            EncryptedStream = new AesStream( stream, decryptedSharedKey );
                            smpReader = new PacketReader( EncryptedStream );
                            smpWriter = new SMPPacketWriter( EncryptedStream );

                            Logger.LogToConsole( "Sending client statuses packet" );
                            ClientStatusesPacket clientStatusesPacket = new ClientStatusesPacket();
                            smpReader.ReadByte();
                            clientStatusesPacket.Read( smpReader );

                            Logger.LogToConsole( "Sending Login request" );
                            new LoginRequestPacket() {
                                EntityID = 15,
                                LevelType = LevelType.FLAT,
                                Gamemode = ( byte )1,
                                Dimension = ( byte )0,
                                Difficulty = ( byte )0,
                                NotUsed = ( byte )0,
                                MaxPlayers = ( byte )8
                            }.Write( smpWriter );

                            Logger.LogToConsole( "Sending Spawn-Position Packet" );
                            new SpawnPositionPacket() { X = 120, Y = 200, Z = 120 }.Write( smpWriter );

                            Logger.LogToConsole( "Sending time" );
                            smpWriter.Write( PacketType.TIME_UPDATE );
                            long l = 5000;
                            long l2 = 5;
                            smpWriter.Write( l );
                            smpWriter.Write( l2 );
                            smpWriter.Flush();

                            Logger.LogToConsole( "Sending locale" );
                            LocaleAndViewDistancePacket lavdPacket = new LocaleAndViewDistancePacket();
                            smpReader.ReadByte();
                            lavdPacket.Read( smpReader );
                            PacketType type = ( PacketType )smpReader.ReadByte();
                            Logger.LogToConsole( type.ToString() );

                            /*Logger.LogToConsole( "Sending Init" );
                            State = SessionState.LoadingMain;
                            SMPPacketWriter.MakeInitChunk( new Chunk( 1, 1, WorldManager.MainWorld ), smpWriter );

                            Logger.LogToConsole( "Sending Map" );
                            SMPPacketWriter.MakeMapChunkPacket( new Chunk( 1, 1, WorldManager.MainWorld ), smpWriter );*/

                            UpdateChunks();

                            Logger.LogToConsole( "Sending Pos+Look Packet" );
                            new PositionAndLookPacket() {
                                X = 120,
                                L = 0,
                                R = 0,
                                Z = 120,
                                Stance = 120,
                                Y = 200,
                                OnGround = true
                            }.Write( smpWriter );
                            Position = new fCraft.Position( 120, 120, 120 );
                            //WorldManager.MainWorld.Players.Send( PacketWriter.MakeAddEntity( 15, "Jonty800", Position ) );

                            new PositionAndLookPacket().Read( smpReader );

                            // Info.ProcessLogin( this ); <-----throws null, find out why
                            World = WorldManager.MainWorld;
                            Position = WorldManager.MainWorld.Map.Spawn;

                            Info = PlayerDB.FindOrCreateInfoForPlayer( Name, IP );
                            ResetAllBinds();
                            if ( RaisePlayerConnectingEvent( this ) ) return;
                            HasFullyConnected = true;
                            State = SessionState.Online;
                            Server.UpdatePlayerList();
                            RaisePlayerReadyEvent( this );

                        } else {
                            new DisconnectPacket() {
                                Message = "Verification tokens don't match."
                            }.Write( smpWriter );

                            stream.Close();
                        }

                        break;
                    default:
                        Logger.LogToConsole( packet.ToString() );
                        Logger.LogToConsole( "Unknown packet received" );
                        break;
                }
            } catch ( Exception ex ) {
                Logger.LogToConsole( "Error in player's session: " + ex + ex.StackTrace );

                stream.Close();
            }
        }
Beispiel #9
0
        // login logic
        private bool LoginSequence() {
            PacketReader loginPacket = new PacketReader( reader );

            if( loginPacket.opcode != (uint)InputCodes.Handshake ) {
                Logger.LogAlert( "Session.LoginSequence: Unexpected opcode in the first packet: " + loginPacket.opcode );
                LoginFailure( "Unexpected handshake message - possible protocol mismatch!" );
                return false;
            }

            int clientProtocolVersion = (int)loginPacket.ReadByte();
            if( clientProtocolVersion != Config.ProtocolVersion ) {
                Logger.LogAlert( "Session.LoginSequence: Wrong protocol version: " + clientProtocolVersion );
                LoginFailure( "Incompatible protocol version!" );
                return false;
            }

            player.name = loginPacket.ReadString();
            if( !Player.IsValidName( player.name ) ) {
                Logger.LogAlert( "Session.LoginSequence: Unacceptible player name: " + player.name );
                LoginFailure( "Invalid characters in player name!" );
                return false;
            }

            Logger.LogAlert( "Session.LoginSequence: Success! " + player.name + " authenticated." );
            LoginSuccess();

            // string verificationKey = loginPacket.ReadString();
            // byte unused = loginPacket.ReadByte();
            return true;
        }