2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
import { createReadStream, statSync } from 'fs';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { IncomingMessage, Server, ServerResponse } from 'http';
|
|
|
|
import { createServer } from 'http';
|
|
|
|
import type { AddressInfo } from 'net';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
import { v4 as getGuid } from 'uuid';
|
2021-11-10 00:56:56 +00:00
|
|
|
import { autoUpdater } from 'electron';
|
2019-08-02 21:11:10 +00:00
|
|
|
import got from 'got';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
import { Updater } from './common';
|
|
|
|
import { explodePromise } from '../util/explodePromise';
|
|
|
|
import * as Errors from '../types/errors';
|
2019-03-28 17:09:26 +00:00
|
|
|
import { markShouldQuit } from '../../app/window_state';
|
2021-08-19 22:56:29 +00:00
|
|
|
import { DialogType } from '../types/Dialogs';
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
export class MacOSUpdater extends Updater {
|
|
|
|
protected async deletePreviousInstallers(): Promise<void> {
|
|
|
|
// No installers are cache on macOS
|
2021-08-19 22:56:29 +00:00
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
protected async installUpdate(updateFilePath: string): Promise<void> {
|
|
|
|
const { logger } = this;
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
try {
|
2021-11-10 00:56:56 +00:00
|
|
|
await this.handToAutoUpdate(updateFilePath);
|
2019-03-28 17:09:26 +00:00
|
|
|
} catch (error) {
|
|
|
|
const readOnly = 'Cannot update while running on a read-only volume';
|
|
|
|
const message: string = error.message || '';
|
2022-03-28 19:05:44 +00:00
|
|
|
this.markCannotUpdate(
|
|
|
|
error,
|
|
|
|
message.includes(readOnly)
|
|
|
|
? DialogType.MacOS_Read_Only
|
|
|
|
: DialogType.Cannot_Update
|
|
|
|
);
|
2019-03-28 17:09:26 +00:00
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, closing the app will cause the update to be installed automatically
|
|
|
|
// because Squirrel has cached the update file and will do the right thing.
|
2021-08-19 22:56:29 +00:00
|
|
|
logger.info('downloadAndInstall: showing update dialog...');
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2022-02-25 18:44:03 +00:00
|
|
|
this.setUpdateListener(async () => {
|
2021-08-23 22:45:11 +00:00
|
|
|
logger.info('performUpdate: calling quitAndInstall...');
|
|
|
|
markShouldQuit();
|
|
|
|
autoUpdater.quitAndInstall();
|
|
|
|
});
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
private async handToAutoUpdate(filePath: string): Promise<void> {
|
|
|
|
const { logger } = this;
|
|
|
|
const { promise, resolve, reject } = explodePromise<void>();
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2019-08-02 21:11:10 +00:00
|
|
|
const token = getGuid();
|
2019-03-28 17:09:26 +00:00
|
|
|
const updateFileUrl = generateFileUrl();
|
|
|
|
const server = createServer();
|
|
|
|
let serverUrl: string;
|
|
|
|
|
|
|
|
server.on('error', (error: Error) => {
|
2021-11-10 00:56:56 +00:00
|
|
|
logger.error(`handToAutoUpdate: ${Errors.toLogFormat(error)}`);
|
|
|
|
this.shutdown(server);
|
2019-03-28 17:09:26 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.on(
|
|
|
|
'request',
|
|
|
|
(request: IncomingMessage, response: ServerResponse) => {
|
|
|
|
const { url } = request;
|
|
|
|
|
|
|
|
if (url === '/') {
|
|
|
|
const absoluteUrl = `${serverUrl}${updateFileUrl}`;
|
|
|
|
writeJSONResponse(absoluteUrl, response);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:11:10 +00:00
|
|
|
if (url === '/token') {
|
|
|
|
writeTokenResponse(token, response);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
if (!url || !url.startsWith(updateFileUrl)) {
|
2021-11-10 00:56:56 +00:00
|
|
|
this.logger.error(
|
|
|
|
`write404: Squirrel requested unexpected url '${url}'`
|
|
|
|
);
|
|
|
|
response.writeHead(404);
|
|
|
|
response.end();
|
2019-03-28 17:09:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
this.pipeUpdateToSquirrel(filePath, server, response, reject);
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-08-02 21:11:10 +00:00
|
|
|
server.listen(0, '127.0.0.1', async () => {
|
|
|
|
try {
|
|
|
|
serverUrl = getServerUrl(server);
|
|
|
|
|
2021-07-01 23:40:19 +00:00
|
|
|
autoUpdater.on('error', (...args) => {
|
2021-11-10 00:56:56 +00:00
|
|
|
logger.error('autoUpdater: error', ...args.map(Errors.toLogFormat));
|
2021-07-01 23:40:19 +00:00
|
|
|
|
|
|
|
const [error] = args;
|
2019-08-02 21:11:10 +00:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
autoUpdater.on('update-downloaded', () => {
|
|
|
|
logger.info('autoUpdater: update-downloaded event fired');
|
2021-11-10 00:56:56 +00:00
|
|
|
this.shutdown(server);
|
2019-08-02 21:11:10 +00:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
const response = await got.get(`${serverUrl}/token`);
|
|
|
|
if (JSON.parse(response.body).token !== token) {
|
|
|
|
throw new Error(
|
|
|
|
'autoUpdater: did not receive token back from updates server'
|
|
|
|
);
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2019-08-02 21:11:10 +00:00
|
|
|
autoUpdater.setFeedURL({
|
|
|
|
url: serverUrl,
|
|
|
|
headers: { 'Cache-Control': 'no-cache' },
|
|
|
|
});
|
|
|
|
autoUpdater.checkForUpdates();
|
|
|
|
} catch (error) {
|
2019-03-28 17:09:26 +00:00
|
|
|
reject(error);
|
2019-08-02 21:11:10 +00:00
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
});
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
return promise;
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
private pipeUpdateToSquirrel(
|
|
|
|
filePath: string,
|
|
|
|
server: Server,
|
|
|
|
response: ServerResponse,
|
|
|
|
reject: (error: Error) => void
|
|
|
|
): void {
|
|
|
|
const { logger } = this;
|
|
|
|
|
|
|
|
const updateFileSize = getFileSize(filePath);
|
|
|
|
const readStream = createReadStream(filePath);
|
|
|
|
|
|
|
|
response.on('error', (error: Error) => {
|
|
|
|
logger.error(
|
|
|
|
`pipeUpdateToSquirrel: update file download request had an error ${Errors.toLogFormat(
|
|
|
|
error
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
this.shutdown(server);
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
readStream.on('error', (error: Error) => {
|
|
|
|
logger.error(
|
|
|
|
`pipeUpdateToSquirrel: read stream error response: ${Errors.toLogFormat(
|
|
|
|
error
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
this.shutdown(server, response);
|
|
|
|
reject(error);
|
|
|
|
});
|
2019-03-28 17:09:26 +00:00
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
response.writeHead(200, {
|
|
|
|
'Content-Type': 'application/zip',
|
|
|
|
'Content-Length': updateFileSize,
|
|
|
|
});
|
|
|
|
|
|
|
|
readStream.pipe(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
private shutdown(server: Server, response?: ServerResponse): void {
|
|
|
|
const { logger } = this;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (server) {
|
|
|
|
server.close();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(
|
|
|
|
`shutdown: Error closing server ${Errors.toLogFormat(error)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (response) {
|
|
|
|
response.end();
|
|
|
|
}
|
|
|
|
} catch (endError) {
|
|
|
|
logger.error(
|
|
|
|
`shutdown: couldn't end response ${Errors.toLogFormat(endError)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-03-28 17:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:56:56 +00:00
|
|
|
// Helpers
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
function writeJSONResponse(url: string, response: ServerResponse) {
|
|
|
|
const data = Buffer.from(
|
|
|
|
JSON.stringify({
|
|
|
|
url,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
response.writeHead(200, {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Content-Length': data.byteLength,
|
|
|
|
});
|
|
|
|
response.end(data);
|
|
|
|
}
|
|
|
|
|
2019-08-02 21:11:10 +00:00
|
|
|
function writeTokenResponse(token: string, response: ServerResponse) {
|
|
|
|
const data = Buffer.from(
|
|
|
|
JSON.stringify({
|
|
|
|
token,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
response.writeHead(200, {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Content-Length': data.byteLength,
|
|
|
|
});
|
|
|
|
response.end(data);
|
|
|
|
}
|
|
|
|
|
2019-03-28 17:09:26 +00:00
|
|
|
function getServerUrl(server: Server) {
|
|
|
|
const address = server.address() as AddressInfo;
|
|
|
|
|
|
|
|
return `http://127.0.0.1:${address.port}`;
|
|
|
|
}
|
|
|
|
function generateFileUrl(): string {
|
|
|
|
return `/${getGuid()}.zip`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFileSize(targetPath: string): number {
|
|
|
|
const { size } = statSync(targetPath);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|