100 lines
3.6 KiB
JavaScript
100 lines
3.6 KiB
JavaScript
/**
|
||
* Replaces the Windows application manifest of the built executable.
|
||
*
|
||
* Why this is needed: with the GNU toolchain the MinGW CRT embeds its own
|
||
* `asInvoker` manifest (RT_MANIFEST id 1) in addition to the one generated by
|
||
* `tauri-winres`. GNU ld reports "multiple non-default manifests" and Windows
|
||
* ends up honouring the CRT's asInvoker manifest, so `requireAdministrator`
|
||
* never takes effect. `rcedit` rewrites the resource table with exactly one
|
||
* manifest, which makes UAC work.
|
||
*
|
||
* Runs automatically from `scripts/tauri.js` after `tauri build`; skipped for
|
||
* the `LD_FDPI_NO_ADMIN=1` test build.
|
||
*/
|
||
import crypto from "node:crypto";
|
||
import fs from "node:fs";
|
||
import os from "node:os";
|
||
import path from "node:path";
|
||
import { spawnSync } from "node:child_process";
|
||
import { ROOT, resolveTargetDir } from "./target-dir.js";
|
||
|
||
const RCEDIT_VERSION = "2.0.0";
|
||
const RCEDIT_URL = `https://github.com/electron/rcedit/releases/download/v${RCEDIT_VERSION}/rcedit-x64.exe`;
|
||
const RCEDIT_SHA256 = "3e7801db1a5edbec91b49a24a094aad776cb4515488ea5a4ca2289c400eade2a";
|
||
const TOOLS_DIR = path.join(
|
||
process.env.LOCALAPPDATA || os.tmpdir(),
|
||
"LD-FDPI",
|
||
"build-tools"
|
||
);
|
||
|
||
function sha256(file) {
|
||
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
|
||
}
|
||
|
||
async function ensureRcedit() {
|
||
const cached = path.join(TOOLS_DIR, "rcedit-x64.exe");
|
||
if (fs.existsSync(cached) && sha256(cached) === RCEDIT_SHA256) {
|
||
return cached;
|
||
}
|
||
fs.mkdirSync(TOOLS_DIR, { recursive: true });
|
||
console.log("[LD FDPI] Downloading rcedit (manifest patcher)...");
|
||
const response = await fetch(RCEDIT_URL, { redirect: "follow" });
|
||
if (!response.ok) {
|
||
throw new Error(`rcedit indirilemedi: HTTP ${response.status}`);
|
||
}
|
||
const buffer = Buffer.from(await response.arrayBuffer());
|
||
const digest = crypto.createHash("sha256").update(buffer).digest("hex");
|
||
if (digest !== RCEDIT_SHA256) {
|
||
throw new Error(`rcedit SHA-256 uyuşmuyor (${digest})`);
|
||
}
|
||
fs.writeFileSync(cached, buffer);
|
||
return cached;
|
||
}
|
||
|
||
export async function patchManifest() {
|
||
const exe = path.join(resolveTargetDir(), "release", "ld-fdpi.exe");
|
||
if (!fs.existsSync(exe)) {
|
||
throw new Error(`Derlenmiş uygulama bulunamadı: ${exe}`);
|
||
}
|
||
|
||
const manifest = path.join(ROOT, "src-tauri", "windows-app-manifest.xml");
|
||
if (!fs.existsSync(manifest)) {
|
||
throw new Error(`Manifest bulunamadı: ${manifest}`);
|
||
}
|
||
|
||
const before = fs.readFileSync(exe);
|
||
if (before.includes(Buffer.from("asInvoker"))) {
|
||
const rcedit = await ensureRcedit();
|
||
const ico = path.join(ROOT, "src-tauri", "icons", "icon.ico");
|
||
const rceditArgs = [exe, "--application-manifest", manifest];
|
||
if (fs.existsSync(ico)) {
|
||
rceditArgs.push("--set-icon", ico);
|
||
}
|
||
const result = spawnSync(rcedit, rceditArgs, {
|
||
stdio: "inherit",
|
||
windowsHide: true,
|
||
});
|
||
if (result.status !== 0) {
|
||
throw new Error(`rcedit başarısız (çıkış kodu ${result.status})`);
|
||
}
|
||
}
|
||
|
||
const after = fs.readFileSync(exe);
|
||
const hasAdmin = after.includes(Buffer.from("requireAdministrator"));
|
||
const hasInvoker = after.includes(Buffer.from("asInvoker"));
|
||
if (!hasAdmin || hasInvoker) {
|
||
throw new Error(
|
||
`Manifest doğrulanamadı (requireAdministrator=${hasAdmin}, asInvoker=${hasInvoker}).`
|
||
);
|
||
}
|
||
console.log("[LD FDPI] UAC manifest doğrulandı: requireAdministrator");
|
||
return exe;
|
||
}
|
||
|
||
// Allow direct invocation: node scripts/patch-manifest.mjs
|
||
if (import.meta.url === `file://${process.argv[1].replace(/\\/g, "/")}`) {
|
||
patchManifest().catch((error) => {
|
||
console.error("[LD FDPI]", error.message);
|
||
process.exit(1);
|
||
});
|
||
}
|