Files
mattermostest/src/main/server/serverAPI.ts
Tasos Boulis 161ae99e94 [MM-47202] - Desktop diagnostics (#2439)
* Add custom assertion to match object in array (#2358)

* Add custom assertion to match object in array

* Remove test that is not implemented yet

* [MM-48213] Add "Run diagnostics" menu item under Help (#2359)

* Add submenu item that runs diagnostics

* Add custom assertion to match object in array

* Remove test that is not implemented yet

* Add tests

* Add translation

* [MM-47206] Diagnostics steps setup (#2361)

* Add baseline code for diagnostics and their steps

* Fix failing test

* [MM-47206] [MM-48155] Obfuscate logs (#2369)

* Add logging hooks to mask sensitive data

* Add hook that truncates long strings in diagnostics logs

* Add template file for creating steps

* Add readme inside diagnostics

* [MM-48145] Diagnostics step 2 - internet connectivity (#2372)

* Add diagnostics step 2 - internet connectivity check

* Update tests

* [MM-48144] Diagnostics Step - Configure logger (#2390)

* Configure logger

* Move configure logger into step1

* Add file extension to fileName variable

* Diagnostics Step 2: Validate configuration (#2391)

* Resolve conflicts with base branch

* Update test and implement Code review suggestion

* Fix failing test

* [MM-48147]Diagnostics step 3 - server connectivity (#2397)

* Add step3: Check server connectivity by using the /api/v4/system/ping endpoint

* Fix failing tests

* Add better obfuscator functions that mask all types of data (#2399)

* Add better obfuscator functions that mask all types of data(string, array, objects)

* Update tests

* [MM-48148] Add Diagnostics step 4 - session validation (#2398)

* Add diagnostics step 4 - session data validation

* Fix failing tests

* [MM-48152] Add diagnostics step 5 - BrowserWindows checks (#2404)

* Add diagnostics step 5 - browserwindow checks for main window

* Add tests

* [MM-48151] Diagnostics step 6 - Permissions (#2409)

* Add diagnostics step 6 - Permissions check

* Check permissions for microphone ond screen onn mac, windows

* Update tests count in tests

* [MM-48551] Diagnostics step 7 - Performance & Memory (#2410)

* Add diagnostics step 6 - Permissions check

* Check permissions for microphone ond screen onn mac, windows

* Update tests count in tests

* Add diagnostics step 7 - performance and memory

* Fix failing tests

* [MM-48153] Add diagnostics step 8 - Log heuristics (#2418)

* Add diagnostics step 8 - Log heuristics

* Add diagnostics step 9 - config (#2422)

* [MM-48556] Diagnostics Step 10 - Crash reports (#2423)

* Add diagnostics step 9 - config

* Add diagnostics step 10 - include crash reports

* Update tests

* Add diagnostics step 11 - cookies report (#2427)

* [MM-48157] Diagnostics report (#2432)

* Add better logging and pretty print report

* Update last step

* Update log message

* Move log after hooks so that path is masked

* Use correct directory for diagnostics files
2022-12-02 16:33:42 +02:00

67 lines
2.5 KiB
TypeScript

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {net, session} from 'electron';
import log from 'electron-log';
import {COOKIE_NAME_AUTH_TOKEN, COOKIE_NAME_CSRF, COOKIE_NAME_USER_ID} from 'common/constants';
export async function getServerAPI<T>(url: URL, isAuthenticated: boolean, onSuccess?: (data: T) => void, onAbort?: () => void, onError?: (error: Error) => void) {
if (isAuthenticated) {
const cookies = await session.defaultSession.cookies.get({});
if (!cookies) {
log.error('Cannot authenticate, no cookies present');
return;
}
// Filter out cookies that aren't part of our domain
const filteredCookies = cookies.filter((cookie) => cookie.domain && url.toString().indexOf(cookie.domain) >= 0);
const userId = filteredCookies.find((cookie) => cookie.name === COOKIE_NAME_USER_ID);
const csrf = filteredCookies.find((cookie) => cookie.name === COOKIE_NAME_CSRF);
const authToken = filteredCookies.find((cookie) => cookie.name === COOKIE_NAME_AUTH_TOKEN);
if (!userId || !csrf || !authToken) {
// Missing cookies needed for req
log.error(`Cannot authenticate, required cookies for ${url.origin} not found`);
return;
}
}
const req = net.request({
url: url.toString(),
session: session.defaultSession,
useSessionCookies: true,
});
if (onSuccess) {
req.on('response', (response: Electron.IncomingMessage) => {
log.silly('getServerAPI.response', response);
if (response.statusCode === 200) {
response.on('data', (chunk: Buffer) => {
log.silly('getServerAPI.response.data', `${chunk}`);
const raw = `${chunk}`;
try {
const data = JSON.parse(raw) as T;
onSuccess(data);
} catch (e) {
const error = `Error parsing server data from ${url.toString()}`;
log.error(error);
onError?.(new Error(error));
}
});
} else {
onError?.(new Error(`Bad status code requesting from ${url.toString()}`));
}
response.on('error', onError || (() => {}));
});
}
if (onAbort) {
req.on('abort', onAbort);
}
if (onError) {
req.on('error', onError);
}
req.end();
}