77 lines
2.3 KiB
C#
77 lines
2.3 KiB
C#
using System.Diagnostics;
|
|
using System.IO.Compression;
|
|
using System.Reflection;
|
|
|
|
namespace OCSentinelBootstrapper;
|
|
|
|
internal static class Program
|
|
{
|
|
private static int Main()
|
|
{
|
|
string tempRoot = Path.Combine(Path.GetTempPath(), "OCSentinelSetup", Guid.NewGuid().ToString("N"));
|
|
string zipPath = Path.Combine(tempRoot, "payload.zip");
|
|
string extractRoot = Path.Combine(tempRoot, "payload");
|
|
|
|
try
|
|
{
|
|
Directory.CreateDirectory(tempRoot);
|
|
Directory.CreateDirectory(extractRoot);
|
|
|
|
ExtractEmbeddedPayload(zipPath);
|
|
ZipFile.ExtractToDirectory(zipPath, extractRoot, overwriteFiles: true);
|
|
|
|
string installScript = Path.Combine(extractRoot, "scripts", "install-ocsentinel.ps1");
|
|
if (!File.Exists(installScript))
|
|
{
|
|
throw new FileNotFoundException("Embedded payload did not contain install-ocsentinel.ps1", installScript);
|
|
}
|
|
|
|
var process = new Process
|
|
{
|
|
StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "powershell.exe",
|
|
Arguments = $"-ExecutionPolicy Bypass -File \"{installScript}\"",
|
|
UseShellExecute = false
|
|
}
|
|
};
|
|
|
|
process.Start();
|
|
process.WaitForExit();
|
|
return process.ExitCode;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Error.WriteLine($"OCSentinel installer failed: {ex}");
|
|
return 1;
|
|
}
|
|
finally
|
|
{
|
|
try
|
|
{
|
|
if (Directory.Exists(tempRoot))
|
|
{
|
|
Directory.Delete(tempRoot, recursive: true);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Best-effort cleanup only.
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void ExtractEmbeddedPayload(string destinationPath)
|
|
{
|
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
using Stream? resourceStream = assembly.GetManifestResourceStream("payload.zip");
|
|
if (resourceStream is null)
|
|
{
|
|
throw new InvalidOperationException("Embedded payload.zip resource was not found.");
|
|
}
|
|
|
|
using FileStream output = File.Create(destinationPath);
|
|
resourceStream.CopyTo(output);
|
|
}
|
|
}
|