Ejemplo n.º 1
0
        public void RequestActivation(string json)
        {
            string request = string.Empty;

            if (json != string.Empty)
            {
                var key = (JObject)JsonConvert.DeserializeObject(json);
                request = key["Key"].Value <string>();
            }
            switch (request)
            {
            case "registration":
                RegistrationJSON registrationJSON = System.Text.Json.JsonSerializer.Deserialize <RegistrationJSON>(json);
                Console.WriteLine($"New Client. Registration request.");
                Console.WriteLine("Requests: " + json);
                Registration(registrationJSON);
                Server.SendUpdateOnline();
                break;

            case "emailconfirmation":
                EmailConfirmationJSON emailConfirmationJSON = System.Text.Json.JsonSerializer.Deserialize <EmailConfirmationJSON>(json);
                Console.WriteLine($"New Client. Email confirmation request.");
                Console.WriteLine("=> Requests: " + json);
                EmailCodeVerification(emailConfirmationJSON);
                break;

            case "authorization":
                AuthorizationJSON authorizationJSON = System.Text.Json.JsonSerializer.Deserialize <AuthorizationJSON>(json);
                Console.WriteLine($"New Client. Authorization request.");
                Console.WriteLine("=> Requests: " + json);
                Authorization(authorizationJSON);
                Server.SendUpdateOnline();
                break;

            case "addfriend":
                AddFriendJSON addFriendJSON = System.Text.Json.JsonSerializer.Deserialize <AddFriendJSON>(json);
                Console.WriteLine($"New Client. Username: {Client.Username} add friend username:{addFriendJSON.FriendUsername}");
                Console.WriteLine("=> Requests: " + json);
                AddFriend(addFriendJSON);
                Server.SendUpdateOnline();
                break;

            case "getfriendlist":
                GetFrindListJSON getFrindListJSON = System.Text.Json.JsonSerializer.Deserialize <GetFrindListJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. FriendList request.");
                Console.WriteLine("=> Requests: " + json);
                GetFriendList(getFrindListJSON);
                break;

            case "getchat":
                GetChatJSON getChatJSON = System.Text.Json.JsonSerializer.Deserialize <GetChatJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. GetChat request.");
                Console.WriteLine("=> Requests: " + json);
                GetChat(getChatJSON);
                break;

            case "sendmessage":
                SendMessageJSON sendMessageJSON = System.Text.Json.JsonSerializer.Deserialize <SendMessageJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. Send message to user {sendMessageJSON.FriendUsername}");
                Console.WriteLine("=> Requests: " + json);
                SendMessage(sendMessageJSON);
                break;

            case "removefriend":
                AddFriendJSON removeFriend = System.Text.Json.JsonSerializer.Deserialize <AddFriendJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. Remove friend {removeFriend.FriendUsername}");
                Console.WriteLine("=> Requests: " + json);
                RemoveFriend(removeFriend);
                Server.SendUpdateOnline();
                break;

            case "statusonline":
                StatusOnlineJSON statusOnlineJSON = System.Text.Json.JsonSerializer.Deserialize <StatusOnlineJSON>(json);
                Console.WriteLine($"Client Username: {Client.Username}. Refresh status online.");
                Console.WriteLine("=> Requests: " + json);
                SendOlineStatus(statusOnlineJSON);
                break;

            default:
                Console.WriteLine($"Client IP: {Client.IPClient}. Sent string.Empty request.");
                Client.Close();
                break;
            }
        }
Ejemplo n.º 2
0
        private void Authorization(AuthorizationJSON authorizationJSON)
        {
            bool   verification = false;
            string reason       = "Сервер не разрешает авторизацию.";

            try
            {
                using (StreamReader sr = new StreamReader(ServerDirectory.Database_path, Encoding.UTF8))
                {
                    string line; //Сделать оптимизацию ввиде брейка
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line == string.Empty)
                        {
                            continue;
                        }
                        string usernameTemp = line;
                        string emailTemp;
                        if (line == authorizationJSON.UsernameOrEmail)
                        {
                            Client.Username = line;
                            emailTemp       = sr.ReadLine();
                            if ((line = sr.ReadLine()) == authorizationJSON.Password)
                            {
                                Client.Email    = emailTemp;
                                Client.Password = line;
                                verification    = true;
                                break;
                            }
                            else
                            {
                                reason = "Неправльный пароль.";
                            }
                        }
                        else
                        {
                            if ((line = sr.ReadLine()) == authorizationJSON.UsernameOrEmail)
                            {
                                Client.Email = line;
                                if ((line = sr.ReadLine()) == authorizationJSON.Password)
                                {
                                    Client.Username = usernameTemp;
                                    Client.Password = line;
                                    verification    = true;
                                    break;
                                }
                                else
                                {
                                    reason = "Неправльный пароль.";
                                }
                            }
                        }
                        line = sr.ReadLine();
                    }
                    sr.Close();
                    sr.Dispose();
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to open database.");
            }

            if (verification)
            {
                AllowAuthorizationJSON allowAuthorizationJSON = new AllowAuthorizationJSON()
                {
                    Allow    = true,
                    Username = Client.Username,
                    Email    = Client.Email,
                    Password = Client.Password,
                    Reason   = "Сервер дозволяє авторизацію."
                };
                string allowJSON = System.Text.Json.JsonSerializer.Serialize <AllowAuthorizationJSON>(allowAuthorizationJSON);
                Client.SendAnswer(allowJSON);
            }
            else
            {
                AllowAuthorizationJSON allowAuthorizationJSON = new AllowAuthorizationJSON()
                {
                    Allow  = false,
                    Reason = "Сервер не дозволяє авторизуватися."
                };
                string allowJSON = System.Text.Json.JsonSerializer.Serialize <AllowAuthorizationJSON>(allowAuthorizationJSON);
                Client.SendAnswer(allowJSON);
            }
        }