Esempio n. 1
0
        public async void AfterServerConfirmed(X509Certificate2 clientCert, ServerInfo info, String address, String password, String userName)
        {
            MainPage = new Loading("Client is being registred...");
            try
            {
                await Task.Run(() =>
                {
                    ConnectionUtils.Register(out connection, out settings, logger, Log, address, clientCert, config, userName, settingsLoader, info);
                });

                Init();
            }
            catch (Exception ex)
            {
                logger.LogException(this, ex);
                MainPage = new ServerSelection(this, clientCert, address, password, userName, ex.Message);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
main:
            logger = new Logger(new ConsoleLoggerOutput());
            WriteLine("Chatovatko client at your service!");
            try {
                config        = new ConsoleClientDatabaseConfig();
                initializator = new DBInitializator(config, logger);
                initializator.DBEnsureCreated();

                settingsLoader = new SettingsLoader(config, logger);

                if (settingsLoader.Exists())
                {
                    Log("Settings exists and will be loaded.");
                    settings   = settingsLoader.GetSettingsCapsula();
                    connection = new Connection(logger, settings);
                }
                else
                {
                    Log("Settings doesn't exist.");
                }

                bool running = true;
                while (running)
                {
                    try
                    {
                        String   command      = ReadLine().Trim();
                        String[] commandParts = command.Split(' ');

                        switch (commandParts[0])
                        {
                        case "init":
                            if (commandParts.Length < 3)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }

                            if (settings != null)
                            {
                                WriteLine("Chatovatko is initialized already.");
                                break;
                            }

                            bool?  newUser       = null;
                            string userName      = null;
                            string serverAddress = commandParts[2];
                            switch (commandParts[1])
                            {
                            case "new":
                                newUser = true;
                                break;

                            case "login":
                                newUser = false;
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            if (newUser != null)
                            {
                                X509Certificate2 clientCert;
                                if (newUser == true)
                                {
                                    WriteLine("Your certificate is being created. Please, be patient.");
                                    clientCert = X509Certificate2Generator.GenerateCACertificate(logger);

                                    WriteLine("Your certificate has been generated. Enter path to save it: [default: ~/.chatovatko/mykey.p12]");
                                    string path = ReadLine();
                                    if (path.Equals(""))
                                    {
                                        path = $"{Utils.GetConfigDirectory()}/mykey.p12";
                                    }
                                    X509Certificate2Utils.ExportToPkcs12File(clientCert, path);
                                    WriteLine("----------------------------------------------------------------");

                                    WriteLine("Enter your new unique username:"******"Enter path to your certificate please: [default: ~/.chatovatko/mykey.p12]");
                                    string path = ReadLine();
                                    if (path.Equals(""))
                                    {
                                        path = $"{Utils.GetConfigDirectory()}/mykey.p12";
                                    }

                                    WriteLine("If you are logining to this server first time, it is nessary to enter you new unique username:"******"Do you trust this server (y/n): ");

                                if (!ReadLine().Equals("y"))
                                {
                                    break;
                                }


                                ConnectionUtils.Register(out connection, out settings, logger, Log, serverAddress, clientCert, config, userName, settingsLoader, info);
                            }
                            break;

                        case "connect":
                            CreateOpenedConnection(true);
                            break;

                        case "disconnect":
                            if (!VerifyConnectionOpened(true))
                            {
                                break;
                            }
                            connection.Disconnect();
                            break;

                        case "search":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }

                            SearchCServerCapsula searchCapsula;

                            if (Validators.ValidateRegexUserName(commandParts[1]))
                            {
                                searchCapsula = connection.SearchContact(commandParts[1]);
                            }
                            else
                            {
                                searchCapsula = connection.SearchContact(Int32.Parse(commandParts[1]));
                            }

                            PrintSearchCapsula(searchCapsula);
                            Write("Do you trust this is the searched user, and do you want to save him? (y/n): ");
                            if (ReadLine().Equals("y"))
                            {
                                SaveUser(searchCapsula);
                            }
                            break;

                        case "push":
                            connection.Push();
                            break;

                        case "pull":
                            connection.Pull();
                            break;

                        case "delete":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "database":
                                initializator.DBDelete();
                                WriteLine();

                                running        = false;
                                config         = null;
                                initializator  = null;
                                settingsLoader = null;
                                settings       = null;
                                connection     = null;
                                goto main;

                            case "message":
                                if (commandParts.Length < 3)
                                {
                                    WriteNotEnoughParameters();
                                    break;
                                }
                                DeleteMessage(Int32.Parse(commandParts[2]));
                                break;

                            case "thread":
                                if (commandParts.Length < 3)
                                {
                                    WriteNotEnoughParameters();
                                    break;
                                }
                                DeleteThread(GetThreadId(commandParts[2]));
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "download":
                            if (commandParts.Length < 3)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "info":
                                WriteServerInfo(commandParts[2]);
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "ls":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "users":
                                WriteUsers();
                                break;

                            case "threads":
                                WriteThreads();
                                break;

                            case "messages":
                                if (commandParts.Length < 3)
                                {
                                    WriteNotEnoughParameters();
                                    break;
                                }
                                WriteMessages(GetThreadId(commandParts[2]));
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "post":
                            if (commandParts.Length < 4)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "thread":
                                PostThread(GetUserId(commandParts[2]), BuildFromRest(commandParts, 3));
                                break;

                            case "message":
                                PostMessage(GetThreadId(commandParts[2]), commandParts[3]);
                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "nickname":
                            if (commandParts.Length < 3)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            int    userId   = GetUserId(commandParts[1]);
                            string nickName = BuildFromRest(commandParts, 2);
                            SetNickName(userId, nickName);
                            break;

                        case "rename":
                            if (commandParts.Length < 3)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }

                            RenameThread(GetThreadId(commandParts[1]), BuildFromRest(commandParts, 2));
                            break;

                        case "trust":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            VerifyConnectionOpened(true);
                            connection.TrustContact(GetUserId(commandParts, 1));
                            break;

                        case "untrust":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            VerifyConnectionOpened(true);
                            connection.UntrustContact(GetUserId(commandParts, 1));
                            break;

                        case "generate":
                            if (commandParts.Length < 2)
                            {
                                WriteNotEnoughParameters();
                                break;
                            }
                            switch (commandParts[1])
                            {
                            case "X509Certificate2":
                                X509Certificate2 cert = X509Certificate2Generator.GenerateCACertificate(logger);
                                WriteLine(X509Certificate2Utils.ExportToBase64(cert));

                                break;

                            default:
                                WriteSyntaxError(commandParts[1]);
                                break;
                            }
                            break;

                        case "aesTrial":
                            if (!VerifyConnectionOpened(true))
                            {
                                break;
                            }
                            AesTrial();
                            break;

                        case "hash":
                            PrintHash(settings.ClientCertificate);
                            break;

                        case "exit":
                        case "quit":
                            running = false;
                            break;

                        case "--":
                        case "":
                        case "#":
                            break;

                        case "status":
                            WriteStatus(settings.Settings, config);
                            break;

                        default:
                            WriteSyntaxError(commandParts[0]);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.LogException(ex, "Program", "Core", "The command has failed.");
                    }
                }

                //Config config;

                //Connection.Connect();
            }
            catch (Exception ex)
            {
                logger.LogException(ex, "Program", "Core", "Core has crashed.");
            }
            finally
            {
                logger.Close();
                logger = null;
            }
        }