Пример #1
0
        private void ClearPacketHandler(byte[] recvbuf, int buflen)
        {
            if (buflen == 0)
            {
                Console.WriteLine("Client sent empty packet. Disconnecting...");
                IsConnected = false;
                return;
            }
            byte[] arrData = new byte[buflen];
            Array.Copy(recvbuf, arrData, buflen);
            Octet data     = new Octet(arrData);
            byte  packetid = data.UnMarshalByte();

            switch (packetid)
            {
            case (byte)LinkServerPacket.LoginRequest:
                Login loginPacket = new Login(data);
                _username     = loginPacket.Username;
                _passwordHash = loginPacket.Hash;
                Console.WriteLine("User {0} is trying to login", _username);
                string passHash     = BitConverter.ToString(_passwordHash).Replace("-", string.Empty);
                string dbHashString = _database.GetUserPasswd(_username, passHash);
                if (dbHashString.Equals(passHash))
                {
                    SMKey smkeyPacket = new SMKey();
                    SendReply(smkeyPacket.GetBytes());
                    _smKey = smkeyPacket.GetSMKey();
                    HMACMD5 hmacmd5 = new HMACMD5(Encoding.ASCII.GetBytes(_username));
                    byte[]  array   = new byte[_passwordHash.Length + _smKey.Length];
                    _passwordHash.CopyTo(array, 0);
                    _smKey.CopyTo(array, _passwordHash.Length);
                    byte[] RC4_C2SKEY = hmacmd5.ComputeHash(array);
                    C2S_Crypto   = new RC4(RC4_C2SKEY);
                    _isEncrypted = true;
                }
                else
                {
                    ErrorInfo errorPacket = new ErrorInfo(2);
                    SendReply(errorPacket.GetBytes());
                    Console.WriteLine("Login error!");
                }
                break;

            default:
                IsConnected = false;
                Console.WriteLine("Unknown packet. Disconnecting...");
                Console.WriteLine(BitConverter.ToString(recvbuf, 0, buflen));
                break;
            }
        }