public static void VersionInfo(LoginClient pClient, Packet pPacket)
 {
     ushort year;
     ushort version;
     if (!pPacket.TryReadUShort(out year) ||
         !pPacket.TryReadUShort(out version))
     {
         Log.WriteLine(LogLevel.Warn, "Invalid client version.");
         pClient.Disconnect();
         return;
     }
     Log.WriteLine(LogLevel.Debug, "Client version {0}:{1}.", year, version);
     using (Packet response = new Packet(SH3Type.VersionAllowed))
     {
         response.WriteShort(1);
         pClient.SendPacket(response);
     }
 }
Exemple #2
0
        public static void Login(LoginClient pClient, Packet pPacket)
        {
            string hash;
            string username;

            if (!pPacket.TryReadString(out username, 18) || !pPacket.TryReadString(out hash, 16))
            {
                Log.WriteLine(LogLevel.Warn, "Could not read user token.");
                SendFailedLogin(pClient, ServerError.EXCEPTION);
                return;
            }

            User user;

            if (Program.Entity.Users.Count() > 0 && Program.Entity.Users.Count(u => u.Username == username) == 1)
            {
                user = Program.Entity.Users.First(u => u.Username == username);
                if (user.Password.ToLower() == hash.ToLower())
                {
                    Log.WriteLine(LogLevel.Debug, "{0} tries to login.", username);
                    if (ClientManager.Instance.IsLoggedIn(user.Username))
                    {
                        Log.WriteLine(LogLevel.Warn, "{0} is trying dual login. Disconnecting.", user.Username);
                        pClient.Disconnect();
                    }
                    else if (user.Banned)
                    {
                        SendFailedLogin(pClient, ServerError.BLOCKED);
                    }
                    else
                    {
                        pClient.IsAuthenticated = true;
                        pClient.Username = user.Username;
                        pClient.AccountID = user.ID;
                        pClient.Admin = user.Admin;
                        AllowFiles(pClient, true);
                        WorldList(pClient, false);
                    }
                }
                else SendFailedLogin(pClient, ServerError.INVALID_CREDENTIALS);
            }
            else SendFailedLogin(pClient, ServerError.INVALID_CREDENTIALS);
        }
Exemple #3
0
        public static void Login(LoginClient pClient, Packet pPacket)
        {
            // Initialize DB
            DatabaseClient dbClient = Program.DatabaseManager.GetClient();

            // Define packet lengths, as these may change with client updates
            int packetLength = 316;
            int loginLength = 17 + 1;
            int passwordLength = 32 + 1;

            string md5 = pPacket.ReadStringForLogin(packetLength);
            char[] md5Char = md5.ToCharArray();
            string username = "";
            string clientPassword = "";

            // Read from 0 --> 17
            for (int i = 0; i < loginLength; i++)
                username += md5Char[i].ToString().Replace("\0", "");

            // Read from 260 --> 292
            for (int i = 260; i < 260 + passwordLength; i++)
                clientPassword += md5Char[i].ToString().Replace("\0", "");

            Log.WriteLine(LogLevel.Debug, "{0} tries to login.", username);

            DataTable loginData = null;

            using (dbClient)
                loginData = dbClient.ReadDataTable("SELECT * FROM accounts WHERE Username= '******'");

            // Auto account creation if no username found
            if (loginData.Rows.Count == 0)
            {
                dbClient.ExecuteQuery("INSERT INTO accounts (username, password) VALUES ('" + username + "','" + clientPassword + "')");

                using (dbClient)
                    loginData = dbClient.ReadDataTable("SELECT * FROM accounts WHERE Username= '******'");
            }

            if (loginData != null)
            {
                if (loginData.Rows.Count > 0)
                {
                    foreach (DataRow row in loginData.Rows)
                    {
                        string uIsername = (string)row["username"];
                        string password = (string)row["password"];
                        bool banned = Database.DataStore.ReadMethods.EnumToBool(row["banned"].ToString());

                        if (clientPassword == password)
                        {
                            if (banned)
                            {
                                SendFailedLogin(pClient, ServerError.Blocked);
                                Log.WriteLine(LogLevel.Debug, "Banned user - {0} tries to login.", username);
                            }

                            else if (ClientManager.Instance.IsLoggedIn(uIsername))
                            {
                                Log.WriteLine(LogLevel.Warn, "{0} is trying dual login. Disconnecting.", uIsername);
                                pClient.Disconnect();

                                break;
                            }
                            else
                            {
                                pClient.Username = uIsername;
                                pClient.IsAuthenticated = true;
                                pClient.Admin = 0; /*(byte)row["Admin"];*/
                                pClient.AccountID = int.Parse(row["id"].ToString());
                                WorldList(pClient, false);
                            }
                        }
                        else
                            SendFailedLogin(pClient, ServerError.InvalidCredentials);
                    }
                }
            }
        }