efb757d733
Introduce IElevatedTaskService and ElevatedTaskService to manage scheduled-task based elevated launch and autostart. Update App.xaml.cs to bootstrap elevation, attempt managed elevated launch, and handle task registration markers. Refactor AutostartService to delegate autostart operations to the elevated task service, remove legacy registry usage where appropriate, add logging, and update DI registration and unit tests. Remove MSIX packaging support and related tooling: delete build/build-msix.ps1, remove MSIX publish/validation/upload steps from GitHub Actions, drop MSIX-related csproj targets and EnableMsixTooling, and update docs/README/.gitignore to no longer reference MSIX artifacts.
521 lines
21 KiB
C#
521 lines
21 KiB
C#
/*
|
|
* ThreadPilot - Advanced Windows Process and Power Plan Manager
|
|
* Copyright (C) 2025 Prime Build
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, version 3 only.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
namespace ThreadPilot.Services
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Win32;
|
|
|
|
/// <summary>
|
|
/// Service for managing Windows autostart functionality using registry.
|
|
/// </summary>
|
|
public partial class AutostartService : IAutostartService
|
|
{
|
|
private const string REGISTRYKEYPATH = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
|
|
private const string APPLICATIONNAME = "ThreadPilot";
|
|
private const string SCHEDULEDTASKNAME = "ThreadPilot_Startup";
|
|
private static readonly string SchTasksExecutablePath = Path.Combine(Environment.SystemDirectory, "schtasks.exe");
|
|
private static readonly TimeSpan ScheduledTaskTimeout = TimeSpan.FromSeconds(20);
|
|
|
|
private readonly ILogger<AutostartService> logger;
|
|
private readonly IElevationService elevationService;
|
|
private readonly IElevatedTaskService elevatedTaskService;
|
|
private bool isAutostartEnabled;
|
|
private string? autostartPath;
|
|
|
|
public event EventHandler<AutostartStatusChangedEventArgs>? AutostartStatusChanged;
|
|
|
|
public bool IsAutostartEnabled => this.isAutostartEnabled;
|
|
|
|
public string? AutostartPath => this.autostartPath;
|
|
|
|
public AutostartService(ILogger<AutostartService> logger, IElevationService elevationService, IElevatedTaskService elevatedTaskService)
|
|
{
|
|
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
this.elevationService = elevationService ?? throw new ArgumentNullException(nameof(elevationService));
|
|
this.elevatedTaskService = elevatedTaskService ?? throw new ArgumentNullException(nameof(elevatedTaskService));
|
|
|
|
// Initialize current status without surfacing startup exceptions.
|
|
TaskSafety.FireAndForget(this.CheckAutostartStatusAsync(), ex =>
|
|
{
|
|
LogAutostartInitializationFailed(this.logger, ex);
|
|
});
|
|
}
|
|
|
|
public async Task<bool> EnableAutostartAsync(bool startMinimized = true)
|
|
{
|
|
try
|
|
{
|
|
var executablePath = this.GetExecutablePath();
|
|
if (string.IsNullOrEmpty(executablePath))
|
|
{
|
|
LogAutostartExecutablePathMissing(this.logger);
|
|
return false;
|
|
}
|
|
|
|
var arguments = this.GetAutostartArguments(startMinimized);
|
|
var fullCommand = $"\"{executablePath}\" {arguments}";
|
|
|
|
// Clean up legacy registry-based startup to keep a single elevated startup mechanism.
|
|
this.TryRemoveLegacyRegistryAutostart();
|
|
|
|
if (!this.elevationService.IsRunningAsAdministrator())
|
|
{
|
|
LogAutostartRequiresElevation(this.logger);
|
|
|
|
var elevationRequested = await this.elevationService.RequestElevationIfNeeded();
|
|
if (!elevationRequested)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
LogAutostartDeferredToElevatedInstance(this.logger);
|
|
return false;
|
|
}
|
|
|
|
var scheduledTaskCreated = await this.elevatedTaskService.EnsureAutostartTaskAsync(executablePath, arguments);
|
|
if (!scheduledTaskCreated)
|
|
{
|
|
LogAutostartTaskRegistrationFailed(this.logger);
|
|
return false;
|
|
}
|
|
|
|
this.isAutostartEnabled = true;
|
|
this.autostartPath = fullCommand;
|
|
|
|
LogAutostartEnabled(this.logger, fullCommand);
|
|
|
|
this.AutostartStatusChanged?.Invoke(this, new AutostartStatusChangedEventArgs(
|
|
true, startMinimized, fullCommand));
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogEnableAutostartFailed(this.logger, ex);
|
|
|
|
this.AutostartStatusChanged?.Invoke(this, new AutostartStatusChangedEventArgs(
|
|
false, startMinimized, null, ex));
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> DisableAutostartAsync()
|
|
{
|
|
try
|
|
{
|
|
if (!this.elevationService.IsRunningAsAdministrator())
|
|
{
|
|
LogAutostartDisableRequiresElevation(this.logger);
|
|
|
|
var elevationRequested = await this.elevationService.RequestElevationIfNeeded();
|
|
if (!elevationRequested)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
LogAutostartDisableDeferredToElevatedInstance(this.logger);
|
|
return false;
|
|
}
|
|
|
|
this.TryRemoveLegacyRegistryAutostart();
|
|
|
|
var scheduledTaskRemoved = await this.elevatedTaskService.RemoveAutostartTaskAsync();
|
|
if (!scheduledTaskRemoved)
|
|
{
|
|
LogAutostartTaskRemovalFailed(this.logger);
|
|
return false;
|
|
}
|
|
|
|
LogAutostartDisabled(this.logger);
|
|
|
|
this.isAutostartEnabled = false;
|
|
this.autostartPath = null;
|
|
|
|
this.AutostartStatusChanged?.Invoke(this, new AutostartStatusChangedEventArgs(false));
|
|
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogDisableAutostartFailed(this.logger, ex);
|
|
|
|
this.AutostartStatusChanged?.Invoke(this, new AutostartStatusChangedEventArgs(
|
|
false, false, null, ex));
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> CheckAutostartStatusAsync()
|
|
{
|
|
try
|
|
{
|
|
var taskRegistered = await this.elevatedTaskService.IsAutostartTaskRegisteredAsync();
|
|
var legacyRegistryValue = this.TryReadLegacyRegistryAutostart();
|
|
|
|
this.isAutostartEnabled = taskRegistered || !string.IsNullOrWhiteSpace(legacyRegistryValue);
|
|
this.autostartPath = taskRegistered
|
|
? $"task://{this.elevatedTaskService.AutostartTaskName}"
|
|
: legacyRegistryValue;
|
|
|
|
LogAutostartStatusChecked(this.logger, this.isAutostartEnabled, this.autostartPath);
|
|
|
|
return this.isAutostartEnabled;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogCheckAutostartStatusFailed(this.logger, ex);
|
|
this.isAutostartEnabled = false;
|
|
this.autostartPath = null;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> UpdateAutostartAsync(bool startMinimized = true)
|
|
{
|
|
if (!this.isAutostartEnabled)
|
|
{
|
|
return await this.EnableAutostartAsync(startMinimized);
|
|
}
|
|
|
|
// Re-enable with new parameters
|
|
await this.DisableAutostartAsync();
|
|
return await this.EnableAutostartAsync(startMinimized);
|
|
}
|
|
|
|
public string GetAutostartArguments(bool startMinimized = true)
|
|
{
|
|
var args = new System.Collections.Generic.List<string>();
|
|
|
|
if (startMinimized)
|
|
{
|
|
args.Add("--start-minimized");
|
|
}
|
|
|
|
// Add any other startup arguments as needed
|
|
args.Add("--autostart");
|
|
|
|
return string.Join(" ", args);
|
|
}
|
|
|
|
private string? GetExecutablePath()
|
|
{
|
|
try
|
|
{
|
|
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
|
|
var location = assembly.Location;
|
|
|
|
// Handle .NET Core/5+ scenarios where Location might be empty
|
|
if (string.IsNullOrEmpty(location))
|
|
{
|
|
location = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName;
|
|
}
|
|
|
|
// If we're running from a .dll, try to find the .exe
|
|
if (!string.IsNullOrEmpty(location) && location.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
var exePath = Path.ChangeExtension(location, ".exe");
|
|
if (File.Exists(exePath))
|
|
{
|
|
location = exePath;
|
|
}
|
|
}
|
|
|
|
return location;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogGetExecutablePathFailed(this.logger, ex);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private string? TryReadLegacyRegistryAutostart()
|
|
{
|
|
try
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(REGISTRYKEYPATH, false);
|
|
return key?.GetValue(APPLICATIONNAME) as string;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogLegacyRegistryReadFailed(this.logger, ex);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private void TryRemoveLegacyRegistryAutostart()
|
|
{
|
|
try
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(REGISTRYKEYPATH, true);
|
|
if (key?.GetValue(APPLICATIONNAME) != null)
|
|
{
|
|
key.DeleteValue(APPLICATIONNAME, false);
|
|
LogLegacyRegistryEntryRemoved(this.logger);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogLegacyRegistryCleanupFailed(this.logger, ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a scheduled task for elevated startup as a backup to registry autostart.
|
|
/// </summary>
|
|
private async Task CreateElevatedStartupTask(string executablePath, string arguments)
|
|
{
|
|
try
|
|
{
|
|
if (!IsValidExecutablePath(executablePath))
|
|
{
|
|
LogSkipElevatedTaskInvalidPath(this.logger, executablePath);
|
|
return;
|
|
}
|
|
|
|
var taskRunCommand = BuildAutostartCommand(executablePath, arguments);
|
|
var result = await RunSchTasksAsync(new List<string>
|
|
{
|
|
"/Create",
|
|
"/TN", SCHEDULEDTASKNAME,
|
|
"/TR", taskRunCommand,
|
|
"/SC", "ONLOGON",
|
|
"/RL", "HIGHEST",
|
|
"/F",
|
|
"/RU", Environment.UserName,
|
|
});
|
|
|
|
if (result.ExitCode == 0)
|
|
{
|
|
LogElevatedStartupTaskCreated(this.logger);
|
|
}
|
|
else
|
|
{
|
|
LogCreateElevatedStartupTaskFailed(this.logger, result.ExitCode, result.StandardError);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogCreateElevatedStartupTaskException(this.logger, ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the elevated startup scheduled task.
|
|
/// </summary>
|
|
private async Task RemoveElevatedStartupTask()
|
|
{
|
|
try
|
|
{
|
|
var result = await RunSchTasksAsync(new List<string>
|
|
{
|
|
"/Delete",
|
|
"/TN", SCHEDULEDTASKNAME,
|
|
"/F",
|
|
});
|
|
|
|
if (result.ExitCode == 0)
|
|
{
|
|
LogElevatedStartupTaskRemoved(this.logger);
|
|
}
|
|
else
|
|
{
|
|
// Task might not exist, which is fine.
|
|
LogScheduledTaskRemovalExitCode(this.logger, result.ExitCode);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogRemoveElevatedStartupTaskException(this.logger, ex);
|
|
}
|
|
}
|
|
|
|
[LoggerMessage(EventId = 4200, Level = LogLevel.Debug, Message = "Autostart status initialization failed")]
|
|
private static partial void LogAutostartInitializationFailed(ILogger logger, Exception ex);
|
|
|
|
[LoggerMessage(EventId = 4201, Level = LogLevel.Error, Message = "Could not determine executable path for autostart")]
|
|
private static partial void LogAutostartExecutablePathMissing(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4202, Level = LogLevel.Error, Message = "Could not open registry key for autostart")]
|
|
private static partial void LogAutostartRegistryKeyMissing(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4203, Level = LogLevel.Information, Message = "Autostart enabled: {Command}")]
|
|
private static partial void LogAutostartEnabled(ILogger logger, string command);
|
|
|
|
[LoggerMessage(EventId = 4204, Level = LogLevel.Error, Message = "Failed to enable autostart")]
|
|
private static partial void LogEnableAutostartFailed(ILogger logger, Exception ex);
|
|
|
|
[LoggerMessage(EventId = 4205, Level = LogLevel.Warning, Message = "Could not open registry key for autostart removal")]
|
|
private static partial void LogAutostartRegistryRemovalKeyMissing(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4206, Level = LogLevel.Information, Message = "Autostart disabled")]
|
|
private static partial void LogAutostartDisabled(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4207, Level = LogLevel.Error, Message = "Failed to disable autostart")]
|
|
private static partial void LogDisableAutostartFailed(ILogger logger, Exception ex);
|
|
|
|
[LoggerMessage(EventId = 4208, Level = LogLevel.Debug, Message = "Autostart status checked: {IsEnabled}, Path: {Path}")]
|
|
private static partial void LogAutostartStatusChecked(ILogger logger, bool isEnabled, string? path);
|
|
|
|
[LoggerMessage(EventId = 4209, Level = LogLevel.Error, Message = "Failed to check autostart status")]
|
|
private static partial void LogCheckAutostartStatusFailed(ILogger logger, Exception ex);
|
|
|
|
[LoggerMessage(EventId = 4210, Level = LogLevel.Error, Message = "Failed to get executable path")]
|
|
private static partial void LogGetExecutablePathFailed(ILogger logger, Exception ex);
|
|
|
|
[LoggerMessage(EventId = 4211, Level = LogLevel.Warning, Message = "Skipping elevated startup task creation due to invalid executable path: {Path}")]
|
|
private static partial void LogSkipElevatedTaskInvalidPath(ILogger logger, string path);
|
|
|
|
[LoggerMessage(EventId = 4212, Level = LogLevel.Information, Message = "Created elevated startup task successfully")]
|
|
private static partial void LogElevatedStartupTaskCreated(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4213, Level = LogLevel.Warning, Message = "Failed to create elevated startup task. Exit code: {ExitCode}, Error: {Error}")]
|
|
private static partial void LogCreateElevatedStartupTaskFailed(ILogger logger, int exitCode, string error);
|
|
|
|
[LoggerMessage(EventId = 4214, Level = LogLevel.Warning, Message = "Failed to create elevated startup task")]
|
|
private static partial void LogCreateElevatedStartupTaskException(ILogger logger, Exception ex);
|
|
|
|
[LoggerMessage(EventId = 4215, Level = LogLevel.Information, Message = "Removed elevated startup task successfully")]
|
|
private static partial void LogElevatedStartupTaskRemoved(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4216, Level = LogLevel.Debug, Message = "Scheduled task removal completed with exit code: {ExitCode}")]
|
|
private static partial void LogScheduledTaskRemovalExitCode(ILogger logger, int exitCode);
|
|
|
|
[LoggerMessage(EventId = 4217, Level = LogLevel.Warning, Message = "Failed to remove elevated startup task")]
|
|
private static partial void LogRemoveElevatedStartupTaskException(ILogger logger, Exception ex);
|
|
|
|
[LoggerMessage(EventId = 4218, Level = LogLevel.Information, Message = "Autostart configuration requires elevation; requesting elevated restart")]
|
|
private static partial void LogAutostartRequiresElevation(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4219, Level = LogLevel.Information, Message = "Autostart enable request delegated to elevated instance")]
|
|
private static partial void LogAutostartDeferredToElevatedInstance(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4220, Level = LogLevel.Warning, Message = "Failed to register elevated autostart task")]
|
|
private static partial void LogAutostartTaskRegistrationFailed(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4221, Level = LogLevel.Information, Message = "Autostart disable requires elevation; requesting elevated restart")]
|
|
private static partial void LogAutostartDisableRequiresElevation(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4222, Level = LogLevel.Information, Message = "Autostart disable request delegated to elevated instance")]
|
|
private static partial void LogAutostartDisableDeferredToElevatedInstance(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4223, Level = LogLevel.Warning, Message = "Failed to remove elevated autostart task")]
|
|
private static partial void LogAutostartTaskRemovalFailed(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4224, Level = LogLevel.Debug, Message = "Legacy HKCU Run autostart value removed")]
|
|
private static partial void LogLegacyRegistryEntryRemoved(ILogger logger);
|
|
|
|
[LoggerMessage(EventId = 4225, Level = LogLevel.Debug, Message = "Failed to read legacy HKCU Run autostart value")]
|
|
private static partial void LogLegacyRegistryReadFailed(ILogger logger, Exception ex);
|
|
|
|
[LoggerMessage(EventId = 4226, Level = LogLevel.Debug, Message = "Failed to remove legacy HKCU Run autostart value")]
|
|
private static partial void LogLegacyRegistryCleanupFailed(ILogger logger, Exception ex);
|
|
|
|
private static bool IsValidExecutablePath(string executablePath)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(executablePath) || !Path.IsPathRooted(executablePath))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return File.Exists(executablePath) &&
|
|
string.Equals(Path.GetExtension(executablePath), ".exe", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private static string BuildAutostartCommand(string executablePath, string arguments)
|
|
{
|
|
var trimmedArguments = arguments?.Trim();
|
|
return string.IsNullOrWhiteSpace(trimmedArguments)
|
|
? $"\"{executablePath}\""
|
|
: $"\"{executablePath}\" {trimmedArguments}";
|
|
}
|
|
|
|
private static async Task<ProcessResult> RunSchTasksAsync(IReadOnlyList<string> arguments)
|
|
{
|
|
if (!File.Exists(SchTasksExecutablePath))
|
|
{
|
|
return new ProcessResult(-1, string.Empty, "schtasks.exe not found in system directory");
|
|
}
|
|
|
|
var processInfo = new ProcessStartInfo
|
|
{
|
|
FileName = SchTasksExecutablePath,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
};
|
|
|
|
foreach (var argument in arguments)
|
|
{
|
|
processInfo.ArgumentList.Add(argument);
|
|
}
|
|
|
|
using var process = Process.Start(processInfo);
|
|
if (process == null)
|
|
{
|
|
return new ProcessResult(-1, string.Empty, "Could not start schtasks.exe");
|
|
}
|
|
|
|
var outputTask = process.StandardOutput.ReadToEndAsync();
|
|
var errorTask = process.StandardError.ReadToEndAsync();
|
|
var exitTask = process.WaitForExitAsync();
|
|
var completedTask = await Task.WhenAny(exitTask, Task.Delay(ScheduledTaskTimeout));
|
|
if (completedTask != exitTask)
|
|
{
|
|
try
|
|
{
|
|
process.Kill(entireProcessTree: true);
|
|
}
|
|
catch
|
|
{
|
|
// Best-effort kill for timeout.
|
|
}
|
|
|
|
return new ProcessResult(-1, await outputTask, $"schtasks timeout after {ScheduledTaskTimeout.TotalSeconds} seconds");
|
|
}
|
|
|
|
await exitTask;
|
|
return new ProcessResult(process.ExitCode, await outputTask, await errorTask);
|
|
}
|
|
|
|
private readonly struct ProcessResult
|
|
{
|
|
public ProcessResult(int exitCode, string standardOutput, string standardError)
|
|
{
|
|
this.ExitCode = exitCode;
|
|
this.StandardOutput = standardOutput;
|
|
this.StandardError = standardError;
|
|
}
|
|
|
|
public int ExitCode { get; }
|
|
|
|
public string StandardOutput { get; }
|
|
|
|
public string StandardError { get; }
|
|
}
|
|
}
|
|
}
|
|
|