Skip to content

Commit

Permalink
Add DeviceClientDto for transfer from agent to server. Refactor to al…
Browse files Browse the repository at this point in the history
…low correct scoping of services in tests.
  • Loading branch information
bitbound committed Jun 23, 2023
1 parent fb0bfe4 commit 7c83371
Show file tree
Hide file tree
Showing 17 changed files with 348 additions and 143 deletions.
5 changes: 3 additions & 2 deletions Agent/Interfaces/IDeviceInformationService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Remotely.Shared.Models;
using Remotely.Shared.Dtos;
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,6 +10,6 @@ namespace Remotely.Agent.Interfaces
{
public interface IDeviceInformationService
{
Task<Device> CreateDevice(string deviceId, string orgId);
Task<DeviceClientDto> CreateDevice(string deviceId, string orgId);
}
}
13 changes: 7 additions & 6 deletions Agent/Services/DeviceInfoGeneratorBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using Remotely.Shared.Dtos;
using Remotely.Shared.Models;
using Remotely.Shared.Utilities;
using System;
Expand All @@ -21,21 +22,21 @@ public DeviceInfoGeneratorBase(ILogger<DeviceInfoGeneratorBase> logger)
_logger = logger;
}

protected Device GetDeviceBase(string deviceID, string orgID)
protected DeviceClientDto GetDeviceBase(string deviceID, string orgID)
{

return new Device()
return new DeviceClientDto()
{
ID = deviceID,
Id = deviceID,
DeviceName = Environment.MachineName,
Platform = EnvironmentHelper.Platform.ToString(),
ProcessorCount = Environment.ProcessorCount,
OSArchitecture = RuntimeInformation.OSArchitecture,
OSDescription = RuntimeInformation.OSDescription,
OsArchitecture = RuntimeInformation.OSArchitecture,
OsDescription = RuntimeInformation.OSDescription,
Is64Bit = Environment.Is64BitOperatingSystem,
IsOnline = true,
MacAddresses = GetMacAddresses().ToArray(),
OrganizationID = orgID,
OrganizationId = orgID,
AgentVersion = AppVersionHelper.GetAppVersion()
};
}
Expand Down
3 changes: 2 additions & 1 deletion Agent/Services/Linux/DeviceInfoGeneratorLinux.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Remotely.Agent.Interfaces;
using Remotely.Shared.Dtos;
using Remotely.Shared.Models;
using Remotely.Shared.Services;
using Remotely.Shared.Utilities;
Expand All @@ -26,7 +27,7 @@ public DeviceInfoGeneratorLinux(
_cpuUtilSampler = cpuUtilSampler;
}

public Task<Device> CreateDevice(string deviceId, string orgId)
public Task<DeviceClientDto> CreateDevice(string deviceId, string orgId)
{
var device = GetDeviceBase(deviceId, orgId);

Expand Down
3 changes: 2 additions & 1 deletion Agent/Services/MacOS/DeviceInfoGeneratorMac.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Logging;
using Remotely.Agent.Interfaces;
using Remotely.Shared.Dtos;
using Remotely.Shared.Models;
using Remotely.Shared.Services;
using System;
Expand All @@ -17,7 +18,7 @@ public DeviceInfoGeneratorMac(IProcessInvoker processInvoker, ILogger<DeviceInfo
{
_processInvoker = processInvoker;
}
public async Task<Device> CreateDevice(string deviceId, string orgId)
public async Task<DeviceClientDto> CreateDevice(string deviceId, string orgId)
{
var device = GetDeviceBase(deviceId, orgId);

Expand Down
3 changes: 2 additions & 1 deletion Agent/Services/Windows/DeviceInfoGeneratorWin.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Immense.RemoteControl.Desktop.Native.Windows;
using Microsoft.Extensions.Logging;
using Remotely.Agent.Interfaces;
using Remotely.Shared.Dtos;
using Remotely.Shared.Models;
using System;
using System.Linq;
Expand All @@ -20,7 +21,7 @@ public DeviceInfoGeneratorWin(
_cpuUtilSampler = cpuUtilSampler;
}

public Task<Device> CreateDevice(string deviceId, string orgId)
public Task<DeviceClientDto> CreateDevice(string deviceId, string orgId)
{
var device = GetDeviceBase(deviceId, orgId);

Expand Down
2 changes: 1 addition & 1 deletion Server/Components/Devices/DeviceCard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
<InputSelect @bind-Value="Device.DeviceGroupID" class="form-control">
<option value="">None</option>

@foreach (var group in DataService.GetDeviceGroups(Username))
@foreach (var group in _deviceGroups)
{
<option @key="group.ID" value="@group.ID">@group.Name</option>
}
Expand Down
3 changes: 3 additions & 0 deletions Server/Components/Devices/DeviceCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public partial class DeviceCard : AuthComponentBase, IDisposable
private ElementReference _card;
private Version _currentVersion = new();
private Theme _theme;
private DeviceGroup[] _deviceGroups = Array.Empty<DeviceGroup>();

[Parameter]
public Device Device { get; set; }

Expand Down Expand Up @@ -78,6 +80,7 @@ protected override async Task OnInitializedAsync()
await base.OnInitializedAsync();
_theme = await AppState.GetEffectiveTheme();
_currentVersion = UpgradeService.GetCurrentVersion();
_deviceGroups = DataService.GetDeviceGroups(Username);
AppState.PropertyChanged += AppState_PropertyChanged;
CircuitConnection.MessageReceived += CircuitConnection_MessageReceived;
}
Expand Down
9 changes: 5 additions & 4 deletions Server/Hubs/AgentHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Remotely.Server.Models;
using Remotely.Server.Services;
using Remotely.Shared;
using Remotely.Shared.Dtos;
using Remotely.Shared.Enums;
using Remotely.Shared.Models;
using Remotely.Shared.Utilities;
Expand Down Expand Up @@ -87,11 +88,11 @@ await Clients.Caller.SendAsync("RunScript",
}
}

public async Task<bool> DeviceCameOnline(Device device)
public async Task<bool> DeviceCameOnline(DeviceClientDto device)
{
try
{
if (CheckForDeviceBan(device.ID, device.DeviceName))
if (CheckForDeviceBan(device.Id, device.DeviceName))
{
return false;
}
Expand Down Expand Up @@ -144,9 +145,9 @@ public async Task<bool> DeviceCameOnline(Device device)
return false;
}

public async Task DeviceHeartbeat(Device device)
public async Task DeviceHeartbeat(DeviceClientDto device)
{
if (CheckForDeviceBan(device.ID, device.DeviceName))
if (CheckForDeviceBan(device.Id, device.DeviceName))
{
return;
}
Expand Down
122 changes: 76 additions & 46 deletions Server/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Remotely.Server.Data;
using Remotely.Server.Models;
using Remotely.Shared;
using Remotely.Shared.Dtos;
using Remotely.Shared.Enums;
using Remotely.Shared.Models;
using Remotely.Shared.Utilities;
Expand All @@ -27,10 +28,10 @@ public interface IDataService
Task AddAlert(string deviceID, string organizationID, string alertMessage, string details = null);

bool AddDeviceGroup(string orgID, DeviceGroup deviceGroup, out string deviceGroupID, out string errorMessage);

Task<Result> AddDeviceToGroup(string deviceId, string groupId);
InviteLink AddInvite(string orgID, InviteViewModel invite);

Task<Result<Device>> AddOrUpdateDevice(Device device);
Task<Result<Device>> AddOrUpdateDevice(DeviceClientDto device);

Task AddOrUpdateSavedScript(SavedScript script, string userId);

Expand Down Expand Up @@ -305,6 +306,34 @@ public bool AddDeviceGroup(string orgID, DeviceGroup deviceGroup, out string dev
return true;
}

public async Task<Result> AddDeviceToGroup(string deviceId, string groupId)
{
using var context = _appDbFactory.GetContext();
var device = await context.Devices.FirstOrDefaultAsync(x => x.ID == deviceId);

if (device is null)
{
return Result.Fail("Device not found.");
}

var group = await context.DeviceGroups.FirstOrDefaultAsync(x =>
x.OrganizationID == device.OrganizationID &&
x.ID == groupId);

if (group is null)
{
return Result.Fail("Group not found.");
}

group.Devices ??= new();
group.Devices.Add(device);
device.DeviceGroup = group;
device.DeviceGroupID = group.ID;
await context.SaveChangesAsync();

return Result.Ok();
}

public InviteLink AddInvite(string orgID, InviteViewModel invite)
{
using var dbContext = _appDbFactory.GetContext();
Expand All @@ -327,61 +356,62 @@ public InviteLink AddInvite(string orgID, InviteViewModel invite)
return inviteLink;
}

public async Task<Result<Device>> AddOrUpdateDevice(Device device)
public async Task<Result<Device>> AddOrUpdateDevice(DeviceClientDto deviceDto)
{
using var dbContext = _appDbFactory.GetContext();

var resultDevice = await dbContext.Devices.FindAsync(device.ID);
var device = await dbContext.Devices.FindAsync(deviceDto.Id);

if (resultDevice != null)
if (device is null)
{
resultDevice.CurrentUser = device.CurrentUser;
resultDevice.DeviceName = device.DeviceName;
resultDevice.Drives = device.Drives;
resultDevice.CpuUtilization = device.CpuUtilization;
resultDevice.UsedMemory = device.UsedMemory;
resultDevice.UsedStorage = device.UsedStorage;
resultDevice.Is64Bit = device.Is64Bit;
resultDevice.IsOnline = true;
resultDevice.OSArchitecture = device.OSArchitecture;
resultDevice.OSDescription = device.OSDescription;
resultDevice.Platform = device.Platform;
resultDevice.ProcessorCount = device.ProcessorCount;
resultDevice.PublicIP = device.PublicIP;
resultDevice.TotalMemory = device.TotalMemory;
resultDevice.TotalStorage = device.TotalStorage;
resultDevice.AgentVersion = device.AgentVersion;
resultDevice.MacAddresses = device.MacAddresses ?? Array.Empty<string>();
resultDevice.DeviceGroupID = device.DeviceGroupID;
resultDevice.LastOnline = DateTimeOffset.Now;
device = new Device
{
OrganizationID = deviceDto.OrganizationId,
ID = deviceDto.Id,
};
await dbContext.Devices.AddAsync(device);
}
else

device.CurrentUser = deviceDto.CurrentUser;
device.DeviceName = deviceDto.DeviceName;
device.Drives = deviceDto.Drives;
device.CpuUtilization = deviceDto.CpuUtilization;
device.UsedMemory = deviceDto.UsedMemory;
device.UsedStorage = deviceDto.UsedStorage;
device.Is64Bit = deviceDto.Is64Bit;
device.IsOnline = true;
device.OSArchitecture = deviceDto.OsArchitecture;
device.OSDescription = deviceDto.OsDescription;
device.Platform = deviceDto.Platform;
device.ProcessorCount = deviceDto.ProcessorCount;
device.PublicIP = deviceDto.PublicIP;
device.TotalMemory = deviceDto.TotalMemory;
device.TotalStorage = deviceDto.TotalStorage;
device.AgentVersion = deviceDto.AgentVersion;
device.MacAddresses = deviceDto.MacAddresses ?? Array.Empty<string>();
device.LastOnline = DateTimeOffset.Now;

if (_hostEnvironment.IsDevelopment() && dbContext.Organizations.Any())
{
device.LastOnline = DateTimeOffset.Now;
if (_hostEnvironment.IsDevelopment() && dbContext.Organizations.Any())
{
var org = await dbContext.Organizations.FirstOrDefaultAsync();
device.Organization = org;
device.OrganizationID = org?.ID;
}
var org = await dbContext.Organizations.FirstOrDefaultAsync();
device.Organization = org;
device.OrganizationID = org?.ID;
}

resultDevice = device;
if (!await dbContext.Organizations.AnyAsync(x => x.ID == device.OrganizationID))
{
_logger.LogInformation(
"Unable to add device {deviceName} because organization {organizationID}" +
"does not exist. Device ID: {ID}.",
device.DeviceName,
device.OrganizationID,
device.ID);

if (!await dbContext.Organizations.AnyAsync(x => x.ID == device.OrganizationID))
{
_logger.LogInformation(
"Unable to add device {deviceName} because organization {organizationID}" +
"does not exist. Device ID: {ID}.",
device.DeviceName,
device.OrganizationID,
device.ID);

return Result.Fail<Device>("Organization does not exist.");
}
await dbContext.Devices.AddAsync(device);
return Result.Fail<Device>("Organization does not exist.");
}

await dbContext.SaveChangesAsync();
return Result.Ok(resultDevice);
return Result.Ok(device);
}

public async Task AddOrUpdateSavedScript(SavedScript script, string userId)
Expand Down
73 changes: 73 additions & 0 deletions Shared/Dtos/DeviceClientDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using Remotely.Shared.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace Remotely.Shared.Dtos
{
[DataContract]
public class DeviceClientDto
{
[DataMember]
public string AgentVersion { get; set; } = string.Empty;

[DataMember]
public double CpuUtilization { get; set; }

[DataMember]
public string CurrentUser { get; set; } = string.Empty;

[DataMember]
public string DeviceName { get; set; } = string.Empty;

[DataMember]
public List<Drive> Drives { get; set; } = new();

[DataMember]
public string Id { get; set; } = string.Empty;

[DataMember]
public bool Is64Bit { get; set; }

[DataMember]
public bool IsOnline { get; set; }

[DataMember]
public string[] MacAddresses { get; set; } = Array.Empty<string>();

[DataMember]
public string OrganizationId { get; set; } = string.Empty;

[DataMember]
public Architecture OsArchitecture { get; set; }

[DataMember]
public string OsDescription { get; set; } = string.Empty;

[DataMember]
public string Platform { get; set; } = string.Empty;

[DataMember]
public int ProcessorCount { get; set; }

[DataMember]
public string PublicIP { get; set; } = string.Empty;

[DataMember]
public double TotalMemory { get; set; }

[DataMember]
public double TotalStorage { get; set; }

[DataMember]
public double UsedMemory { get; set; }

[DataMember]
public double UsedStorage { get; set; }
}
}
Loading

0 comments on commit 7c83371

Please sign in to comment.