2021-01-06 17:23:15 +00:00
|
|
|
// Copyright 2018-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
import { memoize, sortBy } from 'lodash';
|
|
|
|
import { ipcRenderer as ipc } from 'electron';
|
2021-05-25 22:40:04 +00:00
|
|
|
import { z } from 'zod';
|
2021-01-06 17:23:15 +00:00
|
|
|
import FormData from 'form-data';
|
|
|
|
import { gzip } from 'zlib';
|
|
|
|
import pify from 'pify';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { Response } from 'got';
|
|
|
|
import got from 'got';
|
2021-01-06 17:23:15 +00:00
|
|
|
import { getUserAgent } from '../util/getUserAgent';
|
2021-05-13 17:18:51 +00:00
|
|
|
import { maybeParseUrl } from '../util/url';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from './log';
|
|
|
|
import { reallyJsonStringify } from '../util/reallyJsonStringify';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { FetchLogIpcData, LogEntryType } from './shared';
|
2021-09-17 18:27:53 +00:00
|
|
|
import {
|
|
|
|
LogLevel,
|
|
|
|
getLogLevelString,
|
|
|
|
isFetchLogIpcData,
|
|
|
|
isLogEntry,
|
|
|
|
levelMaxLength,
|
|
|
|
} from './shared';
|
|
|
|
import { redactAll } from '../util/privacy';
|
|
|
|
import { getEnvironment } from '../environment';
|
2021-01-06 17:23:15 +00:00
|
|
|
|
|
|
|
const BASE_URL = 'https://debuglogs.org';
|
|
|
|
|
2021-04-01 17:37:10 +00:00
|
|
|
const tokenBodySchema = z
|
|
|
|
.object({
|
|
|
|
fields: z.record(z.unknown()),
|
|
|
|
url: z.string(),
|
|
|
|
})
|
|
|
|
.nonstrict();
|
2021-01-06 17:23:15 +00:00
|
|
|
|
|
|
|
const parseTokenBody = (
|
2021-04-01 17:37:10 +00:00
|
|
|
rawBody: unknown
|
2021-01-06 17:23:15 +00:00
|
|
|
): { fields: Record<string, unknown>; url: string } => {
|
2021-04-01 17:37:10 +00:00
|
|
|
const body = tokenBodySchema.parse(rawBody);
|
2021-01-06 17:23:15 +00:00
|
|
|
|
2021-05-13 17:18:51 +00:00
|
|
|
const parsedUrl = maybeParseUrl(body.url);
|
|
|
|
if (!parsedUrl) {
|
2021-01-06 17:23:15 +00:00
|
|
|
throw new Error("Token body's URL was not a valid URL");
|
|
|
|
}
|
|
|
|
if (parsedUrl.protocol !== 'https:') {
|
|
|
|
throw new Error("Token body's URL was not HTTPS");
|
|
|
|
}
|
|
|
|
|
2021-04-01 17:37:10 +00:00
|
|
|
return body;
|
2021-01-06 17:23:15 +00:00
|
|
|
};
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
export const upload = async (
|
2021-01-06 17:23:15 +00:00
|
|
|
content: string,
|
|
|
|
appVersion: string
|
|
|
|
): Promise<string> => {
|
|
|
|
const headers = { 'User-Agent': getUserAgent(appVersion) };
|
|
|
|
|
2021-10-06 16:25:22 +00:00
|
|
|
const signedForm = await got.get(BASE_URL, { responseType: 'json', headers });
|
2021-01-06 17:23:15 +00:00
|
|
|
const { fields, url } = parseTokenBody(signedForm.body);
|
|
|
|
|
2021-06-01 22:16:10 +00:00
|
|
|
const uploadKey = `${fields.key}.gz`;
|
|
|
|
|
2021-01-06 17:23:15 +00:00
|
|
|
const form = new FormData();
|
|
|
|
// The API expects `key` to be the first field:
|
2021-06-01 22:16:10 +00:00
|
|
|
form.append('key', uploadKey);
|
2021-01-06 17:23:15 +00:00
|
|
|
Object.entries(fields)
|
|
|
|
.filter(([key]) => key !== 'key')
|
|
|
|
.forEach(([key, value]) => {
|
|
|
|
form.append(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
const contentBuffer = await pify(gzip)(Buffer.from(content, 'utf8'));
|
|
|
|
const contentType = 'application/gzip';
|
|
|
|
form.append('Content-Type', contentType);
|
|
|
|
form.append('file', contentBuffer, {
|
|
|
|
contentType,
|
|
|
|
filename: `signal-desktop-debug-log-${appVersion}.txt.gz`,
|
|
|
|
});
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Debug log upload starting...');
|
2021-02-19 21:17:10 +00:00
|
|
|
try {
|
|
|
|
const { statusCode, body } = await got.post(url, { headers, body: form });
|
|
|
|
if (statusCode !== 204) {
|
|
|
|
throw new Error(
|
|
|
|
`Failed to upload to S3, got status ${statusCode}, body '${body}'`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
const response = error.response as Response<string>;
|
|
|
|
throw new Error(
|
|
|
|
`Got threw on upload to S3, got status ${response?.statusCode}, body '${response?.body}' `
|
|
|
|
);
|
2021-01-06 17:23:15 +00:00
|
|
|
}
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Debug log upload complete.');
|
2021-01-06 17:23:15 +00:00
|
|
|
|
2021-06-01 22:16:10 +00:00
|
|
|
return `${BASE_URL}/${uploadKey}`;
|
2021-01-06 17:23:15 +00:00
|
|
|
};
|
2021-09-17 18:27:53 +00:00
|
|
|
|
|
|
|
// The mechanics of preparing a log for publish
|
|
|
|
|
|
|
|
const headerSectionTitle = (title: string) => `========= ${title} =========`;
|
|
|
|
|
|
|
|
const headerSection = (
|
|
|
|
title: string,
|
|
|
|
data: Readonly<Record<string, unknown>>
|
|
|
|
): string => {
|
|
|
|
const sortedEntries = sortBy(Object.entries(data), ([key]) => key);
|
|
|
|
return [
|
|
|
|
headerSectionTitle(title),
|
|
|
|
...sortedEntries.map(
|
|
|
|
([key, value]) => `${key}: ${redactAll(String(value))}`
|
|
|
|
),
|
|
|
|
'',
|
|
|
|
].join('\n');
|
|
|
|
};
|
|
|
|
|
2021-10-06 16:16:51 +00:00
|
|
|
const getHeader = (
|
|
|
|
{
|
|
|
|
capabilities,
|
|
|
|
remoteConfig,
|
|
|
|
statistics,
|
|
|
|
user,
|
|
|
|
}: Omit<FetchLogIpcData, 'logEntries'>,
|
|
|
|
nodeVersion: string,
|
|
|
|
appVersion: string
|
|
|
|
): string =>
|
2021-09-17 18:27:53 +00:00
|
|
|
[
|
|
|
|
headerSection('System info', {
|
|
|
|
Time: Date.now(),
|
|
|
|
'User agent': window.navigator.userAgent,
|
2021-10-06 16:16:51 +00:00
|
|
|
'Node version': nodeVersion,
|
2021-09-17 18:27:53 +00:00
|
|
|
Environment: getEnvironment(),
|
2021-10-06 16:16:51 +00:00
|
|
|
'App version': appVersion,
|
2021-09-17 18:27:53 +00:00
|
|
|
}),
|
|
|
|
headerSection('User info', user),
|
|
|
|
headerSection('Capabilities', capabilities),
|
|
|
|
headerSection('Remote config', remoteConfig),
|
|
|
|
headerSection('Statistics', statistics),
|
|
|
|
headerSectionTitle('Logs'),
|
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
const getLevel = memoize((level: LogLevel): string => {
|
|
|
|
const text = getLogLevelString(level);
|
|
|
|
return text.toUpperCase().padEnd(levelMaxLength, ' ');
|
|
|
|
});
|
|
|
|
|
|
|
|
function formatLine(mightBeEntry: unknown): string {
|
|
|
|
const entry: LogEntryType = isLogEntry(mightBeEntry)
|
|
|
|
? mightBeEntry
|
|
|
|
: {
|
|
|
|
level: LogLevel.Error,
|
|
|
|
msg: `Invalid IPC data when fetching logs. Here's what we could recover: ${reallyJsonStringify(
|
|
|
|
mightBeEntry
|
|
|
|
)}`,
|
|
|
|
time: new Date().toISOString(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return `${getLevel(entry.level)} ${entry.time} ${entry.msg}`;
|
|
|
|
}
|
|
|
|
|
2021-10-06 16:16:51 +00:00
|
|
|
export function fetch(
|
|
|
|
nodeVersion: string,
|
|
|
|
appVersion: string
|
|
|
|
): Promise<string> {
|
2021-09-17 18:27:53 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
ipc.send('fetch-log');
|
|
|
|
|
|
|
|
ipc.on('fetched-log', (_event, data: unknown) => {
|
|
|
|
let header: string;
|
|
|
|
let body: string;
|
|
|
|
if (isFetchLogIpcData(data)) {
|
|
|
|
const { logEntries } = data;
|
2021-10-06 16:16:51 +00:00
|
|
|
header = getHeader(data, nodeVersion, appVersion);
|
2021-09-17 18:27:53 +00:00
|
|
|
body = logEntries.map(formatLine).join('\n');
|
|
|
|
} else {
|
|
|
|
header = headerSectionTitle('Partial logs');
|
|
|
|
const entry: LogEntryType = {
|
|
|
|
level: LogLevel.Error,
|
|
|
|
msg: 'Invalid IPC data when fetching logs; dropping all logs',
|
|
|
|
time: new Date().toISOString(),
|
|
|
|
};
|
|
|
|
body = formatLine(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = `${header}\n${body}`;
|
|
|
|
resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|