Remotely/Server/API/ScriptingController.cs
Jared Goodwin 3ef4cdf81a
Extract remote control functionality into separate library. (#539)
* Convert server to new single-file startup model.

* Add remote control implementations.

* Implement IViewerAuthorizer.

* Update hub endpoints.

* Implement HubEventHandler.

* Implement ViewerHubDataProvider.

* Implement page data provider.

* Implement RCL and refactor.

* Update submodule.

* Replace submodule with NuGet.

* Update copy URL.

* Update NuGet.

* Remove deprecated WebRTC.

* Remove deprecated WebRTC.

* Update Immense.RemoteControl

* Building out desktop projects.

* Bring more services into submodule.

* Update submodule.

* Update submodule.

* Refactoring for module.

* Update submodule.

* Update submodule

* Got Windows desktop app running.

* Refactor for submodule changes.

* FIx unattended session start.

* Switch desktop app out of console mode.

* Fix tests.

* Update publishing.

* Remove ClickOnce middleware.

* Remove ClickOnce remnants.

* Update submodule

* Add some logging.

* Update Linux path.

* Update submodule.

* Add cleanup service for unattended sessions that failed to start.

* Update submodule.

* Fix chat.

* Add ValidateExecutableReferencesMatchSelfContained property.

* Add other submodule projects.  Align checkbox.

* Update submodule.  Reduce deserialization in the browser, resulting in faster renders.

* Update submodule.

* Update submodule.

* Update submodule.

* Update submodule.

* Add orgId back for branding.

* Get branding loading in desktop apps.

* Update submodule.

* Create log dir.

* Refactor version check on config page.

* Update submodule.

* Update submodule.

* Change submodule URL.

* Correct namespace.

* Update submodule.

* Checkout submodules recursively.
2022-12-23 06:39:12 -08:00

108 lines
3.8 KiB
C#

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Remotely.Server.Hubs;
using Remotely.Server.Services;
using Remotely.Shared.Utilities;
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Remotely.Shared.Enums;
using Remotely.Server.Auth;
using Immense.RemoteControl.Server.Abstractions;
using Immense.RemoteControl.Shared.Helpers;
using Remotely.Shared;
namespace Remotely.Server.API
{
[ApiController]
[Route("api/[controller]")]
public class ScriptingController : ControllerBase
{
private readonly IHubContext<ServiceHub> _agentHubContext;
private readonly IDataService _dataService;
private readonly IServiceHubSessionCache _serviceSessionCache;
private readonly IExpiringTokenService _expiringTokenService;
private readonly UserManager<RemotelyUser> _userManager;
public ScriptingController(UserManager<RemotelyUser> userManager,
IDataService dataService,
IServiceHubSessionCache serviceSessionCache,
IExpiringTokenService expiringTokenService,
IHubContext<ServiceHub> agentHub)
{
_dataService = dataService;
_serviceSessionCache = serviceSessionCache;
_expiringTokenService = expiringTokenService;
_userManager = userManager;
_agentHubContext = agentHub;
}
[ServiceFilter(typeof(ApiAuthorizationFilter))]
[HttpPost("[action]/{mode}/{deviceID}")]
public async Task<ActionResult<ScriptResult>> ExecuteCommand(string mode, string deviceID)
{
if (!Enum.TryParse<ScriptingShell>(mode, true, out var shell))
{
return BadRequest("Unable to parse shell type. Use either PSCore, WinPS, Bash, or CMD.");
}
var command = string.Empty;
using (var sr = new StreamReader(Request.Body))
{
command = await sr.ReadToEndAsync();
}
var userID = string.Empty;
if (Request.HttpContext.User.Identity.IsAuthenticated)
{
var username = Request.HttpContext.User.Identity.Name;
var user = await _userManager.FindByNameAsync(username);
userID = user.Id;
if (!_dataService.DoesUserHaveAccessToDevice(deviceID, user))
{
return Unauthorized();
}
}
Request.Headers.TryGetValue("OrganizationID", out var orgID);
if (!_serviceSessionCache.TryGetByDeviceId(deviceID, out var device))
{
return NotFound();
}
if (!_serviceSessionCache.TryGetConnectionId(deviceID, out var connectionId))
{
return NotFound();
}
if (device.OrganizationID != orgID)
{
return Unauthorized();
}
var requestID = Guid.NewGuid().ToString();
var authToken = _expiringTokenService.GetToken(Time.Now.AddMinutes(AppConstants.ScriptRunExpirationMinutes));
await _agentHubContext.Clients.Client(connectionId).SendAsync("ExecuteCommandFromApi", shell, authToken, requestID, command, User?.Identity?.Name);
var success = await WaitHelper.WaitForAsync(() => ServiceHub.ApiScriptResults.TryGetValue(requestID, out _), TimeSpan.FromSeconds(30));
if (!success)
{
return NotFound();
}
ServiceHub.ApiScriptResults.TryGetValue(requestID, out var commandID);
ServiceHub.ApiScriptResults.Remove(requestID);
var result = _dataService.GetScriptResult(commandID.ToString(), orgID);
return result;
}
}
}