2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
/* eslint-disable no-console */
|
2019-03-28 17:09:26 +00:00
|
|
|
import {
|
|
|
|
createWriteStream,
|
|
|
|
statSync,
|
|
|
|
writeFile as writeFileCallback,
|
|
|
|
} from 'fs';
|
2021-12-03 22:49:15 +00:00
|
|
|
import { promisify } from 'util';
|
|
|
|
import { execFile } from 'child_process';
|
2021-11-10 00:56:56 +00:00
|
|
|
import { join, normalize, dirname } from 'path';
|
2019-03-28 17:09:26 +00:00
|
|
|
import { tmpdir } from 'os';
|
2021-08-19 22:56:29 +00:00
|
|
|
import { throttle } from 'lodash';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ParserConfiguration } from 'dashdash';
|
|
|
|
import { createParser } from 'dashdash';
|
2019-03-28 17:09:26 +00:00
|
|
|
import ProxyAgent from 'proxy-agent';
|
|
|
|
import { FAILSAFE_SCHEMA, safeLoad } from 'js-yaml';
|
|
|
|
import { gt } from 'semver';
|
2021-08-27 20:21:42 +00:00
|
|
|
import config from 'config';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StrictOptions as GotOptions } from 'got';
|
|
|
|
import got from 'got';
|
2019-03-28 17:09:26 +00:00
|
|
|
import { v4 as getGuid } from 'uuid';
|
|
|
|
import pify from 'pify';
|
|
|
|
import mkdirp from 'mkdirp';
|
|
|
|
import rimraf from 'rimraf';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { BrowserWindow } from 'electron';
|
|
|
|
import { app, ipcMain } from 'electron';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
import * as durations from '../util/durations';
|
2021-10-27 17:54:16 +00:00
|
|
|
import { getTempPath } from '../util/attachments';
|
2021-08-19 22:56:29 +00:00
|
|
|
import { DialogType } from '../types/Dialogs';
|
2021-11-10 00:56:56 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2020-09-09 22:50:44 +00:00
|
|
|
import { getUserAgent } from '../util/getUserAgent';
|
2021-08-06 21:21:01 +00:00
|
|
|
import { isAlpha, isBeta } from '../util/version';
|
2019-05-24 01:27:42 +00:00
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
import * as packageJson from '../../package.json';
|
2021-11-10 00:56:56 +00:00
|
|
|
import {
|
|
|
|
hexToBinary,
|
|
|
|
verifySignature,
|
|
|
|
getSignatureFileName,
|
|
|
|
} from './signature';
|
2020-08-27 18:08:37 +00:00
|
|
|
import { isPathInside } from '../util/isPathInside';
|
2021-11-10 00:56:56 +00:00
|
|
|
import type { SettingsChannel } from '../main/settingsChannel';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
const writeFile = pify(writeFileCallback);
|
|
|
|
const mkdirpPromise = pify(mkdirp);
|
|
|
|
const rimrafPromise = pify(rimraf);
|
|
|
|
|
2021-07-16 00:57:34 +00:00
|
|
|
export const GOT_CONNECT_TIMEOUT = 2 * 60 * 1000;
|
|
|
|
export const GOT_LOOKUP_TIMEOUT = 2 * 60 * 1000;
|
|
|
|
export const GOT_SOCKET_TIMEOUT = 2 * 60 * 1000;
|
2020-02-12 21:30:58 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
const INTERVAL = 30 * durations.MINUTE;
|
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
type JSONUpdateSchema = {
|
|
|
|
version: string;
|
|
|
|
files: Array<{
|
|
|
|
url: string;
|
|
|
|
sha512: string;
|
|
|
|
size: string;
|
|
|
|
blockMapSize?: string;
|
|
|
|
}>;
|
|
|
|
path: string;
|
|
|
|
sha512: string;
|
|
|
|
releaseDate: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type UpdateInformationType = {
|
|
|
|
fileName: string;
|
|
|
|
size: number;
|
|
|
|
version: string;
|
|
|
|
};
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
export abstract class Updater {
|
|
|
|
protected fileName: string | undefined;
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
protected version: string | undefined;
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
protected updateFilePath: string | undefined;
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
constructor(
|
|
|
|
protected readonly logger: LoggerType,
|
|
|
|
private readonly settingsChannel: SettingsChannel,
|
|
|
|
protected readonly getMainWindow: () => BrowserWindow | undefined
|
|
|
|
) {}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
//
|
|
|
|
// Public APIs
|
|
|
|
//
|
2021-08-19 22:56:29 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
public async force(): Promise<void> {
|
|
|
|
return this.checkForUpdatesMaybeInstall(true);
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
public async start(): Promise<void> {
|
|
|
|
this.logger.info('updater/start: starting checks...');
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
app.once('quit', () => this.quitHandler());
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
setInterval(async () => {
|
|
|
|
try {
|
|
|
|
await this.checkForUpdatesMaybeInstall();
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.error(`updater/start: ${Errors.toLogFormat(error)}`);
|
|
|
|
}
|
|
|
|
}, INTERVAL);
|
2019-08-02 21:11:10 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
await this.deletePreviousInstallers();
|
|
|
|
await this.checkForUpdatesMaybeInstall();
|
2019-08-02 21:11:10 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
public quitHandler(): void {
|
|
|
|
if (this.updateFilePath) {
|
|
|
|
this.deleteCache(this.updateFilePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Abstract methods
|
|
|
|
//
|
|
|
|
|
|
|
|
protected abstract deletePreviousInstallers(): Promise<void>;
|
|
|
|
|
|
|
|
protected abstract installUpdate(updateFilePath: string): Promise<void>;
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
//
|
|
|
|
// Protected methods
|
|
|
|
//
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
protected setUpdateListener(performUpdateCallback: () => void): void {
|
|
|
|
ipcMain.removeAllListeners('start-update');
|
|
|
|
ipcMain.once('start-update', performUpdateCallback);
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
//
|
|
|
|
// Private methods
|
|
|
|
//
|
|
|
|
|
|
|
|
private async downloadAndInstall(
|
|
|
|
newFileName: string,
|
|
|
|
newVersion: string,
|
|
|
|
updateOnProgress?: boolean
|
|
|
|
): Promise<void> {
|
|
|
|
const { logger } = this;
|
|
|
|
try {
|
|
|
|
const oldFileName = this.fileName;
|
|
|
|
const oldVersion = this.version;
|
|
|
|
|
|
|
|
if (this.updateFilePath) {
|
|
|
|
this.deleteCache(this.updateFilePath);
|
|
|
|
}
|
|
|
|
this.fileName = newFileName;
|
|
|
|
this.version = newVersion;
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.updateFilePath = await this.downloadUpdate(
|
|
|
|
this.fileName,
|
|
|
|
updateOnProgress
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
// Restore state in case of download error
|
|
|
|
this.fileName = oldFileName;
|
|
|
|
this.version = oldVersion;
|
|
|
|
throw error;
|
|
|
|
}
|
2019-08-02 21:11:10 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
const publicKey = hexToBinary(config.get('updatesPublicKey'));
|
|
|
|
const verified = await verifySignature(
|
|
|
|
this.updateFilePath,
|
|
|
|
this.version,
|
|
|
|
publicKey
|
|
|
|
);
|
|
|
|
if (!verified) {
|
|
|
|
// Note: We don't delete the cache here, because we don't want to continually
|
|
|
|
// re-download the broken release. We will download it only once per launch.
|
|
|
|
throw new Error(
|
|
|
|
'Downloaded update did not pass signature verification ' +
|
|
|
|
`(version: '${this.version}'; fileName: '${this.fileName}')`
|
|
|
|
);
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
await this.installUpdate(this.updateFilePath);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
const mainWindow = this.getMainWindow();
|
2021-08-19 22:56:29 +00:00
|
|
|
if (mainWindow) {
|
2021-11-10 00:56:56 +00:00
|
|
|
mainWindow.webContents.send('show-update-dialog', DialogType.Update, {
|
|
|
|
version: this.version,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logger.warn(
|
|
|
|
'downloadAndInstall: no mainWindow, cannot show update dialog'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(`downloadAndInstall: ${Errors.toLogFormat(error)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async checkForUpdatesMaybeInstall(force = false): Promise<void> {
|
|
|
|
const { logger } = this;
|
2021-08-19 22:56:29 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
logger.info('checkForUpdatesMaybeInstall: checking for update...');
|
|
|
|
const result = await this.checkForUpdates(force);
|
|
|
|
if (!result) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { fileName: newFileName, version: newVersion } = result;
|
|
|
|
|
|
|
|
if (
|
|
|
|
force ||
|
|
|
|
this.fileName !== newFileName ||
|
|
|
|
!this.version ||
|
|
|
|
gt(newVersion, this.version)
|
|
|
|
) {
|
|
|
|
const autoDownloadUpdates = await this.getAutoDownloadUpdateSetting();
|
|
|
|
if (!autoDownloadUpdates) {
|
|
|
|
this.setUpdateListener(async () => {
|
|
|
|
logger.info(
|
|
|
|
'checkForUpdatesMaybeInstall: have not downloaded update, going to download'
|
|
|
|
);
|
|
|
|
await this.downloadAndInstall(newFileName, newVersion, true);
|
|
|
|
});
|
|
|
|
const mainWindow = this.getMainWindow();
|
|
|
|
|
|
|
|
if (mainWindow) {
|
2021-08-19 22:56:29 +00:00
|
|
|
mainWindow.webContents.send(
|
|
|
|
'show-update-dialog',
|
2021-11-10 00:56:56 +00:00
|
|
|
DialogType.DownloadReady,
|
|
|
|
{
|
|
|
|
downloadSize: result.size,
|
|
|
|
version: result.version,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
logger.warn(
|
|
|
|
'checkForUpdatesMaybeInstall: no mainWindow, cannot show update dialog'
|
2021-08-19 22:56:29 +00:00
|
|
|
);
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await this.downloadAndInstall(newFileName, newVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async checkForUpdates(
|
|
|
|
forceUpdate = false
|
|
|
|
): Promise<UpdateInformationType | null> {
|
|
|
|
const yaml = await getUpdateYaml();
|
|
|
|
const parsedYaml = parseYaml(yaml);
|
|
|
|
const version = getVersion(parsedYaml);
|
2021-08-19 22:56:29 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
if (!version) {
|
|
|
|
this.logger.warn(
|
|
|
|
'checkForUpdates: no version extracted from downloaded yaml'
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forceUpdate || isVersionNewer(version)) {
|
|
|
|
this.logger.info(
|
|
|
|
`checkForUpdates: found newer version ${version} ` +
|
|
|
|
`forceUpdate=${forceUpdate}`
|
|
|
|
);
|
|
|
|
|
2021-12-03 22:49:15 +00:00
|
|
|
const fileName = getUpdateFileName(
|
|
|
|
parsedYaml,
|
|
|
|
process.platform,
|
|
|
|
await this.getArch()
|
|
|
|
);
|
2021-11-10 00:56:56 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
fileName,
|
|
|
|
size: getSize(parsedYaml, fileName),
|
|
|
|
version,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.info(
|
|
|
|
`checkForUpdates: ${version} is not newer; no new update available`
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async downloadUpdate(
|
|
|
|
fileName: string,
|
|
|
|
updateOnProgress?: boolean
|
|
|
|
): Promise<string> {
|
|
|
|
const baseUrl = getUpdatesBase();
|
|
|
|
const updateFileUrl = `${baseUrl}/${fileName}`;
|
|
|
|
|
|
|
|
const signatureFileName = getSignatureFileName(fileName);
|
|
|
|
const signatureUrl = `${baseUrl}/${signatureFileName}`;
|
|
|
|
|
|
|
|
let tempDir;
|
|
|
|
try {
|
|
|
|
tempDir = await createTempDir();
|
|
|
|
const targetUpdatePath = join(tempDir, fileName);
|
|
|
|
const targetSignaturePath = join(tempDir, getSignatureFileName(fileName));
|
|
|
|
|
|
|
|
validatePath(tempDir, targetUpdatePath);
|
|
|
|
validatePath(tempDir, targetSignaturePath);
|
|
|
|
|
|
|
|
this.logger.info(`downloadUpdate: Downloading signature ${signatureUrl}`);
|
|
|
|
const { body } = await got.get(signatureUrl, getGotOptions());
|
|
|
|
await writeFile(targetSignaturePath, body);
|
|
|
|
|
|
|
|
this.logger.info(`downloadUpdate: Downloading update ${updateFileUrl}`);
|
|
|
|
const downloadStream = got.stream(updateFileUrl, getGotOptions());
|
|
|
|
const writeStream = createWriteStream(targetUpdatePath);
|
|
|
|
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
const mainWindow = this.getMainWindow();
|
|
|
|
if (updateOnProgress && mainWindow) {
|
|
|
|
let downloadedSize = 0;
|
|
|
|
|
|
|
|
const throttledSend = throttle(() => {
|
|
|
|
mainWindow.webContents.send(
|
|
|
|
'show-update-dialog',
|
|
|
|
DialogType.Downloading,
|
|
|
|
{ downloadedSize }
|
|
|
|
);
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
downloadStream.on('data', data => {
|
|
|
|
downloadedSize += data.length;
|
|
|
|
throttledSend();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadStream.on('error', error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
downloadStream.on('end', () => {
|
|
|
|
resolve();
|
2021-08-19 22:56:29 +00:00
|
|
|
});
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
writeStream.on('error', error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
downloadStream.pipe(writeStream);
|
2019-03-28 17:09:26 +00:00
|
|
|
});
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
return targetUpdatePath;
|
|
|
|
} catch (error) {
|
|
|
|
if (tempDir) {
|
|
|
|
await deleteTempDir(tempDir);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getAutoDownloadUpdateSetting(): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
return await this.settingsChannel.getSettingFromMainWindow(
|
|
|
|
'autoDownloadUpdate'
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(
|
|
|
|
'getAutoDownloadUpdateSetting: Failed to fetch, returning false',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
private async deleteCache(filePath: string | null): Promise<void> {
|
|
|
|
if (!filePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const tempDir = dirname(filePath);
|
|
|
|
try {
|
2019-03-28 17:09:26 +00:00
|
|
|
await deleteTempDir(tempDir);
|
2021-11-10 00:56:56 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.logger.error(`quitHandler: ${Errors.toLogFormat(error)}`);
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
2021-12-03 22:49:15 +00:00
|
|
|
|
|
|
|
private async getArch(): Promise<typeof process.arch> {
|
|
|
|
if (process.platform !== 'darwin' || process.arch === 'arm64') {
|
|
|
|
return process.arch;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// We might be running under Rosetta
|
|
|
|
if (promisify(execFile)('uname', ['-m']).toString().trim() === 'arm64') {
|
|
|
|
this.logger.info('updater: running under Rosetta');
|
|
|
|
return 'arm64';
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.warn(
|
|
|
|
`updater: "uname -m" failed with ${Errors.toLogFormat(error)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.info('updater: not running under Rosetta');
|
|
|
|
return process.arch;
|
|
|
|
}
|
2021-11-10 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function validatePath(basePath: string, targetPath: string): void {
|
|
|
|
const normalized = normalize(targetPath);
|
|
|
|
|
|
|
|
if (!isPathInside(normalized, basePath)) {
|
|
|
|
throw new Error(
|
|
|
|
`validatePath: Path ${normalized} is not under base path ${basePath}`
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
export function getUpdateCheckUrl(): string {
|
|
|
|
return `${getUpdatesBase()}/${getUpdatesFileName()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUpdatesBase(): string {
|
2021-08-27 20:21:42 +00:00
|
|
|
return config.get('updatesUrl');
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
export function getCertificateAuthority(): string {
|
2021-08-27 20:21:42 +00:00
|
|
|
return config.get('certificateAuthority');
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
export function getProxyUrl(): string | undefined {
|
|
|
|
return process.env.HTTPS_PROXY || process.env.https_proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUpdatesFileName(): string {
|
2021-08-06 21:21:01 +00:00
|
|
|
const prefix = getChannel();
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-12-03 22:49:15 +00:00
|
|
|
if (process.platform === 'darwin') {
|
2019-03-28 17:09:26 +00:00
|
|
|
return `${prefix}-mac.yml`;
|
|
|
|
}
|
2020-09-16 19:31:05 +00:00
|
|
|
|
|
|
|
return `${prefix}.yml`;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:21:01 +00:00
|
|
|
function getChannel(): string {
|
|
|
|
const { version } = packageJson;
|
|
|
|
|
|
|
|
if (isAlpha(version)) {
|
|
|
|
return 'alpha';
|
|
|
|
}
|
|
|
|
if (isBeta(version)) {
|
|
|
|
return 'beta';
|
|
|
|
}
|
|
|
|
return 'latest';
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isVersionNewer(newVersion: string): boolean {
|
|
|
|
const { version } = packageJson;
|
|
|
|
|
|
|
|
return gt(newVersion, version);
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
export function getVersion(info: JSONUpdateSchema): string | null {
|
2020-09-16 19:31:05 +00:00
|
|
|
return info && info.version;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
const validFile = /^[A-Za-z0-9.-]+$/;
|
|
|
|
export function isUpdateFileNameValid(name: string): boolean {
|
2019-08-02 21:11:10 +00:00
|
|
|
return validFile.test(name);
|
|
|
|
}
|
|
|
|
|
2021-12-03 22:49:15 +00:00
|
|
|
export function getUpdateFileName(
|
|
|
|
info: JSONUpdateSchema,
|
|
|
|
platform: typeof process.platform,
|
|
|
|
arch: typeof process.arch
|
|
|
|
): string {
|
2019-08-02 21:11:10 +00:00
|
|
|
if (!info || !info.path) {
|
|
|
|
throw new Error('getUpdateFileName: No path present in YAML file');
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 22:49:15 +00:00
|
|
|
let path: string | undefined;
|
|
|
|
if (platform === 'darwin') {
|
|
|
|
const { files } = info;
|
|
|
|
|
|
|
|
const candidates = files.filter(
|
|
|
|
({ url }) => url.includes(arch) && url.endsWith('.zip')
|
|
|
|
);
|
|
|
|
|
|
|
|
if (candidates.length === 1) {
|
|
|
|
path = candidates[0].url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
path = path ?? info.path;
|
|
|
|
|
2019-08-02 21:11:10 +00:00
|
|
|
if (!isUpdateFileNameValid(path)) {
|
|
|
|
throw new Error(
|
|
|
|
`getUpdateFileName: Path '${path}' contains invalid characters`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 22:56:29 +00:00
|
|
|
function getSize(info: JSONUpdateSchema, fileName: string): number {
|
|
|
|
if (!info || !info.files) {
|
|
|
|
throw new Error('getUpdateFileName: No files present in YAML file');
|
|
|
|
}
|
|
|
|
|
|
|
|
const foundFile = info.files.find(file => file.url === fileName);
|
|
|
|
|
|
|
|
return Number(foundFile?.size) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function parseYaml(yaml: string): JSONUpdateSchema {
|
2019-03-28 17:09:26 +00:00
|
|
|
return safeLoad(yaml, { schema: FAILSAFE_SCHEMA, json: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getUpdateYaml(): Promise<string> {
|
|
|
|
const targetUrl = getUpdateCheckUrl();
|
2021-10-06 16:25:22 +00:00
|
|
|
const body = await got(targetUrl, getGotOptions()).text();
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
if (!body) {
|
|
|
|
throw new Error('Got unexpected response back from update check');
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:25:22 +00:00
|
|
|
return body;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 16:25:22 +00:00
|
|
|
function getGotOptions(): GotOptions {
|
|
|
|
const certificateAuthority = getCertificateAuthority();
|
2019-03-28 17:09:26 +00:00
|
|
|
const proxyUrl = getProxyUrl();
|
2021-10-06 16:25:22 +00:00
|
|
|
const agent = proxyUrl
|
|
|
|
? {
|
|
|
|
http: new ProxyAgent(proxyUrl),
|
|
|
|
https: new ProxyAgent(proxyUrl),
|
|
|
|
}
|
|
|
|
: undefined;
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
agent,
|
2021-10-06 16:25:22 +00:00
|
|
|
https: {
|
|
|
|
certificateAuthority,
|
|
|
|
},
|
2019-03-28 17:09:26 +00:00
|
|
|
headers: {
|
|
|
|
'Cache-Control': 'no-cache',
|
2020-09-09 22:50:44 +00:00
|
|
|
'User-Agent': getUserAgent(packageJson.version),
|
2019-03-28 17:09:26 +00:00
|
|
|
},
|
2021-07-16 00:57:34 +00:00
|
|
|
timeout: {
|
|
|
|
connect: GOT_CONNECT_TIMEOUT,
|
|
|
|
lookup: GOT_LOOKUP_TIMEOUT,
|
|
|
|
|
|
|
|
// This timeout is reset whenever we get new data on the socket
|
|
|
|
socket: GOT_SOCKET_TIMEOUT,
|
|
|
|
},
|
2019-03-28 17:09:26 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBaseTempDir() {
|
|
|
|
// We only use tmpdir() when this code is run outside of an Electron app (as in: tests)
|
2019-05-24 01:27:42 +00:00
|
|
|
return app ? getTempPath(app.getPath('userData')) : tmpdir();
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
export async function createTempDir(): Promise<string> {
|
2019-03-28 17:09:26 +00:00
|
|
|
const baseTempDir = getBaseTempDir();
|
|
|
|
const uniqueName = getGuid();
|
|
|
|
const targetDir = join(baseTempDir, uniqueName);
|
|
|
|
await mkdirpPromise(targetDir);
|
|
|
|
|
|
|
|
return targetDir;
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
export async function deleteTempDir(targetDir: string): Promise<void> {
|
2019-03-28 17:09:26 +00:00
|
|
|
const pathInfo = statSync(targetDir);
|
|
|
|
if (!pathInfo.isDirectory()) {
|
|
|
|
throw new Error(
|
|
|
|
`deleteTempDir: Cannot delete path '${targetDir}' because it is not a directory`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const baseTempDir = getBaseTempDir();
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(targetDir, baseTempDir)) {
|
2019-03-28 17:09:26 +00:00
|
|
|
throw new Error(
|
|
|
|
`deleteTempDir: Cannot delete path '${targetDir}' since it is not within base temp dir`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await rimrafPromise(targetDir);
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
export function getCliOptions<T>(options: ParserConfiguration['options']): T {
|
2019-03-28 17:09:26 +00:00
|
|
|
const parser = createParser({ options });
|
|
|
|
const cliOptions = parser.parse(process.argv);
|
|
|
|
|
|
|
|
if (cliOptions.help) {
|
|
|
|
const help = parser.help().trimRight();
|
|
|
|
console.log(help);
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
return cliOptions as unknown as T;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|