Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode Global Settings File #389

Merged
merged 9 commits into from
Sep 27, 2023
3 changes: 3 additions & 0 deletions src/renderer/src/electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export let remote = {};
export let app = null;
export let path = null;
export let log = null;
export let crypto = null;

if (isElectron) {
try {
Expand All @@ -19,6 +20,8 @@ if (isElectron) {
fs = require("fs-extra"); // File System
os = require("os");

crypto = require("crypto");

remote = require("@electron/remote");
app = remote.app;

Expand Down
60 changes: 55 additions & 5 deletions src/renderer/src/progress/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import Swal from "sweetalert2";

import { guidedProgressFilePath, reloadPageToHome, isStorybook, appDirectory } from "../dependencies/simple.js";
import { fs } from "../electron/index.js";
import {
guidedProgressFilePath,
reloadPageToHome,
isStorybook,
appDirectory,
homeDirectory,
} from "../dependencies/simple.js";
import { fs, crypto } from "../electron/index.js";

import { joinPath, runOnLoad } from "../globals.js";
import { merge } from "../stories/pages/utils.js";
import { updateAppProgress, updateFile } from "./update.js";
Expand All @@ -11,18 +18,61 @@ import * as operations from "./operations";

export * from "./update";

var re = /[0-9A-Fa-f]{6}/g;

function encode(message) {
if (!crypto) return message;
const mykey = crypto.createCipher("aes-128-cbc", homeDirectory);
const mystr = mykey.update(message, "utf8", "hex");
return mystr + mykey.final("hex");
}

// Try to decode the value
function decode(message) {
if (!crypto || !/[0-9A-Fa-f]{6}/g.test(message)) return message;

try {
const mykey = crypto.createDecipher("aes-128-cbc", homeDirectory);
const mystr = mykey.update(message, "hex", "utf8");
return mystr + mykey.final("utf8");
} catch {
return message;
}
}

function drill(o, callback) {
if (o && typeof o === "object") {
const copy = { ...o };
for (let k in copy) copy[k] = drill(copy[k], callback);
return copy;
} else return callback(o);
}

function encodeObject(o) {
return drill(o, (v) => (typeof v === "string" ? encode(v) : v));
}

function decodeObject(o) {
return drill(o, (v) => (typeof v === "string" ? decode(v) : v));
}

class GlobalAppConfig {
path = `${appDirectory}/config.json`;
data = {};

constructor() {
const exists = fs ? fs.existsSync(this.path) : localStorage[this.path];
if (exists) this.data = JSON.parse(fs ? fs.readFileSync(this.path) : localStorage.getItem(this.path));
if (exists) {
const data = JSON.parse(fs ? fs.readFileSync(this.path) : localStorage.getItem(this.path));
this.data = decodeObject(data);
}
}

save() {
if (fs) fs.writeFileSync(this.path, JSON.stringify(this.data, null, 2));
else localStorage.setItem(this.path, JSON.stringify(this.data));
const encoded = encodeObject(this.data);

if (fs) fs.writeFileSync(this.path, JSON.stringify(encoded, null, 2));
else localStorage.setItem(this.path, JSON.stringify(encoded));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/stories/pages/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class Page extends LitElement {
save = async (overrides, runBeforeSave = true) => {
if (runBeforeSave) await this.beforeSave();
save(this, overrides);
this.info.states.saved = true;
if ("states" in this.info) this.info.states.saved = true;
this.unsavedUpdates = false;
};

Expand Down
Loading