LD-FDPI/scripts/download-sidecar.js

117 lines
3.8 KiB
JavaScript

/**
* LD FDPI — fetch the *real* SpoofDPI Windows x64 engine.
*
* There is no stub branch: this script either installs a verified official binary
* (SpoofDPI 0.12.2, the last release that ships a Windows build) or exits with an
* error. The old version silently wrote a 32-byte "MZ-...-STUB" file, which made
* `tauri build` succeed while the application could never bypass anything.
*
* Usage: npm run setup:sidecar [-- --force]
*/
import fs from "node:fs";
import path from "node:path";
import crypto from "node:crypto";
import { execFileSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(__dirname, "..");
const BIN_DIR = path.join(ROOT, "src-tauri", "binaries");
const VERSION = "0.12.2";
const ASSET_URL = `https://github.com/xvzc/spoofdpi/releases/download/v${VERSION}/spoofdpi-windows-amd64.exe`;
// SHA-256 of the official release asset (computed from the downloaded v0.12.2 exe).
const SHA256 = "16a5c81265d908d31aaf9be69d577ec91588282ae5c366cc73e8bff878a3ea5e";
const MIN_SIZE = 1_500_000;
const TARGETS = [
path.join(BIN_DIR, "spoofdpi-x86_64-pc-windows-gnu.exe"),
path.join(BIN_DIR, "spoofdpi-x86_64-pc-windows-msvc.exe"),
];
const force = process.argv.includes("--force");
function sha256(file) {
return crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
}
function isValid(file) {
try {
const stat = fs.statSync(file);
if (!stat.isFile() || stat.size < MIN_SIZE) return false;
const fd = fs.openSync(file, "r");
const header = Buffer.alloc(2);
fs.readSync(fd, header, 0, 2, 0);
fs.closeSync(fd);
return header[0] === 0x4d && header[1] === 0x5a && sha256(file) === SHA256;
} catch {
return false;
}
}
function printVersion(file) {
try {
execFileSync(file, ["-v"], { stdio: "inherit", windowsHide: true });
return true;
} catch (error) {
console.error("[LD FDPI] Binary could not be executed:", error.message);
return false;
}
}
fs.mkdirSync(BIN_DIR, { recursive: true });
fs.writeFileSync(path.join(BIN_DIR, ".gitkeep"), "");
if (!force && TARGETS.every(isValid)) {
console.log("[LD FDPI] Verified SpoofDPI", VERSION, "is already installed.");
printVersion(TARGETS[0]);
process.exit(0);
}
for (const existing of TARGETS) {
if (fs.existsSync(existing) && fs.statSync(existing).size < MIN_SIZE) {
console.warn("[LD FDPI] Removing placeholder sidecar:", existing);
fs.unlinkSync(existing);
}
}
const tempFile = path.join(BIN_DIR, `spoofdpi-${VERSION}.download.exe`);
console.log("[LD FDPI] Downloading official SpoofDPI", VERSION, "Windows x64 engine...");
console.log(" ", ASSET_URL);
const response = await fetch(ASSET_URL, { redirect: "follow" });
if (!response.ok) {
console.error(`[LD FDPI] Download failed: HTTP ${response.status} ${response.statusText}`);
process.exit(1);
}
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync(tempFile, buffer);
const digest = sha256(tempFile);
if (digest !== SHA256) {
fs.unlinkSync(tempFile);
console.error("[LD FDPI] SHA-256 mismatch — refusing to install an unverified binary.");
console.error(" expected:", SHA256);
console.error(" actual: ", digest);
process.exit(1);
}
if (buffer.length < MIN_SIZE || buffer[0] !== 0x4d || buffer[1] !== 0x5a) {
fs.unlinkSync(tempFile);
console.error("[LD FDPI] Downloaded file is not a valid Windows PE binary.");
process.exit(1);
}
for (const target of TARGETS) {
fs.copyFileSync(tempFile, target);
}
fs.unlinkSync(tempFile);
console.log("[LD FDPI] Installed verified engine (SHA-256 OK,", buffer.length, "bytes):");
for (const target of TARGETS) {
console.log(" ", target);
}
if (!printVersion(TARGETS[0])) {
process.exit(1);
}
console.log("[LD FDPI] Sidecar ready.");