Exemplo n.º 1
0
        private void CmdSaveConfig()
        {
            //Ensure session
            if (!EnsureSessionExists())
            {
                return;
            }

            //Make a copy
            if (File.Exists(cfg._savePath))
            {
                CLITools.PrintText("Backing up...");
                if (File.Exists(cfg._savePath + ".bak"))
                {
                    File.Delete(cfg._savePath + ".bak");
                }
                File.Copy(cfg._savePath, cfg._savePath + ".bak");
            }

            //Save
            CLITools.PrintText("Saving...");
            cfg.Save();
            CLITools.PrintText("Saved to disk successfully.", ConsoleColor.Green);
            unsaved = false;
        }
Exemplo n.º 2
0
        private void CmdSuperuserRemove()
        {
            //Ensure session
            if (!EnsureSessionExists())
            {
                return;
            }

            //Prompt username + password
            string username = CLITools.PromptFormTextInput("Remove Superuser - Username", "Type the username of the superuser you want to remove.");

            //Find
            DeltaAdminAccount account = null;

            foreach (var u in cfg.admin_credentials)
            {
                if (u.username == username)
                {
                    account = u;
                }
            }

            //Remove
            if (account != null)
            {
                cfg.admin_credentials.Remove(account);
                unsaved = true;
                CLITools.PrintText($"Removed user \"{username}\".", ConsoleColor.Green);
            }
            else
            {
                CLITools.PrintText("Could not find that user. No superusers were removed.", ConsoleColor.Red);
            }
        }
Exemplo n.º 3
0
        private bool EnsureSessionExists()
        {
            //Make sure we have a session
            if (cfg != null)
            {
                return(true);
            }

            //We don't
            CLITools.PrintText("You haven't created or loaded a session. Run the \"new\" or \"open\" command first.", ConsoleColor.Red);
            return(false);
        }
Exemplo n.º 4
0
        private bool ConfirmUnsavedExit()
        {
            //Return true if we're saved
            if (cfg == null || !unsaved)
            {
                return(true);
            }

            //Confirm
            CLITools.PrintText("You have unsaved changes. Run the \"save\" command to commit them.", ConsoleColor.Yellow);
            CLITools.PrintText("Are you sure you want to exit without saving these changes?");
            return(CLITools.PromptSelect("DON'T EXIT", "EXIT") == "EXIT");
        }
Exemplo n.º 5
0
        private void CmdSuperuserList()
        {
            //Ensure session
            if (!EnsureSessionExists())
            {
                return;
            }

            //List all
            foreach (var su in cfg.admin_credentials)
            {
                CLITools.PrintText($"* {su.username} (added {su.addedAt.ToShortDateString()} {su.addedAt.ToLongTimeString()} UTC)");
            }
            CLITools.PrintText(cfg.admin_credentials.Count + " superusers", ConsoleColor.Green);
        }
Exemplo n.º 6
0
        public void Run()
        {
            //Write header
            CLITools.PrintText("DeltaWebMap MasterControl CLI (C) DeltaWebMap, RomanPort 2020", ConsoleColor.White, ConsoleColor.Blue);
            CLITools.PrintText($"App Version {Program.APP_VERSION_MAJOR}.{Program.APP_VERSION_MINOR} - Lib Version {DeltaConnection.LIB_VERSION_MAJOR}.{DeltaConnection.LIB_VERSION_MINOR}", ConsoleColor.Blue);
            CLITools.PrintText("You're seeing this CLI because you ran this program without any arguments.");
            CLITools.PrintText("To run the master control server normally, pass in the path to the config file.\n");
            CLITools.PrintText("Type your command. Type \"help\" for a list of commands.");

            //Loop
            while (true)
            {
                //Read command
                string[] args = CLITools.PromptString("help").Split(' ');
                string   cmd  = args[0];

                //Respond
                switch (cmd)
                {
                case "help": PrintHelp(); break;

                case "quit": if (ConfirmUnsavedExit())
                    {
                        return;
                    }
                    else
                    {
                        break;
                    }

                case "new": CmdNewConfig(); break;

                case "open": CmdLoadConfig(); break;

                case "save": CmdSaveConfig(); break;

                case "edit_cfg": CfgEditConfig(); break;

                case "superuser_add": CmdSuperuserAdd(); break;

                case "superuser_list": CmdSuperuserList(); break;

                case "superuser_remove": CmdSuperuserRemove(); break;

                default: CLITools.PrintText("Unknown command. Type \"help\" for a list of commands.", ConsoleColor.Red); break;
                }
            }
        }
Exemplo n.º 7
0
 private void PrintHelp()
 {
     CLITools.PrintText("DeltaWebMap CLI Help", ConsoleColor.Blue);
     CLITools.PrintText("<- CLI Tools ->", ConsoleColor.Cyan);
     CLITools.PrintText("[help] - Shows this menu.");
     CLITools.PrintText("[quit] - Quits to the command line.");
     CLITools.PrintText("<- File Tools ->", ConsoleColor.Cyan);
     CLITools.PrintText("[new] - Creates a new Delta Web Map config file.");
     CLITools.PrintText("[open] - Opens an existing Delta Web Map config file.");
     CLITools.PrintText("[save] - Saves the current config file to disk.");
     CLITools.PrintText("<- Editing Tools ->", ConsoleColor.Cyan);
     CLITools.PrintText("[edit_cfg] - Edits the general config.");
     CLITools.PrintText("<- Superuser Tools ->", ConsoleColor.Cyan);
     CLITools.PrintText("[superuser_add] - Adds a new superuser.");
     CLITools.PrintText("[superuser_list] - Lists the current superusers.");
     CLITools.PrintText("[superuser_remove] - Removes a superuser by username.");
 }
Exemplo n.º 8
0
        private void CmdLoadConfig()
        {
            //Prompt install location
            string installPath = CLITools.PromptFormTextInput("Load Config - Specify Path", "Type the filename to load.");

            //Verify
            if (!File.Exists(installPath))
            {
                CLITools.PrintText("ERROR: The file path you specified already exists.", ConsoleColor.Red);
                return;
            }

            //Load
            cfg     = DeltaConfig.OpenConfig(installPath);
            unsaved = false;

            //Write ack
            CLITools.PrintText("Configuration was successfully loaded.", ConsoleColor.Green);
        }
Exemplo n.º 9
0
        private void CmdSuperuserAdd()
        {
            //Ensure session
            if (!EnsureSessionExists())
            {
                return;
            }

            //Prompt username + password
            string username = CLITools.PromptFormTextInput("Add Superuser - Username", "Superusers have full access to the admin control panel. Type the username.");
            string password = CLITools.PromptFormTextInput("Add Superuser - Password", "Type the password to the account. This will be salted+hashed and stored locally.");

            //Make sure we don't already have a user with this
            foreach (var u in cfg.admin_credentials)
            {
                if (u.username == username)
                {
                    CLITools.PrintText("ERROR: A user already exists with that username.", ConsoleColor.Red);
                    return;
                }
            }

            //Generate
            CLITools.PrintText("Generating...");
            byte[] hash = PasswordTool.HashPassword(password, out byte[] salt);

            //Add
            cfg.admin_credentials.Add(new DeltaAdminAccount
            {
                username     = username,
                passwordSalt = Convert.ToBase64String(salt),
                passwordHash = Convert.ToBase64String(hash),
                addedAt      = DateTime.UtcNow
            });
            unsaved = true;

            //Ack
            CLITools.PrintText($"Added user \"{username}\"!", ConsoleColor.Green);
        }
Exemplo n.º 10
0
        private void CmdNewConfig()
        {
            //Prompt install location
            string installPath = CLITools.PromptFormTextInput("New Config - Specify Path", "Type the filename to save as. This will be a JSON file.");

            //Verify
            if (!Path.IsPathFullyQualified(installPath))
            {
                CLITools.PrintText("ERROR: The install path you specified is invalid.", ConsoleColor.Red);
                return;
            }
            if (File.Exists(installPath))
            {
                CLITools.PrintText("ERROR: The file path you specified already exists.", ConsoleColor.Red);
                return;
            }

            //Make
            cfg     = DeltaConfig.NewConfig(installPath);
            unsaved = true;

            //Write ack
            CLITools.PrintText("Configuration was successfully created.", ConsoleColor.Green);
        }