encode config

This commit is contained in:
fyears 2022-01-05 10:04:58 +08:00
parent f0dd3bb5ef
commit 9eddfb02d6
4 changed files with 107 additions and 2 deletions

60
src/configPersist.ts Normal file
View File

@ -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;
};

View File

@ -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() {

View File

@ -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("");
};

View File

@ -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);
});
});