insight/src/Agent/Insight.Agent/Network/Handlers/DriveHandler.cs

182 lines
8.8 KiB
C#
Raw Normal View History

2023-09-21 22:10:55 +02:00
using Insight.Domain.Interfaces;
2023-11-17 17:12:41 +01:00
using Insight.Domain.Network;
using Insight.Domain.Network.Agent.Messages;
2023-09-21 18:58:32 +02:00
using System.Management;
using System.Runtime.Versioning;
2023-09-21 22:10:55 +02:00
namespace Insight.Agent.Network.Handlers;
[SupportedOSPlatform("windows")]
public class DriveHandler : IMessageHandler<AgentSession>
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
public async ValueTask HandleAsync<TMessage>(AgentSession sender, TMessage message, CancellationToken cancellationToken) where TMessage : IMessage
2023-09-21 18:58:32 +02:00
{
2023-11-17 17:12:41 +01:00
switch (message)
2023-09-21 18:58:32 +02:00
{
2023-11-17 17:12:41 +01:00
case InventoryRequest:
{
var result = new Collection<Drive>();
result.AddRange(GetDrives());
2023-09-21 18:58:32 +02:00
2023-11-17 17:12:41 +01:00
await sender.SendAsync(result, cancellationToken);
break;
}
2023-09-21 18:58:32 +02:00
}
2023-09-21 22:10:55 +02:00
}
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
private static List<Drive> GetDrives()
{
using var searcher = new ManagementObjectSearcher
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
Scope = new ManagementScope(@"root\cimv2"),
Query = new ObjectQuery("select index, name, caption, model, manufacturer, serialNumber, size, status, interfacetype, firmwarerevision, deviceid, pnpdeviceid from win32_diskdrive")
};
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
if (searcher.TryGet(out var collection) is false)
{
searcher.Query = new ObjectQuery("select * from win32_diskdrive");
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
if (searcher.TryGet(out collection) is false) throw new InvalidOperationException("WMI Collection NULL");
}
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
var drives = new List<Drive>();
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
using (collection)
{
foreach (ManagementObject @object in collection.Cast<ManagementObject>())
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
var drive = new Drive();
var properties = @object.GetPropertyHashes();
drive.Index = @object.GetValue<uint>(properties, "index");
drive.Id = @object.GetValue<string>(properties, "deviceid")?.Trim();
drive.Name = @object.GetValue<string>(properties, "model")?.Trim();
drive.Manufacturer = @object.GetValue<string>(properties, "manufacturer")?.Trim();
drive.SerialNumber = @object.GetValue<string>(properties, "serialnumber")?.Trim();
drive.Size = @object.GetValue<ulong>(properties, "size");
drive.Status = @object.GetValue<string>(properties, "status")?.Trim();
drive.InterfaceType = @object.GetValue<string>(properties, "interfacetype")?.Trim();
drive.FirmwareRevision = @object.GetValue<string>(properties, "firmwarerevision")?.Trim();
drive.PNPDeviceID = @object.GetValue<string>(properties, "pnpdeviceid")?.Trim();
drive.Volumes = new List<Volume>();
var diskpartition = @object.GetRelated("win32_diskpartition");
using (diskpartition)
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
foreach (ManagementObject dp in diskpartition.Cast<ManagementObject>())
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
var volume = new Volume();
var dpProperties = dp.GetPropertyHashes();
volume.NumberOfBlocks = dp.GetValue<ulong>(dpProperties, "numberofblocks");
volume.BootPartition = dp.GetValue<bool>(dpProperties, "bootpartition");
volume.PrimaryPartition = dp.GetValue<bool>(dpProperties, "primarypartition");
volume.Size = dp.GetValue<ulong>(dpProperties, "size");
volume.Index = dp.GetValue<uint>(dpProperties, "index");
volume.Type = dp.GetValue<string>(dpProperties, "type")?.Trim();
volume.Bootable = dp.GetValue<bool>(dpProperties, "bootable");
volume.BlockSize = dp.GetValue<ulong>(dpProperties, "blocksize");
volume.StartingOffset = dp.GetValue<ulong>(dpProperties, "startingoffset");
var logicaldisk = dp.GetRelated("win32_logicaldisk");
using (logicaldisk)
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
foreach (ManagementObject ld in logicaldisk.Cast<ManagementObject>())
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
var ldProperties = ld.GetPropertyHashes();
volume.Id = ld.GetValue<string>(ldProperties, "deviceid")?.Trim();
volume.Name = ld.GetValue<string>(ldProperties, "volumename")?.Trim();
volume.SerialNumber = ld.GetValue<string>(ldProperties, "volumeserialnumber")?.Trim();
volume.DriveType = (DriveType)ld.GetValue<uint>(ldProperties, "drivetype");
volume.FileSystem = ld.GetValue<string>(ldProperties, "filesystem")?.Trim();
volume.Compressed = ld.GetValue<bool>(ldProperties, "compressed");
volume.Size = ld.GetValue<ulong>(ldProperties, "size");
volume.FreeSpace = ld.GetValue<ulong>(ldProperties, "freespace");
volume.ProviderName = ld.GetValue<string>(ldProperties, "providername")?.Trim();
2023-09-21 18:58:32 +02:00
}
}
2023-09-21 22:10:55 +02:00
drive.Volumes.Add(volume);
}
2023-09-21 18:58:32 +02:00
}
2023-09-21 22:10:55 +02:00
drives.Add(drive);
}
2023-09-21 18:58:32 +02:00
}
2023-09-21 22:10:55 +02:00
return drives;
}
private static List<Volume> GetVolumes()
{
using var searcher = new ManagementObjectSearcher
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
Scope = new ManagementScope(@"root\cimv2"),
Query = new ObjectQuery("select deviceid, volumename, volumeserialnumber, drivetype, filesystem, compressed, size, freeSpace, providername from win32_logicaldisk")
};
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
// per device query
// "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + driveDeviceId + "'} WHERE AssocClass=Win32_DiskDriveToDiskPartition"
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
if (searcher.TryGet(out var collection) is false)
{
searcher.Query = new ObjectQuery("select * from win32_logicaldisk");
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
if (searcher.TryGet(out collection) is false) throw new InvalidOperationException("WMI Collection NULL");
}
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
var volumes = new List<Volume>();
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
using (collection)
{
foreach (ManagementObject @object in collection.Cast<ManagementObject>())
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
var volume = new Volume();
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
var properties = @object.GetPropertyHashes();
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
//volume.DeviceId = @object.GetValue<string>(properties, "deviceid")?.Trim();
//volume.VolumeName = @object.GetValue<string>(properties, "volumename")?.Trim();
//volume.VolumeSerialNumber = @object.GetValue<string>(properties, "volumeserialnumber")?.Trim();
volume.DriveType = (DriveType)@object.GetValue<uint>(properties, "drivetype");
volume.FileSystem = @object.GetValue<string>(properties, "filesystem")?.Trim();
volume.Compressed = @object.GetValue<bool>(properties, "compressed");
volume.Size = @object.GetValue<ulong>(properties, "size");
volume.FreeSpace = @object.GetValue<ulong>(properties, "freespace");
volume.ProviderName = @object.GetValue<string>(properties, "providername")?.Trim();
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
if (volume.Id is not null)
{
searcher.Query = new ObjectQuery("associators of {win32_logicaldisk.deviceid='" + volume.Id + "'} where assocclass=win32_logicaldisktopartition");
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
if (searcher.TryGet(out var collection2))
{
using (collection2)
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
foreach (ManagementObject @object2 in collection2)
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
var properties2 = @object2.GetPropertyHashes();
volume.Index = @object2.GetValue<uint>(properties2, "index");
//volume.DiskIndex = @object2.GetValue<uint>(properties2, "diskindex");
volume.Type = @object2.GetValue<string>(properties2, "type")?.Trim();
volume.Bootable = @object2.GetValue<bool>(properties2, "bootable");
volume.PrimaryPartition = @object2.GetValue<bool>(properties2, "primarypartition");
volume.BootPartition = @object2.GetValue<bool>(properties2, "bootpartition");
volume.BlockSize = @object2.GetValue<ulong>(properties2, "blocksize");
volume.NumberOfBlocks = @object2.GetValue<ulong>(properties2, "numberofblocks");
volume.StartingOffset = @object2.GetValue<ulong>(properties2, "startingoffset");
2023-09-21 18:58:32 +02:00
}
}
}
}
2023-09-21 22:10:55 +02:00
volumes.Add(volume);
}
2023-09-21 18:58:32 +02:00
}
2023-09-21 22:10:55 +02:00
return volumes;
2023-09-21 18:58:32 +02:00
}
}