Sign stub executable and rename it to --packTitle

This commit is contained in:
Caelan Sayler
2024-01-26 12:07:17 +00:00
parent cbaf951872
commit 04cf01cc87
2 changed files with 27 additions and 20 deletions

View File

@@ -11,11 +11,10 @@ use std::{
os::windows::process::CommandExt,
process::{Command as Process, ExitCode},
};
use windows::Win32::UI::WindowsAndMessaging::AllowSetForegroundWindow;
fn main() -> ExitCode {
let my_path = std::env::current_exe().unwrap();
let my_name = my_path.file_name().unwrap().to_string_lossy();
let default_log_file = {
let mut my_dir = env::current_exe().unwrap();
my_dir.pop();
@@ -35,16 +34,18 @@ fn main() -> ExitCode {
let mut args: Vec<String> = std::env::args().skip(1).collect();
args.insert(0, "start".to_owned());
args.insert(1, my_name.to_string());
args.insert(2, "--".to_owned());
args.insert(1, "--".to_owned());
info!("Stub {} about to start Update.exe ({}) with args: {:?}", my_name, update_exe.to_string_lossy(), args);
info!("Stub about to start Update.exe ({}) with args: {:?}", update_exe.to_string_lossy(), args);
const CREATE_NO_WINDOW: u32 = 0x08000000;
if let Err(e) = Process::new(update_exe).args(args).creation_flags(CREATE_NO_WINDOW).spawn() {
error!("Stub failed to start Update.exe: {}", e);
return ExitCode::FAILURE;
match Process::new(update_exe).args(args).creation_flags(CREATE_NO_WINDOW).spawn() {
Ok(res) => {
let _ = unsafe { AllowSetForegroundWindow(res.id()) };
ExitCode::SUCCESS
}, Err(e) => {
error!("Stub failed to start Update.exe: {}", e);
ExitCode::FAILURE
}
}
ExitCode::SUCCESS
}

View File

@@ -1,6 +1,4 @@
using System.Runtime.Versioning;
using AsmResolver.PE;
using AsmResolver.PE.File.Headers;
using Microsoft.Extensions.Logging;
using Velopack.Compression;
using Velopack.NuGet;
@@ -57,6 +55,12 @@ public class WindowsPackCommandRunner : PackageBuilder<WindowsPackOptions>
}
File.Copy(updatePath, Path.Combine(packDir, "Squirrel.exe"), true);
// create a stub for portable packages
var mainPath = Path.Combine(packDir, MainExeName);
var stubPath = Path.Combine(packDir, Path.GetFileNameWithoutExtension(MainExeName) + "_ExecutionStub.exe");
CreateExecutableStubForExe(mainPath, stubPath);
return Task.FromResult(packDir);
}
@@ -155,9 +159,12 @@ public class WindowsPackCommandRunner : PackageBuilder<WindowsPackOptions>
CopyFiles(new DirectoryInfo(packDir), current, Utility.CreateProgressDelegate(progress, 0, 30));
var mainExeName = Options.EntryExecutableName ?? Options.PackId + ".exe";
var mainExe = Path.Combine(packDir, mainExeName);
CreateExecutableStubForExe(mainExe, dir.FullName);
File.Delete(Path.Combine(current.FullName, "Squirrel.exe"));
// move the stub to the root of the portable package
var stubPath = Path.Combine(current.FullName, Path.GetFileNameWithoutExtension(MainExeName) + "_ExecutionStub.exe");
var stubName = (Options.PackTitle ?? Options.PackId) + ".exe";
File.Move(stubPath, Path.Combine(dir.FullName, stubName));
await EasyZip.CreateZipFromDirectoryAsync(Log, outputPath, dir.FullName, Utility.CreateProgressDelegate(progress, 40, 100));
progress(100);
@@ -171,18 +178,17 @@ public class WindowsPackCommandRunner : PackageBuilder<WindowsPackOptions>
return dict;
}
private void CreateExecutableStubForExe(string exeToCopy, string targetDir)
private void CreateExecutableStubForExe(string exeToCopy, string targetStubPath)
{
try {
var target = Path.Combine(targetDir, Path.GetFileName(exeToCopy));
Utility.Retry(() => File.Copy(HelperFile.StubExecutablePath, target, true));
Utility.Retry(() => File.Copy(HelperFile.StubExecutablePath, targetStubPath, true));
Utility.Retry(() => {
if (VelopackRuntimeInfo.IsWindows) {
using var writer = new Microsoft.NET.HostModel.ResourceUpdater(target, true);
using var writer = new Microsoft.NET.HostModel.ResourceUpdater(targetStubPath, true);
writer.AddResourcesFromPEImage(exeToCopy);
writer.Update();
} else {
Log.Warn($"Cannot set resources/icon for {target} (only supported on windows).");
Log.Warn($"Cannot set resources/icon for {targetStubPath} (only supported on windows).");
}
});
} catch (Exception ex) {