2025-11-23 20:54:56 -06:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Security.Principal;
|
|
|
|
|
using CommandLine;
|
|
|
|
|
using LANCommander.Launcher.Models;
|
|
|
|
|
using LANCommander.SDK;
|
|
|
|
|
using LANCommander.SDK.Enums;
|
|
|
|
|
using LANCommander.SDK.PowerShell;
|
|
|
|
|
|
|
|
|
|
namespace LANCommander.Launcher.Services;
|
|
|
|
|
|
2025-11-26 01:09:52 -06:00
|
|
|
public class ElevatedScriptInterceptor : IScriptInterceptor
|
2025-11-23 20:54:56 -06:00
|
|
|
{
|
|
|
|
|
public async Task<bool> ExecuteAsync(PowerShellScript script)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bool isElevated = false;
|
|
|
|
|
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
{
|
|
|
|
|
var identity = WindowsIdentity.GetCurrent();
|
|
|
|
|
var principal = new WindowsPrincipal(identity);
|
|
|
|
|
|
|
|
|
|
isElevated = principal.IsInRole(WindowsBuiltInRole.Administrator);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
isElevated = Environment.UserName == "root";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (script.RunAsAdmin && !isElevated)
|
|
|
|
|
{
|
2025-11-29 18:24:27 -06:00
|
|
|
var manifest = script.Variables.GetValue<SDK.Models.Manifest.Game>("GameManifest");
|
2025-11-23 20:54:56 -06:00
|
|
|
|
|
|
|
|
var options = new RunScriptCommandLineOptions
|
|
|
|
|
{
|
|
|
|
|
InstallDirectory = script.Variables.GetValue<string>("InstallDirectory"),
|
|
|
|
|
GameId = manifest.Id,
|
|
|
|
|
Type = script.Type,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (script.Type == ScriptType.KeyChange)
|
|
|
|
|
options.AllocatedKey = script.Variables.GetValue<string>("AllocatedKey");
|
|
|
|
|
|
|
|
|
|
if (script.Type == ScriptType.NameChange)
|
|
|
|
|
{
|
|
|
|
|
options.OldPlayerAlias = script.Variables.GetValue<string>("OldPlayerAlias");
|
|
|
|
|
options.NewPlayerAlias = script.Variables.GetValue<string>("NewPlayerAlias");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var arguments = Parser.Default.FormatCommandLine(options);
|
|
|
|
|
var path = Process.GetCurrentProcess().MainModule!.FileName;
|
|
|
|
|
|
|
|
|
|
var process = new Process();
|
|
|
|
|
|
|
|
|
|
process.StartInfo.FileName = path;
|
|
|
|
|
process.StartInfo.Verb = "runas";
|
|
|
|
|
process.StartInfo.UseShellExecute = true;
|
|
|
|
|
process.StartInfo.WorkingDirectory = script.WorkingDirectory;
|
|
|
|
|
process.StartInfo.Arguments = arguments;
|
|
|
|
|
|
|
|
|
|
process.Start();
|
|
|
|
|
|
|
|
|
|
await process.WaitForExitAsync();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// Not running as admin
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|