public JsonResult Index([Deserialize] CommandContext commandContext, string cli, bool parseAsHtml = false)
        {
            _terminalApi.Username       = User.Identity.IsAuthenticated ? User.Identity.Name : null;
            _terminalApi.IPAddress      = Request.UserHostAddress;
            _terminalApi.CommandContext = commandContext;
            _terminalApi.ParseAsHtml    = parseAsHtml;
            var commandResult = _terminalApi.ExecuteCommand(cli);

            if (User.Identity.IsAuthenticated)
            {
                if (commandResult.CurrentUser == null)
                {
                    FormsAuthentication.SignOut();
                }
            }
            else
            {
                if (commandResult.CurrentUser != null)
                {
                    FormsAuthentication.SetAuthCookie(commandResult.CurrentUser.Username, true);
                }
            }

            var displayItems = new List <ApiDisplayItem>();

            commandResult.DisplayItems.ForEach(x => displayItems.Add(new ApiDisplayItem
            {
                Text     = x.Text,
                Bold     = (x.DisplayMode & DisplayMode.Bold) != 0,
                Dim      = (x.DisplayMode & DisplayMode.Dim) != 0,
                DontType = (x.DisplayMode & DisplayMode.DontType) != 0,
                Inverted = (x.DisplayMode & DisplayMode.Inverted) != 0,
                Italics  = (x.DisplayMode & DisplayMode.Italics) != 0,
                Mute     = (x.DisplayMode & DisplayMode.Mute) != 0,
                Parse    = (x.DisplayMode & DisplayMode.Parse) != 0
            }));

            var apiResult = new ApiResult
            {
                ClearScreen   = commandResult.ClearScreen,
                Command       = commandResult.Command,
                ContextStatus = commandResult.CommandContext.Status.ToString(),
                ContextText   = commandResult.CommandContext.Command
                                + (commandResult.CommandContext.Text.IsNullOrEmpty()
                ? null : string.Format(" {0}", _terminalApi.CommandContext.Text)),
                CurrentUser    = commandResult.CurrentUser != null ? commandResult.CurrentUser.Username : null,
                DisplayItems   = displayItems,
                EditText       = commandResult.EditText,
                Exit           = commandResult.Exit,
                PasswordField  = commandResult.PasswordField,
                ScrollToBottom = commandResult.ScrollToBottom,
                SessionId      = Session.SessionID,
                TerminalTitle  = commandResult.TerminalTitle,
                CommandContext = commandResult.CommandContext.Serialize()
            };

            //string json = new JavaScriptSerializer().Serialize(apiResult);
            //return callback != null ? string.Format("{0}({1});", callback, json) : json;
            return(Json(apiResult));
        }
Exemple #2
0
        /// <summary>
        /// Pass the command string to the terminal core for execution, then examine the resulting instructions.
        /// </summary>
        /// <param name="commandString">The command string passed from the command line.</param>
        private static void InvokeCommand(string commandString)
        {
            // Instantiate the Ninject kernel and pass in the pre-defined Ninject Module from Terminal.Core.Ninject.
            var kernel = new StandardKernel(new TerminalBindings(false));

            // Grab the terminal API object from Ninject.
            _terminalApi = kernel.Get <TerminalApi>();

            // Set the username on the API. This tells the API if someone is logged in or not. If nobody is logged in
            // then _username is null by default;
            _terminalApi.Username = _username;
            // Set the CommandContext object. This persists state information between API requests.
            _terminalApi.CommandContext = _commandContext;

            // Launch a separate thread to print a loading message while waiting for the terminal API to execute commands.
            var loadingThread = new Thread(ShowLoading);

            loadingThread.Start();

            // Pass command string to the terminal API. No pre-parsing necessary, just pass the string.
            var commandResult = _terminalApi.ExecuteCommand(commandString);

            // Stop the loading message thread when API returns.
            loadingThread.Abort();
            loadingThread.Join();

            // Pass result object to special method that will determine how to display the results.
            InterpretResult(commandResult);
        }
Exemple #3
0
        public string Index(string cli, string callback, bool parseAsHtml = false)
        {
            AppSettings.ConnectionString = ConfigurationManager.ConnectionStrings["EntityContainer"].ConnectionString;

            if (Session["commandContext"] != null)
            {
                _commandContext = (CommandContext)Session["commandContext"];
            }
            _terminalApi.Username       = Session["currentUser"] != null ? Session["currentUser"].ToString() : null;
            _terminalApi.CommandContext = _commandContext;
            _terminalApi.ParseAsHtml    = parseAsHtml;
            var commandResult = _terminalApi.ExecuteCommand(cli);

            Session["currentUser"]    = commandResult.CurrentUser != null ? commandResult.CurrentUser.Username : null;
            Session["commandContext"] = commandResult.CommandContext;

            var displayItems = new List <ApiDisplayItem>();

            commandResult.Display.ForEach(x => displayItems.Add(new ApiDisplayItem
            {
                Text     = x.Text,
                Bold     = (x.DisplayMode & DisplayMode.Bold) != 0,
                Dim      = (x.DisplayMode & DisplayMode.Dim) != 0,
                DontType = (x.DisplayMode & DisplayMode.DontType) != 0,
                Inverted = (x.DisplayMode & DisplayMode.Inverted) != 0,
                Italics  = (x.DisplayMode & DisplayMode.Italics) != 0,
                Mute     = (x.DisplayMode & DisplayMode.Mute) != 0,
                Parse    = (x.DisplayMode & DisplayMode.Parse) != 0
            }));

            var apiResult = new ApiResult
            {
                ClearScreen   = commandResult.ClearScreen,
                Command       = commandResult.Command,
                ContextStatus = commandResult.CommandContext.Status.ToString(),
                ContextText   = commandResult.CommandContext.Command
                                + (commandResult.CommandContext.Text.IsNullOrEmpty()
                ? null : string.Format(" {0}", _terminalApi.CommandContext.Text)),
                CurrentUser    = commandResult.CurrentUser != null ? commandResult.CurrentUser.Username : null,
                DisplayItems   = displayItems,
                EditText       = commandResult.EditText,
                Exit           = commandResult.Exit,
                PasswordField  = commandResult.PasswordField,
                ScrollToBottom = commandResult.ScrollToBottom,
                SessionId      = Session.SessionID,
                TerminalTitle  = commandResult.TerminalTitle
            };
            string json = new JavaScriptSerializer().Serialize(apiResult);

            return(callback != null?string.Format("{0}({1});", callback, json) : json);
        }
Exemple #4
0
        /// <summary>
        /// Pass the command string to the terminal API for execution, then examine the resulting instructions.
        /// </summary>
        /// <param name="commandString">The command string passed from the command line.</param>
        private static void InvokeCommand(string commandString)
        {
            var kernel = new StandardKernel(new U413Bindings(false));

            _terminalApi                = kernel.Get <TerminalApi>();
            _terminalApi.Username       = _username;
            _terminalApi.CommandContext = _commandContext;

            var loadingThread = new Thread(ShowLoading);

            loadingThread.Start();

            var commandResult = _terminalApi.ExecuteCommand(commandString);

            loadingThread.Abort();
            loadingThread.Join();

            InterpretResult(commandResult);
        }
        public ViewResult Index(string Cli, string Display, [Deserialize] CommandContext CommandContext)
        {
            if (User.Identity.IsAuthenticated || Session["hasAccess"] != null)
            {
                ModelState.Clear();
                _terminalCore.Username       = User.Identity.IsAuthenticated ? User.Identity.Name : null;
                _terminalCore.IPAddress      = Request.UserHostAddress;
                _terminalCore.CommandContext = CommandContext;
                _terminalCore.ParseAsHtml    = true;
                var commandResult = _terminalCore.ExecuteCommand(Cli);

                if (commandResult.ClearScreen)
                {
                    Display = null;
                }

                if (User.Identity.IsAuthenticated)
                {
                    if (commandResult.CurrentUser == null)
                    {
                        FormsAuthentication.SignOut();
                    }
                }
                else
                {
                    if (commandResult.CurrentUser != null)
                    {
                        FormsAuthentication.SetAuthCookie(commandResult.CurrentUser.Username, true);
                    }
                }

                var display = new StringBuilder();
                foreach (var displayItem in commandResult.DisplayItems)
                {
                    display.Append(displayItem.Text);
                    display.Append("<br />");
                }

                if (Display != null)
                {
                    Display += "<br />";
                }

                var viewModel = new TerminalViewModel
                {
                    Cli         = commandResult.EditText,
                    ContextText = commandResult.CommandContext.Command
                                  + (commandResult.CommandContext.Text.IsNullOrEmpty()
                    ? null : string.Format(" {0}", _terminalCore.CommandContext.Text)),
                    Display        = Display + display.ToString(),
                    PasswordField  = commandResult.PasswordField,
                    Notifications  = string.Empty,
                    Title          = commandResult.TerminalTitle,
                    CommandContext = commandResult.CommandContext
                };

                return(View(viewModel));
            }
            else
            {
                return(View("Facade"));
            }
        }
Exemple #6
0
        public ViewResult Index(string Cli, string Display)
        {
            AppSettings.ConnectionString = ConfigurationManager.ConnectionStrings["EntityContainer"].ConnectionString;

            if (Session["apiSessionId"] == null)
            {
                Session["apiSessionId"] = new WebClient().DownloadString(ConfigurationManager.AppSettings["ApiUrl"] + "GetSessionId");
            }

            ModelState.Clear();
            if (Session["commandContext"] != null)
            {
                _commandContext = (CommandContext)Session["commandContext"];
            }
            _terminalApi.Username       = User.Identity.IsAuthenticated ? User.Identity.Name : null;
            _terminalApi.CommandContext = _commandContext;
            _terminalApi.ParseAsHtml    = true;
            var commandResult = _terminalApi.ExecuteCommand(Cli);

            if (commandResult.ClearScreen)
            {
                Display = null;
            }

            if (User.Identity.IsAuthenticated)
            {
                if (commandResult.CurrentUser == null)
                {
                    FormsAuthentication.SignOut();
                }
            }
            else
            {
                if (commandResult.CurrentUser != null)
                {
                    FormsAuthentication.SetAuthCookie(commandResult.CurrentUser.Username, false);
                }
            }

            Session["commandContext"] = commandResult.CommandContext;

            var display = new StringBuilder();

            foreach (var displayItem in commandResult.Display)
            {
                display.Append(displayItem.Text);
                display.Append("<br />");
            }

            if (Display != null)
            {
                Display += "<br />";
            }

            var viewModel = new TerminalViewModel
            {
                Cli         = commandResult.EditText,
                ContextText = commandResult.CommandContext.Command
                              + (commandResult.CommandContext.Text.IsNullOrEmpty()
                ? null : string.Format(" {0}", _terminalApi.CommandContext.Text)),
                Display       = Display + display.ToString(),
                PasswordField = commandResult.PasswordField,
                Notifications = string.Empty,
                Title         = commandResult.TerminalTitle,
                SessionId     = Session["apiSessionId"].ToString()
            };

            return(View(viewModel));
        }