From 9eddfb02d6e0c8daeca609af51a83cc1be08b291 Mon Sep 17 00:00:00 2001 From: fyears Date: Wed, 5 Jan 2022 10:04:58 +0800 Subject: [PATCH] encode config --- src/configPersist.ts | 60 +++++++++++++++++++++++++++++++++++++ src/main.ts | 5 ++-- src/misc.ts | 9 ++++++ tests/configPersist.test.ts | 35 ++++++++++++++++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/configPersist.ts create mode 100644 tests/configPersist.test.ts diff --git a/src/configPersist.ts b/src/configPersist.ts new file mode 100644 index 0000000..21ca829 --- /dev/null +++ b/src/configPersist.ts @@ -0,0 +1,60 @@ +import { base64, base64url } from "rfc4648"; +import { reverseString } from "./misc"; + +import type { RemotelySavePluginSettings } from "./baseTypes"; + +import * as origLog from "loglevel"; +const log = origLog.getLogger("rs-default"); + +const DEFAULT_README: string = + "Do NOT modify this manually. It's generated automatically."; + +interface MessyConfigType { + readme: string; + d: string; +} + +/** + * this should accept the result after loadData(); + */ +export const messyConfigToNormal = ( + x: MessyConfigType | RemotelySavePluginSettings +): RemotelySavePluginSettings => { + log.debug("loading, original config on disk:"); + log.debug(x); + if ("readme" in x && "d" in x) { + // we should decode + const y = JSON.parse( + ( + base64url.parse(reverseString(x["d"]), { + out: Buffer.allocUnsafe as any, + loose: true, + }) as Buffer + ).toString("utf-8") + ); + log.debug("loading, parsed config is:"); + log.debug(y); + return y; + } else { + // return as is + log.debug("loading, parsed config is the same"); + return x; + } +}; + +/** + * this should accept the result of original config + */ +export const normalConfigToMessy = (x: RemotelySavePluginSettings) => { + const y = { + readme: DEFAULT_README, + d: reverseString( + base64url.stringify(Buffer.from(JSON.stringify(x), "utf-8"), { + pad: false, + }) + ), + }; + log.debug("encoding, encoded config is:"); + log.debug(y); + return y; +}; diff --git a/src/main.ts b/src/main.ts index 4a90e34..178fb13 100644 --- a/src/main.ts +++ b/src/main.ts @@ -34,6 +34,7 @@ import { DEFAULT_WEBDAV_CONFIG } from "./remoteForWebdav"; import { RemotelySaveSettingTab } from "./settings"; import type { SyncStatusType } from "./sync"; import { doActualSync, getSyncPlan, isPasswordOk } from "./sync"; +import { messyConfigToNormal, normalConfigToMessy } from "./configPersist"; import * as origLog from "loglevel"; const log = origLog.getLogger("rs-default"); @@ -384,7 +385,7 @@ export default class RemotelySavePlugin extends Plugin { this.settings = Object.assign( {}, cloneDeep(DEFAULT_SETTINGS), - await this.loadData() + messyConfigToNormal(await this.loadData()) ); if (this.settings.dropbox.clientID === "") { this.settings.dropbox.clientID = DEFAULT_SETTINGS.dropbox.clientID; @@ -398,7 +399,7 @@ export default class RemotelySavePlugin extends Plugin { } async saveSettings() { - await this.saveData(this.settings); + await this.saveData(normalConfigToMessy(this.settings)); } async checkIfOauthExpires() { diff --git a/src/misc.ts b/src/misc.ts index cfc7a08..b143f2f 100644 --- a/src/misc.ts +++ b/src/misc.ts @@ -199,3 +199,12 @@ export const getRandomArrayBuffer = (byteLength: number) => { const k = window.crypto.getRandomValues(new Uint8Array(byteLength)); return bufferToArrayBuffer(k); }; + +/** + * https://stackoverflow.com/questions/958908 + * @param x + * @returns + */ +export const reverseString = (x: string) => { + return [...x].reverse().join(""); +}; diff --git a/tests/configPersist.test.ts b/tests/configPersist.test.ts new file mode 100644 index 0000000..cde0a7c --- /dev/null +++ b/tests/configPersist.test.ts @@ -0,0 +1,35 @@ +import * as chai from "chai"; +import chaiAsPromised from "chai-as-promised"; + +import { RemotelySavePluginSettings } from "../src/baseTypes"; +import { messyConfigToNormal, normalConfigToMessy } from "../src/configPersist"; + +chai.use(chaiAsPromised); +const expect = chai.expect; + +const DEFAULT_SETTINGS: RemotelySavePluginSettings = { + s3: { + s3AccessKeyID: "acc", + } as any, + webdav: { + address: "addr", + } as any, + dropbox: { + username: "ζ΅‹θ―•δΈ­ζ–‡", + } as any, + onedrive: { + username: "test 🍎 emoji", + } as any, + password: "password", + serviceType: "s3", + currLogLevel: "info", +}; + +describe("Config Persist tests", () => { + it("should encrypt go back and forth conrrectly", async () => { + const k = DEFAULT_SETTINGS; + const k2 = normalConfigToMessy(k); + const k3 = messyConfigToNormal(k2); + expect(k3).to.deep.equal(k); + }); +});