From 2ce2214d5e43fb625c2f9cde22f791ee097643e1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 21 Jan 2017 21:27:25 -0500 Subject: Commands can now be locked out of remote sessions Try using sos.shutdown remotely... it won't work. :stuck_out_tongue_winking_eye: --- ShiftOS_TheReturn/TerminalBackend.cs | 150 ++++++++++++++++++++--------------- 1 file changed, 86 insertions(+), 64 deletions(-) (limited to 'ShiftOS_TheReturn/TerminalBackend.cs') diff --git a/ShiftOS_TheReturn/TerminalBackend.cs b/ShiftOS_TheReturn/TerminalBackend.cs index de9e179..4edc709 100644 --- a/ShiftOS_TheReturn/TerminalBackend.cs +++ b/ShiftOS_TheReturn/TerminalBackend.cs @@ -66,7 +66,7 @@ namespace ShiftOS.Engine public static string LastCommand = ""; - public static void InvokeCommand(string text) + public static void InvokeCommand(string text, bool isRemote = false) { try { @@ -75,7 +75,7 @@ namespace ShiftOS.Engine var args = GetArgs(ref text); - bool commandWasClient = RunClient(text, args); + bool commandWasClient = RunClient(text, args, isRemote); if (!commandWasClient && !string.IsNullOrWhiteSpace(text)) { @@ -109,7 +109,21 @@ namespace ShiftOS.Engine TerminalRequested?.Invoke(); } - public static bool RunClient(string text, Dictionary args) + public static bool CanRunRemotely(MethodInfo mth, bool isRemote) + { + if (!isRemote) + return true; + + foreach(var attr in mth.GetCustomAttributes(false)) + { + if (attr is RemoteLockAttribute) + return false; + } + + return true; + } + + public static bool RunClient(string text, Dictionary args, bool isRemote = false) { latestCommmand = text; @@ -136,97 +150,105 @@ namespace ShiftOS.Engine { if (Shiftorium.UpgradeAttributesUnlocked(method)) { - foreach (var ma in method.GetCustomAttributes(false)) + if (CanRunRemotely(method, isRemote)) { - if (ma is Command) + foreach (var ma in method.GetCustomAttributes(false)) { - var cmd = ma as Command; - if (text.Split('.')[1] == cmd.name) + if (ma is Command) { + var cmd = ma as Command; + if (text.Split('.')[1] == cmd.name) + { - var attr = method.GetCustomAttribute(); + var attr = method.GetCustomAttribute(); - if (attr != null) - { - string newcommand = attr.newcommand; - if (attr.warn) + if (attr != null) { - Console.WriteLine(Localization.Parse((newcommand == "" ? "{ERROR}" : "{WARN}") + attr.reason, new Dictionary() { + string newcommand = attr.newcommand; + if (attr.warn) + { + Console.WriteLine(Localization.Parse((newcommand == "" ? "{ERROR}" : "{WARN}") + attr.reason, new Dictionary() { {"%newcommand", newcommand} })); - } - if (newcommand != "") - { - // redo the entire process running newcommand + } + if (newcommand != "") + { + // redo the entire process running newcommand - return RunClient(newcommand, args); + return RunClient(newcommand, args); + } } - } - var requiresArgs = method.GetCustomAttributes(); + var requiresArgs = method.GetCustomAttributes(); - bool error = false; - bool providedusage = false; + bool error = false; + bool providedusage = false; - foreach (RequiresArgument argument in requiresArgs) - { - if (!args.ContainsKey(argument.argument)) + foreach (RequiresArgument argument in requiresArgs) { - - if (!providedusage) + if (!args.ContainsKey(argument.argument)) { - string usageparse = "{COMMAND_" + ns.name.ToUpper() + "_" + cmd.name.ToUpper() + "_USAGE}"; - if (usageparse == Localization.Parse(usageparse)) - usageparse = ""; - else - usageparse = Shiftorium.UpgradeInstalled("help_usage") ? Localization.Parse("{ERROR}{USAGE}" + usageparse, new Dictionary() { + + if (!providedusage) + { + string usageparse = "{COMMAND_" + ns.name.ToUpper() + "_" + cmd.name.ToUpper() + "_USAGE}"; + if (usageparse == Localization.Parse(usageparse)) + usageparse = ""; + else + usageparse = Shiftorium.UpgradeInstalled("help_usage") ? Localization.Parse("{ERROR}{USAGE}" + usageparse, new Dictionary() { {"%ns", ns.name}, {"%cmd", cmd.name} }) : ""; - Console.WriteLine(usageparse); + Console.WriteLine(usageparse); - providedusage = true; - } + providedusage = true; + } - if (Shiftorium.UpgradeInstalled("help_usage")) - { - Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED}", new Dictionary() { + if (Shiftorium.UpgradeInstalled("help_usage")) + { + Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED}", new Dictionary() { {"%argument", argument.argument} })); - } - else - { - Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED_NO_USAGE}")); - } + } + else + { + Console.WriteLine(Localization.Parse("{ERROR_ARGUMENT_REQUIRED_NO_USAGE}")); + } - error = true; + error = true; + } } - } - if (error) - { - throw new Exception("{ERROR_COMMAND_WRONG}"); - } + if (error) + { + throw new Exception("{ERROR_COMMAND_WRONG}"); + } - try - { - return (bool)method.Invoke(null, new[] { args }); - } - catch (TargetInvocationException e) - { - Console.WriteLine(Localization.Parse("{ERROR_EXCEPTION_THROWN_IN_METHOD}")); - Console.WriteLine(e.InnerException.Message); - Console.WriteLine(e.InnerException.StackTrace); - return true; - } - catch - { - return (bool)method.Invoke(null, new object[] { }); + try + { + return (bool)method.Invoke(null, new[] { args }); + } + catch (TargetInvocationException e) + { + Console.WriteLine(Localization.Parse("{ERROR_EXCEPTION_THROWN_IN_METHOD}")); + Console.WriteLine(e.InnerException.Message); + Console.WriteLine(e.InnerException.StackTrace); + return true; + } + catch + { + return (bool)method.Invoke(null, new object[] { }); + } } } } } + else + { + Console.WriteLine(text + " cannot be ran in a remote session"); + return true; + } } } } @@ -256,7 +278,7 @@ namespace ShiftOS.Engine IsForwardingConsoleWrites = true; if (TerminalBackend.InStory == false) { - TerminalBackend.InvokeCommand(text3); + TerminalBackend.InvokeCommand(text3, true); } if (TerminalBackend.PrefixEnabled) { -- cgit v1.2.3