Beispiel #1
0
        protected override bool OnHandleMessage(IMessage message, PhotonServerPeer serverPeer)
        {
            var para = new Dictionary <byte, object>
            {
                { (byte)ClientParameterCode.PeerId, message.Parameters[(byte)ClientParameterCode.PeerId] },
                { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] },
            };

            var operation = new LoginSecurely(serverPeer.Protocol, message);

            if (!operation.IsValid)
            {
                serverPeer.SendOperationResponse(new OperationResponse(message.Code, new Dictionary <byte, object> {
                    { (byte)ClientParameterCode.PeerId, message.Parameters[(byte)ClientParameterCode.PeerId] }
                })
                {
                    ReturnCode   = (int)ErrorCode.OperationInvalid,
                    DebugMessage = operation.GetErrorMessage()
                }, new SendParameters());

                return(true);
            }

            if (operation.UserName == "" || operation.Password == "")
            {
                serverPeer.SendOperationResponse(new OperationResponse(message.Code, para)
                {
                    ReturnCode   = (int)ErrorCode.UserNamePasswordInvalid,
                    DebugMessage = "Username or password is incorrect"
                }, new SendParameters());
                return(true);
            }

            try
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        Log.Debug("About to look for user account " + operation.UserName);
                        var userList = session.QueryOver <User>().Where(u => u.UserName == operation.UserName).List();
                        if (userList.Count > 0)
                        {
                            User user = userList[0];
                            var  hash = BitConverter.ToString(SHA1CryptoServiceProvider.Create()
                                                              .ComputeHash(Encoding.UTF8.GetBytes(user.Salt + operation.Password)))
                                        .Replace("-", "");
                            if (String.Equals(hash.Trim(), user.Password.Trim(), StringComparison.OrdinalIgnoreCase))
                            {
                                LoginServer server = Server as LoginServer;
                                if (server != null)
                                {
                                    //check if user is already logged in
                                    bool founduser = false;
                                    foreach (var subServerClientPeer in server.ConnectionCollection <SubServerConnectionCollection>().Clients)
                                    {
                                        if (subServerClientPeer.Value.ClientData <CharacterData>().UserId == user.Id)
                                        {
                                            founduser = true;
                                        }
                                    }

                                    if (founduser)
                                    {
                                        serverPeer.SendOperationResponse(new OperationResponse((byte)ClientOperationCode.Login)
                                        {
                                            Parameters = para, ReturnCode = (short)ErrorCode.UserCurrentlyLoggedIn, DebugMessage = "User is currently logged in"
                                        }, new SendParameters());
                                    }
                                    else                                     //if not logged in - allow log in and add to Client dictionaries
                                    {
                                        server.ConnectionCollection <SubServerConnectionCollection>().Clients.Add(new Guid((Byte[])message.Parameters[(byte)ClientParameterCode.PeerId]), _clientFactory(new Guid((Byte[])message.Parameters[(byte)ClientParameterCode.PeerId])));
                                        server.ConnectionCollection <SubServerConnectionCollection>().Clients[new Guid((Byte[])message.Parameters[(byte)ClientParameterCode.PeerId])].ClientData <CharacterData>().UserId = user.Id;
                                        Log.Debug("Login Handler sucessfully found character to log in.");

                                        para.Add((byte)ClientParameterCode.UserId, user.Id);

                                        serverPeer.SendOperationResponse(new OperationResponse((byte)ClientOperationCode.Login)
                                        {
                                            Parameters = para
                                        }, new SendParameters());
                                    }
                                }
                                return(true);
                            }
                            else
                            {
                                serverPeer.SendOperationResponse(new OperationResponse(message.Code, para)
                                {
                                    ReturnCode   = (int)ErrorCode.UserNamePasswordInvalid,
                                    DebugMessage = "Username or password is incorrect"
                                }, new SendParameters());
                                //Log.DebugFormat("server: {0}", serverPeer.ApplicationName);
                                return(true);
                            }
                        }
                        else
                        {
                            Log.DebugFormat("Account name does not exist {0}", operation.UserName);
                            transaction.Commit();                             //closing transaction
                            serverPeer.SendOperationResponse(new OperationResponse(message.Code, para)
                            {
                                ReturnCode   = (int)ErrorCode.UserNamePasswordInvalid,
                                DebugMessage = "Username or password is incorrect"
                            }, new SendParameters());

                            return(true);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("Error Occured", e);
                serverPeer.SendOperationResponse(new OperationResponse(message.Code, para)
                {
                    ReturnCode   = (int)ErrorCode.UserNameInUse,
                    DebugMessage = e.ToString()
                }, new SendParameters());
            }
            return(true);
        }
        protected override bool OnHandleMessage(IMessage message, PhotonServerPeer serverPeer)
        {
            Log.DebugFormat("Login message being handled message code {0}", message.Code);
            var operation = new LoginSecurely(serverPeer.Protocol, message);

            if (!operation.IsValid)
            {
                serverPeer.SendOperationResponse(new OperationResponse(message.Code,
                                                                       new Dictionary <byte, object>
                {
                    { (byte)ClientParameterCode.PeerId, message.Parameters[(byte)ClientParameterCode.PeerId] }
                })
                {
                    ReturnCode   = (int)ErrorCode.OperationInvalid,
                    DebugMessage = operation.GetErrorMessage()
                }, new SendParameters());
                return(true);
            }

            if (operation.UserName == "" | operation.Password == "")
            {
                serverPeer.SendOperationResponse(new OperationResponse(message.Code,
                                                                       new Dictionary <byte, object>
                {
                    { (byte)ClientParameterCode.PeerId, message.Parameters[(byte)ClientParameterCode.PeerId] }
                })
                {
                    ReturnCode   = (int)ErrorCode.IncorrectUserNameOrPassword,
                    DebugMessage = "User name or password is incorrect"
                }, new SendParameters());
                return(true);
            }
            try
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        Log.DebugFormat("about to look for user account {0}", operation.UserName);
                        var userList = session.QueryOver <User>().Where(u => u.Username == operation.UserName).List();
                        if (userList.Count > 0)
                        {
                            Log.DebugFormat("found user {0} in database", operation.UserName);
                            var user = userList[0];
                            var hash = BitConverter.ToString(SHA1.Create().ComputeHash(
                                                                 Encoding.UTF8.GetBytes(user.Salt + operation.Password)))
                                       .Replace("-", "");
                            Log.DebugFormat("original pass {0}", hash.Trim());
                            Log.DebugFormat("login pass {0}", user.Password.Trim());

                            if (String.Equals(hash.Trim(), user.Password.Trim(), StringComparison.OrdinalIgnoreCase))
                            {
                                LoginServer server = Server as LoginServer;
                                if (server != null)
                                {
                                    bool founduser = false;
                                    foreach (var subServerClientPeer in server.ConnectionCollection <SubServerConnectionCollection>().Clients)
                                    {
                                        if (subServerClientPeer.Value.ClientData <CharacterData>().UserId == user.Id)
                                        {
                                            founduser = true;
                                        }
                                    }
                                    if (founduser)
                                    {
                                        Log.DebugFormat("user is already logged in");
                                        var para = new Dictionary <byte, object>
                                        {
                                            { (byte)ClientParameterCode.PeerId, message.Parameters[(byte)ClientParameterCode.PeerId] },
                                            { (byte)ClientParameterCode.SubOperationCode, message.Parameters[(byte)ClientParameterCode.SubOperationCode] }
                                        };
                                        serverPeer.SendOperationResponse(
                                            new OperationResponse((byte)ClientOperationCode.Login)
                                        {
                                            Parameters   = para,
                                            ReturnCode   = (short)ErrorCode.UserCurrentlyLoggedIn,
                                            DebugMessage = "User is currently logged in."
                                        }, new SendParameters());
                                    }
                                    else
                                    {
                                        Log.Debug("Login handler successfully found character to log in.");

                                        server.ConnectionCollection <SubServerConnectionCollection>().Clients.Add(new Guid((Byte[])message.Parameters[(byte)ClientParameterCode.PeerId]), _clientFactory());
                                        server.ConnectionCollection <SubServerConnectionCollection>().Clients[new Guid((Byte[])message.Parameters[(byte)ClientParameterCode.PeerId])].ClientData <CharacterData>().UserId = user.Id;
                                        var para = new Dictionary <byte, object>
                                        {
                                            {
                                                (byte)ClientParameterCode.PeerId,
                                                message.Parameters[(byte)ClientParameterCode.PeerId]
                                            },
                                            {
                                                (byte)ClientParameterCode.SubOperationCode,
                                                message.Parameters[(byte)ClientParameterCode.SubOperationCode]
                                            },
                                            { (byte)ClientParameterCode.UserId, user.Id }
                                        };
                                        serverPeer.SendOperationResponse(
                                            new OperationResponse((byte)ClientOperationCode.Login)
                                        {
                                            Parameters = para
                                        },
                                            new SendParameters());
                                    }
                                }
                                return(true);
                            }
                            Log.Debug("password does not match.");

                            serverPeer.SendOperationResponse(new OperationResponse(message.Code,
                                                                                   new Dictionary <byte, object>
                            {
                                {
                                    (byte)ClientParameterCode.PeerId,
                                    message.Parameters[(byte)ClientParameterCode.PeerId]
                                }
                            })
                            {
                                ReturnCode   = (int)ErrorCode.IncorrectUserNameOrPassword,
                                DebugMessage = "User name or password is incorrect"
                            }, new SendParameters());
                            return(true);
                        }
                        Log.DebugFormat("Account name does not exist {0}", operation.UserName);
                        transaction.Commit();
                        serverPeer.SendOperationResponse(
                            new OperationResponse(message.Code)
                        {
                            ReturnCode   = (int)ErrorCode.IncorrectUserNameOrPassword,
                            DebugMessage = "User name or password is incorrect."
                        }, new SendParameters());
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("Error Occured", e);
                serverPeer.SendOperationResponse(new OperationResponse(message.Code,
                                                                       new Dictionary <byte, object>
                {
                    { (byte)ClientParameterCode.PeerId, message.Parameters[(byte)ClientParameterCode.PeerId] }
                })
                {
                    ReturnCode = (int)ErrorCode.IncorrectUserNameOrPassword,
                    //remove debug msg later
                    DebugMessage = e.ToString()
                }, new SendParameters());
            }
            return(true);
        }