Migration to a ServiceWorker for most tasks.

Implementation of basic PatchComplianceTask. First real iteration, basic/raw asf.
This commit is contained in:
2025-11-04 13:55:04 +08:00
parent aacd8e0293
commit d880ebeedb
23 changed files with 1527 additions and 6 deletions

View File

@@ -14,6 +14,14 @@ namespace LD_SysInfo
[JsonProperty("SystemInfoInterval")] public int SystemInfoInterval { get; set; } = 60;
[JsonProperty("ClientIdentifier")] public string ClientIdentifier { get; set; } = "your-default-client-id";
[JsonProperty("Auth")] public AuthConfig Auth { get; set; } = new();
[JsonProperty("PatchCompliance")] public PatchComplianceConfig PatchCompliance { get; set; } = new();
}
public class PatchComplianceConfig
{
[JsonProperty("Enabled")] public bool Enabled { get; set; } = false;
[JsonProperty("CheckIntervalHours")] public int CheckIntervalHours { get; set; } = 24;
[JsonProperty("LastCheckTime")] public DateTime? LastCheckTime { get; set; } = null;
}
public class AuthConfig

View File

@@ -72,6 +72,7 @@
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Newtonsoft.Json" />
<PackageReference Include="System.Management" />
<PackageReference Include="System.ServiceProcess.ServiceController" />
</ItemGroup>
<ItemGroup>

View File

@@ -230,6 +230,7 @@
<TabItem Header="InstalledApps" FontSize="16"/>
<TabItem Header="Status" FontSize="16"/>
<TabItem Header="Osquery" FontSize="16"/>
<TabItem Header="Service" FontSize="16"/>
</TabControl>
<!--
@@ -456,6 +457,106 @@
</Grid>
</TabItem>
<!-- 🔹 Service Tab -->
<TabItem Header="Service">
<Grid Background="{DynamicResource BackgroundDarkBrush}" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Service Status Section -->
<Border Grid.Row="0" Background="{DynamicResource CardBackgroundBrush}"
CornerRadius="8" Padding="15" Margin="0,0,0,15">
<StackPanel>
<TextBlock Text="PSG-Oversight Service Status"
FontSize="16" FontWeight="Bold"
Foreground="{DynamicResource TextDarkBrush}"
Margin="0,0,0,10"/>
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="Status:" FontWeight="Bold" Width="100"
Foreground="{DynamicResource TextDarkBrush}"/>
<TextBlock x:Name="ServiceStatusText" Text="Unknown"
Foreground="{DynamicResource AccentBrush}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="Last Check:" FontWeight="Bold" Width="100"
Foreground="{DynamicResource TextDarkBrush}"/>
<TextBlock x:Name="ServiceLastCheckText" Text="Never"
Foreground="{DynamicResource TextDarkBrush}"/>
</StackPanel>
<Button Content="Refresh Status" Click="RefreshServiceStatus_Click"
Style="{StaticResource SmallRoundedButtonStyle}"
Margin="0,10,0,0" Width="150" HorizontalAlignment="Left"/>
</StackPanel>
</Border>
<!-- Test Operations Section -->
<Border Grid.Row="1" Background="{DynamicResource CardBackgroundBrush}"
CornerRadius="8" Padding="15" Margin="0,0,0,15">
<StackPanel>
<TextBlock Text="Test Operations"
FontSize="16" FontWeight="Bold"
Foreground="{DynamicResource TextDarkBrush}"
Margin="0,0,0,10"/>
<TextBlock Text="Click buttons below to test service operations:"
TextWrapping="Wrap"
Foreground="{DynamicResource TextDarkBrush}"
Margin="0,0,0,10"/>
<Button Content="🔍 Run Patch Compliance Check Now"
Click="TestPatchCompliance_Click"
Style="{StaticResource SmallRoundedButtonStyle}"
Margin="0,5" Width="280" HorizontalAlignment="Left"/>
<Button Content="📊 View Task Scheduler State"
Click="ViewSchedulerState_Click"
Style="{StaticResource SmallRoundedButtonStyle}"
Margin="0,5" Width="280" HorizontalAlignment="Left"/>
<Button Content="🔄 Test API Connection"
Click="TestAPIConnection_Click"
Style="{StaticResource SmallRoundedButtonStyle}"
Margin="0,5" Width="280" HorizontalAlignment="Left"/>
</StackPanel>
</Border>
<!-- Log Output Section -->
<Border Grid.Row="2" Background="{DynamicResource CardBackgroundBrush}"
CornerRadius="8" Padding="15">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Service Logs"
FontSize="14" FontWeight="Bold"
Foreground="{DynamicResource TextDarkBrush}"
Margin="0,0,0,10"/>
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
<TextBox x:Name="ServiceLogOutput"
Background="#1e1e1e"
Foreground="#dcdcdc"
FontFamily="Consolas"
FontSize="12"
AcceptsReturn="True"
IsReadOnly="True"
TextWrapping="Wrap"
MinHeight="200"
Text="Service logs will appear here..."/>
</ScrollViewer>
</Grid>
</Border>
</Grid>
</TabItem>
</TabControl>
</Border>

View File

@@ -901,5 +901,121 @@ private async void RefreshButton_Click(object sender, RoutedEventArgs e)
}
}
// ============================
// Service Tab Event Handlers
// ============================
private void RefreshServiceStatus_Click(object sender, RoutedEventArgs e)
{
try
{
var serviceName = "PSG-Oversight";
var service = System.ServiceProcess.ServiceController.GetServices()
.FirstOrDefault(s => s.ServiceName == serviceName);
if (service != null)
{
ServiceStatusText.Text = service.Status.ToString();
ServiceStatusText.Foreground = service.Status == System.ServiceProcess.ServiceControllerStatus.Running
? new SolidColorBrush(Colors.Green)
: new SolidColorBrush(Colors.Red);
}
else
{
ServiceStatusText.Text = "Not Installed";
ServiceStatusText.Foreground = new SolidColorBrush(Colors.Orange);
}
ServiceLastCheckText.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
catch (Exception ex)
{
ServiceStatusText.Text = $"Error: {ex.Message}";
ServiceStatusText.Foreground = new SolidColorBrush(Colors.Red);
}
}
private async void TestPatchCompliance_Click(object sender, RoutedEventArgs e)
{
ServiceLogOutput.Text = "Running Patch Compliance check...\n\n";
try
{
// Create instances of services
var apiClient = new Services.ApiClient(_config);
var patchTask = new Services.PatchComplianceTask(_config, apiClient);
// Execute the task
await patchTask.ExecuteAsync();
ServiceLogOutput.Text += $"[{DateTime.Now}] Patch compliance check completed successfully.\n";
ServiceLogOutput.Text += "Check %LOCALAPPDATA%\\PSG-Oversight\\patch_compliance.log for details.\n";
}
catch (Exception ex)
{
ServiceLogOutput.Text += $"[{DateTime.Now}] ERROR: {ex.Message}\n";
ServiceLogOutput.Text += $"Stack Trace:\n{ex.StackTrace}\n";
}
}
private void ViewSchedulerState_Click(object sender, RoutedEventArgs e)
{
try
{
var stateFilePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"PSG-Oversight",
"task_scheduler_state.json"
);
if (File.Exists(stateFilePath))
{
var json = File.ReadAllText(stateFilePath);
var formatted = JsonConvert.SerializeObject(
JsonConvert.DeserializeObject(json),
Formatting.Indented
);
ServiceLogOutput.Text = $"Task Scheduler State:\n\n{formatted}";
}
else
{
ServiceLogOutput.Text = "Task scheduler state file not found.\nThe service may not have run any tasks yet.";
}
}
catch (Exception ex)
{
ServiceLogOutput.Text = $"Error reading scheduler state:\n{ex.Message}";
}
}
private async void TestAPIConnection_Click(object sender, RoutedEventArgs e)
{
ServiceLogOutput.Text = "Testing API connection...\n\n";
try
{
var apiClient = new Services.ApiClient(_config);
// Test connectivity
bool isConnected = await apiClient.CheckConnectivity();
if (isConnected)
{
ServiceLogOutput.Text += $"[{DateTime.Now}] ✅ API is reachable!\n";
ServiceLogOutput.Text += $"Server URL: {_config.ServerUrl}\n";
}
else
{
ServiceLogOutput.Text += $"[{DateTime.Now}] ❌ API is NOT reachable!\n";
ServiceLogOutput.Text += $"Server URL: {_config.ServerUrl}\n";
}
}
catch (Exception ex)
{
ServiceLogOutput.Text += $"[{DateTime.Now}] ERROR: {ex.Message}\n";
}
}
}
}

View File

@@ -264,6 +264,44 @@ namespace LD_SysInfo.Services
return response;
}
/// <summary>
/// Sends Windows update history (patch compliance data) to the backend API
/// </summary>
public async Task<bool> PostPatchComplianceAsync(object patchData)
{
try
{
if (string.IsNullOrEmpty(jwtToken))
{
Console.WriteLine("❌ Not authenticated. Cannot send patch compliance data.");
return false;
}
var jsonContent = JsonConvert.SerializeObject(patchData);
var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
var response = await SendWithAutoReauthAsync(() =>
httpClient.PostAsync(BuildUrl("/api/patch-compliance"), content)
);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("✅ Patch compliance data sent successfully.");
return true;
}
else
{
Console.WriteLine($"❌ Failed to send patch compliance data: {response.StatusCode}");
return false;
}
}
catch (Exception ex)
{
Console.WriteLine($"❌ Exception sending patch compliance data: {ex.Message}");
return false;
}
}
}
}

View File

@@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json;
namespace LD_SysInfo.Services
{
/// <summary>
/// Manages scheduled osquery-based tasks
/// Designed to be portable to Windows Service (OversightService) later
/// </summary>
public class OsqueryTaskScheduler
{
private readonly List<IScheduledTask> _tasks = new();
private readonly Timer _timer;
private readonly string _stateFilePath;
private bool _isRunning;
public OsqueryTaskScheduler()
{
// Store task state in AppData to persist last run times
var appDataDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"PSG-Oversight"
);
Directory.CreateDirectory(appDataDir);
_stateFilePath = Path.Combine(appDataDir, "task_scheduler_state.json");
// Check every minute for tasks that need to run
_timer = new Timer(CheckAndRunTasks, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
}
/// <summary>
/// Register a task with the scheduler
/// </summary>
public void RegisterTask(IScheduledTask task)
{
_tasks.Add(task);
LoadTaskState(task);
}
/// <summary>
/// Start the scheduler
/// </summary>
public void Start()
{
_isRunning = true;
}
/// <summary>
/// Stop the scheduler
/// </summary>
public void Stop()
{
_isRunning = false;
_timer?.Change(Timeout.Infinite, Timeout.Infinite);
}
private async void CheckAndRunTasks(object? state)
{
if (!_isRunning) return;
foreach (var task in _tasks.Where(t => t.ShouldRun()))
{
try
{
await task.ExecuteAsync();
task.LastRun = DateTime.Now;
SaveTaskState(task);
}
catch (Exception ex)
{
LogError($"Task '{task.TaskName}' failed: {ex.Message}");
}
}
}
private void LoadTaskState(IScheduledTask task)
{
try
{
if (!File.Exists(_stateFilePath)) return;
var json = File.ReadAllText(_stateFilePath);
var states = JsonConvert.DeserializeObject<Dictionary<string, DateTime>>(json);
if (states != null && states.TryGetValue(task.TaskName, out var lastRun))
{
task.LastRun = lastRun;
}
}
catch (Exception ex)
{
LogError($"Failed to load task state: {ex.Message}");
}
}
private void SaveTaskState(IScheduledTask task)
{
try
{
Dictionary<string, DateTime> states;
if (File.Exists(_stateFilePath))
{
var json = File.ReadAllText(_stateFilePath);
states = JsonConvert.DeserializeObject<Dictionary<string, DateTime>>(json)
?? new Dictionary<string, DateTime>();
}
else
{
states = new Dictionary<string, DateTime>();
}
states[task.TaskName] = task.LastRun ?? DateTime.Now;
var updatedJson = JsonConvert.SerializeObject(states, Formatting.Indented);
File.WriteAllText(_stateFilePath, updatedJson);
}
catch (Exception ex)
{
LogError($"Failed to save task state: {ex.Message}");
}
}
private void LogError(string message)
{
try
{
var logDir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"PSG-Oversight"
);
var logPath = Path.Combine(logDir, "scheduler_error.log");
File.AppendAllText(logPath, $"[{DateTime.Now}] {message}\n");
}
catch
{
// Silently fail if we can't write logs
}
}
public void Dispose()
{
Stop();
_timer?.Dispose();
}
}
}

View File

@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LD_SysInfo.Services
{
/// <summary>
/// Scheduled task that collects Windows update history using osquery
/// and sends it to the backend API
/// </summary>
public class PatchComplianceTask : ScheduledTask
{
private readonly AppConfig _config;
private readonly ApiClient _apiClient;
public override string TaskName => "PatchCompliance";
public override TimeSpan Interval =>
TimeSpan.FromHours(_config.PatchCompliance.CheckIntervalHours);
public PatchComplianceTask(AppConfig config, ApiClient apiClient)
{
_config = config;
_apiClient = apiClient;
// Initialize LastRun from config if available
LastRun = _config.PatchCompliance.LastCheckTime;
}
public override bool ShouldRun()
{
// Don't run if disabled in config
if (!_config.PatchCompliance.Enabled)
return false;
return base.ShouldRun();
}
public override async Task ExecuteAsync()
{
try
{
// Query windows_update_history from osquery
var updates = OsqueryService.Query(@"
SELECT
date,
title,
update_id
FROM windows_update_history
ORDER BY date DESC;
");
if (!updates.Any())
{
LogInfo("No Windows update history found.");
return;
}
// Transform to API-friendly format
var patchData = updates.Select(u => new
{
date = u.GetValueOrDefault("date"),
title = u.GetValueOrDefault("title"),
updateId = u.GetValueOrDefault("update_id")
}).ToList();
LogInfo($"Collected {patchData.Count} Windows updates.");
// Send to API
await _apiClient.PostPatchComplianceAsync(patchData);
// Update config with last check time
_config.PatchCompliance.LastCheckTime = DateTime.Now;
SaveConfig();
LogInfo("Patch compliance data sent successfully.");
}
catch (Exception ex)
{
LogError($"PatchComplianceTask failed: {ex.Message}");
throw;
}
}
private void SaveConfig()
{
try
{
var configPath = System.IO.Path.Combine(AppContext.BaseDirectory, "config.json");
var json = Newtonsoft.Json.JsonConvert.SerializeObject(_config, Newtonsoft.Json.Formatting.Indented);
System.IO.File.WriteAllText(configPath, json);
}
catch (Exception ex)
{
LogError($"Failed to save config: {ex.Message}");
}
}
private void LogInfo(string message)
{
if (!_config.EnableLogging) return;
try
{
var logDir = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"PSG-Oversight"
);
System.IO.Directory.CreateDirectory(logDir);
var logPath = System.IO.Path.Combine(logDir, "patch_compliance.log");
System.IO.File.AppendAllText(logPath, $"[{DateTime.Now}] INFO: {message}\n");
}
catch { }
}
private void LogError(string message)
{
try
{
var logDir = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"PSG-Oversight"
);
System.IO.Directory.CreateDirectory(logDir);
var logPath = System.IO.Path.Combine(logDir, "patch_compliance.log");
System.IO.File.AppendAllText(logPath, $"[{DateTime.Now}] ERROR: {message}\n");
}
catch { }
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
namespace LD_SysInfo.Services
{
/// <summary>
/// Base interface for scheduled tasks
/// </summary>
public interface IScheduledTask
{
string TaskName { get; }
TimeSpan Interval { get; }
DateTime? LastRun { get; set; }
Task ExecuteAsync();
bool ShouldRun();
}
/// <summary>
/// Base class for scheduled tasks with common logic
/// </summary>
public abstract class ScheduledTask : IScheduledTask
{
public abstract string TaskName { get; }
public abstract TimeSpan Interval { get; }
public DateTime? LastRun { get; set; }
public abstract Task ExecuteAsync();
public virtual bool ShouldRun()
{
if (LastRun == null) return true;
return DateTime.Now - LastRun >= Interval;
}
}
}