Skip to content

Commit

Permalink
Change device id to GUID.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Sep 22, 2024
1 parent 29f2ac3 commit 8c93772
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 53 deletions.
4 changes: 2 additions & 2 deletions ControlR.Agent/Interfaces/IDeviceDataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

public interface IDeviceDataGenerator
{
Task<DeviceDto> CreateDevice(double cpuUtilization, string deviceId);
Task<DeviceDto> CreateDevice(double cpuUtilization, Guid deviceId);

string GetAgentVersion();

List<Drive> GetAllDrives();

DeviceDto GetDeviceBase(
string deviceId,
Guid deviceId,
string currentUser,
List<Drive> drives,
double usedStorage,
Expand Down
9 changes: 3 additions & 6 deletions ControlR.Agent/Services/AgentHubConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,9 @@ public Task<Result<AgentAppSettings>> GetAgentAppSettings()
[SupportedOSPlatform("windows6.0.6000")]
public Task<WindowsSession[]> GetWindowsSessions()
{
if (environmentHelper.Platform != SystemPlatform.Windows)
{
return Array.Empty<WindowsSession>().AsTaskResult();
}

return win32Interop.GetActiveSessions().ToArray().AsTaskResult();
return environmentHelper.Platform == SystemPlatform.Windows
? win32Interop.GetActiveSessions().ToArray().AsTaskResult()
: Array.Empty<WindowsSession>().AsTaskResult();
}

public Task<Result> ReceiveAgentAppSettings(AgentAppSettings appSettings)
Expand Down
4 changes: 2 additions & 2 deletions ControlR.Agent/Services/Base/AgentInstallerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ protected async Task UpdateAppSettings(Uri? serverUri, string? authorizedKey, st
logger.LogInformation("Setting server URI to {ServerUri}.", updatedServerUri);
currentOptions.ServerUri = updatedServerUri;

if (string.IsNullOrWhiteSpace(currentOptions.DeviceId))
if (currentOptions.DeviceId == Guid.Empty)
{
logger.LogInformation("DeviceId is empty. Generating new one.");
currentOptions.DeviceId = RandomGenerator.CreateDeviceToken();
currentOptions.DeviceId = Guid.NewGuid();
}

logger.LogInformation("Writing results to disk.");
Expand Down
2 changes: 1 addition & 1 deletion ControlR.Agent/Services/Base/DeviceDataGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public List<Drive> GetAllDrives()
}

public DeviceDto GetDeviceBase(
string deviceId,
Guid deviceId,
string currentUser,
List<Drive> drives,
double usedStorage,
Expand Down
2 changes: 1 addition & 1 deletion ControlR.Agent/Services/Linux/DeviceGeneratorLinux.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class DeviceDataGeneratorLinux(
private readonly ILogger<DeviceDataGeneratorLinux> _logger = logger;
private readonly IProcessManager _processInvoker = processInvoker;

public async Task<DeviceDto> CreateDevice(double cpuUtilization, string deviceId)
public async Task<DeviceDto> CreateDevice(double cpuUtilization, Guid deviceId)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion ControlR.Agent/Services/Mac/DeviceGeneratorMac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal class DeviceDataGeneratorMac(
private readonly ILogger<DeviceDataGeneratorMac> _logger = logger;
private readonly IProcessManager _processService = processInvoker;

public async Task<DeviceDto> CreateDevice(double cpuUtilization, string deviceId)
public async Task<DeviceDto> CreateDevice(double cpuUtilization, Guid deviceId)
{
try
{
Expand Down
4 changes: 2 additions & 2 deletions ControlR.Agent/Services/SettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ControlR.Agent.Services;

internal interface ISettingsProvider
{
string DeviceId { get; }
Guid DeviceId { get; }
bool IsConnectedToPublicServer { get; }
Uri ServerUri { get; }
string GetAppSettingsPath();
Expand All @@ -23,7 +23,7 @@ internal class SettingsProvider(
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
private readonly SemaphoreSlim _updateLock = new(1, 1);

public string DeviceId => appOptions.CurrentValue.DeviceId;
public Guid DeviceId => appOptions.CurrentValue.DeviceId;

public Uri ServerUri =>
appOptions.CurrentValue.ServerUri ??
Expand Down
2 changes: 1 addition & 1 deletion ControlR.Agent/Services/Windows/DeviceDataGeneratorWin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class DeviceDataGeneratorWin(
{
private readonly ILogger<DeviceDataGeneratorWin> _logger = logger;

public async Task<DeviceDto> CreateDevice(double cpuUtilization, string deviceId)
public async Task<DeviceDto> CreateDevice(double cpuUtilization, Guid deviceId)
{
try
{
Expand Down
6 changes: 3 additions & 3 deletions ControlR.Viewer/Services/DevicesCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public interface IDeviceCache

Task SetAllOffline();

bool TryGet(string deviceId, [NotNullWhen(true)] out DeviceDto? device);
bool TryGet(Guid deviceId, [NotNullWhen(true)] out DeviceDto? device);
}

internal class DeviceCache(IFileSystem fileSystem, IFileIo fileIo, ILogger<DeviceCache> logger)
: IDeviceCache
{
private static readonly ConcurrentDictionary<string, DeviceDto> _cache = new();
private static readonly ConcurrentDictionary<Guid, DeviceDto> _cache = new();
private static readonly SemaphoreSlim _fileLock = new(1, 1);
private readonly string _deviceCachePath = Path.Combine(fileSystem.AppDataDirectory, "DeviceCache.json");

Expand Down Expand Up @@ -101,7 +101,7 @@ public async Task SetAllOffline()
await TrySaveCache();
}

public bool TryGet(string deviceId, [NotNullWhen(true)] out DeviceDto? device)
public bool TryGet(Guid deviceId, [NotNullWhen(true)] out DeviceDto? device)
{
return _cache.TryGetValue(deviceId, out device);
}
Expand Down
6 changes: 3 additions & 3 deletions ControlR.Viewer/Services/ViewerHubConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface IViewerHubConnection : IHubConnectionBase
Task<Uri?> GetWebsocketBridgeOrigin();
Task<Result<WindowsSession[]>> GetWindowsSessions(DeviceDto device);

Task InvokeCtrlAltDel(string deviceId);
Task InvokeCtrlAltDel(Guid deviceId);

Task Reconnect(CancellationToken cancellationToken);

Expand Down Expand Up @@ -103,7 +103,7 @@ await TryInvoke(
}


public async Task CloseTerminalSession(string deviceId, Guid terminalId)
public async Task CloseTerminalSession(Guid deviceId, Guid terminalId)
{
await TryInvoke(async () =>
{
Expand Down Expand Up @@ -204,7 +204,7 @@ await Connection.InvokeAsync<WindowsSession[]>(nameof(IViewerHub.GetWindowsSessi
}
}

public async Task InvokeCtrlAltDel(string deviceId)
public async Task InvokeCtrlAltDel(Guid deviceId)
{
await TryInvoke(async () =>
{
Expand Down
6 changes: 3 additions & 3 deletions ControlR.Web.Client/Services/DevicesCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public interface IDeviceCache

Task SetAllOffline();

bool TryGet(string deviceId, [NotNullWhen(true)] out DeviceDto? device);
bool TryGet(Guid deviceId, [NotNullWhen(true)] out DeviceDto? device);
}

internal class DeviceCache(ILogger<DeviceCache> logger) : IDeviceCache
{
private static readonly ConcurrentDictionary<string, DeviceDto> _cache = new();
private static readonly ConcurrentDictionary<Guid, DeviceDto> _cache = new();
private static readonly SemaphoreSlim _initLock = new(1, 1);

public IEnumerable<DeviceDto> Devices => _cache.Values;
Expand Down Expand Up @@ -68,7 +68,7 @@ public Task SetAllOffline()
return Task.CompletedTask;
}

public bool TryGet(string deviceId, [NotNullWhen(true)] out DeviceDto? device)
public bool TryGet(Guid deviceId, [NotNullWhen(true)] out DeviceDto? device)
{
return _cache.TryGetValue(deviceId, out device);
}
Expand Down
11 changes: 5 additions & 6 deletions ControlR.Web.Client/Services/ViewerHubConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ControlR.Web.Client.Services;
public interface IViewerHubConnection : IHubConnectionBase
{
Task ClearAlert();
Task CloseTerminalSession(string agentConnectionId, Guid terminalId);
Task CloseTerminalSession(Guid deviceId, Guid terminalId);

Task<Result<TerminalSessionRequestResult>> CreateTerminalSession(string agentConnectionId, Guid terminalId);

Expand All @@ -23,7 +23,7 @@ public interface IViewerHubConnection : IHubConnectionBase
Task<Uri?> GetWebsocketBridgeOrigin();
Task<Result<WindowsSession[]>> GetWindowsSessions(DeviceDto device);

Task InvokeCtrlAltDel(string deviceId);
Task InvokeCtrlAltDel(Guid deviceId);

Task Reconnect(CancellationToken cancellationToken);

Expand Down Expand Up @@ -102,7 +102,7 @@ await TryInvoke(
}


public async Task CloseTerminalSession(string deviceId, Guid terminalId)
public async Task CloseTerminalSession(Guid deviceId, Guid terminalId)
{
await TryInvoke(async () =>
{
Expand All @@ -112,8 +112,7 @@ await TryInvoke(async () =>
});
}

public async Task<Result<TerminalSessionRequestResult>> CreateTerminalSession(string agentConnectionId,
Guid terminalId)
public async Task<Result<TerminalSessionRequestResult>> CreateTerminalSession(string agentConnectionId, Guid terminalId)
{
return await TryInvoke(
async () =>
Expand Down Expand Up @@ -197,7 +196,7 @@ public async Task<Result<WindowsSession[]>> GetWindowsSessions(DeviceDto device)
}
}

public async Task InvokeCtrlAltDel(string deviceId)
public async Task InvokeCtrlAltDel(Guid deviceId)
{
await TryInvoke(async () =>
{
Expand Down
2 changes: 1 addition & 1 deletion ControlR.Web.Server/Hubs/AgentHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task UpdateDevice(DeviceDto device)

// TODO: Save to DB.

await Groups.AddToGroupAsync(Context.ConnectionId, device.Id);
await Groups.AddToGroupAsync(Context.ConnectionId, HubGroupNames.GetDeviceGroupName(device.Id));

Device = device;

Expand Down
9 changes: 7 additions & 2 deletions ControlR.Web.Server/Hubs/HubGroupNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@

public static class HubGroupNames
{
public const string ServerAdministrators = "server-administrators";
}
public const string ServerAdministrators = "server-administrators";

public static string GetDeviceGroupName(Guid deviceId)
{
return $"device-{deviceId}";
}
}
4 changes: 2 additions & 2 deletions ControlR.Web.Server/Hubs/ViewerHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ public async Task<Result> SendAlertBroadcast(AlertBroadcastDto alertDto)
}
}

public async Task SendDtoToAgent(string deviceId, DtoWrapper wrapper)
public async Task SendDtoToAgent(Guid deviceId, DtoWrapper wrapper)
{
using var scope = logger.BeginMemberScope();

await agentHub.Clients.Group(deviceId).ReceiveDto(wrapper);
await agentHub.Clients.Group(HubGroupNames.GetDeviceGroupName(deviceId)).ReceiveDto(wrapper);
}

public Task SendDtoToUserGroups(DtoWrapper wrapper)
Expand Down
3 changes: 2 additions & 1 deletion ControlR.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=controlr/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/UserDictionary/Words/=controlr/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=dtos/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion Libraries/ControlR.Libraries.Shared/Dtos/DeviceDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class DeviceDto

[MsgPackKey]
[Display(Name = "Device Id")]
public string Id { get; init; } = string.Empty;
public Guid Id { get; init; }

[MsgPackKey]
[Display(Name = "64-bit")]
Expand Down
29 changes: 15 additions & 14 deletions Libraries/ControlR.Libraries.Shared/Hubs/IViewerHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ namespace ControlR.Libraries.Shared.Hubs;

public interface IViewerHub
{
Task<bool> CheckIfServerAdministrator();
Task<bool> CheckIfServerAdministrator();

Task<Result> ClearAlert();
Task<Result> ClearAlert();

Task<Result<TerminalSessionRequestResult>> CreateTerminalSession(string agentConnectionId, TerminalSessionRequest requestDto);
Task<Result<TerminalSessionRequestResult>> CreateTerminalSession(string agentConnectionId,
TerminalSessionRequest requestDto);

Task<Result<AgentAppSettings>> GetAgentAppSettings(string agentConnectionId);
Task<Result<AgentAppSettings>> GetAgentAppSettings(string agentConnectionId);

Task<Result<AlertBroadcastDto>> GetCurrentAlert();
Task<Result<AlertBroadcastDto>> GetCurrentAlert();

Task<Result<ServerStatsDto>> GetServerStats();
Task<Result<ServerStatsDto>> GetServerStats();

Task<Uri?> GetWebSocketBridgeOrigin();
Task<WindowsSession[]> GetWindowsSessions(string agentConnectionId);
Task<Uri?> GetWebSocketBridgeOrigin();
Task<WindowsSession[]> GetWindowsSessions(string agentConnectionId);

Task<Result> RequestStreamingSession(string agentConnectionId, StreamerSessionRequestDto sessionRequestDto);
Task<Result> SendAgentAppSettings(string agentConnectionId, AgentAppSettings signedDto);
Task<Result> RequestStreamingSession(string agentConnectionId, StreamerSessionRequestDto sessionRequestDto);
Task<Result> SendAgentAppSettings(string agentConnectionId, AgentAppSettings signedDto);

Task<Result> SendAlertBroadcast(AlertBroadcastDto signedDto);
Task SendDtoToAgent(string deviceId, DtoWrapper wrapper);
Task SendDtoToUserGroups(DtoWrapper wrapper);
Task<Result> SendTerminalInput(string agentConnectionId, TerminalInputDto dto);
Task<Result> SendAlertBroadcast(AlertBroadcastDto signedDto);
Task SendDtoToAgent(Guid deviceId, DtoWrapper wrapper);
Task SendDtoToUserGroups(DtoWrapper wrapper);
Task<Result> SendTerminalInput(string agentConnectionId, TerminalInputDto dto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class AgentAppOptions
public const string SectionKey = "AppOptions";

[MsgPackKey]
public string? DeviceId { get; set; }
public Guid DeviceId { get; set; }

[MsgPackKey]
public Uri? ServerUri { get; set; }
Expand Down

0 comments on commit 8c93772

Please sign in to comment.