コード例 #1
0
ファイル: ClientEndpoint.cs プロジェクト: andriy-f/MyChat
        private void ProcessConnectionInvalidCredentials()
        {
            var resp = new ServiceMessageResponse {
                Message = "Invalid user credentials!"
            };

            this.cryptoWrapper.Send(resp.ToBytes());
            this.FreeTCPClient();
            Log.DebugFormat(
                "Logon from IP '{0}' failed: Login '{1}'//Password not recognized",
                this.clientIpAddress,
                this.Login);
        }
コード例 #2
0
ファイル: ClientEndpoint.cs プロジェクト: andriy-f/MyChat
        private void ProcessLogon(ServiceMessage serviceMessage)
        {
            // Logon attempt
            var credentials = LogonCredentials.FromBytes(serviceMessage.Data);

            if (!this.dataContext.ValidateLoginPass(credentials.Login, credentials.Password))
            {
                this.ProcessConnectionInvalidCredentials();
                return;
            }

            // Check if user with same login is already logged in
            if (this.server.IsLoggedIn(credentials.Login))
            {
                var existingClient = this.server.GetChatClient(credentials.Login);
                if (existingClient.PokeForAlive())
                {
                    // Client with login <login> still alive -> new login attempt invalid
                    var resp = new ServiceMessageResponse {
                        Message = "This login is already used"
                    };
                    this.cryptoWrapper.Send(resp.ToBytes());
                    this.FreeTCPClient();
                    Log.DebugFormat(
                        "Logon from IP '{0}' failed: User '{1}' already logged on",
                        this.clientIpAddress,
                        credentials.Login);
                }
                else
                {
                    // Old client app which used current login is unresponsive -> dispose of it and add new
                    this.server.RemoveClient(credentials.Login);
                    Log.DebugFormat(
                        "Old client app which used login '{0}' is unresponsive -> dispose of it and add new",
                        credentials.Login);
                    this.server.AddLoggedInUser(credentials.Login, this);
                }
            }
            else
            {
                this.server.AddLoggedInUser(credentials.Login, this);
                this.cryptoWrapper.Send(ServiceMessageResponse.Success.ToBytes());
                Log.DebugFormat(
                    "Logon from IP '{0}' success: User '{1}' from IP  logged on",
                    this.clientIpAddress,
                    credentials.Login);
            }

            this.Login = credentials.Login;
        }
コード例 #3
0
ファイル: ChatClient.cs プロジェクト: andriy-f/MyChat
        public int LogOnInvalidMessage1()
        {
            try
            {
                var creds = new LogonCredentials {
                    Login = this.Login, Password = this.password
                };
                var serializedCreds = creds.ToBytes();
                var serviceMessage  = new ServiceMessage {
                    MessageType = MessageType.Logon, Data = serializedCreds
                };

                var serviceMessageSerialized = serviceMessage.ToBytes();

                // Craft corrupted message
                var data = new byte[4 + 10];
                BitConverter.GetBytes(12).CopyTo(data, 0);
                this.stream.Write(data, 0, data.Length); // Send corrupted message

                var respData = this.cryptoWrapper.Receive();
                var smr      = ServiceMessageResponse.FromBytes(respData);
                if (smr.IsSuccess)
                {
                    Logger.Debug(string.Format("Logon succeeded for user '{0}'", this.Login));
                    return(0);
                }
                else
                {
                    Logger.Debug(string.Format("Logon failed for user '{0}'. Reason: '{1}'", this.Login, smr.Message));
                    this.FreeClient();
                    return(1);
                }
            }
            catch (ArgumentNullException ex)
            {
                Logger.Error(ex.ToString);
                return(3);
            }
            catch (SocketException ex)
            {
                Logger.Error(ex.ToString);
                return(4);
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString);
                return(5);
            }
        }
コード例 #4
0
ファイル: ChatClient.cs プロジェクト: andriy-f/MyChat
 public int LogOn()
 {
     try
     {
         var creds = new LogonCredentials {
             Login = this.Login, Password = this.password
         };
         var serializedCreds = creds.ToBytes();
         var serviceMessage  = new ServiceMessage {
             MessageType = MessageType.Logon, Data = serializedCreds
         };
         this.cryptoWrapper.Send(serviceMessage.ToBytes());
         var respData = this.cryptoWrapper.Receive();
         var smr      = ServiceMessageResponse.FromBytes(respData);
         if (smr.IsSuccess)
         {
             Logger.Debug(string.Format("Logon succeeded for user '{0}'", this.Login));
             return(0);
         }
         else
         {
             Logger.Debug(string.Format("Logon failed for user '{0}'. Reason: '{1}'", this.Login, smr.Message));
             this.FreeClient();
             return(1);
         }
     }
     catch (ArgumentNullException ex)
     {
         Logger.Error(ex.ToString);
         return(3);
     }
     catch (SocketException ex)
     {
         Logger.Error(ex.ToString);
         return(4);
     }
     catch (Exception ex)
     {
         Logger.Error(ex.ToString);
         return(5);
     }
 }
コード例 #5
0
        private void ProcessLogon(ServiceMessage serviceMessage)
        {
            // Logon attempt
            var credentials = LogonCredentials.FromBytes(serviceMessage.Data);
            if (!this.dataContext.ValidateLoginPass(credentials.Login, credentials.Password))
            {
                this.ProcessConnectionInvalidCredentials();
                return;
            }

            // Check if user with same login is already logged in
            if (this.server.IsLoggedIn(credentials.Login))
            {
                var existingClient = this.server.GetChatClient(credentials.Login);
                if (existingClient.PokeForAlive())
                {
                    // Client with login <login> still alive -> new login attempt invalid
                    var resp = new ServiceMessageResponse { Message = "This login is already used"};
                    this.cryptoWrapper.Send(resp.ToBytes());
                    this.FreeTCPClient();
                    Log.DebugFormat(
                            "Logon from IP '{0}' failed: User '{1}' already logged on",
                            this.clientIpAddress,
                            credentials.Login);
                }
                else
                {
                    // Old client app which used current login is unresponsive -> dispose of it and add new
                    this.server.RemoveClient(credentials.Login);
                    Log.DebugFormat(
                            "Old client app which used login '{0}' is unresponsive -> dispose of it and add new",
                            credentials.Login);
                    this.server.AddLoggedInUser(credentials.Login, this);
                }
            }
            else
            {
                this.server.AddLoggedInUser(credentials.Login, this);
                this.cryptoWrapper.Send(ServiceMessageResponse.Success.ToBytes());
                Log.DebugFormat(
                        "Logon from IP '{0}' success: User '{1}' from IP  logged on",
                        this.clientIpAddress,
                        credentials.Login);
            }

            this.Login = credentials.Login;
        }
コード例 #6
0
 private void ProcessConnectionInvalidCredentials()
 {
     var resp = new ServiceMessageResponse { Message = "Invalid user credentials!" };
     this.cryptoWrapper.Send(resp.ToBytes());
     this.FreeTCPClient();
     Log.DebugFormat(
             "Logon from IP '{0}' failed: Login '{1}'//Password not recognized",
             this.clientIpAddress,
             this.Login);
 }