From a4fd12c59755fa261ade692d69bd41ddf87b6d87 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Sun, 2 Feb 2020 09:03:36 -0800 Subject: [PATCH] Create scripting controller. --- Agent/Services/DeviceSocket.cs | 11 +++++++++++ Server/API/ScriptingController.cs | 23 +++++++++++++++++++++++ Server/Services/BrowserSocketHub.cs | 3 +-- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Agent/Services/DeviceSocket.cs b/Agent/Services/DeviceSocket.cs index aa16824b..8ef5955d 100644 --- a/Agent/Services/DeviceSocket.cs +++ b/Agent/Services/DeviceSocket.cs @@ -114,6 +114,17 @@ namespace Remotely.Agent.Services await CommandExecutor.ExecuteCommand(mode, command, commandID, senderConnectionID, HubConnection); })); + HubConnection.On("ExecuteCommandFromApi", (async (string mode, string requesterID, string command, string commandID, string senderConnectionID) => + { + if (!IsServerVerified) + { + Logger.Write($"Command attempted before server was verified. Mode: {mode}. Command: {command}. Sender: {senderConnectionID}"); + Uninstaller.UninstallAgent(); + return; + } + + await CommandExecutor.ExecuteCommandFromApi(mode, requesterID, command, commandID, senderConnectionID, HubConnection); + })); HubConnection.On("TransferFiles", async (string transferID, List fileIDs, string requesterID) => { Logger.Write($"File transfer started by {requesterID}."); diff --git a/Server/API/ScriptingController.cs b/Server/API/ScriptingController.cs index 117bcc3e..be9410fa 100644 --- a/Server/API/ScriptingController.cs +++ b/Server/API/ScriptingController.cs @@ -9,5 +9,28 @@ namespace Remotely.Server.API [ApiController] public class ScriptingController : ControllerBase { + //public Task ExecuteCommand(string mode, string command, string[] deviceIDs) + //{ + // deviceIDs = DataService.FilterDeviceIDsByUserPermission(deviceIDs, RemotelyUser); + // var connections = GetActiveClientConnections(deviceIDs); + + // var commandContext = new CommandContext() + // { + // CommandMode = mode, + // CommandText = command, + // SenderConnectionID = Context.ConnectionId, + // SenderUserID = Context.UserIdentifier, + // TargetDeviceIDs = connections.Select(x => x.Value.ID).ToArray(), + // OrganizationID = RemotelyUser.OrganizationID + // }; + // DataService.AddOrUpdateCommandContext(commandContext); + // Clients.Caller.SendAsync("CommandContextCreated", commandContext); + // foreach (var connection in connections) + // { + // DeviceHub.Clients.Client(connection.Key).SendAsync("ExecuteCommand", mode, command, commandContext.ID, Context.ConnectionId); + // } + + // return Task.CompletedTask; + //} } } diff --git a/Server/Services/BrowserSocketHub.cs b/Server/Services/BrowserSocketHub.cs index fb8f2bf1..90cdaec3 100644 --- a/Server/Services/BrowserSocketHub.cs +++ b/Server/Services/BrowserSocketHub.cs @@ -102,8 +102,7 @@ namespace Remotely.Server.Services return Task.CompletedTask; } - - public override async Task OnConnectedAsync() + public override async Task OnConnectedAsync() { RemotelyUser = DataService.GetUserByID(Context.UserIdentifier); if (await IsConnectionValid() == false)