示例#1
0
        void messageHandler()
        {
            uint idFrom = BitConverter.ToUInt32(fromClient(), 0);
            uint idTo   = BitConverter.ToUInt32(fromClient(), 0);

            transformD.Dispose();
            transformD = cryptP.CreateDecryptor();
            byte[] userByte = fromClient();
            userByte = transformD.TransformFinalBlock(userByte, 0, userByte.Length);
            string userFrom = Encoding.UTF8.GetString(userByte);

            transformD.Dispose();
            transformD = cryptP.CreateDecryptor();
            byte[] messageByte = fromClient();
            messageByte = transformD.TransformFinalBlock(messageByte, 0, messageByte.Length);
            string message = Encoding.UTF8.GetString(messageByte);

            Console.WriteLine("DEBUG " + (debug++) + ")");
            if (Program.clientList.ContainsKey(idTo))
            {
                SocketCommunication x       = Program.clientList[idTo] as SocketCommunication;
                Socket msgClient            = x.client;
                AesCryptoServiceProvider cp = x.cryptP;
                sendMessage(msgClient, cp, idFrom, userFrom, message);
                regMessage(Program.messageCP, message, idFrom, idTo, 1);
            }
            else
            {
                regMessage(Program.messageCP, message, idFrom, idTo, 0);
            }
        }
示例#2
0
        public void sending()
        {
            while (true)
            {
                using (var sqlCon = new MySqlConnection(Program.cs)) {
                    sqlCon.Open();
                    MySqlCommand cmd = sqlCon.CreateCommand();
                    cmd.CommandText = "SELECT id_message, message, fromU, toU FROM messages WHERE sent = 0;";
                    try {
                        MySqlDataReader dr = cmd.ExecuteReader();
                        if (dr.HasRows)
                        {
                            while (dr.Read())
                            {
                                UInt64 id_m   = dr.GetUInt64(dr.GetOrdinal("id_message"));
                                string msg    = dr.GetString(dr.GetOrdinal("message"));
                                uint   idFrom = dr.GetUInt32(dr.GetOrdinal("fromU"));
                                uint   idTo   = dr.GetUInt32(dr.GetOrdinal("toU"));
                                if (Program.clientList.ContainsKey(idTo))
                                {
                                    using (var sqlConUser = new MySqlConnection(Program.cs)) {
                                        sqlConUser.Open();
                                        MySqlCommand cmdUser = sqlConUser.CreateCommand();
                                        cmdUser.CommandText = "SELECT username FROM users WHERE id_user = @id";
                                        cmdUser.Parameters.AddWithValue("@id", idFrom);
                                        SocketCommunication sc = Program.clientList[idTo] as SocketCommunication;
                                        ICryptoTransform    dec;
                                        dec = Program.messageCP.CreateDecryptor();
                                        byte[] msgByte = Convert.FromBase64String(msg);
                                        msgByte = dec.TransformFinalBlock(msgByte, 0, msgByte.Length);
                                        msg     = Encoding.UTF8.GetString(msgByte);
                                        try {
                                            MySqlDataReader drUser = cmdUser.ExecuteReader();
                                            drUser.Read();
                                            SocketCommunication.sendMessage(sc.client, sc.cryptP, idFrom, drUser.GetString(drUser.GetOrdinal("username")), msg);
                                        } catch (Exception e) {
                                            Console.WriteLine("Errore: " + e.ToString());
                                        }
                                    }
                                    using (var sqlConUpdate = new MySqlConnection(Program.cs)) {
                                        sqlConUpdate.Open();
                                        MySqlCommand cmdUpdate = sqlConUpdate.CreateCommand();
                                        cmdUpdate.CommandText = "UPDATE messages SET sent = 1 WHERE id_message = @id;";
                                        cmdUpdate.Parameters.AddWithValue("@id", id_m);
                                        cmdUpdate.ExecuteNonQuery();
                                    }
                                }
                            }
                        }
                    } catch (MySqlException e) {
                        Console.WriteLine("Errore durante la lettura dei messaggi: " + e.ToString());
                    }
                }

                Thread.Sleep(5000);
            }
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Avvio Server Ijime");
            const int PORT = 10816;
            Socket    server;
            Socket    accClient = null;

            #region SOCKET BINDING
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, PORT);

            Console.WriteLine("Info: \nServer bind->" + localEndPoint.ToString());
            server.Bind(localEndPoint);
            server.Listen(100);

            #endregion

            #region MYSQL CONNECTION TEST
            MySqlConnection conn = null;

            try {
                conn = new MySqlConnection(cs);
                conn.Open();
                Console.WriteLine("MySQL Version: " + conn.ServerVersion + "\n" + "Database: " + conn.Database.ToString() + "\n" + "Stato: " + conn.State.ToString() + "\n---------------\n\n");
                conn.Close();
            } catch (MySqlException e) {
                Console.WriteLine("Error: " + e.ToString());
            }

            #endregion


            const string MSGKEY = "OGacb4z3VEwPqHdLIE1v8lU9r7wde5uitgDInqQKXBY=";
            const string MSGIV  = "IRx3Af40/klYajPj8gBmuQ==";

            messageCP.BlockSize = 128;
            messageCP.KeySize   = 256;
            messageCP.IV        = Convert.FromBase64String(MSGIV);
            messageCP.Key       = Convert.FromBase64String(MSGKEY);
            messageCP.Mode      = CipherMode.CBC;
            messageCP.Padding   = PaddingMode.PKCS7;
            messageSemder msgSender = new messageSemder();
            Thread        senderTh  = new Thread(msgSender.sending);
            senderTh.Start();

            while (true)
            {
                try {
                    accClient = server.Accept();
                    SocketCommunication sc = new SocketCommunication();
                    sc.client = accClient;
                    Thread th = new Thread(new ThreadStart(sc.tWork));
                    th.Start();
                } catch {
                    break;
                }
                Console.WriteLine("\n# NEW CONNECTION: Local Endpoint:" + accClient.LocalEndPoint.ToString() + " Remote Endpoint:" + accClient.RemoteEndPoint.ToString() + "\n");
            }
            server.Close();
            accClient.Close();
        }
示例#4
0
        public void tWork()
        {
            cryptSetup();

            cryptP.IV = inVec = fromClient();

            cryptP.Key = key = fromClient();

            transformD = cryptP.CreateDecryptor();
            transformE = cryptP.CreateEncryptor();
            Console.WriteLine("@ KEY RETRIVED: " + Convert.ToBase64String(key));
            try {
                while (flag)
                {
                    request = Int32.Parse(Encoding.ASCII.GetString(fromClient()));
                    Console.WriteLine("## Log: richiesta=" + request);
                    switch (request)
                    {
                    case (1):     //LOGIN
                        Console.WriteLine("\n -----------\nLogin Attempt");
                        login();
                        Console.WriteLine("Login Finish\n -----------");
                        break;

                    case (2):     //LOGOUT
                        if (Program.clientList.ContainsKey(id))
                        {
                            Console.WriteLine("\n -----------\nLogout Attempt");
                            SocketCommunication x = Program.clientList[id] as SocketCommunication;
                            Console.WriteLine("IP: " + x.client.RemoteEndPoint.ToString());
                            Program.clientList.Remove(id);
                            updateConnected(id, 0);

                            Console.WriteLine("Logout Finish\n -----------");
                        }
                        flag = false;
                        break;

                    case (3):     //GET PSY
                        getPsy();
                        break;

                    case (4):     //GET MESSAGE
                        messageHandler();
                        break;

                    default:
                        flag = false;
                        break;
                    }
                }
                Console.WriteLine("Client Disconnected");
                client.Close();
            } catch (Exception e) {
                Console.WriteLine("********************\nTHREAD CRASH\n********************");
                Console.WriteLine("Error: " + e.ToString());
                if (Program.clientList.ContainsKey(id))
                {
                    Program.clientList.Remove(id);
                    updateConnected(id, 0);
                }
                client.Close();
            }
        }