public bool HandleMessage(IMessage message, IServerPeer peer)
        {
            Response response;

            // If not enough arguments, ok to return Invalid - Normally we return InvalidUserPass
            if (!message.Parameters.ContainsKey((byte)MessageParameterCode.LoginName) || !message.Parameters.ContainsKey((byte)MessageParameterCode.Password))
            {
                Log.DebugFormat("Sending Invalid Operation Response");
                response = new Response(Code, SubCode, new Dictionary <byte, object>()
                {
                    { (byte)MessageParameterCode.SubCodeParameterCode, SubCode }, { (byte)MessageParameterCode.PeerId, message.Parameters[(byte)MessageParameterCode.PeerId] }
                }, "Not enough arguments", (short)ReturnCode.OperationInvalid);
                peer.SendMessage(response);
            }
            else
            {
                // Use our preferred Authorization Service to check if authorized
                User user       = null;
                var  returnCode = AuthService.IsAuthorized(out user, (string)message.Parameters[(byte)MessageParameterCode.LoginName], (string)message.Parameters[(byte)MessageParameterCode.Password]);


                if (returnCode == ReturnCode.OK)
                {
                    Log.DebugFormat("Found User and good password");
                    // Need to add this login to the list of clients connected complete with the user id for other login purposes.
                    var clientpeer = PeerFactory.CreatePeer <IClientPeer>(new PeerConfig());
                    clientpeer.PeerId = new Guid((byte[])message.Parameters[(byte)MessageParameterCode.PeerId]);

                    // Add to connection collection
                    ConnectionCollection.Connect(clientpeer);

                    // Add our user id to the client peer for when we do character selection/etc
                    clientpeer.ClientData <CharacterData>().UserId = user.Id;
                    Log.DebugFormat("Found User ID - {0}", user.Id);

                    // We know it's OK. send the user ID back to proxy server, to send to the client.
                    response = new Response(Code, SubCode, new Dictionary <byte, object>()
                    {
                        { (byte)MessageParameterCode.SubCodeParameterCode, SubCode }, { (byte)MessageParameterCode.PeerId, message.Parameters[(byte)MessageParameterCode.PeerId] }, { (byte)MessageParameterCode.UserId, user.Id }
                    }, "", (short)returnCode);
                    peer.SendMessage(response);
                }
                else
                {
                    Log.DebugFormat("Found user, bad password");
                    // UserPass is not good.
                    response = new Response(Code, SubCode, new Dictionary <byte, object>()
                    {
                        { (byte)MessageParameterCode.SubCodeParameterCode, SubCode }, { (byte)MessageParameterCode.PeerId, message.Parameters[(byte)MessageParameterCode.PeerId] }
                    }, "Invalid Username or Password", (short)returnCode);
                    peer.SendMessage(response);
                }
            }
            return(true);
        }
 public PhotonClientPeer(InitRequest initRequest,
                         ILogger log,
                         IServerApplication server,
                         IEnumerable <IClientData> clientData,
                         IConnectionCollection <IClientPeer> connectionCollection,
                         IHandlerList <IClientPeer> handlerList)
     : base(initRequest)
 {
     Log          = log;
     _server      = server;
     _handlerList = handlerList;
     Log.DebugFormat("Created Client Peer");
     ConnectionCollection = connectionCollection;
     ConnectionCollection.Connect(this);
     PeerId      = Guid.NewGuid();
     _clientData = new Dictionary <Type, IClientData>();
     foreach (var data in clientData)
     {
         _clientData.Add(data.GetType(), data);
     }
 }
예제 #3
0
        public bool HandleMessage(IMessage message, IServerPeer peer)
        {
            var serverPeer = peer as PhotonServerPeer;
            var operation  = new LoginOperation(serverPeer.protocol, message);

            if (!operation.IsValid)
            {
                Response response = new Response(Code, SubCode, new Dictionary <byte, object>()
                {
                    { (byte)MessageParameterCode.SubCodeParameterCode, SubCode },
                    { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] },
                }, operation.GetErrorMessage(), (int)ReturnCode.OperationInvalid);

                peer.SendMessage(response);

                return(true);
            }

            if (operation.Login.Length < 6 || operation.Password.Length < 6)
            {
                peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>()
                {
                    { (byte)MessageParameterCode.SubCodeParameterCode, SubCode },
                    { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] },
                }, "Login and password can't be less than 6 symbols.", (int)ReturnCode.OperationInvalid));

                return(true);
            }

            try
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        var account = session.QueryOver <AccountModel>().Where(a => a.Login == operation.Login).SingleOrDefault();

                        if (account == null)
                        {
                            transaction.Commit();

                            peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>()
                            {
                                { (byte)MessageParameterCode.SubCodeParameterCode, SubCode },
                                { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] },
                            }, "Login or password incorrect.", (int)ReturnCode.LoginOrPasswordIncorrect));

                            return(true);
                        }

                        string Password = BitConverter.ToString(SHA512.Create().ComputeHash(
                                                                    Encoding.UTF8.GetBytes(account.Salt + operation.Password))).Replace("-", "");

                        if (Password != account.Password)
                        {
                            transaction.Commit();

                            peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>()
                            {
                                { (byte)MessageParameterCode.SubCodeParameterCode, SubCode },
                                { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] },
                            }, "Login or password incorrect.", (int)ReturnCode.LoginOrPasswordIncorrect));

                            return(true);
                        }

                        var clientPeer = peerFactory.CreatePeer <IClientPeer>(new PeerConfig());
                        clientPeer.PeerId = new Guid((byte[])message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode]);

                        connectionCollection.Connect(clientPeer);

                        //clientPeer.ClientData<CharacterData>().UserId = account.Id;

                        log.DebugFormat("clients - {0}", connectionCollection.GetPeers <IClientPeer>().Count);

                        transaction.Commit();

                        peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>()
                        {
                            { (byte)MessageParameterCode.SubCodeParameterCode, SubCode },
                            { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] },
                            { (byte)MessageParameterCode.UserId, account.Id }
                        }, "", (int)ReturnCode.OK));

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Error login handler: {0}", ex);

                peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>()
                {
                    { (byte)MessageParameterCode.SubCodeParameterCode, SubCode },
                    { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] },
                }, ex.ToString(), (int)ReturnCode.OperationDenied));

                return(true);
            }
        }
예제 #4
0
 private void CreateSubServers()
 {
     subServerCollection.Connect(new LoginSubServer(authService, this));
     subServerCollection.Connect(new MenuSubServer(this));
 }