Пример #1
0
        public static void CreateNewDialog(long idClient, long idClient2)//Создание диалога
        {
            var             setting    = (Settings)Data.Settings;
            OleDbConnection connection = new OleDbConnection(ConnectCmd);

            connection.Open();

            OleDbCommand command = new OleDbCommand($"CREATE TABLE w_{idClient}_{idClient2} (w_message STRING" +
                                                    $" w_nick STRING)"
                                                    , connection); //This line is vulnerable to SQL injections. Please, use OleDbCommand.Parameters in future

            setting.MessageInfoChats.Add(new Data.IMessageInfoChat(0, $"w_{idClient}_{idClient2}", setting.LastIdChat,
                                                                   idClient, idClient2));
            Data.Settings = setting;

            SettingsManager.Save();
            command.ExecuteNonQuery();
            connection.Close();
        }
Пример #2
0
        private static void CheckNewConnect(object i)//Проверка нового подключения
        {
            WriteLine("Новое подключение!", ConsoleColor.Green);
            byte[]    buffer = new byte[4098];
            TcpClient client = (TcpClient)i;
            //NetworkStream networkClient = client.GetStream();

            //Task.Delay(30).Wait();//Ждём отправки сообщения
            //int messi = networkClient.Read(buffer, 0, buffer.Length);
            int messi = client.Client.Receive(buffer);

            Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, messi));

            Task.Delay(100).Wait();

            //Провека проги
            try
            {
                if (Encoding.UTF8.GetString(buffer, 0, messi) != "TCPCHAT 1.0")//!!!
                {
                    WriteLine("Ошибка: Cтарый или другой клиент!", ConsoleColor.Red);
                    client.Close();
                    return;//Проверить! (Готово)
                }
            }
            catch
            {
                WriteLine("Ошибка! Клиент!", ConsoleColor.Red);
                client.Close();
                return;
            }


            //Команды


            Task.Delay(10).Wait();


linkCommand:
            Task.Delay(100).Wait();

            //messi = client.Receive(buffer);
            try
            {
                messi = client.Client.Receive(buffer);
            } catch
            {
                WriteLine("Клиент " + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString() + " отключён.", ConsoleColor.Yellow);
                return;
            }
            string answer = Encoding.UTF8.GetString(buffer, 0, messi);

            WriteLine("Проверка нового подклюение...", ConsoleColor.Yellow);

            if (answer.Contains("%REG"))//регистрация
            {
                //email

                Match  regex = Regex.Match(answer, "%REG:(.*):(.*):(.*):(.*)");//Антон!
                string email = regex.Groups[1].Value;

                //string email = answer.
                //TODO: Сделать проверку email через подтверждение (Нужен smtp сервер)

                //пароль

                string pValue = regex.Groups[2].Value;
                if (pValue.Length > 64 || pValue.Length < 4)
                {
                    return;                        //Фикс длинных/коротких паролей
                }
                string passworld = SHA256(pValue); //Фикс CWE-257
                Console.WriteLine(passworld);

                //Nick

                string nick = regex.Groups[3].Value;
                Console.WriteLine(nick);

                //Аватарка

                int avatar = int.Parse(regex.Groups[4].Value);

                //Проверка

                bool checkNewAccount = Database.CheckClientEmail(email);
                //Если true, то email такой есть

                if (checkNewAccount)//!!!
                {
                    Console.WriteLine("0");
                    client.Client.Send(Encoding.UTF8.GetBytes("%REGWRONGEMAIL"));
                    goto linkCommand;
                }
                else
                {
                    Console.WriteLine("1");
                    Settings set = (Settings)Data.Settings;
                    set.LastIdUser = Database.GetLastIdAccount() + 1;
                    Database.AccountAdd(email, passworld, nick, set.LastIdUser, avatar, false);
                    client.Client.Send(Encoding.UTF8.GetBytes("%REGOD"));

                    WriteLine($"Новый аккаунт! {email}, {passworld}", ConsoleColor.Green);
                    Data.Settings = set;
                    SettingsManager.Save();

                    //Проверка подтверждения
                    //1 - есть подтверждение, 0 - нет (Но злой Антон всё переделал :) )

                    messi = client.Client.Receive(buffer);

                    answer = Encoding.UTF8.GetString(buffer, 0, messi);

                    if (answer == "1")
                    {
CheckDataConfirm:

                        //email

                        regex = Regex.Match(answer, "%LOG:(.*):(.*)");//Антон!
                        string emailCheck = regex.Groups[1].Value;

                        //пароль

                        string passworldCheck = regex.Groups[2].Value;

                        //Проверка
                        //1 - успешно, 0 - не успешно

                        if (emailCheck != email)
                        {
                            ErrorConfirmData(client, email, passworld);
                            goto CheckDataConfirm;
                        }
                        else if (passworldCheck != passworld)
                        {
                            ErrorConfirmData(client, email, passworld);
                            goto CheckDataConfirm;
                        }
                        else
                        {
                            client.Client.Send(Encoding.UTF8.GetBytes("1"));
                        }
                    }

                    return;
                }
            }
            else if (answer.Contains("%LOG"))//Вход
            {
                //email

                answer = Encoding.UTF8.GetString(buffer, 0, messi);

                Match  regex = Regex.Match(answer, "%LOG:(.*):(.*)");//Антон!
                string email = regex.Groups[1].Value;
                Console.WriteLine(email);
                //пароль

                string passworld = regex.Groups[2].Value;
                Console.WriteLine(passworld);
                //Проверка email

                bool checkClient = Database.CheckClientEmail(email);

                if (!checkClient)
                {
                    //networkClient.Write(Encoding.UTF8.GetBytes("0"), 0, buffer.Length);
                    client.Client.Send(Encoding.UTF8.GetBytes("%LOGWRONGEMAIL"));// False
                    WriteLine("Неправильный email!", ConsoleColor.Red);
                    goto linkCommand;
                }
                else
                {
                    //Проверка пароля

                    bool checkPassworld = Database.CheckClientPassworld(passworld);

                    if (!checkPassworld)//!!!
                    {
                        Console.WriteLine("%LOGWRONGEPASS");
                        client.Client.Send(Encoding.UTF8.GetBytes("%LOGWRONGEPASS"));
                        WriteLine($"Неправильный пароли в {email}!", ConsoleColor.Red);
                        goto linkCommand;
                    }
                    else
                    {
                        Console.WriteLine("%LOGOD");
                        client.Client.Send(Encoding.UTF8.GetBytes("%LOGOD"));

                        //Инцилизация!

                        Data.ClientConnectOnly onlyClient = new Data.ClientConnectOnly(client,
                                                                                       Database.GetNickClient(email), email, passworld, Database.GetIdClient(email),
                                                                                       Data.UserAvatar.Avatar1, false);//TODO

                        Data.ClientsOnlyData.Add(onlyClient);

                        Console.WriteLine($"{onlyClient.Email} {onlyClient.ID} {onlyClient.Nick} " +
                                          $"{onlyClient.Passworld}");

                        Thread thread = new Thread(new ParameterizedThreadStart(MessagesClient))
                        {
                            IsBackground = true
                        };

                        thread.Start(onlyClient);
                        WriteLine($"Вход аккаунт! {email}, {passworld}", ConsoleColor.Green);

                        return;
                    }
                }
            }
        }