Пример #1
0
        public bool IsAuthenticated()
        {
            if (!Globals.Offlinemode)
            {
                try
                {
                    var uri = new Uri(
                        string.Format(
                            "http://session.minecraft.net/game/checkserver.jsp?user={0}&serverId={1}",
                            Username,
                            PacketCryptography.JavaHexDigest(Encoding.UTF8.GetBytes("")
                                                             .Concat(Wrapper.SharedKey)
                                                             .Concat(PacketCryptography.PublicKeyToAsn1(Globals.ServerKey))
                                                             .ToArray())
                            ));

                    var authenticated = new WebClient().DownloadString(uri);
                    if (authenticated.Contains("NO"))
                    {
                        ConsoleFunctions.WriteInfoLine("Response: " + authenticated);
                        return(false);
                    }
                }
                catch (Exception exc)
                {
                    //client.Kick("Error while authenticating...");
                    //client.Logger.Log(exc);
                    return(false);
                }

                return(true);
            }

            return(true);
        }
Пример #2
0
        public bool IsAuthenticated()
        {
            if (Server.ServerSettings.OnlineMode)
            {
                try
                {
                    var uri = new Uri(
                        string.Format(
                            "http://session.minecraft.net/game/checkserver.jsp?user={0}&serverId={1}",
                            Username,
                            PacketCryptography.JavaHexDigest(Encoding.UTF8.GetBytes("")
                                                             .Concat(Wrapper.SharedKey)
                                                             .Concat(PacketCryptography.PublicKeyToAsn1(Globals.ServerKey))
                                                             .ToArray())
                            ));

                    var authenticated = new WebClient().DownloadString(uri);
                    if (authenticated.Contains("NO"))
                    {
                        ConsoleFunctions.WriteInfoLine("Response: " + authenticated);
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    ConsoleFunctions.WriteWarningLine("Caught Exception in PlayerEntity.cs IsAuth function... {0}, {1}", true, e.StackTrace, e.Message);
                    return(false);
                }
            }
            return(true);
        }
Пример #3
0
        public override void ReadPacket()
        {
            SharedSecretLength = Read <VarInt>();
            SharedSecret       = ReadArray <byte>(SharedSecretLength);
            VerifyTokenLength  = Read <VarInt>();
            VerifyToken        = ReadArray <byte>(VerifyTokenLength);

            VerifyToken = PacketCryptography.Decrypt(VerifyToken);

            if (!VerifyToken.SequenceEqual(PacketCryptography.VerifyToken))
            {
                Client.Send(new DisconnectLoginPaket(Client)
                {
                    Reason = new ChatMessage()
                    {
                        Text = "Verification Failed"
                    }
                });
                return;
            }

            SharedSecret = PacketCryptography.Decrypt(SharedSecret);

            var v = Utils.GetUUID(Player, (Utils.GetHash(Utils.BaseServerHash,
                                                         SharedSecret,
                                                         PacketCryptography.PublicKeyToAsn1(Utils.ServerKey))));

            Client.Crypto = PacketCryptography.GenerateAes(SharedSecret);

            var v2 = v.GetAwaiter().GetResult();

            if (v2 == null)
            {
                Client.Send(new DisconnectLoginPaket(Client)
                {
                    Reason = new ChatMessage()
                    {
                        Text = "Coud not get UUID"
                    }
                });
                return;
            }
            else
            {
                Player.UUID = v2;
            }

            Client.Send(new LoginSuccessPacket(Client));
        }
Пример #4
0
        public override void WritePacket()
        {
            Username = Player.Name;

            if ((Client.Server).Configuration.OnlineMode)
            {
                if (Client.Encryption)
                {
                    Player.UUID = Utils.GetUUID(Player,
                                                Utils.GetHash(Utils.BaseServerHash,
                                                              Client.Crypto.Key,
                                                              PacketCryptography.PublicKeyToAsn1(Utils.ServerKey)))
                                  .Result;
                }
                else
                {
                    Player.UUID = Utils.GetUUID(Player).Result;
                }
            }
            else
            {
                Player.UUID = Utils.NameGuidFromBytes(Encoding.UTF8.GetBytes($"OfflinePlayer:{Player.Name}"));
            }

            UUID = Player.UUID.ToString();
            Write(UUID);
            Write(Username);

            Client.state = State.Play;

            Client.Send(new JoinGamePacket(Client));
            Client.Send(new ServerDifficultyPacket(Client));
            Client.Send(new SpawnPositionPacket(Client));
            Client.Send(new PlayerAbilitiesPacket(Client));
            DoTheChunks();
            DoTheEntities();
            //TODO: Enable Declare Commands
            //Client.Send(new DeclareCommandsPacket(Client));
            Client.Send(new PlayerPositionAndLookPacketClient(Client)
            {
                Flags = TransformFlags.NONE
            });
            DoWelcomeStuff();
        }
Пример #5
0
        private void HandleLogin(int protocol)
        {
            if (Buffer != null)
            {
                var usernameRaw = Buffer.ReadString();
                var username    =
                    new string(usernameRaw.Where(c => char.IsLetter(c) || char.IsPunctuation(c) || char.IsDigit(c)).ToArray());
                //username = Regex.Replace(username, @"[^\u0000-\u007F]", string.Empty);
                var uuid = getUUID(username);

                if (!Globals.Offlinemode)
                {
                    if (Globals.EncryptionEnabled)
                    {
                        Client.PacketMode = PacketMode.Login;
                        Client.Username   = username;
                        new EncryptionRequest(Client)
                        {
                            PublicKey         = PacketCryptography.PublicKeyToAsn1(Globals.ServerKey),
                            VerificationToken = PacketCryptography.GetRandomToken()
                        }.Write();
                        return;
                    }
                    if (!Client.Player.IsAuthenticated())
                    {
                        new LoginSucces(Client)
                        {
                            Username = username, UUID = uuid
                        }.Write();
                        new Disconnect(Client)
                        {
                            Reason = "§4SharpMC\n§fAuthentication failed!"
                        }.Write();
                        return;
                    }
                }

                new LoginSucces(Client)
                {
                    Username = username, UUID = uuid
                }.Write();

                if (Encoding.UTF8.GetBytes(username).Length == 0)
                {
                    new Disconnect(Client)
                    {
                        Reason = "§4SharpMC\n§fSomething went wrong while decoding your username!"
                    }.Write();
                    return;
                }

                //Protocol check!
                if (protocol < Globals.ProtocolVersion)                 //Protocol to old?
                {
                    new Disconnect(Client)
                    {
                        Reason = "§4SharpMC\n§fYour Minecraft version is to old!\nPlease update in order to play!"
                    }
                    .Write();
                    return;
                }

                if (protocol > Globals.ProtocolVersion)                 //Protocol to new?
                {
                    new Disconnect(Client)
                    {
                        Reason =
                            "§4SharpMC\n§fThis server is not yet updated for your version of Minecraft!\nIn order to play you have to use " +
                            Globals.MCProtocolName
                    }.Write();
                    return;
                }

                Client.Player = new Player(Globals.LevelManager.MainLevel)
                {
                    Uuid     = uuid,
                    Username = username,
                    Wrapper  = Client,
                    Gamemode = Globals.LevelManager.MainLevel.DefaultGamemode
                };
                Client.PacketMode = PacketMode.Play;

                new SetCompression(Client).Write();

                new JoinGame(Client)
                {
                    Player = Client.Player
                }.Write();
                new SpawnPosition(Client).Write();
                Client.StartKeepAliveTimer();
                Client.Player.InitializePlayer();
            }
        }
Пример #6
0
        public override void Read()
        {
            if (Buffer == null)
            {
                return;
            }
            var usernameRaw = Buffer.ReadString();
            var username    =
                new string(usernameRaw.Where(c => char.IsLetter(c) || char.IsPunctuation(c) || char.IsDigit(c)).ToArray());

            var uuid = GetUuid(username);

            if (Core.Server.ServerSettings.OnlineMode)
            {
                if (Core.Server.ServerSettings.EncryptionEnabled)
                {
                    Client.PacketMode = PacketMode.Login;
                    Client.Username   = username;
                    new EncryptionRequest(Client)
                    {
                        PublicKey         = PacketCryptography.PublicKeyToAsn1(Globals.ServerKey),
                        VerificationToken = PacketCryptography.GetRandomToken()
                    }.Write();
                    return;
                }
                if (!Client.Player.IsAuthenticated())
                {
                    new Disconnect(Client)
                    {
                        Reason = new ChatText("Authentication failed!", TextColor.Reset)
                    }.Write();
                    return;
                }
            }

            if (Encoding.UTF8.GetBytes(username).Length == 0)
            {
                new Disconnect(Client)
                {
                    Reason = new ChatText("Something went wrong while decoding your username!", TextColor.Reset)
                }.Write();
                return;
            }

            //Protocol check!
            if (Client.Protocol < Globals.ProtocolVersion)             //Protocol to old?
            {
                new Disconnect(Client)
                {
                    Reason = new ChatText("Your Minecraft version is too old!\nPlease update in order to play!", TextColor.Reset)
                }
                .Write();
                return;
            }

            if (Client.Protocol > Globals.ProtocolVersion)             //Protocol to new?
            {
                new Disconnect(Client)
                {
                    Reason =
                        new ChatText("This server is not yet updated for your version of Minecraft!\nIn order to play you have to use " +
                                     Globals.OfficialProtocolName, TextColor.Reset)
                }.Write();
                return;
            }

            new LoginSucces(Client)
            {
                Username = username, Uuid = uuid
            }.Write();

            Client.Player = new Player(Globals.LevelManager.MainLevel)
            {
                Uuid     = uuid,
                Username = username,
                Wrapper  = Client,
                Gamemode = Globals.LevelManager.MainLevel.DefaultGamemode
            };
            Client.PacketMode = PacketMode.Play;

            new SetCompression(Client)
            {
                CompressionLevel = Core.Server.ServerSettings.UseCompression ? Core.Server.ServerSettings.CompressionThreshold : -1
            }.Write();

            new JoinGame(Client)
            {
                Player = Client.Player
            }.Write();
            new SpawnPosition(Client).Write();
            Client.Player.InitializePlayer();
        }
Пример #7
0
 public override void Initialize(int length, Stream stream)
 {
     base.Initialize(length, stream);
     VerifyToken = PacketCryptography.GetRandomToken();
     PublicKey   = PacketCryptography.PublicKeyToAsn1(Utils.ServerKey);
 }