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

130 lines
4.4 KiB
C#
Raw Normal View History

2023-09-21 22:10:55 +02:00
using Insight.Domain.Interfaces;
using Insight.Domain.Messages;
using Insight.Domain.Messages.Agent;
2023-09-21 18:58:32 +02:00
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using WUApiLib;
2023-09-21 22:10:55 +02:00
using static Insight.Domain.Messages.Agent.Update;
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
namespace Insight.Agent.Network.Handlers;
[SupportedOSPlatform("windows")]
public class UpdateHandler : 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-09-21 22:10:55 +02:00
if (message is InventoryRequest)
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
await sender.SendAsync(GetUpdates(), cancellationToken);
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 UpdateList GetUpdates()
{
return new UpdateList
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
Installed = QueryInstalledUpdates(),
Pending = QueryPendingUpdates()
};
}
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
private static List<Update> QueryInstalledUpdates()
{
var updates = new List<Update>();
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
var session = new UpdateSessionClass();
var searcher = session.CreateUpdateSearcher();
searcher.Online = false;
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
var count = searcher.GetTotalHistoryCount();
var result = searcher.QueryHistory(0, count);
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
foreach (IUpdateHistoryEntry wupdate in result)
{
var update = new Update
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
Id = wupdate.UpdateIdentity.UpdateID,
Date = wupdate.Date,
Name = wupdate.Title,
Description = wupdate.Description,
Result = wupdate.ResultCode switch
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
OperationResultCode.orcNotStarted => OsUpdateResultCodeEnum.NotStarted,
OperationResultCode.orcInProgress => OsUpdateResultCodeEnum.InProgress,
OperationResultCode.orcSucceeded => OsUpdateResultCodeEnum.Succeeded,
OperationResultCode.orcSucceededWithErrors => OsUpdateResultCodeEnum.SucceededWithErrors,
OperationResultCode.orcFailed => OsUpdateResultCodeEnum.Failed,
OperationResultCode.orcAborted => OsUpdateResultCodeEnum.Aborted,
_ => null
},
SupportUrl = wupdate.SupportUrl,
};
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
try
{
var rx = new Regex(@"KB(\d+)");
update.Hotfix = rx.Match(wupdate.Title).Value;
}
catch (Exception)
{
2023-09-21 18:58:32 +02:00
}
2023-09-21 22:10:55 +02:00
updates.Add(update);
2023-09-21 18:58:32 +02:00
}
2023-09-21 22:10:55 +02:00
return updates;
}
private static List<Update> QueryPendingUpdates()
{
var updates = new List<Update>();
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
var session = new UpdateSessionClass();
var searcher = session.CreateUpdateSearcher();
searcher.Online = true;
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
var result = searcher.Search("IsInstalled=0");
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
foreach (IUpdate wupdate in result.Updates)
{
var update = new Update
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
Id = wupdate.Identity.UpdateID,
Type = wupdate.Type switch
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
UpdateType.utSoftware => OsUpdateTypeEnum.Software,
UpdateType.utDriver => OsUpdateTypeEnum.Driver,
_ => null
},
Date = wupdate.LastDeploymentChangeTime,
Name = wupdate.Title,
Description = wupdate.Description,
SupportUrl = wupdate.SupportUrl,
Size = wupdate.MaxDownloadSize,
IsDownloaded = wupdate.IsDownloaded,
CanRequestUserInput = wupdate.InstallationBehavior.CanRequestUserInput,
RebootBehavior = wupdate.InstallationBehavior.RebootBehavior switch
2023-09-21 18:58:32 +02:00
{
2023-09-21 22:10:55 +02:00
InstallationRebootBehavior.irbNeverReboots => OsUpdateRebootBehaviorEnum.NeverReboots,
InstallationRebootBehavior.irbAlwaysRequiresReboot => OsUpdateRebootBehaviorEnum.AlwaysRequiresReboot,
InstallationRebootBehavior.irbCanRequestReboot => OsUpdateRebootBehaviorEnum.CanRequestReboot,
_ => null
},
};
2023-09-21 18:58:32 +02:00
2023-09-21 22:10:55 +02:00
if (wupdate.KBArticleIDs.Count > 0)
{
foreach (var id in wupdate.KBArticleIDs)
{
update.Hotfix = $"KB{id}";
break;
}
2023-09-21 18:58:32 +02:00
}
2023-09-21 22:10:55 +02:00
updates.Add(update);
2023-09-21 18:58:32 +02:00
}
2023-09-21 22:10:55 +02:00
return updates;
2023-09-21 18:58:32 +02:00
}
}