Skip to content

Commit

Permalink
Merge branch 'master' into jaredg-respect-httplogging-setting
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed May 22, 2023
2 parents 1a79650 + 6297175 commit 5099191
Show file tree
Hide file tree
Showing 42 changed files with 248 additions and 604 deletions.
13 changes: 12 additions & 1 deletion Agent/Services/CpuUtilizationSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public interface ICpuUtilizationSampler : IHostedService

internal class CpuUtilizationSampler : BackgroundService, ICpuUtilizationSampler
{
private readonly HashSet<int> _ignoredProcesses = new();
private readonly ILogger<CpuUtilizationSampler> _logger;
private double _currentUtilization;

Expand Down Expand Up @@ -47,7 +48,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
}

private static async Task<double> GetCpuUtilization(CancellationToken cancelToken)
private async Task<double> GetCpuUtilization(CancellationToken cancelToken)
{
double totalUtilization = 0;
var utilizations = new Dictionary<int, Tuple<DateTimeOffset, TimeSpan>>();
Expand All @@ -60,14 +61,24 @@ private static async Task<double> GetCpuUtilization(CancellationToken cancelToke
return 0;
}


try
{
// Processes in other sessions (e.g. session 0) will be inaccessible
// when debugging in a non-privileged process. This prevents errors
// from clogging up the output window.
if (_ignoredProcesses.Contains(proc.Id))
{
continue;
}

var startTime = DateTimeOffset.Now;
var startCpuUsage = proc.TotalProcessorTime;
utilizations.Add(proc.Id, new Tuple<DateTimeOffset, TimeSpan>(startTime, startCpuUsage));
}
catch
{
_ignoredProcesses.Add(proc.Id);
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Desktop.Linux/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Desktop.Linux": {
"commandName": "Project",
"commandLineArgs": "-m Attended -s some-session-id -a vERyLonGAndCOMpleXKeY -o Immense -r Han"
"commandLineArgs": "-m Attended -h https://localhost:5001"
}
}
}
2 changes: 1 addition & 1 deletion Desktop.Win/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"Desktop.Win": {
"commandName": "Project",
"commandLineArgs": "-m Attended -s some-session-id -a vERyLonGAndCOMpleXKeY -o Immense -r Han"
"commandLineArgs": "-m Attended -h https://localhost:5001"
}
}
}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ To avoid injection attacks, ASP.NET Core defaults to only accepting forwarded he
- Data for Remotely will be saved in `/var/www/remotely/` within two files: appsettings.json and Remotely.db.
- These files will persist through teardown and setup of new Remotely containers.
- If upgrading from a non-Docker version of Remotely, overwrite these files with the ones from your previous installation.
- In that case, please note that you may need to change _SQLite_ parameter in your non-Docker appsettings.json. You may have something like:
```
"SQLite": "DataSource=Remotely.db",
```
but this should be changed to reflect the new Remotely.db location (relative to the container):
```
"SQLite": "DataSource=/remotely-data/Remotely.db",
```
- Use Caddy as a reverse proxy if you want to expose the site to the internet.
- If this is the first run, create your account by clicking the `Register` button on the main page.
- This account will be both the server admin and organization admin.
Expand Down
5 changes: 4 additions & 1 deletion Remotely.sln
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Immense.RemoteControl.Share
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Immense.RemoteControl.Desktop", "submodules\Immense.RemoteControl\Immense.RemoteControl.Desktop\Immense.RemoteControl.Desktop.csproj", "{034984DC-4B47-42E9-8648-F258C40C7926}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Immense.RemoteControl.Desktop.Native", "submodules\Immense.RemoteControl\Immense.RemoteControl.Desktop.Native\Immense.RemoteControl.Desktop.Native.csproj", "{7A8378BC-8169-4A33-93C6-47C699C5E74D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Immense.RemoteControl.Desktop.Native", "submodules\Immense.RemoteControl\Immense.RemoteControl.Desktop.Native\Immense.RemoteControl.Desktop.Native.csproj", "{7A8378BC-8169-4A33-93C6-47C699C5E74D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{E4D83C37-8B98-44FB-898B-9AA1BB223C66}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -305,6 +307,7 @@ Global
{FEF0D431-EB2F-4C08-A125-8DF59AFDA525} = {48C738FB-359E-43DB-B338-FD7CB1CCF6A8}
{034984DC-4B47-42E9-8648-F258C40C7926} = {48C738FB-359E-43DB-B338-FD7CB1CCF6A8}
{7A8378BC-8169-4A33-93C6-47C699C5E74D} = {48C738FB-359E-43DB-B338-FD7CB1CCF6A8}
{E4D83C37-8B98-44FB-898B-9AA1BB223C66} = {48C738FB-359E-43DB-B338-FD7CB1CCF6A8}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EAE10B28-119B-437C-9E68-06F0EE3F968A}
Expand Down
11 changes: 11 additions & 0 deletions Remotely.sln.startup.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@
"ProfileName": "Agent"
}
}
},
"Server+Desktop": {
"Projects": {
"Server\\Server.csproj": {
"ProfileName": "Server"
},
"Desktop.Win": {
"ProfileName": "Desktop.Win"
}
}
}

}
}
3 changes: 2 additions & 1 deletion Server/API/RemoteControlController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ await _serviceHub.Clients.Client(serviceConnectionId).SendAsync("RemoteControl",
accessKey,
HttpContext.Connection.Id,
string.Empty,
orgName);
orgName,
orgID);

var waitResult = await session.WaitForSessionReady(TimeSpan.FromSeconds(30));
if (!waitResult)
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Scripts/RunScript.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<span class="align-top">Show only mine</span>
</div>
</div>
<TreeView DataSource="ParentPage.FilteredScriptNodes"
<TreeView DataSource="ParentPage.TreeNodes"
ItemTypeSelector="x => x.ItemType"
ItemHeaderSelector="x => x.Name"
ItemSelected="ScriptSelected"
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Scripts/SavedScripts.razor
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<span class="align-top">Show only mine</span>
</div>
</div>
<TreeView DataSource="ParentPage.FilteredScriptNodes"
<TreeView DataSource="ParentPage.TreeNodes"
ItemTypeSelector="x => x.ItemType"
ItemHeaderSelector="x => x.Name"
ItemSelected="ScriptSelected"
Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Scripts/ScriptSchedules.razor
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<input type="checkbox" @bind="ParentPage.ShowOnlyMyScripts" />
<span class="align-top">Show only mine</span>
</div>
<TreeView DataSource="ParentPage.FilteredScriptNodes"
<TreeView DataSource="ParentPage.TreeNodes"
ItemTypeSelector="x => x.ItemType"
ItemHeaderSelector="x => x.Name"
ItemSelected="ScriptSelected"
Expand Down
1 change: 1 addition & 0 deletions Server/Components/Scripts/ScriptTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ScriptTreeNode
public string Id { get; } = Guid.NewGuid().ToString();
public TreeItemType ItemType { get; set; }
public string Name { get; init; }
public ScriptTreeNode? ParentNode { get; set; }
public List<ScriptTreeNode> ChildItems { get; } = new();
public SavedScript Script { get; init; }
}
Expand Down
2 changes: 1 addition & 1 deletion Server/Data/AppDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class AppDb : IdentityDbContext
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.ConfigureWarnings(x => x.Ignore(RelationalEventId.MultipleCollectionIncludeWarning));
options.LogTo((message) => System.Diagnostics.Debug.Write(message));
//options.LogTo((message) => System.Diagnostics.Debug.Write(message));
}

protected override void OnModelCreating(ModelBuilder builder)
Expand Down
105 changes: 58 additions & 47 deletions Server/Pages/ScriptsPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@


@code {
private IEnumerable<ScriptTreeNode>? _filteredScriptNodes;
private readonly List<ScriptTreeNode> _treeNodes = new();
private IEnumerable<SavedScript> _allScripts = Enumerable.Empty<SavedScript>();

private bool _showOnlyMyScripts = true;

[Parameter]
Expand All @@ -43,34 +45,24 @@
get => _showOnlyMyScripts;
set
{
_filteredScriptNodes = null;
_showOnlyMyScripts = value;
_treeNodes.Clear();
}
}

public List<ScriptTreeNode> TreeNodes { get; } = new();

public IEnumerable<ScriptTreeNode> FilteredScriptNodes
public IEnumerable<ScriptTreeNode> TreeNodes
{
get
{
if (_filteredScriptNodes?.Any() == true)
if (_treeNodes?.Any() == true)
{
return _filteredScriptNodes;
return _treeNodes;
}

if (ShowOnlyMyScripts)
{
_filteredScriptNodes = TreeNodes.Where(x =>
x.Script.CreatorId == User.Id);
}
else
{
_filteredScriptNodes = TreeNodes.Where(x =>
x.Script.IsPublic || x.Script.CreatorId == User.Id);
}
RefreshTreeNodes();

return _filteredScriptNodes;
return _treeNodes;
}
}

Expand All @@ -85,31 +77,9 @@

public async Task RefreshScripts()
{
TreeNodes.Clear();
_filteredScriptNodes = null;

var allScripts = await DataService.GetSavedScriptsWithoutContent(User.Id, User.OrganizationID);

foreach (var script in allScripts)
{
var root = BuildFolderPath(script.FolderPath);
root.Add(new ScriptTreeNode()
{
Name = script.Name,
Script = script,
ItemType = TreeItemType.Item
});
}
_treeNodes.Clear();

TreeNodes.Sort((a, b) =>
{
if (a.ItemType != b.ItemType)
{
return Comparer.Default.Compare(a.ItemType, b.ItemType);
}

return Comparer.Default.Compare(a.Name, b.Name);
});
_allScripts = await DataService.GetSavedScriptsWithoutContent(User.Id, User.OrganizationID);
}

protected override async Task OnInitializedAsync()
Expand All @@ -119,13 +89,14 @@
}


private List<ScriptTreeNode> BuildFolderPath(string folderPath)
private void CreateTreeNode(SavedScript script)
{
var root = TreeNodes;
var root = _treeNodes;
ScriptTreeNode? targetParent = null;

if (!string.IsNullOrWhiteSpace(folderPath))
if (!string.IsNullOrWhiteSpace(script.FolderPath))
{
var paths = folderPath.Split("/", StringSplitOptions.RemoveEmptyEntries);
var paths = script.FolderPath.Split("/", StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i < paths.Length; i++)
{
var existingParent = root.Find(x => x.Name == paths[i]);
Expand All @@ -135,18 +106,58 @@
var newItem = new ScriptTreeNode()
{
Name = paths[i],
ItemType = TreeItemType.Folder
ItemType = TreeItemType.Folder,
ParentNode = existingParent
};
root.Add(newItem);
root = newItem.ChildItems;
targetParent = newItem;
}
else
{
root = existingParent.ChildItems;
targetParent = existingParent;
}
}
}

return root;
var scriptNode = new ScriptTreeNode()
{
Name = script.Name,
Script = script,
ItemType = TreeItemType.Item,
ParentNode = targetParent
};

root.Add(scriptNode);
}

private void RefreshTreeNodes()
{
_treeNodes.Clear();

foreach (var script in _allScripts)
{
var showScript = ShowOnlyMyScripts ?
script.CreatorId == User.Id :
script.CreatorId == User.Id || script.IsPublic;

if (!showScript)
{
continue;
}

CreateTreeNode(script);
}

_treeNodes.Sort((a, b) =>
{
if (a.ItemType != b.ItemType)
{
return Comparer.Default.Compare(a.ItemType, b.ItemType);
}

return Comparer.Default.Compare(a.Name, b.Name);
});
}
}
1 change: 1 addition & 0 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
services.AddSignalR(options =>
{
options.EnableDetailedErrors = builder.Environment.IsDevelopment();
options.MaximumParallelInvocationsPerClient = 5;
options.MaximumReceiveMessageSize = 100_000;
})
.AddJsonProtocol(options =>
Expand Down
13 changes: 7 additions & 6 deletions Server/Services/RcImplementations/ViewerAuthorizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Caching.Memory;
using Remotely.Shared.Utilities;
using System;
using System.Threading.Tasks;

namespace Remotely.Server.Services.RcImplementations
{
Expand All @@ -20,25 +21,25 @@ public ViewerAuthorizer(IApplicationConfig appConfig, IOtpProvider otpProvider)

public string UnauthorizedRedirectUrl { get; } = "/Identity/Account/Login";

public bool IsAuthorized(AuthorizationFilterContext context)
public Task<bool> IsAuthorized(AuthorizationFilterContext context)
{
if (!_appConfig.RemoteControlRequiresAuthentication)
{
return true;
return Task.FromResult(true);
}

if (context.HttpContext.User.Identity.IsAuthenticated)
if (context.HttpContext.User.Identity?.IsAuthenticated == true)
{
return true;
return Task.FromResult(true);
}

if (context.HttpContext.Request.Query.TryGetValue("otp", out var otp) &&
_otpProvider.Exists($"{otp}"))
{
return true;
return Task.FromResult(true);
}

return false;
return Task.FromResult(false);
}
}
}
Loading

0 comments on commit 5099191

Please sign in to comment.