summaryrefslogtreecommitdiffhomepage
path: root/desktop/packages/mullvad-vpn/src/main/problem-report.ts
blob: be84814d7298e18df6ef27dac9c150ca78892063 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { execFile } from 'child_process';
import { randomUUID } from 'crypto';
import { app, shell } from 'electron';
import * as path from 'path';

import log from '../shared/logging';
import { IpcMainEventChannel } from './ipc-event-channel';
import { resolveBin } from './proc';

export function registerIpcListeners() {
  IpcMainEventChannel.problemReport.handleCollectLogs(collectLogs);

  IpcMainEventChannel.problemReport.handleSendReport(({ email, message, savedReportId }) => {
    return send(email, message, savedReportId);
  });

  IpcMainEventChannel.problemReport.handleViewLog((savedReportId) =>
    shell.openPath(getProblemReportPath(savedReportId)),
  );
}

function collectLogs(toRedact?: string): Promise<string> {
  const id = randomUUID();
  const reportPath = getProblemReportPath(id);
  const executable = resolveBin('mullvad-problem-report');
  const args = ['collect', '--output', reportPath];
  if (toRedact) {
    args.push('--redact', toRedact);
  }

  return new Promise((resolve, reject) => {
    execFile(executable, args, { windowsHide: true }, (error, stdout, stderr) => {
      if (error) {
        log.error(
          `Failed to collect a problem report.
            Stdout: ${stdout.toString()}
            Stderr: ${stderr.toString()}`,
        );
        reject(error.message);
      } else {
        log.verbose(`Problem report was written to ${reportPath}`);
        resolve(id);
      }
    });
  });
}

function send(email: string, message: string, savedReportId: string): Promise<void> {
  const executable = resolveBin('mullvad-problem-report');
  const reportPath = getProblemReportPath(savedReportId);
  const args = ['send', '--email', email, '--message', message, '--report', reportPath];

  return new Promise((resolve, reject) => {
    execFile(executable, args, { windowsHide: true }, (error, stdout, stderr) => {
      if (error) {
        log.error(
          `Failed to send a problem report.
          Stdout: ${stdout.toString()}
          Stderr: ${stderr.toString()}`,
        );
        reject(error.message);
      } else {
        log.info('Problem report was sent.');
        resolve();
      }
    });
  });
}

function getProblemReportPath(id: string): string {
  return path.join(app.getPath('temp'), `${id}.log`);
}