Skip to content

Commit f21c548

Browse files
Jared Goodwinbitbound
authored andcommitted
Merged PR 12: Websockets
1 parent 2bc8d73 commit f21c548

File tree

287 files changed

+4824
-17903
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

287 files changed

+4824
-17903
lines changed

.build/Build.ps1

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,11 @@ if ($BuildViewer) {
124124

125125

126126
if ($BuildStreamer) {
127-
dotnet publish --configuration Release -p:PublishProfile=Artifacts -p:Version=$CurrentVersion -p:FileVersion=$CurrentVersion -p:IncludeAllContentForSelfExtract=true -p:EnableCompressionInSingleFile=true -p:IncludeAppSettingsInSingleFile=true "$Root\ControlR.Streamer.Sidecar\"
128-
&"$SignToolPath" sign /fd SHA256 /f "$CertificatePath" /p $CertificatePassword /t http://timestamp.digicert.com "$Root\ControlR.Streamer\.artifacts\ControlR.Streamer.Sidecar.exe"
127+
dotnet publish --configuration Release -p:PublishProfile=win-x86 -p:Version=$CurrentVersion -p:FileVersion=$CurrentVersion "$Root\ControlR.Streamer\"
128+
&"$SignToolPath" sign /fd SHA256 /f "$CertificatePath" /p $CertificatePassword /t http://timestamp.digicert.com "$Root\ControlR.Streamer\bin\publish\ControlR.Streamer.exe"
129129
Check-LastExitCode
130130

131-
[string]$PackageJson = Get-Content -Path "$Root\ControlR.Streamer\package.json"
132-
$Package = $PackageJson | ConvertFrom-Json
133-
$Package.version = $CurrentVersion.Split(".", [System.StringSplitOptions]::RemoveEmptyEntries) | Select-Object -First 3 | Join-String -Separator "."
134-
[string]$PackageJson = $Package | ConvertTo-Json
135-
[System.IO.File]::WriteAllText("$Root\ControlR.Streamer\package.json", $PackageJson)
136-
Push-Location "$Root\ControlR.Streamer"
137-
npm install
138-
npm run make-pwsh
139-
Pop-Location
140-
131+
Compress-Archive -Path "$Root\ControlR.Streamer\bin\publish\*" -DestinationPath "$DownloadsFolder\win-x86\ControlR.Streamer.zip" -Force
141132
}
142133

143134
dotnet publish -p:ExcludeApp_Data=true --runtime linux-x64 --configuration Release --output $OutputPath --self-contained true "$Root\ControlR.Server\"

ControlR.Agent/ControlR.Agent.csproj

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
<ApplicationIcon>appicon.ico</ApplicationIcon>
99
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
1010
<Authors>jaredg</Authors>
11-
<Configurations>Debug;Release;DebugStore</Configurations>
11+
<Configurations>Debug;Release</Configurations>
1212
</PropertyGroup>
1313

14-
<ItemGroup>
15-
<Compile Remove="Resources\**" />
16-
<EmbeddedResource Remove="Resources\**" />
17-
<None Remove="Resources\**" />
18-
</ItemGroup>
19-
2014
<ItemGroup>
2115
<None Remove="appsettings.Development.json" />
2216
<None Remove="appsettings.json" />
@@ -34,7 +28,7 @@
3428

3529
<ItemGroup>
3630
<PackageReference Include="Bitbound.SimpleIpc" Version="2.0.0" />
37-
<PackageReference Include="Bitbound.SimpleMessenger" Version="2.1.0" />
31+
<PackageReference Include="Bitbound.SimpleMessenger" Version="2.2.1" />
3832
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.6" />
3933
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
4034
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0" />

ControlR.Agent/Interfaces/IAgentInstaller.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ public interface IAgentInstaller
44
{
55
Task Install(
66
Uri? serverUri = null,
7-
string? authorizedPublicKey = null);
7+
string? authorizedPublicKey = null,
8+
string? label = null);
89

910
Task Uninstall();
1011
}

ControlR.Agent/Interfaces/IDeviceDataGenerator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
using ControlR.Libraries.Shared.Models;
1+
using ControlR.Libraries.Shared.Dtos;
2+
using ControlR.Libraries.Shared.Models;
23

34
namespace ControlR.Agent.Interfaces;
45

56
public interface IDeviceDataGenerator
67
{
7-
Task<Device> CreateDevice(double cpuUtilization, IEnumerable<string> authorizedKeys, string deviceId);
8+
Task<Device> CreateDevice(double cpuUtilization, IEnumerable<AuthorizedKeyDto> authorizedKeys, string deviceId);
89

910
string GetAgentVersion();
1011

1112
List<Drive> GetAllDrives();
1213

13-
Device GetDeviceBase(IEnumerable<string> authorizedKeys, string deviceId);
14+
Device GetDeviceBase(IEnumerable<AuthorizedKeyDto> authorizedKeys, string deviceId);
1415

1516
Task<(double usedGB, double totalGB)> GetMemoryInGB();
1617

ControlR.Agent/Interfaces/IStreamerLauncher.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ Task<Result> CreateSession(
99
byte[] authorizedKey,
1010
int targetWindowsSession = -1,
1111
bool notifyUserOnSessionStart = false,
12-
bool lowerUacDuringSession = false,
1312
string? viewerName = null);
1413
}

ControlR.Agent/Interfaces/IStreamerUpdater.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ControlR.Libraries.Shared.Dtos;
1+
using ControlR.Libraries.Shared.Dtos.StreamerDtos;
22
using Microsoft.Extensions.Hosting;
33

44
namespace ControlR.Agent.Interfaces;

ControlR.Agent/Models/StreamingSession.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
using System.Diagnostics;
33

44
namespace ControlR.Agent.Models;
5-
internal class StreamingSession(string _viewerConnectionId, bool _lowerUacDuringSession) : IDisposable
5+
internal class StreamingSession(string _viewerConnectionId) : IDisposable
66
{
7-
public bool LowerUacDuringSession { get; } = _lowerUacDuringSession;
87
public string ViewerConnectionId { get; } = _viewerConnectionId;
98
public Process? StreamerProcess { get; set; }
109
public void Dispose()

ControlR.Agent/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using ControlR.Agent.Startup;
2-
using Microsoft.AspNetCore.SignalR.Client;
32
using System.CommandLine;
43
using System.CommandLine.Parsing;
54

ControlR.Agent/Services/AgentHubConnection.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using MessagePack;
44
using Microsoft.AspNetCore.Http.Connections.Client;
55
using Microsoft.AspNetCore.SignalR.Client;
6-
using Microsoft.Extensions.DependencyInjection;
76
using Microsoft.Extensions.Hosting;
87
using Microsoft.Extensions.Logging;
98
using Microsoft.Extensions.Options;
@@ -21,6 +20,7 @@
2120
using ControlR.Libraries.Shared.Services;
2221
using ControlR.Libraries.Shared.Models;
2322
using ControlR.Libraries.Shared.Primitives;
23+
using ControlR.Libraries.Shared.Dtos.StreamerDtos;
2424

2525
namespace ControlR.Agent.Services;
2626

@@ -33,7 +33,7 @@ internal interface IAgentHubConnection : IHubConnectionBase, IHostedService
3333

3434
internal class AgentHubConnection(
3535
IHostApplicationLifetime _appLifetime,
36-
IServiceScopeFactory _scopeFactory,
36+
IServiceProvider _services,
3737
IDeviceDataGenerator _deviceCreator,
3838
IEnvironmentHelper _environmentHelper,
3939
ISettingsProvider _settings,
@@ -46,9 +46,9 @@ internal class AgentHubConnection(
4646
ITerminalStore _terminalStore,
4747
IDelayer _delayer,
4848
IWin32Interop _win32Interop,
49-
IOptionsMonitor<AgentAppOptions> _agentOptions,
49+
IOptionsMonitor<AgentAppOptions> _appOptions,
5050
ILogger<AgentHubConnection> _logger)
51-
: HubConnectionBase(_scopeFactory, _messenger, _delayer, _logger), IAgentHubConnection, IAgentHubClient
51+
: HubConnectionBase(_services, _messenger, _delayer, _logger), IAgentHubConnection, IAgentHubClient
5252
{
5353
public async Task<bool> CreateStreamingSession(SignedPayloadDto signedDto)
5454
{
@@ -76,7 +76,6 @@ public async Task<bool> CreateStreamingSession(SignedPayloadDto signedDto)
7676
signedDto.PublicKey,
7777
dto.TargetSystemSession,
7878
dto.NotifyUserOnSessionStart,
79-
dto.LowerUacDuringSession,
8079
dto.ViewerName)
8180
.ConfigureAwait(false);
8281

@@ -121,7 +120,7 @@ public Task<Result<AgentAppSettings>> GetAgentAppSettings(SignedPayloadDto signe
121120
return Result.Fail<AgentAppSettings>("Signature verification failed.").AsTaskResult();
122121
}
123122

124-
var agentOptions = _agentOptions.CurrentValue;
123+
var agentOptions = _appOptions.CurrentValue;
125124
var settings = new AgentAppSettings()
126125
{
127126
AppOptions = agentOptions
@@ -208,15 +207,15 @@ public async Task SendDeviceHeartbeat()
208207
return;
209208
}
210209

211-
if (_settings.AuthorizedKeys.Count == 0)
210+
if (_settings.AuthorizedKeys2.Count == 0)
212211
{
213212
_logger.LogWarning("There are no authorized keys in appsettings. Aborting heartbeat.");
214213
return;
215214
}
216215

217216
var device = await _deviceCreator.CreateDevice(
218217
_cpuSampler.CurrentUtilization,
219-
_settings.AuthorizedKeys,
218+
_settings.AuthorizedKeys2,
220219
_settings.DeviceId);
221220

222221
var result = device.TryCloneAs<Device, DeviceDto>();
@@ -260,6 +259,7 @@ await Connect(
260259
() => $"{_settings.ServerUri}hubs/agent",
261260
ConfigureConnection,
262261
ConfigureHttpOptions,
262+
useReconnect: true,
263263
_appLifetime.ApplicationStopping);
264264

265265
await SendDeviceHeartbeat();
@@ -314,7 +314,7 @@ private bool VerifySignedDto(SignedPayloadDto signedDto)
314314
return false;
315315
}
316316

317-
if (!_settings.AuthorizedKeys.Contains(signedDto.PublicKeyBase64))
317+
if (!_settings.AuthorizedKeys2.Any(x => x.PublicKey == signedDto.PublicKeyBase64))
318318
{
319319
_logger.LogCritical("Public key does not exist in authorized keys list: {key}", signedDto.PublicKey);
320320
return false;

ControlR.Agent/Services/Base/AgentInstallerBase.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.Logging;
77
using Microsoft.Extensions.Options;
88
using System.Text.Json;
9+
using ControlR.Libraries.Shared.Dtos;
910

1011
namespace ControlR.Agent.Services.Base;
1112

@@ -17,7 +18,7 @@ internal abstract class AgentInstallerBase(
1718
{
1819
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
1920

20-
protected async Task UpdateAppSettings(Uri? serverUri, string? authorizedKey)
21+
protected async Task UpdateAppSettings(Uri? serverUri, string? authorizedKey, string? label)
2122
{
2223
using var _ = _logger.BeginMemberScope();
2324

@@ -31,22 +32,32 @@ protected async Task UpdateAppSettings(Uri? serverUri, string? authorizedKey)
3132
_logger.LogInformation("Setting server URI to {ServerUri}.", updatedServerUri);
3233
appOptions.ServerUri = updatedServerUri;
3334

35+
var authorizedKeys = appOptions.AuthorizedKeys2 ?? [];
3436

35-
if (!string.IsNullOrWhiteSpace(authorizedKey) &&
36-
!appOptions.AuthorizedKeys.Contains(authorizedKey))
37+
var obsoleteKeys = appOptions.AuthorizedKeys
38+
.ExceptBy(authorizedKeys.Select(x => x.PublicKey), x => x)
39+
.Select(x => new AuthorizedKeyDto("", x));
40+
41+
authorizedKeys.AddRange(obsoleteKeys);
42+
43+
_logger.LogInformation("Updating authorized keys. Initial count: {KeyCount}", authorizedKeys.Count);
44+
45+
if (!string.IsNullOrWhiteSpace(authorizedKey))
3746
{
38-
_logger.LogInformation("Adding key passed in from arguments.");
39-
appOptions.AuthorizedKeys.Add(authorizedKey);
40-
_logger.LogInformation("Key Count: {num}", appOptions.AuthorizedKeys.Count);
47+
var currentKeyIndex = authorizedKeys.FindIndex(x => x.PublicKey == authorizedKey);
48+
if (currentKeyIndex == -1)
49+
{
50+
authorizedKeys.Add(new AuthorizedKeyDto(label ?? "", authorizedKey));
51+
}
52+
else
53+
{
54+
var currentKey = authorizedKeys[currentKeyIndex];
55+
var newLabel = currentKey.Label ?? label ?? "";
56+
authorizedKeys[currentKeyIndex] = currentKey with { Label = newLabel };
57+
}
4158
}
4259

43-
_logger.LogInformation("Removing duplicates.");
44-
appOptions.AuthorizedKeys.RemoveDuplicates();
45-
_logger.LogInformation("Key Count: {num}", appOptions.AuthorizedKeys.Count);
46-
47-
_logger.LogInformation("Removing empties.");
48-
appOptions.AuthorizedKeys.RemoveAll(string.IsNullOrWhiteSpace);
49-
_logger.LogInformation("Key Count: {num}", appOptions.AuthorizedKeys.Count);
60+
appOptions.AuthorizedKeys2 = authorizedKeys;
5061

5162
if (string.IsNullOrWhiteSpace(appOptions.DeviceId))
5263
{

0 commit comments

Comments
 (0)