Exemplo n.º 1
0
        private static MonitoringChannel OpenMonitoringChannel()
        {
            NetTcpBinding binding = new NetTcpBinding();
            string        address = "net.tcp://localhost:7000/WCFMonitoringServer";

            MonitoringChannel proxy = new MonitoringChannel(binding, new EndpointAddress(address));

            return(proxy);
        }
Exemplo n.º 2
0
        static int Main(string[] args)
        {
            string users = String.Empty;
            string clientId;
            int    otherClient;
            string encryptedSecretKey = String.Empty;
            string decryptedSecretKey;
            RSA_Asimm_Algorithm_C rsa = new RSA_Asimm_Algorithm_C();

            string publicKey = rsa.GenerateKeys();

            WCFClient proxy = BindToCentralServer();

            Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            IIdentity       identity        = Thread.CurrentPrincipal.Identity;
            WindowsIdentity winIdentity     = identity as WindowsIdentity;
            string          senderName      = Formatter.ParseName(identity.Name);
            int             peerServicePort = 6000;

            try
            {
                clientId = proxy.Connect(identity.Name, winIdentity.User.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Connection failed. \nDetails: " + e.Message);
                Console.WriteLine("Press any key to close connection. ");
                Console.ReadKey();
                proxy.Abort();
                return(0);
            }

            peerServicePort += Int32.Parse(clientId);
            Console.WriteLine("Current: peer_" + clientId);

            ServiceHost       host            = OpenPeerService(peerServicePort);
            MonitoringChannel proxyMonitoring = OpenMonitoringChannel();    //open channel for logging all messages

            try
            {
                encryptedSecretKey = proxy.GenerateSecretKey(publicKey); //klijent dobija kriptovan tajni kljuc
            }
            catch (Exception e)
            {
                Console.WriteLine("Encryption of secret key failed. \nDetails: " + e.Message);
                Console.WriteLine("Press any key to close connection. ");
                Console.ReadKey();
                proxy.Abort();
                return(0);
            }
            decryptedSecretKey = rsa.DecryptData(encryptedSecretKey);

            string publicKeyFromMonitoring = String.Empty;

            try
            {
                publicKeyFromMonitoring = proxyMonitoring.GenerateRSAKeys();
            }
            catch (Exception e)
            {
                Console.WriteLine("Getting public RSA key from monitoring failed. \nDetails: " + e.Message);
            }

            string encryptedSecretKeyWithMonitoringKey = rsa.EncryptData(publicKeyFromMonitoring, decryptedSecretKey);

            try
            {
                proxyMonitoring.SendSecretKey(encryptedSecretKeyWithMonitoringKey);
            }
            catch (FaultException e)
            {
                Console.WriteLine("Sending secret key to monitoring failed. \nDetails: " + e.Message);
            }

            while (true)
            {
                int m = Menu();
                if (m == 1)
                {
                    try
                    {
                        users = proxy.GetConnectedClients();
                    }
                    catch (FaultException e)
                    {
                        Console.WriteLine("Getting list of connected clients from central server failed. \nDetails: " + e.Message);
                    }
                    DeserializeJson(users);
                    PrintConnectedClients();
                    otherClient = ChooseClient();//otherUser i otherClient se ne podudaraju ali otherUser = dict vrednost sa keyom otherClient

                    if (otherClient == 0)
                    {
                        continue;
                    }
                    User otherUser = DBClients.connectedClients[otherClient];
                    if (otherUser.Counter == Int32.Parse(clientId))
                    {
                        Console.WriteLine("This is your account. Please choose again.");
                        continue;
                    }
                    Peer proxyPeerClient = OpenPeerClient(otherUser.Counter);
                    while (true)
                    {
                        Console.WriteLine("Enter message to send [type 'x' for disconection]:");
                        string messageToSend = Console.ReadLine();
                        if (messageToSend.Equals("x") || messageToSend.Equals("X"))
                        {
                            break;
                        }
                        try
                        {
                            proxyPeerClient.SendMessage(messageToSend);
                        }catch (Exception e)
                        {
                            Console.WriteLine("Sending message to client failed. \nDetails: " + e.Message);
                            break;
                        }
                        byte[] encryptedMessage      = AES_ENCRYPTION.EncryptFile(messageToSend, decryptedSecretKey);
                        byte[] encryptedSenderName   = AES_ENCRYPTION.EncryptFile(senderName, decryptedSecretKey);
                        byte[] encryptedRecieverName = AES_ENCRYPTION.EncryptFile(otherUser.Name, decryptedSecretKey);
                        try
                        {
                            proxyMonitoring.LogMessage(encryptedMessage, encryptedSenderName, encryptedRecieverName);
                        }
                        catch (FaultException e)
                        {
                            Console.WriteLine("Logging message on server failed. \nDetails: " + e.Message);
                            break;
                        }
                    }
                    proxyPeerClient.Abort();
                }
                if (m == 0)
                {
                    break;
                }
            }

            Console.WriteLine("Press any key to close..");
            Console.ReadKey();
            try
            {
                proxy.Disconnect(winIdentity.User.ToString());
            } catch (FaultException e)
            {
                Console.WriteLine("Disconnection failed. \nDetails: " + e.Message);
            }
            host.Close();
            proxy.Close();
            return(0);
        }