Ejemplo n.º 1
0
        private string ReadPublicKey(TcpClient tcpClient)
        {
            Message msgKey = new Message();

            msgKey.ReadFrom(tcpClient.GetStream());
            return(msgKey.Body);
        }
Ejemplo n.º 2
0
        private string ReadPublicKey(TcpClient tcpClient)
        {
            Message msgKey = new Message();

            msgKey.ReadFrom(tcpClient.GetStream());
            Console.WriteLine("Server receive from client Public key " + msgKey.Body);
            return(msgKey.Body);
        }
Ejemplo n.º 3
0
        public void Start()
        {
            //start the server
            IPAddress iPAddress = Dns.GetHostEntry(hOST).AddressList[0];

            TcpListener = new TcpListener(iPAddress, pORT);


            try
            {
                TcpListener.Start();
                Console.WriteLine("Server started...");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            while (true)
            {
                TcpClient tcpClient = TcpListener.AcceptTcpClient();
                //hand shake
                //Server send public key to client
                SendPublicKey(tcpClient);
                //Server read public key from client
                string publicKeyFromClient = ReadPublicKey(tcpClient);
                //handle login
                //Read username and password from user
                Message request = new Message();
                var     rsa     = new EncryptionRSA();
                rsa.SetPrivateKey(PrivateKey);
                request.ReadFrom(tcpClient.GetStream(), rsa);

                Message response = handlers[request.Header].Invoke(request);

                if (response.Header.Equals("OK"))
                {
                    Console.WriteLine("Client authenticated is connected...");
                    Console.WriteLine("server sends head: " + response.Header + " body " + response.Body);
                    var rsa2 = new EncryptionRSA();
                    rsa2.SetPublicKey(publicKeyFromClient);
                    response.WriteTo(tcpClient.GetStream(), rsa2);
                    RegisterClient(response.Body, publicKeyFromClient, tcpClient);
                    Thread thread = new Thread(() => HandleRequest(tcpClient));
                    thread.Start();
                }
                else
                {
                    var rsa2 = new EncryptionRSA();
                    rsa2.SetPublicKey(publicKeyFromClient);
                    response.WriteTo(tcpClient.GetStream(), rsa2);
                    Console.WriteLine("Client failed to connect");
                }
            }
        }
Ejemplo n.º 4
0
 private void HandleRequest(TcpClient tcpClient)
 {
     while (true)
     {
         Thread.Sleep(10);
         Message message = new Message();
         message.ReadFrom(tcpClient.GetStream());
         Console.WriteLine("Client send: body" + message.Body + "header" + message.Header);
         Message response = handlers[message.Header].Invoke(message);
     }
 }
Ejemplo n.º 5
0
        public void Start()
        {
            //start the server
            IPAddress iPAddress = Dns.GetHostEntry(hOST).AddressList[0];

            TcpListener = new TcpListener(iPAddress, pORT);


            try
            {
                TcpListener.Start();
                Console.WriteLine("Server started...");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            while (true)
            {
                TcpClient tcpClient = TcpListener.AcceptTcpClient();
                //handle login

                Message message = new Message();
                message.ReadFrom(tcpClient.GetStream());
                string username = message.Body.Split(';')[0];
                Console.WriteLine("username : "******"password : "******"Client authenticated is connected...");
                    RegisterClient(username, tcpClient);
                    Message response = new Message();

                    response.Body = username;
                    handlers["IS_AUTHENTICATED"].Invoke(response);
                    Thread thread = new Thread(() => HandleRequest(tcpClient));
                    thread.Start();
                }
                else
                {
                    Message response = new Message();
                    response.Header = "404";
                    response.Body   = "Username of password is not valid";
                    handlers["AUTHENTICATION_FAILED"].Invoke(response);
                }
            }
        }
Ejemplo n.º 6
0
 private void HandleRequest(TcpClient tcpClient)
 {
     while (true)
     {
         Thread.Sleep(10);
         Message message = new Message();
         var     rsa     = new EncryptionRSA();
         rsa.SetPrivateKey(PrivateKey);
         try
         {
             message.ReadFrom(tcpClient.GetStream(), rsa);
             Console.WriteLine("Client send: body" + message.Body + "header" + message.Header);
             handlers[message.Header].Invoke(message);
             broadcast(message);
         }
         catch (Exception e)
         {
             Console.WriteLine("Thread stop listening");
             break;
         }
     }
 }