From 02480d8915b478bcbc982d84d64cdb2dbb05f2e4 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Sat, 1 Feb 2020 13:03:12 -0800 Subject: [PATCH] Create ScriptingController. --- Agent/Services/CommandExecutor.cs | 47 +++++++++++++++++++++++++++++- Server/API/ScriptingController.cs | 13 +++++++++ Server/Services/DeviceSocketHub.cs | 9 +++++- 3 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 Server/API/ScriptingController.cs diff --git a/Agent/Services/CommandExecutor.cs b/Agent/Services/CommandExecutor.cs index 015ef119..d743c527 100644 --- a/Agent/Services/CommandExecutor.cs +++ b/Agent/Services/CommandExecutor.cs @@ -40,7 +40,6 @@ namespace Remotely.Agent.Services } break; } - case "winps": if (OSUtils.IsWindows) { @@ -81,6 +80,7 @@ namespace Remotely.Agent.Services if (Encoding.UTF8.GetBytes(serializedResult).Length > 400000) { await SendResultsViaAjax("Bash", result); + await hubConnection.InvokeAsync("BashResultViaAjax", commandID); } else { @@ -98,6 +98,51 @@ namespace Remotely.Agent.Services await hubConnection.InvokeAsync("DisplayMessage", "There was an error executing the command. It has been logged on the client device.", "Error executing command.", senderConnectionID); } } + + public async Task ExecuteCommandFromApi(string mode, string requestID, string command, string commandID, string senderConnectionID, HubConnection hubConnection) + { + try + { + switch (mode.ToLower()) + { + case "pscore": + var psCoreResult = PSCore.GetCurrent(senderConnectionID).WriteInput(command, commandID); + await SendResultsViaAjax("PSCore", psCoreResult); + break; + + case "winps": + if (OSUtils.IsWindows) + { + var result = WindowsPS.GetCurrent(senderConnectionID).WriteInput(command, commandID); + await SendResultsViaAjax("WinPS", result); + } + break; + case "cmd": + if (OSUtils.IsWindows) + { + var result = CMD.GetCurrent(senderConnectionID).WriteInput(command, commandID); + await SendResultsViaAjax("CMD", result); + } + break; + case "bash": + if (OSUtils.IsLinux) + { + var result = Bash.GetCurrent(senderConnectionID).WriteInput(command, commandID); + await SendResultsViaAjax("Bash", result); + } + break; + default: + break; + } + + await hubConnection.InvokeAsync("CommandResultViaApi", commandID, requestID); + } + catch (Exception ex) + { + Logger.Write(ex); + await hubConnection.InvokeAsync("DisplayMessage", "There was an error executing the command. It has been logged on the client device.", "Error executing command.", senderConnectionID); + } + } private async Task SendResultsViaAjax(string resultType, object result) { var targetURL = ConfigService.GetConnectionInfo().Host + $"/API/Commands/{resultType}"; diff --git a/Server/API/ScriptingController.cs b/Server/API/ScriptingController.cs new file mode 100644 index 00000000..117bcc3e --- /dev/null +++ b/Server/API/ScriptingController.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Remotely.Server.API +{ + [ApiController] + public class ScriptingController : ControllerBase + { + } +} diff --git a/Server/Services/DeviceSocketHub.cs b/Server/Services/DeviceSocketHub.cs index 648f380b..1a777f33 100644 --- a/Server/Services/DeviceSocketHub.cs +++ b/Server/Services/DeviceSocketHub.cs @@ -24,6 +24,7 @@ namespace Remotely.Server.Services } public static ConcurrentDictionary ServiceConnections { get; } = new ConcurrentDictionary(); + public static ConcurrentDictionary ApiScriptResults { get; } = new ConcurrentDictionary(); public IHubContext RCBrowserHub { get; } private IHubContext BrowserHub { get; } private DataService DataService { get; } @@ -44,6 +45,7 @@ namespace Remotely.Server.Services var commandContext = DataService.GetCommandContext(commandID); return BrowserHub.Clients.Client(commandContext.SenderConnectionID).SendAsync("BashResultViaAjax", commandID, Device.ID); } + public Task Chat(string message, string senderConnectionID) { return BrowserHub.Clients.Client(senderConnectionID).SendAsync("Chat", Device.DeviceName, message); @@ -55,7 +57,7 @@ namespace Remotely.Server.Services return BrowserHub.Clients.Client(commandContext.SenderConnectionID).SendAsync("CMDResultViaAjax", commandID, Device.ID); } - public Task CommandResult(GenericCommandResult result) + public Task CommandResult(GenericCommandResult result) { result.DeviceID = Device.ID; var commandContext = DataService.GetCommandContext(result.CommandContextID); @@ -64,6 +66,11 @@ namespace Remotely.Server.Services return BrowserHub.Clients.Client(commandContext.SenderConnectionID).SendAsync("CommandResult", result); } + public void CommandResultViaApi(string commandID, string requestID) + { + ApiScriptResults.AddOrUpdate(requestID, commandID, (k, v) => commandID); + } + public Task DeviceCameOnline(Device device) { try