feat(launcher): support signed in-place updates for portable builds
This commit is contained in:
1 parent
e284ea7ed6
commit
82ec1c47ff
8 files changed
+477
-46
No files matched your search
@@ -1,9 +1,9 @@
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { cp, lstat, mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises';
|
||||
import { cp, lstat, mkdir, mkdtemp, readdir, readFile, realpath, rm, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { dirname, join, relative, resolve, sep } from 'node:path';
|
||||
import { Arch, build, Platform } from 'electron-builder';
|
||||
import { launcherReleaseFiles } from '../apps/launcher/src/shared/schemas/app-update.ts';
|
||||
import { launcherReleaseFiles, portableLauncherReleaseFiles } from '../apps/launcher/src/shared/schemas/app-update.ts';
|
||||
import { formatManifest, MANIFEST_FILE, PATCH_PACK_FORMAT, recordedGameVersions } from '../packages/patcher/src/index.ts';
|
||||
|
||||
const repository = resolve(import.meta.dirname, '..');
|
||||
@@ -97,9 +97,13 @@ async function cleanInstallerOutput(version: string): Promise<void> {
|
||||
if (relative(output, root) !== '') throw new Error('The installer output directory is redirected.');
|
||||
|
||||
const { installer, manifest, signature } = launcherReleaseFiles(version);
|
||||
const portable = `wtoffline-launcher-${version}-win-x64-portable.zip`;
|
||||
const portable = portableLauncherReleaseFiles(version);
|
||||
const legacy = `WTOffline Launcher Setup ${version}.exe`;
|
||||
const names = [installer, installer + '.blockmap', portable, legacy, legacy + '.blockmap', manifest, signature, 'win-unpacked'];
|
||||
const names = [
|
||||
installer, installer + '.blockmap',
|
||||
portable.archive, portable.manifest, portable.signature,
|
||||
legacy, legacy + '.blockmap', manifest, signature, 'win-unpacked',
|
||||
];
|
||||
const artifacts = [];
|
||||
for (const name of names) {
|
||||
const path = join(root, name);
|
||||
@@ -161,17 +165,55 @@ async function main(): Promise<void> {
|
||||
},
|
||||
});
|
||||
for (const artifact of artifacts) console.info(artifact);
|
||||
const portable = join(output, `wtoffline-launcher-${manifest.version}-win-x64-portable.zip`);
|
||||
const unpackedGlob = join(output, 'win-unpacked', '*');
|
||||
execFileSync('powershell.exe', [
|
||||
'-NoProfile', '-NonInteractive', '-Command',
|
||||
`Compress-Archive -Path "${unpackedGlob}" -DestinationPath "${portable}" -Force`,
|
||||
], { cwd: repository, stdio: 'inherit', windowsHide: true });
|
||||
console.info(portable);
|
||||
const portablePath = await packagePortableArchive(output, manifest.version);
|
||||
console.info(portablePath);
|
||||
} finally {
|
||||
const actual = await realpath(stage);
|
||||
if (actual === stage && dirname(actual) === temp) await rm(stage, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function listRelativeFiles(root: string): Promise<string[]> {
|
||||
const files: string[] = [];
|
||||
const pending = [root];
|
||||
while (pending.length) {
|
||||
const folder = pending.pop()!;
|
||||
for (const entry of await readdir(folder, { withFileTypes: true })) {
|
||||
const fullPath = join(folder, entry.name);
|
||||
if (entry.isDirectory()) pending.push(fullPath);
|
||||
else if (entry.isFile()) files.push(relative(root, fullPath).split(sep).join('/'));
|
||||
}
|
||||
}
|
||||
return files.sort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Package win-unpacked into the portable archive with a portable marker and an inventory of
|
||||
* launcher-owned files so portable in-place updates only replace launcher files and never remove
|
||||
* unrelated user files in the extraction folder.
|
||||
*/
|
||||
async function packagePortableArchive(outputFolder: string, version: string): Promise<string> {
|
||||
const unpacked = join(outputFolder, 'win-unpacked');
|
||||
const resources = join(unpacked, 'resources');
|
||||
const markerPath = join(resources, 'portable-mode');
|
||||
const inventoryPath = join(resources, 'portable-files.json');
|
||||
const portable = join(outputFolder, portableLauncherReleaseFiles(version).archive);
|
||||
try {
|
||||
await writeFile(markerPath, 'portable\n', 'utf8');
|
||||
await writeFile(inventoryPath, '', 'utf8');
|
||||
const ownedFiles = await listRelativeFiles(unpacked);
|
||||
await writeFile(inventoryPath, `${JSON.stringify({ format: 1, version, files: ownedFiles }, null, 2)}\n`, 'utf8');
|
||||
const unpackedGlob = join(unpacked, '*').replaceAll("'", "''");
|
||||
const destination = portable.replaceAll("'", "''");
|
||||
execFileSync('powershell.exe', [
|
||||
'-NoProfile', '-NonInteractive', '-Command',
|
||||
`Compress-Archive -Path '${unpackedGlob}' -DestinationPath '${destination}' -Force`,
|
||||
], { cwd: repository, stdio: 'inherit', windowsHide: true });
|
||||
return portable;
|
||||
} finally {
|
||||
await rm(markerPath, { force: true }).catch(() => undefined);
|
||||
await rm(inventoryPath, { force: true }).catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
await main();
|
||||
@@ -1,15 +1,21 @@
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
import { access, readFile, writeFile } from 'node:fs/promises';
|
||||
import { dirname, join, resolve } from 'node:path';
|
||||
import { packSigningPublicKey, signEd25519, verifyEd25519 } from '@wto/patcher';
|
||||
import { installerDigest, launcherSignaturePayload } from '../apps/launcher/src/main/services/app-update/installer-integrity.ts';
|
||||
import { launcherReleaseFiles, LauncherReleaseManifest } from '../apps/launcher/src/shared/schemas/app-update.ts';
|
||||
import {
|
||||
installerDigest, launcherSignaturePayload, portableSignaturePayload,
|
||||
} from '../apps/launcher/src/main/services/app-update/installer-integrity.ts';
|
||||
import {
|
||||
launcherReleaseFiles, LauncherReleaseManifest,
|
||||
portableLauncherReleaseFiles, PortableReleaseManifest,
|
||||
} from '../apps/launcher/src/shared/schemas/app-update.ts';
|
||||
|
||||
const repository = resolve(import.meta.dirname, '..');
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const { version } = JSON.parse(await readFile(join(repository, 'apps', 'launcher', 'package.json'), 'utf8')) as { version: string };
|
||||
const files = launcherReleaseFiles(version);
|
||||
const installer = join(repository, 'build', 'windows-installer', files.installer);
|
||||
const outputFolder = join(repository, 'build', 'windows-installer');
|
||||
const installer = join(outputFolder, files.installer);
|
||||
const privateKey = process.env['PATCH_SIGNING_KEY'];
|
||||
const publicKey = packSigningPublicKey();
|
||||
if (!privateKey || !publicKey) throw new Error('The project signing key pair is required.');
|
||||
@@ -29,6 +35,38 @@ async function main(): Promise<void> {
|
||||
await writeFile(join(folder, files.manifest), bytes);
|
||||
await writeFile(join(folder, files.signature), signature, 'utf8');
|
||||
for (const file of Object.values(files)) console.info(join(folder, file));
|
||||
|
||||
// Sign the portable archive with a separate manifest so older launchers whose installer manifest
|
||||
// schema is strict keep validating releases without error.
|
||||
const portableFiles = portableLauncherReleaseFiles(version);
|
||||
const portableArchive = join(folder, portableFiles.archive);
|
||||
if (await fileExists(portableArchive)) {
|
||||
const portableManifest = PortableReleaseManifest.parse({
|
||||
kind: 'wto-launcher-portable-release',
|
||||
format: 1,
|
||||
platform: 'win32-x64',
|
||||
version,
|
||||
...await installerDigest(portableArchive),
|
||||
});
|
||||
const portableBytes = Buffer.from(JSON.stringify(portableManifest, null, 2) + '\n', 'utf8');
|
||||
const portablePayload = portableSignaturePayload(portableBytes);
|
||||
const portableSig = signEd25519(portablePayload, privateKey);
|
||||
if (!verifyEd25519(portablePayload, portableSig, publicKey)) {
|
||||
throw new Error('The portable release key does not match the bundled public key.');
|
||||
}
|
||||
await writeFile(join(folder, portableFiles.manifest), portableBytes);
|
||||
await writeFile(join(folder, portableFiles.signature), portableSig, 'utf8');
|
||||
for (const file of Object.values(portableFiles)) console.info(join(folder, file));
|
||||
}
|
||||
}
|
||||
|
||||
async function fileExists(path: string): Promise<boolean> {
|
||||
try {
|
||||
await access(path);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
await main();
|
||||
Reference in new issue
Block a user