Release v2.1.65: safe MCP updates and measured upload scheduling
CI / verify (push) Canceled after 0s
CI / verify (push) Canceled after 0s
This commit is contained in:
1 parent
e3c03d195e
commit
1ff552fc40
26 files changed
+552
-92
No files matched your search
+53
-22
@@ -232,12 +232,6 @@ async function parseLatestYml(url, expected = {}, fetchImpl = fetch) {
|
||||
}
|
||||
}
|
||||
|
||||
function verifyExeHeader(buf) {
|
||||
// Check MZ header
|
||||
if (buf.length < 128 * 1024) return false;
|
||||
return buf[0] === 0x4D && buf[1] === 0x5A; // 'MZ'
|
||||
}
|
||||
|
||||
async function prepareUpdate(onProgress, options = {}) {
|
||||
if (activeAbort) activeAbort.abort();
|
||||
const controller = new AbortController();
|
||||
@@ -246,6 +240,7 @@ async function prepareUpdate(onProgress, options = {}) {
|
||||
const fetchImpl = options.fetchImpl || fetch;
|
||||
const now = typeof options.now === 'function' ? options.now : Date.now;
|
||||
let stagedInstallerPath = '';
|
||||
let downloadDirectory = '';
|
||||
|
||||
try {
|
||||
// Stage: starting
|
||||
@@ -273,9 +268,12 @@ async function prepareUpdate(onProgress, options = {}) {
|
||||
|
||||
// Stage: downloading
|
||||
const tmpDir = options.tempDir || app.getPath('temp');
|
||||
const installerPath = path.join(tmpDir, check.assetName);
|
||||
downloadDirectory = await fs.promises.mkdtemp(path.join(tmpDir, 'mhu-update-'));
|
||||
const installerPath = path.join(downloadDirectory, path.basename(check.assetName));
|
||||
stagedInstallerPath = installerPath;
|
||||
|
||||
let fileBuffer;
|
||||
let actualSha;
|
||||
let firstBytes;
|
||||
for (let attempt = 1; attempt <= 3; attempt++) {
|
||||
signal.throwIfAborted();
|
||||
const downloadController = new AbortController();
|
||||
@@ -307,7 +305,10 @@ async function prepareUpdate(onProgress, options = {}) {
|
||||
let downloadedBytes = 0;
|
||||
let lastReportedPercent = -1;
|
||||
const downloadStartedAt = now();
|
||||
const chunks = [];
|
||||
const output = await fs.promises.open(installerPath, 'w', 0o600);
|
||||
const hash = crypto.createHash('sha512');
|
||||
firstBytes = Buffer.alloc(0);
|
||||
try {
|
||||
if (attempt > 1 && onProgress) onProgress({ stage: 'downloading', percent: 0, bytesDownloaded: 0, bytesTotal: manifest.size, bytesPerSecond: 0, etaSeconds: null, attempt });
|
||||
while (true) {
|
||||
signal.throwIfAborted();
|
||||
@@ -317,7 +318,9 @@ async function prepareUpdate(onProgress, options = {}) {
|
||||
if (done) break;
|
||||
downloadedBytes += value.length;
|
||||
if (downloadedBytes > manifest.size) throw new Error('Heruntergeladene Datei hat eine abweichende Größe');
|
||||
chunks.push(value);
|
||||
if (firstBytes.length < 2) firstBytes = Buffer.concat([firstBytes, Buffer.from(value).subarray(0, 2 - firstBytes.length)]);
|
||||
hash.update(value);
|
||||
await output.writeFile(value);
|
||||
const percent = Math.floor((downloadedBytes / manifest.size) * 100);
|
||||
if (onProgress && percent !== lastReportedPercent) {
|
||||
const elapsedMs = Math.max(1, now() - downloadStartedAt);
|
||||
@@ -334,7 +337,9 @@ async function prepareUpdate(onProgress, options = {}) {
|
||||
error.retryable = true;
|
||||
throw error;
|
||||
}
|
||||
fileBuffer = Buffer.concat(chunks);
|
||||
await output.sync();
|
||||
actualSha = hash.digest('base64');
|
||||
} finally { await output.close(); }
|
||||
} catch (error) {
|
||||
signal.throwIfAborted();
|
||||
retry = timedOut || error.retryable === true || ['UND_ERR_SOCKET', 'ECONNRESET', 'ETIMEDOUT', 'EPIPE', 'UND_ERR_CONNECT_TIMEOUT', 'UND_ERR_BODY_TIMEOUT'].includes(error.cause?.code || error.code) || /^(terminated|fetch failed)$/i.test(error.message);
|
||||
@@ -356,36 +361,35 @@ async function prepareUpdate(onProgress, options = {}) {
|
||||
// Stage: verifying
|
||||
if (onProgress) onProgress({ stage: 'verifying', percent: 0 });
|
||||
|
||||
if (!verifyExeHeader(fileBuffer)) {
|
||||
if (manifest.size < 128 * 1024 || firstBytes[0] !== 0x4d || firstBytes[1] !== 0x5a) {
|
||||
throw new Error('Heruntergeladene Datei ist keine gültige EXE');
|
||||
}
|
||||
|
||||
const actualSha = crypto.createHash('sha512').update(fileBuffer).digest('base64');
|
||||
if (actualSha !== expectedSha) {
|
||||
const actualHex = crypto.createHash('sha512').update(fileBuffer).digest('hex');
|
||||
const actualHex = Buffer.from(actualSha, 'base64').toString('hex');
|
||||
if (actualHex !== expectedSha.toLowerCase()) {
|
||||
throw new Error('SHA-512 Prüfung fehlgeschlagen');
|
||||
}
|
||||
}
|
||||
|
||||
stagedInstallerPath = path.join(tmpDir, `${path.basename(check.assetName)}.${crypto.randomUUID()}.download`);
|
||||
fs.writeFileSync(stagedInstallerPath, fileBuffer);
|
||||
fs.rmSync(installerPath, { force: true });
|
||||
fs.renameSync(stagedInstallerPath, installerPath);
|
||||
stagedInstallerPath = '';
|
||||
|
||||
const prepared = {
|
||||
installerPath,
|
||||
sha512: actualSha,
|
||||
size: manifest.size,
|
||||
sha512Verified: true,
|
||||
assetName: check.assetName,
|
||||
remoteVersion: check.remoteVersion || '',
|
||||
transportTag: check.transportTag || ''
|
||||
};
|
||||
await verifyPreparedUpdate(prepared);
|
||||
stagedInstallerPath = '';
|
||||
if (onProgress) onProgress({ stage: 'prepared', percent: 100 });
|
||||
return prepared;
|
||||
|
||||
} catch (err) {
|
||||
const failure = signal.aborted ? new Error('Update abgebrochen') : err;
|
||||
if (stagedInstallerPath) fs.rmSync(stagedInstallerPath, { force: true });
|
||||
if (stagedInstallerPath) await fs.promises.unlink(stagedInstallerPath).catch(() => {});
|
||||
if (downloadDirectory) await fs.promises.rmdir(downloadDirectory).catch(() => {});
|
||||
if (onProgress) onProgress({ stage: signal.aborted ? 'aborted' : 'error', error: failure.message });
|
||||
throw failure;
|
||||
} finally {
|
||||
@@ -393,6 +397,32 @@ async function prepareUpdate(onProgress, options = {}) {
|
||||
}
|
||||
}
|
||||
|
||||
async function verifyPreparedUpdate(prepared) {
|
||||
if (!prepared?.sha512Verified || !prepared.sha512 || !Number.isSafeInteger(prepared.size)) throw new Error('Update-Prüfsumme fehlt');
|
||||
const stat = await fs.promises.lstat(prepared.installerPath);
|
||||
if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1 || stat.size !== prepared.size) throw new Error('Update-Datei wurde verändert');
|
||||
const handle = await fs.promises.open(prepared.installerPath, 'r');
|
||||
try {
|
||||
const hash = crypto.createHash('sha512');
|
||||
const before = await handle.stat();
|
||||
if (before.ino !== stat.ino || before.dev !== stat.dev || before.size !== stat.size) throw new Error('Update-Datei wurde ausgetauscht');
|
||||
for await (const chunk of handle.createReadStream({ autoClose: false })) hash.update(chunk);
|
||||
const after = await handle.stat();
|
||||
if (after.size !== before.size || after.mtimeMs !== before.mtimeMs || after.ctimeMs !== before.ctimeMs || hash.digest('base64') !== prepared.sha512) throw new Error('SHA-512 Prüfung fehlgeschlagen');
|
||||
} finally { await handle.close(); }
|
||||
return true;
|
||||
}
|
||||
|
||||
async function launchVerifiedUpdate(prepared, directory, options = {}) {
|
||||
await verifyPreparedUpdate(prepared);
|
||||
const spawn = options.spawnImpl || require('node:child_process').spawn;
|
||||
const child = spawn(prepared.installerPath, ['/S', '--updated', '--force-run', `/D=${directory}`], { detached: true, windowsHide: true, stdio: 'ignore' });
|
||||
await new Promise((resolve, reject) => { child.once('error', reject); child.once('spawn', resolve); });
|
||||
child.unref();
|
||||
launchedInstallerPaths.add(path.resolve(prepared.installerPath).toLowerCase());
|
||||
return true;
|
||||
}
|
||||
|
||||
function launchPreparedUpdate(prepared, options = {}) {
|
||||
const installerPath = prepared && typeof prepared.installerPath === 'string' ? prepared.installerPath : '';
|
||||
if (!installerPath) throw new Error('Vorbereitetes Update ist unvollständig');
|
||||
@@ -403,6 +433,7 @@ function launchPreparedUpdate(prepared, options = {}) {
|
||||
try {
|
||||
spawnImpl(installerPath, ['/S', '--updated', '--force-run'], {
|
||||
detached: true,
|
||||
windowsHide: true,
|
||||
stdio: 'ignore'
|
||||
}).unref();
|
||||
return true;
|
||||
@@ -418,4 +449,4 @@ function abortUpdate() {
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = { checkForUpdate, fetchGithubReleaseNotes, prepareUpdate, launchPreparedUpdate, abortUpdate, isNewer, resolveReleaseVersion, pickSetupAsset, parseLatestYml, createUpdateAnnouncementState };
|
||||
module.exports = { checkForUpdate, fetchGithubReleaseNotes, prepareUpdate, verifyPreparedUpdate, launchVerifiedUpdate, launchPreparedUpdate, abortUpdate, isNewer, resolveReleaseVersion, pickSetupAsset, parseLatestYml, createUpdateAnnouncementState };
|
||||
Reference in new issue
Block a user