2022-01-28 08:03:07 +00:00
|
|
|
import { app } from 'electron';
|
|
|
|
import fs from 'fs';
|
2022-05-27 09:39:24 +00:00
|
|
|
import fetch from 'node-fetch';
|
2022-01-28 08:03:07 +00:00
|
|
|
import path from 'path';
|
2022-05-27 09:39:24 +00:00
|
|
|
import { Creds } from 'utils/types';
|
|
|
|
import { rendererLog } from './helpers';
|
2022-01-28 08:03:07 +00:00
|
|
|
|
2022-05-27 09:39:24 +00:00
|
|
|
export function getUrlAndTokenString(): Creds {
|
2022-01-28 08:03:07 +00:00
|
|
|
const inProduction = app.isPackaged;
|
2022-05-27 09:39:24 +00:00
|
|
|
const empty: Creds = { errorLogUrl: '', telemetryUrl: '', tokenString: '' };
|
2022-01-28 12:38:01 +00:00
|
|
|
let errLogCredsPath = path.join(
|
|
|
|
process.resourcesPath,
|
2022-03-14 07:01:09 +00:00
|
|
|
'../creds/log_creds.txt'
|
2022-01-28 12:38:01 +00:00
|
|
|
);
|
|
|
|
if (!fs.existsSync(errLogCredsPath)) {
|
2022-03-14 07:01:09 +00:00
|
|
|
errLogCredsPath = path.join(__dirname, '../log_creds.txt');
|
2022-01-28 12:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync(errLogCredsPath)) {
|
|
|
|
!inProduction && console.log(`${errLogCredsPath} doesn't exist, can't log`);
|
2022-03-14 07:01:09 +00:00
|
|
|
return empty;
|
2022-01-28 12:38:01 +00:00
|
|
|
}
|
2022-01-28 08:03:07 +00:00
|
|
|
|
2022-05-27 09:39:24 +00:00
|
|
|
let apiKey, apiSecret, errorLogUrl, telemetryUrl;
|
2022-01-28 08:03:07 +00:00
|
|
|
try {
|
2022-05-27 09:39:24 +00:00
|
|
|
[apiKey, apiSecret, errorLogUrl, telemetryUrl] = fs
|
2022-01-28 08:03:07 +00:00
|
|
|
.readFileSync(errLogCredsPath, 'utf-8')
|
|
|
|
.split('\n')
|
|
|
|
.filter((f) => f.length);
|
|
|
|
} catch (err) {
|
|
|
|
if (!inProduction) {
|
2022-01-28 12:38:01 +00:00
|
|
|
console.log(`logging error using creds at: ${errLogCredsPath} failed`);
|
2022-01-28 08:03:07 +00:00
|
|
|
console.log(err);
|
|
|
|
}
|
2022-03-14 07:01:09 +00:00
|
|
|
return empty;
|
2022-01-28 08:03:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 07:01:09 +00:00
|
|
|
return {
|
2022-05-27 09:39:24 +00:00
|
|
|
errorLogUrl: encodeURI(errorLogUrl),
|
2022-03-14 07:01:09 +00:00
|
|
|
telemetryUrl: encodeURI(telemetryUrl),
|
|
|
|
tokenString: `token ${apiKey}:${apiSecret}`,
|
|
|
|
};
|
2022-01-28 08:03:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-27 09:39:24 +00:00
|
|
|
export async function sendError(body: string) {
|
|
|
|
const { errorLogUrl, tokenString } = getUrlAndTokenString();
|
2022-01-28 08:03:07 +00:00
|
|
|
const headers = {
|
|
|
|
Authorization: tokenString,
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
};
|
|
|
|
|
2022-05-27 09:39:24 +00:00
|
|
|
await fetch(errorLogUrl, { method: 'POST', headers, body }).catch((err) => {
|
|
|
|
rendererLog(err);
|
2022-02-02 07:18:03 +00:00
|
|
|
});
|
2022-01-28 08:03:07 +00:00
|
|
|
}
|