コード例 #1
0
ファイル: ClientHandler.cs プロジェクト: Machiry/securechat
 public ClientHandler(TcpClient connection)
 {
     this.dhKeyHelper = new DHHelper.DHHelper(false);
     this.clientConnection = connection;
     this.authHelper = new AuthenticationHelper();
     this.userName = String.Empty;
     this.isAuthenticated = false;
     this.socketReader = new StreamReader(this.clientConnection.GetStream());
     this.socketWriter = new StreamWriter(this.clientConnection.GetStream());
     this.probeTimeSpan = new TimeSpan(0, 0, Helper.GlobalConstants.ProbeInterval);
 }
コード例 #2
0
ファイル: ClientHandler.cs プロジェクト: Machiry/securechat
        private void Talk()
        {
            //Set up a shared secret key using DH algorithm
            //Get the public exponent and base prime
             AuthTimeOut = new System.Timers.Timer(Helper.GlobalConstants.TimeOutForAuthentication * 1000);
             AuthTimeOut.Elapsed += new ElapsedEventHandler(AuthTimeOut_Elapsed);
            try
            {
                if (SetupDHSharedKey())
                {
                    AuthTimeOut.Enabled = false;
                    AuthTimeOut.Interval = Helper.GlobalConstants.TimeOutForAuthentication * 1000;
                    AuthTimeOut.Enabled = true;
                    string currentLine;
                    int noOfTries = 3;
                    while (noOfTries > 0 && !this.isAuthenticated && this.isClientAlive)
                    {
                        currentLine = GetDecryptedLine();
                        string[] credentials = currentLine.Split(null);
                        if (credentials.Length == 2)
                        {
                            AuthenticationHelper authHelper = new AuthenticationHelper();
                            //If the user name and password are valid for the user
                            if (authHelper.IsValid(credentials[0], credentials[1]))
                            {
                                this.isAuthenticated = true;
                                //Tell client that Authentication is sucessfull
                                AuthTimeOut.Enabled = false;
                                AuthTimeOut.Dispose();
                                this.SendLineEncrypted(Helper.GlobalConstants.AuthSuccessMessage);
                                this.userName = credentials[0];
                                Logger.Logger.WriteInfo(String.Format("User \"{0}\" successfully authenticated", credentials[0]));
                                break;
                            }
                        }
                        if (noOfTries == 1)
                        {
                            this.SendLineEncrypted(Helper.GlobalConstants.AuthFailureNoRetryMessage);
                        }
                        else
                        {
                            this.SendLineEncrypted(Helper.GlobalConstants.AuthFailureMessage);
                        }
                        Logger.Logger.WriteInfo(String.Format("User \"{0}\" failed to authenticate. Trials remained: {1}", credentials[0], noOfTries-1));
                        noOfTries--;
                    }
                    if (this.isAuthenticated)
                    {
                        this.probeAliveTimer = new System.Timers.Timer(Helper.GlobalConstants.ProbeInterval * 1000);//this.SendAliveProbe, Helper.GlobalConstants.ProbeMessage, this.probeTimeSpan, this.probeTimeSpan);
                        this.probeAliveTimer.Elapsed += new ElapsedEventHandler(probeAliveTimer_Elapsed);
                        this.probeAliveTimer.AutoReset = true;
                        this.chatThread = new Thread(this.StartChat);
                        this.chatThread.Start();
                        this.probeAliveTimer.Start();
                    }
                    else
                    {
                        AuthTimeOut.Enabled = false;
                        AuthTimeOut.Dispose();
                        this.clientConnection.Close();
                        Logger.Logger.WriteError("Authentication failed");
                    }
                }

            }
            catch (Exception e)
            {
                this.isClientAlive = false;
                this.isAuthenticated = false;
                this.clientConnection.Close();
                Logger.Logger.WriteError("Problem occured while trying to establish a secure connection with the client");
                Logger.Logger.WriteException(e);
            }

            if (!this.isAuthenticated)
            {
                this.isClientAlive = false;
                this.isAuthenticated = false;
            }
            //Validate the provided user name and password

            //Start Listining for any text from client
        }