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';
|
2019-08-02 21:11:10 +00:00
|
|
|
import { join, normalize } from 'path';
|
2019-03-28 17:09:26 +00:00
|
|
|
import { tmpdir } from 'os';
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
import { createParser, ParserConfiguration } 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';
|
|
|
|
import { get as getFromConfig } from 'config';
|
|
|
|
import { get, GotOptions, stream } from 'got';
|
|
|
|
import { v4 as getGuid } from 'uuid';
|
|
|
|
import pify from 'pify';
|
|
|
|
import mkdirp from 'mkdirp';
|
|
|
|
import rimraf from 'rimraf';
|
2020-02-12 21:30:58 +00:00
|
|
|
import { app, BrowserWindow, dialog, ipcMain } from 'electron';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2019-05-24 01:27:42 +00:00
|
|
|
import { getTempPath } from '../../app/attachments';
|
2020-02-12 21:30:58 +00:00
|
|
|
import { Dialogs } from '../types/Dialogs';
|
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';
|
|
|
|
import { getSignatureFileName } from './signature';
|
2020-08-27 18:08:37 +00:00
|
|
|
import { isPathInside } from '../util/isPathInside';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
import { LocaleType } from '../types/I18N';
|
|
|
|
import { LoggerType } from '../types/Logging';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
const writeFile = pify(writeFileCallback);
|
|
|
|
const mkdirpPromise = pify(mkdirp);
|
|
|
|
const rimrafPromise = pify(rimraf);
|
|
|
|
const { platform } = process;
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
export const ACK_RENDER_TIMEOUT = 10000;
|
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-06-30 21:27:18 +00:00
|
|
|
export type UpdaterInterface = {
|
|
|
|
force(): Promise<void>;
|
|
|
|
};
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
export async function checkForUpdates(
|
2021-06-30 21:27:18 +00:00
|
|
|
logger: LoggerType,
|
|
|
|
forceUpdate = false
|
2019-03-28 17:09:26 +00:00
|
|
|
): Promise<{
|
|
|
|
fileName: string;
|
|
|
|
version: string;
|
|
|
|
} | null> {
|
|
|
|
const yaml = await getUpdateYaml();
|
|
|
|
const version = getVersion(yaml);
|
|
|
|
|
|
|
|
if (!version) {
|
|
|
|
logger.warn('checkForUpdates: no version extracted from downloaded yaml');
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-06-30 21:27:18 +00:00
|
|
|
if (forceUpdate || isVersionNewer(version)) {
|
|
|
|
logger.info(
|
|
|
|
`checkForUpdates: found newer version ${version} ` +
|
|
|
|
`forceUpdate=${forceUpdate}`
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
fileName: getUpdateFileName(yaml),
|
|
|
|
version,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
`checkForUpdates: ${version} is not newer; no new update available`
|
|
|
|
);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
export function validatePath(basePath: string, targetPath: string): void {
|
2019-08-02 21:11:10 +00:00
|
|
|
const normalized = normalize(targetPath);
|
|
|
|
|
2020-08-27 18:08:37 +00:00
|
|
|
if (!isPathInside(normalized, basePath)) {
|
2019-08-02 21:11:10 +00:00
|
|
|
throw new Error(
|
|
|
|
`validatePath: Path ${normalized} is not under base path ${basePath}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
export async function downloadUpdate(
|
|
|
|
fileName: string,
|
|
|
|
logger: LoggerType
|
|
|
|
): 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));
|
|
|
|
|
2019-08-02 21:11:10 +00:00
|
|
|
validatePath(tempDir, targetUpdatePath);
|
|
|
|
validatePath(tempDir, targetSignaturePath);
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
logger.info(`downloadUpdate: Downloading ${signatureUrl}`);
|
|
|
|
const { body } = await get(signatureUrl, getGotOptions());
|
|
|
|
await writeFile(targetSignaturePath, body);
|
|
|
|
|
|
|
|
logger.info(`downloadUpdate: Downloading ${updateFileUrl}`);
|
|
|
|
const downloadStream = stream(updateFileUrl, getGotOptions());
|
|
|
|
const writeStream = createWriteStream(targetUpdatePath);
|
|
|
|
|
2020-12-18 17:09:31 +00:00
|
|
|
await new Promise<void>((resolve, reject) => {
|
2019-03-28 17:09:26 +00:00
|
|
|
downloadStream.on('error', error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
downloadStream.on('end', () => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
writeStream.on('error', error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
downloadStream.pipe(writeStream);
|
|
|
|
});
|
|
|
|
|
|
|
|
return targetUpdatePath;
|
|
|
|
} catch (error) {
|
|
|
|
if (tempDir) {
|
|
|
|
await deleteTempDir(tempDir);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 23:41:05 +00:00
|
|
|
let showingUpdateDialog = false;
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
async function showFallbackUpdateDialog(
|
2019-03-28 17:09:26 +00:00
|
|
|
mainWindow: BrowserWindow,
|
2020-02-13 18:14:08 +00:00
|
|
|
locale: LocaleType
|
2020-02-21 23:41:05 +00:00
|
|
|
): Promise<boolean> {
|
|
|
|
if (showingUpdateDialog) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
const RESTART_BUTTON = 0;
|
|
|
|
const LATER_BUTTON = 1;
|
|
|
|
const options = {
|
|
|
|
type: 'info',
|
|
|
|
buttons: [
|
2020-02-13 18:14:08 +00:00
|
|
|
locale.messages.autoUpdateRestartButtonLabel.message,
|
|
|
|
locale.messages.autoUpdateLaterButtonLabel.message,
|
2019-03-28 17:09:26 +00:00
|
|
|
],
|
2020-02-13 18:14:08 +00:00
|
|
|
title: locale.messages.autoUpdateNewVersionTitle.message,
|
|
|
|
message: locale.messages.autoUpdateNewVersionMessage.message,
|
|
|
|
detail: locale.messages.autoUpdateNewVersionInstructions.message,
|
2019-03-28 17:09:26 +00:00
|
|
|
defaultId: LATER_BUTTON,
|
2020-01-23 22:52:47 +00:00
|
|
|
cancelId: LATER_BUTTON,
|
2019-03-28 17:09:26 +00:00
|
|
|
};
|
|
|
|
|
2020-02-21 23:41:05 +00:00
|
|
|
showingUpdateDialog = true;
|
|
|
|
|
2019-08-19 17:59:30 +00:00
|
|
|
const { response } = await dialog.showMessageBox(mainWindow, options);
|
|
|
|
|
2020-02-21 23:41:05 +00:00
|
|
|
showingUpdateDialog = false;
|
|
|
|
|
2019-08-19 17:59:30 +00:00
|
|
|
return response === RESTART_BUTTON;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
export function showUpdateDialog(
|
|
|
|
mainWindow: BrowserWindow,
|
2020-02-13 18:14:08 +00:00
|
|
|
locale: LocaleType,
|
2020-02-12 21:30:58 +00:00
|
|
|
performUpdateCallback: () => void
|
|
|
|
): void {
|
|
|
|
let ack = false;
|
|
|
|
|
|
|
|
ipcMain.once('show-update-dialog-ack', () => {
|
|
|
|
ack = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow.webContents.send('show-update-dialog', Dialogs.Update);
|
|
|
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
if (!ack) {
|
2020-02-21 23:41:05 +00:00
|
|
|
const shouldUpdate = await showFallbackUpdateDialog(mainWindow, locale);
|
|
|
|
if (shouldUpdate) {
|
|
|
|
performUpdateCallback();
|
|
|
|
}
|
2020-02-12 21:30:58 +00:00
|
|
|
}
|
|
|
|
}, ACK_RENDER_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2020-02-21 23:41:05 +00:00
|
|
|
let showingCannotUpdateDialog = false;
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
async function showFallbackCannotUpdateDialog(
|
2019-03-28 17:09:26 +00:00
|
|
|
mainWindow: BrowserWindow,
|
2020-02-13 18:14:08 +00:00
|
|
|
locale: LocaleType
|
2020-02-21 23:41:05 +00:00
|
|
|
): Promise<void> {
|
|
|
|
if (showingCannotUpdateDialog) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
const options = {
|
|
|
|
type: 'error',
|
2020-02-13 18:14:08 +00:00
|
|
|
buttons: [locale.messages.ok.message],
|
|
|
|
title: locale.messages.cannotUpdate.message,
|
|
|
|
message: locale.i18n('cannotUpdateDetail', ['https://signal.org/download']),
|
2019-03-28 17:09:26 +00:00
|
|
|
};
|
|
|
|
|
2020-02-21 23:41:05 +00:00
|
|
|
showingCannotUpdateDialog = true;
|
|
|
|
|
2019-08-19 17:59:30 +00:00
|
|
|
await dialog.showMessageBox(mainWindow, options);
|
2020-02-21 23:41:05 +00:00
|
|
|
|
|
|
|
showingCannotUpdateDialog = false;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 21:30:58 +00:00
|
|
|
export function showCannotUpdateDialog(
|
|
|
|
mainWindow: BrowserWindow,
|
2020-02-13 18:14:08 +00:00
|
|
|
locale: LocaleType
|
2020-02-12 21:30:58 +00:00
|
|
|
): void {
|
|
|
|
let ack = false;
|
|
|
|
|
|
|
|
ipcMain.once('show-update-dialog-ack', () => {
|
|
|
|
ack = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
mainWindow.webContents.send('show-update-dialog', Dialogs.Cannot_Update);
|
|
|
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
if (!ack) {
|
2020-02-13 18:14:08 +00:00
|
|
|
await showFallbackCannotUpdateDialog(mainWindow, locale);
|
2020-02-12 21:30:58 +00:00
|
|
|
}
|
|
|
|
}, ACK_RENDER_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
// Helper functions
|
|
|
|
|
|
|
|
export function getUpdateCheckUrl(): string {
|
|
|
|
return `${getUpdatesBase()}/${getUpdatesFileName()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getUpdatesBase(): string {
|
|
|
|
return getFromConfig('updatesUrl');
|
|
|
|
}
|
|
|
|
export function getCertificateAuthority(): string {
|
|
|
|
return getFromConfig('certificateAuthority');
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
if (platform === 'darwin') {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
export function getVersion(yaml: string): string | null {
|
2019-03-28 17:09:26 +00:00
|
|
|
const info = parseYaml(yaml);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
// Reliant on third party parser that returns any
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
export function getUpdateFileName(yaml: string): any {
|
2019-03-28 17:09:26 +00:00
|
|
|
const info = parseYaml(yaml);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
const { path } = info;
|
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
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
// Reliant on third party parser that returns any
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2019-03-28 17:09:26 +00:00
|
|
|
function parseYaml(yaml: string): any {
|
|
|
|
return safeLoad(yaml, { schema: FAILSAFE_SCHEMA, json: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getUpdateYaml(): Promise<string> {
|
|
|
|
const targetUrl = getUpdateCheckUrl();
|
|
|
|
const { body } = await get(targetUrl, getGotOptions());
|
|
|
|
|
|
|
|
if (!body) {
|
|
|
|
throw new Error('Got unexpected response back from update check');
|
|
|
|
}
|
|
|
|
|
|
|
|
return body.toString('utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getGotOptions(): GotOptions<null> {
|
|
|
|
const ca = getCertificateAuthority();
|
|
|
|
const proxyUrl = getProxyUrl();
|
|
|
|
const agent = proxyUrl ? new ProxyAgent(proxyUrl) : undefined;
|
|
|
|
|
|
|
|
return {
|
|
|
|
agent,
|
|
|
|
ca,
|
|
|
|
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
|
|
|
},
|
|
|
|
useElectronNet: false,
|
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);
|
|
|
|
}
|
|
|
|
|
2021-07-01 23:40:19 +00:00
|
|
|
export function getPrintableError(error: Error | string): Error | string {
|
|
|
|
if (typeof error === 'string') {
|
|
|
|
return error;
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
return error && error.stack ? error.stack : error;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:31:05 +00:00
|
|
|
return (cliOptions as unknown) as T;
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
2020-07-20 18:42:26 +00:00
|
|
|
|
|
|
|
export function setUpdateListener(performUpdateCallback: () => void): void {
|
|
|
|
ipcMain.once('start-update', performUpdateCallback);
|
|
|
|
}
|