Esempio n. 1
0
        public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
        {
            if (stream.Length - stream.Position >= 2)
            {
                short length = stream.ReadShort();
                if (stream.Length - stream.Position >= length)
                {
                    client.Username = stream.ReadString(length);

                    //response

                    // - = no password
                    // + = password
                    // hash = check with minecraft.net

                    if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Online)
                    {
                        Random random = new Random();
                        client.Hash = random.Next().ToString("X");
                        client.Send(MinecraftPacketCreator.GetHandshake(client.Hash));
                    }
                    else if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Offline)
                    {
                        client.Send(MinecraftPacketCreator.GetHandshake("-"));
                    }
                    else if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Password)
                    {
                        client.Send(MinecraftPacketCreator.GetHandshake("+"));
                    }
                    return true;
                }
            }
            return false;
        }
Esempio n. 2
0
 public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
 {
     if (stream.Length - stream.Position >= 2)
     {
         short length = stream.ReadShort();
         if (stream.Length - stream.Position >= length)
         {
             string message = stream.ReadString(length);
             if (message.Substring(0, 1) == "/")
             {
                 string[] splitted = message.Substring(1).Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
                 if (MinecraftServer.Instance.CommandManager.CommandExists(splitted[0].ToLower()))
                 {
                     MinecraftServer.Instance.CommandManager.RunCommand(client, splitted[0].ToLower(), splitted);
                 }
                 else if (splitted[0].ToLower() == "help")
                 {
                     client.Send(MinecraftPacketCreator.GetChatMessage("Availiable commands are:"));
                     foreach (string key in MinecraftServer.Instance.CommandManager.GetCommands())
                     {
                         client.Send(MinecraftPacketCreator.GetChatMessage(key));
                     }
                 }
                 else
                 {
                     client.Send(MinecraftPacketCreator.GetChatMessage("Command " + splitted[0] + " does not exist."));
                 }
             }
             else
             {
                 foreach (MinecraftClient c in MinecraftServer.Instance.Clients)
                 {
                     c.Send(MinecraftPacketCreator.GetChatMessage(client.Username, message));
                 }
             }
             return true;
         }
     }
     return false;
 }
Esempio n. 3
0
 public void Run(MinecraftServer server, MinecraftClient client, string[] args)
 {
     StringBuilder builder = new StringBuilder();
     builder.Append("Connected users: ");
     foreach (MinecraftClient c in server.Clients)
     {
         builder.Append(c.Username);
         builder.Append(", ");
     }
     builder.Remove(builder.Length - 2, 2);
     builder.Append(".");
     client.Send(MinecraftPacketCreator.GetChatMessage(builder.ToString()));
 }
Esempio n. 4
0
        public bool HandlePacket(MinecraftClient client, MinecraftPacketStream stream)
        {
            if (stream.Length - stream.Position >= 4)
            {
                int version = stream.ReadInt();
                if (version == MinecraftServer.Instance.Version)
                {
                    if (stream.Length - stream.Position >= 2)
                    {
                        short ulength = stream.ReadShort();
                        if (stream.Length - stream.Position >= ulength)
                        {
                            string username = stream.ReadString(ulength);
                            if (stream.Length - stream.Position >= 2)
                            {
                                short plength = stream.ReadShort();
                                if (stream.Length - stream.Position >= plength)
                                {
                                    string password = stream.ReadString(plength);
                                    if (stream.Length - stream.Position >= 8)
                                    {
                                        stream.ReadLong();
                                        if (stream.Length - stream.Position >= 1)
                                        {
                                            stream.ReadByte();

                                            if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Online)
                                            {
                                                //REFRACTOR?
                                                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.minecraft.net/game/checkserver.jsp?user="******"&serverId=" + client.Hash);
                                                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                                                byte[] buffer = new byte[3];
                                                response.GetResponseStream().Read(buffer, 0, buffer.Length);
                                                if (UTF8Encoding.UTF8.GetString(buffer) == "YES")
                                                {
                                                    client.Send(MinecraftPacketCreator.GetLoginRequest());
                                                    client.Load();
                                                    return true;
                                                }
                                                else
                                                {
                                                    client.Disconnect("Failed to verify username.");
                                                }
                                            }
                                            else if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Offline)
                                            {
                                                client.Send(MinecraftPacketCreator.GetLoginRequest());
                                                client.Load();
                                                return true;
                                            }
                                            else if (MinecraftServer.Instance.Authentication == MinecraftAuthentication.Password)
                                            {
                                                // Insert validation function here...
                                                // Password doesn't even work.
                                                client.Disconnect("Password authentication not implemented.");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    client.Disconnect("Invalid version.");
                }
            }
            return false;
        }