Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09d33523b6 | ||
|
|
170cbbfb4f | ||
|
|
9d5fb7e966 | ||
|
|
dc11d21d72 | ||
|
|
774e31ef27 | ||
|
|
7c4a981434 | ||
|
|
358311e8ac | ||
|
|
2551462003 | ||
|
|
60d5b78df0 | ||
|
|
f04017e05b | ||
|
|
cb0098ec85 | ||
|
|
d007d5f230 | ||
|
|
07c6fbe747 | ||
|
|
5e3d413b33 | ||
|
|
bcf8586fbf | ||
|
|
6431182ec3 | ||
|
|
cef623f493 | ||
|
|
0c00c1a2e3 | ||
|
|
ab8805f272 | ||
|
|
da180fcc92 | ||
|
|
a9d80bef8f | ||
|
|
010561596e | ||
|
|
9fc67e37f6 | ||
|
|
151ae50a07 | ||
|
|
b0c6165fee | ||
|
|
6a3ed65e06 | ||
|
|
4395ab508f | ||
|
|
a681ceb4f5 | ||
|
|
58cd51d776 | ||
|
|
b450fa5c44 |
@@ -0,0 +1,35 @@
|
|||||||
|
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
|
||||||
|
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
|
||||||
|
|
||||||
|
name: BuildCI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [master]
|
||||||
|
pull_request:
|
||||||
|
branches: [master]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
node-version: [16.x]
|
||||||
|
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Use Node.js ${{ matrix.node-version }}
|
||||||
|
uses: actions/setup-node@v2
|
||||||
|
with:
|
||||||
|
node-version: ${{ matrix.node-version }}
|
||||||
|
- run: npm install
|
||||||
|
- run: npm test
|
||||||
|
- run: npm run build
|
||||||
|
- uses: actions/upload-artifact@v2
|
||||||
|
with:
|
||||||
|
name: my-dist
|
||||||
|
path: |
|
||||||
|
main.js
|
||||||
|
manifest.json
|
||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"id": "obsdian-save-remote",
|
"id": "obsdian-save-remote",
|
||||||
"name": "Save remote",
|
"name": "Save remote",
|
||||||
"version": "0.0.2",
|
"version": "0.0.7",
|
||||||
"minAppVersion": "0.12.15",
|
"minAppVersion": "0.12.15",
|
||||||
"description": "This is yet another plugin allowing users to sync notes between local device and the cloud.",
|
"description": "This is yet another plugin allowing users to sync notes between local device and the cloud.",
|
||||||
"author": "fyears",
|
"author": "fyears",
|
||||||
|
|||||||
+15
-3
@@ -1,22 +1,29 @@
|
|||||||
{
|
{
|
||||||
"name": "obsidian-save-remote",
|
"name": "obsidian-save-remote",
|
||||||
"version": "0.0.1",
|
"version": "0.0.7",
|
||||||
"description": "This is yet another sync plugin for Obsidian app.",
|
"description": "This is yet another sync plugin for Obsidian app.",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "webpack --mode development --watch",
|
"dev": "webpack --mode development --watch",
|
||||||
"build": "webpack --mode production",
|
"build": "webpack --mode production",
|
||||||
"format": "npx prettier --write .",
|
"format": "npx prettier --write .",
|
||||||
"clean": "npx rimraf main.js"
|
"clean": "npx rimraf main.js",
|
||||||
|
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha -r ts-node/register 'tests/**/*.ts'"
|
||||||
},
|
},
|
||||||
"source": "main.ts",
|
"source": "main.ts",
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/chai": "^4.2.22",
|
||||||
|
"@types/mime-types": "^2.1.1",
|
||||||
|
"@types/mocha": "^9.0.0",
|
||||||
"@types/node": "^14.14.37",
|
"@types/node": "^14.14.37",
|
||||||
|
"chai": "^4.3.4",
|
||||||
|
"mocha": "^9.1.3",
|
||||||
"prettier": "^2.4.1",
|
"prettier": "^2.4.1",
|
||||||
"terser-webpack-plugin": "^5.2.4",
|
"terser-webpack-plugin": "^5.2.4",
|
||||||
"ts-loader": "^9.2.6",
|
"ts-loader": "^9.2.6",
|
||||||
|
"ts-node": "^10.4.0",
|
||||||
"tslib": "^2.2.0",
|
"tslib": "^2.2.0",
|
||||||
"typescript": "^4.4.4",
|
"typescript": "^4.4.4",
|
||||||
"webdav-server": "^2.6.2",
|
"webdav-server": "^2.6.2",
|
||||||
@@ -26,13 +33,18 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@aws-sdk/client-s3": "^3.37.0",
|
"@aws-sdk/client-s3": "^3.37.0",
|
||||||
"@aws-sdk/signature-v4-crt": "^3.37.0",
|
"@aws-sdk/signature-v4-crt": "^3.37.0",
|
||||||
"@types/mime-types": "^2.1.1",
|
"acorn": "^8.5.0",
|
||||||
"aws-crt": "^1.10.1",
|
"aws-crt": "^1.10.1",
|
||||||
|
"buffer": "^6.0.3",
|
||||||
"codemirror": "^5.63.1",
|
"codemirror": "^5.63.1",
|
||||||
"lovefield-ts": "^0.7.0",
|
"lovefield-ts": "^0.7.0",
|
||||||
"mime-types": "^2.1.33",
|
"mime-types": "^2.1.33",
|
||||||
"obsidian": "^0.12.0",
|
"obsidian": "^0.12.0",
|
||||||
|
"path-browserify": "^1.0.1",
|
||||||
|
"process": "^0.11.10",
|
||||||
|
"rfc4648": "^1.5.0",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
|
"stream-browserify": "^3.0.0",
|
||||||
"webdav": "^4.7.0",
|
"webdav": "^4.7.0",
|
||||||
"webdav-fs": "^4.0.0"
|
"webdav-fs": "^4.0.0"
|
||||||
}
|
}
|
||||||
|
|||||||
+135
@@ -0,0 +1,135 @@
|
|||||||
|
import { base32, base64 } from "rfc4648";
|
||||||
|
import {
|
||||||
|
bufferToArrayBuffer,
|
||||||
|
arrayBufferToBuffer,
|
||||||
|
hexStringToTypedArray,
|
||||||
|
arrayBufferToHex,
|
||||||
|
} from "./misc";
|
||||||
|
|
||||||
|
const DEFAULT_ITER = 10000;
|
||||||
|
|
||||||
|
const getKeyIVFromPassword = async (
|
||||||
|
salt: Uint8Array,
|
||||||
|
password: string,
|
||||||
|
rounds: number = DEFAULT_ITER
|
||||||
|
) => {
|
||||||
|
const k1 = await window.crypto.subtle.importKey(
|
||||||
|
"raw",
|
||||||
|
new TextEncoder().encode(password),
|
||||||
|
{ name: "PBKDF2" },
|
||||||
|
false,
|
||||||
|
["deriveKey", "deriveBits"]
|
||||||
|
);
|
||||||
|
|
||||||
|
const k2 = await window.crypto.subtle.deriveBits(
|
||||||
|
{
|
||||||
|
name: "PBKDF2",
|
||||||
|
salt: salt,
|
||||||
|
iterations: rounds,
|
||||||
|
hash: "SHA-256",
|
||||||
|
},
|
||||||
|
k1,
|
||||||
|
256 + 128
|
||||||
|
);
|
||||||
|
|
||||||
|
return k2;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const encryptArrayBuffer = async (
|
||||||
|
arrBuf: ArrayBuffer,
|
||||||
|
password: string,
|
||||||
|
rounds: number = DEFAULT_ITER,
|
||||||
|
saltHex: string = ""
|
||||||
|
) => {
|
||||||
|
let salt: Uint8Array;
|
||||||
|
if (saltHex !== "") {
|
||||||
|
salt = hexStringToTypedArray(saltHex);
|
||||||
|
} else {
|
||||||
|
salt = window.crypto.getRandomValues(new Uint8Array(8));
|
||||||
|
}
|
||||||
|
|
||||||
|
const derivedKey = await getKeyIVFromPassword(salt, password, rounds);
|
||||||
|
const key = derivedKey.slice(0, 32);
|
||||||
|
const iv = derivedKey.slice(32, 32 + 16);
|
||||||
|
|
||||||
|
const keyCrypt = await window.crypto.subtle.importKey(
|
||||||
|
"raw",
|
||||||
|
key,
|
||||||
|
{ name: "AES-CBC" },
|
||||||
|
false,
|
||||||
|
["encrypt", "decrypt"]
|
||||||
|
);
|
||||||
|
|
||||||
|
const enc = (await window.crypto.subtle.encrypt(
|
||||||
|
{ name: "AES-CBC", iv },
|
||||||
|
keyCrypt,
|
||||||
|
arrBuf
|
||||||
|
)) as ArrayBuffer;
|
||||||
|
|
||||||
|
const prefix = new TextEncoder().encode("Salted__");
|
||||||
|
|
||||||
|
const res = new Uint8Array([...prefix, ...salt, ...new Uint8Array(enc)]);
|
||||||
|
|
||||||
|
return bufferToArrayBuffer(res);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const decryptArrayBuffer = async (
|
||||||
|
arrBuf: ArrayBuffer,
|
||||||
|
password: string,
|
||||||
|
rounds: number = DEFAULT_ITER
|
||||||
|
) => {
|
||||||
|
const prefix = arrBuf.slice(0, 8);
|
||||||
|
const salt = arrBuf.slice(8, 16);
|
||||||
|
const derivedKey = await getKeyIVFromPassword(
|
||||||
|
new Uint8Array(salt),
|
||||||
|
password,
|
||||||
|
rounds
|
||||||
|
);
|
||||||
|
const key = derivedKey.slice(0, 32);
|
||||||
|
const iv = derivedKey.slice(32, 32 + 16);
|
||||||
|
|
||||||
|
const keyCrypt = await window.crypto.subtle.importKey(
|
||||||
|
"raw",
|
||||||
|
key,
|
||||||
|
{ name: "AES-CBC" },
|
||||||
|
false,
|
||||||
|
["encrypt", "decrypt"]
|
||||||
|
);
|
||||||
|
|
||||||
|
const dec = (await window.crypto.subtle.decrypt(
|
||||||
|
{ name: "AES-CBC", iv },
|
||||||
|
keyCrypt,
|
||||||
|
arrBuf.slice(16)
|
||||||
|
)) as ArrayBuffer;
|
||||||
|
|
||||||
|
return dec;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const encryptStringToBase32 = async (
|
||||||
|
text: string,
|
||||||
|
password: string,
|
||||||
|
rounds: number = DEFAULT_ITER,
|
||||||
|
saltHex: string = ""
|
||||||
|
) => {
|
||||||
|
const enc = await encryptArrayBuffer(
|
||||||
|
bufferToArrayBuffer(new TextEncoder().encode(text)),
|
||||||
|
password,
|
||||||
|
rounds,
|
||||||
|
saltHex
|
||||||
|
);
|
||||||
|
return base32.stringify(new Uint8Array(enc));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const decryptBase32ToString = async (
|
||||||
|
text: string,
|
||||||
|
password: string,
|
||||||
|
rounds: number = DEFAULT_ITER
|
||||||
|
) => {
|
||||||
|
return new TextDecoder().decode(
|
||||||
|
await decryptArrayBuffer(
|
||||||
|
bufferToArrayBuffer(base32.parse(text)),
|
||||||
|
password,
|
||||||
|
rounds
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
+19
-9
@@ -27,10 +27,12 @@ import { DEFAULT_S3_CONFIG, getS3Client, listFromRemote, S3Config } from "./s3";
|
|||||||
|
|
||||||
interface SaveRemotePluginSettings {
|
interface SaveRemotePluginSettings {
|
||||||
s3?: S3Config;
|
s3?: S3Config;
|
||||||
|
password?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_SETTINGS: SaveRemotePluginSettings = {
|
const DEFAULT_SETTINGS: SaveRemotePluginSettings = {
|
||||||
s3: DEFAULT_S3_CONFIG,
|
s3: DEFAULT_S3_CONFIG,
|
||||||
|
password: "",
|
||||||
};
|
};
|
||||||
|
|
||||||
export default class SaveRemotePlugin extends Plugin {
|
export default class SaveRemotePlugin extends Plugin {
|
||||||
@@ -60,13 +62,6 @@ export default class SaveRemotePlugin extends Plugin {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// this.addRibbonIcon("dice", "Misc", async () => {
|
|
||||||
// const a = this.app.vault.getAllLoadedFiles();
|
|
||||||
// console.log(a);
|
|
||||||
// const h = await getAllRecords(this.db);
|
|
||||||
// console.log(h);
|
|
||||||
// });
|
|
||||||
|
|
||||||
this.addRibbonIcon("switch", "Save Remote", async () => {
|
this.addRibbonIcon("switch", "Save Remote", async () => {
|
||||||
if (this.syncStatus !== "idle") {
|
if (this.syncStatus !== "idle") {
|
||||||
new Notice("Save Remote already running!");
|
new Notice("Save Remote already running!");
|
||||||
@@ -87,7 +82,8 @@ export default class SaveRemotePlugin extends Plugin {
|
|||||||
remoteRsp.Contents,
|
remoteRsp.Contents,
|
||||||
local,
|
local,
|
||||||
localHistory,
|
localHistory,
|
||||||
this.db
|
this.db,
|
||||||
|
this.settings.password
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const [key, val] of Object.entries(mixedStates)) {
|
for (const [key, val] of Object.entries(mixedStates)) {
|
||||||
@@ -106,7 +102,8 @@ export default class SaveRemotePlugin extends Plugin {
|
|||||||
this.settings.s3,
|
this.settings.s3,
|
||||||
this.db,
|
this.db,
|
||||||
this.app.vault,
|
this.app.vault,
|
||||||
mixedStates
|
mixedStates,
|
||||||
|
this.settings.password
|
||||||
);
|
);
|
||||||
|
|
||||||
new Notice("Save Remote finish!");
|
new Notice("Save Remote finish!");
|
||||||
@@ -230,5 +227,18 @@ class SaveRemoteSettingTab extends PluginSettingTab {
|
|||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName("password")
|
||||||
|
.setDesc("password")
|
||||||
|
.addText((text) =>
|
||||||
|
text
|
||||||
|
.setPlaceholder("")
|
||||||
|
.setValue(`${this.plugin.settings.password}`)
|
||||||
|
.onChange(async (value) => {
|
||||||
|
this.plugin.settings.password = value;
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+41
-1
@@ -1,6 +1,8 @@
|
|||||||
import { Vault } from "obsidian";
|
import { Vault } from "obsidian";
|
||||||
import * as path from "path";
|
import * as path from "path";
|
||||||
|
|
||||||
|
import { base32 } from "rfc4648";
|
||||||
|
|
||||||
export type SUPPORTED_SERVICES_TYPE = "s3" | "webdav" | "ftp";
|
export type SUPPORTED_SERVICES_TYPE = "s3" | "webdav" | "ftp";
|
||||||
|
|
||||||
export const ignoreHiddenFiles = (item: string) => {
|
export const ignoreHiddenFiles = (item: string) => {
|
||||||
@@ -46,6 +48,44 @@ export const mkdirpInVault = async (thePath: string, vault: Vault) => {
|
|||||||
* @param b Buffer
|
* @param b Buffer
|
||||||
* @returns ArrayBuffer
|
* @returns ArrayBuffer
|
||||||
*/
|
*/
|
||||||
export const bufferToArrayBuffer = (b: Buffer) => {
|
export const bufferToArrayBuffer = (b: Buffer | Uint8Array) => {
|
||||||
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
|
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple func.
|
||||||
|
* @param b
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export const arrayBufferToBuffer = (b: ArrayBuffer) => {
|
||||||
|
return Buffer.from(b);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const arrayBufferToBase64 = (b: ArrayBuffer) => {
|
||||||
|
return arrayBufferToBuffer(b).toString("base64");
|
||||||
|
};
|
||||||
|
|
||||||
|
export const arrayBufferToHex = (b: ArrayBuffer) => {
|
||||||
|
return arrayBufferToBuffer(b).toString("hex");
|
||||||
|
};
|
||||||
|
|
||||||
|
export const base64ToArrayBuffer = (b64text: string) => {
|
||||||
|
return bufferToArrayBuffer(Buffer.from(b64text, "base64"));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* https://stackoverflow.com/questions/43131242
|
||||||
|
* @param hex
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export const hexStringToTypedArray = (hex: string) => {
|
||||||
|
return new Uint8Array(
|
||||||
|
hex.match(/[\da-f]{2}/gi).map(function (h) {
|
||||||
|
return parseInt(h, 16);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const base64ToBase32 = (a: string) => {
|
||||||
|
return base32.stringify(Buffer.from(a, "base64"));
|
||||||
|
};
|
||||||
|
|||||||
@@ -14,8 +14,13 @@ import {
|
|||||||
|
|
||||||
import type { _Object } from "@aws-sdk/client-s3";
|
import type { _Object } from "@aws-sdk/client-s3";
|
||||||
|
|
||||||
import { bufferToArrayBuffer, mkdirpInVault } from "./misc";
|
import {
|
||||||
|
arrayBufferToBuffer,
|
||||||
|
bufferToArrayBuffer,
|
||||||
|
mkdirpInVault,
|
||||||
|
} from "./misc";
|
||||||
import * as mime from "mime-types";
|
import * as mime from "mime-types";
|
||||||
|
import { decryptArrayBuffer, encryptArrayBuffer } from "./encrypt";
|
||||||
|
|
||||||
export interface S3Config {
|
export interface S3Config {
|
||||||
s3Endpoint: string;
|
s3Endpoint: string;
|
||||||
@@ -65,8 +70,14 @@ export const uploadToRemote = async (
|
|||||||
s3Config: S3Config,
|
s3Config: S3Config,
|
||||||
fileOrFolderPath: string,
|
fileOrFolderPath: string,
|
||||||
vault: Vault,
|
vault: Vault,
|
||||||
isRecursively: boolean = false
|
isRecursively: boolean = false,
|
||||||
|
password: string = "",
|
||||||
|
remoteEncryptedKey: string = ""
|
||||||
) => {
|
) => {
|
||||||
|
let uploadFile = fileOrFolderPath;
|
||||||
|
if (password !== "") {
|
||||||
|
uploadFile = remoteEncryptedKey;
|
||||||
|
}
|
||||||
const isFolder = fileOrFolderPath.endsWith("/");
|
const isFolder = fileOrFolderPath.endsWith("/");
|
||||||
|
|
||||||
const DEFAULT_CONTENT_TYPE = "application/octet-stream";
|
const DEFAULT_CONTENT_TYPE = "application/octet-stream";
|
||||||
@@ -79,7 +90,7 @@ export const uploadToRemote = async (
|
|||||||
await s3Client.send(
|
await s3Client.send(
|
||||||
new PutObjectCommand({
|
new PutObjectCommand({
|
||||||
Bucket: s3Config.s3BucketName,
|
Bucket: s3Config.s3BucketName,
|
||||||
Key: fileOrFolderPath,
|
Key: uploadFile,
|
||||||
Body: "",
|
Body: "",
|
||||||
ContentType: contentType,
|
ContentType: contentType,
|
||||||
})
|
})
|
||||||
@@ -88,20 +99,28 @@ export const uploadToRemote = async (
|
|||||||
} else {
|
} else {
|
||||||
// file
|
// file
|
||||||
// we ignore isRecursively parameter here
|
// we ignore isRecursively parameter here
|
||||||
const contentType =
|
let contentType = DEFAULT_CONTENT_TYPE;
|
||||||
mime.contentType(mime.lookup(fileOrFolderPath) || DEFAULT_CONTENT_TYPE) ||
|
if (password === "") {
|
||||||
DEFAULT_CONTENT_TYPE;
|
contentType =
|
||||||
const content = await vault.adapter.readBinary(fileOrFolderPath);
|
mime.contentType(
|
||||||
const body = Buffer.from(content);
|
mime.lookup(fileOrFolderPath) || DEFAULT_CONTENT_TYPE
|
||||||
|
) || DEFAULT_CONTENT_TYPE;
|
||||||
|
}
|
||||||
|
const localContent = await vault.adapter.readBinary(fileOrFolderPath);
|
||||||
|
let remoteContent = localContent;
|
||||||
|
if (password !== "") {
|
||||||
|
remoteContent = await encryptArrayBuffer(localContent, password);
|
||||||
|
}
|
||||||
|
const body = arrayBufferToBuffer(remoteContent);
|
||||||
await s3Client.send(
|
await s3Client.send(
|
||||||
new PutObjectCommand({
|
new PutObjectCommand({
|
||||||
Bucket: s3Config.s3BucketName,
|
Bucket: s3Config.s3BucketName,
|
||||||
Key: fileOrFolderPath,
|
Key: uploadFile,
|
||||||
Body: body,
|
Body: body,
|
||||||
ContentType: contentType,
|
ContentType: contentType,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
return await getRemoteMeta(s3Client, s3Config, fileOrFolderPath);
|
return await getRemoteMeta(s3Client, s3Config, uploadFile);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -169,22 +188,35 @@ export const downloadFromRemote = async (
|
|||||||
s3Config: S3Config,
|
s3Config: S3Config,
|
||||||
fileOrFolderPath: string,
|
fileOrFolderPath: string,
|
||||||
vault: Vault,
|
vault: Vault,
|
||||||
mtime: number
|
mtime: number,
|
||||||
|
password: string = "",
|
||||||
|
remoteEncryptedKey: string = ""
|
||||||
) => {
|
) => {
|
||||||
const isFolder = fileOrFolderPath.endsWith("/");
|
const isFolder = fileOrFolderPath.endsWith("/");
|
||||||
|
|
||||||
await mkdirpInVault(fileOrFolderPath, vault);
|
await mkdirpInVault(fileOrFolderPath, vault);
|
||||||
|
|
||||||
|
// the file is always local file
|
||||||
|
// we need to encrypt it
|
||||||
|
|
||||||
if (isFolder) {
|
if (isFolder) {
|
||||||
// mkdirp locally is enough
|
// mkdirp locally is enough
|
||||||
// do nothing here
|
// do nothing here
|
||||||
} else {
|
} else {
|
||||||
const content = await downloadFromRemoteRaw(
|
let downloadFile = fileOrFolderPath;
|
||||||
|
if (password !== "") {
|
||||||
|
downloadFile = remoteEncryptedKey;
|
||||||
|
}
|
||||||
|
const remoteContent = await downloadFromRemoteRaw(
|
||||||
s3Client,
|
s3Client,
|
||||||
s3Config,
|
s3Config,
|
||||||
fileOrFolderPath
|
downloadFile
|
||||||
);
|
);
|
||||||
await vault.adapter.writeBinary(fileOrFolderPath, content, {
|
let localContent = remoteContent;
|
||||||
|
if (password !== "") {
|
||||||
|
localContent = await decryptArrayBuffer(remoteContent, password);
|
||||||
|
}
|
||||||
|
await vault.adapter.writeBinary(fileOrFolderPath, localContent, {
|
||||||
mtime: mtime,
|
mtime: mtime,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -200,12 +232,25 @@ export const downloadFromRemote = async (
|
|||||||
export const deleteFromRemote = async (
|
export const deleteFromRemote = async (
|
||||||
s3Client: S3Client,
|
s3Client: S3Client,
|
||||||
s3Config: S3Config,
|
s3Config: S3Config,
|
||||||
fileOrFolderPath: string
|
fileOrFolderPath: string,
|
||||||
|
password: string = "",
|
||||||
|
remoteEncryptedKey: string = ""
|
||||||
) => {
|
) => {
|
||||||
if (fileOrFolderPath === "/") {
|
if (fileOrFolderPath === "/") {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (fileOrFolderPath.endsWith("/")) {
|
let remoteFileName = fileOrFolderPath;
|
||||||
|
if (password !== "") {
|
||||||
|
remoteFileName = remoteEncryptedKey;
|
||||||
|
}
|
||||||
|
await s3Client.send(
|
||||||
|
new DeleteObjectCommand({
|
||||||
|
Bucket: s3Config.s3BucketName,
|
||||||
|
Key: remoteFileName,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
if (fileOrFolderPath.endsWith("/") && password === "") {
|
||||||
const x = await listFromRemote(s3Client, s3Config, fileOrFolderPath);
|
const x = await listFromRemote(s3Client, s3Config, fileOrFolderPath);
|
||||||
x.Contents.forEach(async (element) => {
|
x.Contents.forEach(async (element) => {
|
||||||
await s3Client.send(
|
await s3Client.send(
|
||||||
@@ -215,12 +260,9 @@ export const deleteFromRemote = async (
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
} else if (fileOrFolderPath.endsWith("/") && password !== "") {
|
||||||
|
// TODO
|
||||||
} else {
|
} else {
|
||||||
await s3Client.send(
|
// pass
|
||||||
new DeleteObjectCommand({
|
|
||||||
Bucket: s3Config.s3BucketName,
|
|
||||||
Key: fileOrFolderPath,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+41
-9
@@ -17,6 +17,7 @@ import {
|
|||||||
downloadFromRemote,
|
downloadFromRemote,
|
||||||
} from "./s3";
|
} from "./s3";
|
||||||
import { mkdirpInVault } from "./misc";
|
import { mkdirpInVault } from "./misc";
|
||||||
|
import { decryptBase32ToString, encryptStringToBase32 } from "./encrypt";
|
||||||
|
|
||||||
type DecisionType =
|
type DecisionType =
|
||||||
| "undecided"
|
| "undecided"
|
||||||
@@ -44,26 +45,32 @@ interface FileOrFolderMixedState {
|
|||||||
decision?: DecisionType;
|
decision?: DecisionType;
|
||||||
syncDone?: "done";
|
syncDone?: "done";
|
||||||
decision_branch?: number;
|
decision_branch?: number;
|
||||||
|
remote_encrypted_key?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ensembleMixedStates = async (
|
export const ensembleMixedStates = async (
|
||||||
remote: S3ObjectType[],
|
remote: S3ObjectType[],
|
||||||
local: TAbstractFile[],
|
local: TAbstractFile[],
|
||||||
deleteHistory: FileFolderHistoryRecord[],
|
deleteHistory: FileFolderHistoryRecord[],
|
||||||
db: lf.DatabaseConnection
|
db: lf.DatabaseConnection,
|
||||||
|
password: string = ""
|
||||||
) => {
|
) => {
|
||||||
const results = {} as Record<string, FileOrFolderMixedState>;
|
const results = {} as Record<string, FileOrFolderMixedState>;
|
||||||
|
|
||||||
if (remote !== undefined) {
|
if (remote !== undefined) {
|
||||||
for (const entry of remote) {
|
for (const entry of remote) {
|
||||||
|
const remoteEncryptedKey = entry.Key;
|
||||||
|
let key = remoteEncryptedKey;
|
||||||
|
if (password !== "") {
|
||||||
|
key = await decryptBase32ToString(remoteEncryptedKey, password);
|
||||||
|
}
|
||||||
const backwardMapping = await getSyncMetaMappingByRemoteKeyS3(
|
const backwardMapping = await getSyncMetaMappingByRemoteKeyS3(
|
||||||
db,
|
db,
|
||||||
entry.Key,
|
key,
|
||||||
entry.LastModified.valueOf(),
|
entry.LastModified.valueOf(),
|
||||||
entry.ETag
|
entry.ETag
|
||||||
);
|
);
|
||||||
|
|
||||||
let key = entry.Key;
|
|
||||||
let r = {} as FileOrFolderMixedState;
|
let r = {} as FileOrFolderMixedState;
|
||||||
if (backwardMapping !== undefined) {
|
if (backwardMapping !== undefined) {
|
||||||
key = backwardMapping.local_key;
|
key = backwardMapping.local_key;
|
||||||
@@ -72,6 +79,7 @@ export const ensembleMixedStates = async (
|
|||||||
exist_remote: true,
|
exist_remote: true,
|
||||||
mtime_remote: backwardMapping.local_mtime,
|
mtime_remote: backwardMapping.local_mtime,
|
||||||
size_remote: backwardMapping.local_size,
|
size_remote: backwardMapping.local_size,
|
||||||
|
remote_encrypted_key: remoteEncryptedKey,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
r = {
|
r = {
|
||||||
@@ -79,6 +87,7 @@ export const ensembleMixedStates = async (
|
|||||||
exist_remote: true,
|
exist_remote: true,
|
||||||
mtime_remote: entry.LastModified.valueOf(),
|
mtime_remote: entry.LastModified.valueOf(),
|
||||||
size_remote: entry.Size,
|
size_remote: entry.Size,
|
||||||
|
remote_encrypted_key: remoteEncryptedKey,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
if (results.hasOwnProperty(key)) {
|
if (results.hasOwnProperty(key)) {
|
||||||
@@ -86,6 +95,7 @@ export const ensembleMixedStates = async (
|
|||||||
results[key].exist_remote = r.exist_remote;
|
results[key].exist_remote = r.exist_remote;
|
||||||
results[key].mtime_remote = r.mtime_remote;
|
results[key].mtime_remote = r.mtime_remote;
|
||||||
results[key].size_remote = r.size_remote;
|
results[key].size_remote = r.size_remote;
|
||||||
|
results[key].remote_encrypted_key = r.remote_encrypted_key;
|
||||||
} else {
|
} else {
|
||||||
results[key] = r;
|
results[key] = r;
|
||||||
}
|
}
|
||||||
@@ -277,13 +287,21 @@ export const doActualSync = async (
|
|||||||
s3Config: S3Config,
|
s3Config: S3Config,
|
||||||
db: lf.DatabaseConnection,
|
db: lf.DatabaseConnection,
|
||||||
vault: Vault,
|
vault: Vault,
|
||||||
keyStates: Record<string, FileOrFolderMixedState>
|
keyStates: Record<string, FileOrFolderMixedState>,
|
||||||
|
password: string = ""
|
||||||
) => {
|
) => {
|
||||||
Object.entries(keyStates)
|
Object.entries(keyStates)
|
||||||
.sort((k, v) => -(k as string).length)
|
.sort((k, v) => -(k as string).length)
|
||||||
.map(async ([k, v]) => {
|
.map(async ([k, v]) => {
|
||||||
const key = k as string;
|
const key = k as string;
|
||||||
const state = v as FileOrFolderMixedState;
|
const state = v as FileOrFolderMixedState;
|
||||||
|
let remoteEncryptedKey = key;
|
||||||
|
if (password !== "") {
|
||||||
|
remoteEncryptedKey = state.remote_encrypted_key;
|
||||||
|
if (remoteEncryptedKey === undefined || remoteEncryptedKey === "") {
|
||||||
|
remoteEncryptedKey = await encryptStringToBase32(key, password);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
state.decision === undefined ||
|
state.decision === undefined ||
|
||||||
@@ -299,7 +317,9 @@ export const doActualSync = async (
|
|||||||
s3Config,
|
s3Config,
|
||||||
state.key,
|
state.key,
|
||||||
vault,
|
vault,
|
||||||
state.mtime_remote
|
state.mtime_remote,
|
||||||
|
password,
|
||||||
|
remoteEncryptedKey
|
||||||
);
|
);
|
||||||
await clearDeleteRenameHistoryOfKey(db, state.key);
|
await clearDeleteRenameHistoryOfKey(db, state.key);
|
||||||
} else if (state.decision === "upload_clearhist") {
|
} else if (state.decision === "upload_clearhist") {
|
||||||
@@ -308,7 +328,9 @@ export const doActualSync = async (
|
|||||||
s3Config,
|
s3Config,
|
||||||
state.key,
|
state.key,
|
||||||
vault,
|
vault,
|
||||||
false
|
false,
|
||||||
|
password,
|
||||||
|
remoteEncryptedKey
|
||||||
);
|
);
|
||||||
await upsertSyncMetaMappingDataS3(
|
await upsertSyncMetaMappingDataS3(
|
||||||
db,
|
db,
|
||||||
@@ -328,10 +350,18 @@ export const doActualSync = async (
|
|||||||
s3Config,
|
s3Config,
|
||||||
state.key,
|
state.key,
|
||||||
vault,
|
vault,
|
||||||
state.mtime_remote
|
state.mtime_remote,
|
||||||
|
password,
|
||||||
|
remoteEncryptedKey
|
||||||
);
|
);
|
||||||
} else if (state.decision === "delremote_clearhist") {
|
} else if (state.decision === "delremote_clearhist") {
|
||||||
await deleteFromRemote(s3Client, s3Config, state.key);
|
await deleteFromRemote(
|
||||||
|
s3Client,
|
||||||
|
s3Config,
|
||||||
|
state.key,
|
||||||
|
password,
|
||||||
|
remoteEncryptedKey
|
||||||
|
);
|
||||||
await clearDeleteRenameHistoryOfKey(db, state.key);
|
await clearDeleteRenameHistoryOfKey(db, state.key);
|
||||||
} else if (state.decision === "upload") {
|
} else if (state.decision === "upload") {
|
||||||
const remoteObjMeta = await uploadToRemote(
|
const remoteObjMeta = await uploadToRemote(
|
||||||
@@ -339,7 +369,9 @@ export const doActualSync = async (
|
|||||||
s3Config,
|
s3Config,
|
||||||
state.key,
|
state.key,
|
||||||
vault,
|
vault,
|
||||||
false
|
false,
|
||||||
|
password,
|
||||||
|
remoteEncryptedKey
|
||||||
);
|
);
|
||||||
await upsertSyncMetaMappingDataS3(
|
await upsertSyncMetaMappingDataS3(
|
||||||
db,
|
db,
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
A secret text 你好世界
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
import * as fs from "fs";
|
||||||
|
import { expect } from "chai";
|
||||||
|
import { base64ToBase32 } from "../src/misc";
|
||||||
|
import {
|
||||||
|
decryptBase32ToString,
|
||||||
|
encryptStringToBase32,
|
||||||
|
} from "../src/encrypt";
|
||||||
|
|
||||||
|
describe("Encryption tests", () => {
|
||||||
|
beforeEach(function () {
|
||||||
|
global.window = {
|
||||||
|
crypto: require("crypto").webcrypto,
|
||||||
|
} as any;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should encrypt string", async () => {
|
||||||
|
const k = "dkjdhkfhdkjgsdklxxd";
|
||||||
|
const password = "hey";
|
||||||
|
expect(await encryptStringToBase32(k, password)).to.not.equal(k);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should encrypt and decrypt string and get the same result returned", async () => {
|
||||||
|
const k = "jfkkjkjbce7983ycdeknkkjckooAIUHIDIBIE((*BII)njD/d/dd/d/sjxhux";
|
||||||
|
const password = "hfiuibec989###oiu982bj1`";
|
||||||
|
const enc = await encryptStringToBase32(k, password);
|
||||||
|
console.log(enc);
|
||||||
|
const dec = await decryptBase32ToString(enc, password);
|
||||||
|
console.log(dec);
|
||||||
|
expect(dec).equal(k);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should encrypt and get the same result as openssl", async () => {
|
||||||
|
const fileContent = (
|
||||||
|
await fs.readFileSync(__dirname + "/sometext.txt")
|
||||||
|
).toString("utf-8");
|
||||||
|
const password = "somepassword";
|
||||||
|
const saltHex = "8302F586FAB491EC";
|
||||||
|
const enc = await encryptStringToBase32(
|
||||||
|
fileContent,
|
||||||
|
password,
|
||||||
|
undefined,
|
||||||
|
saltHex
|
||||||
|
);
|
||||||
|
|
||||||
|
// two command returns same result:
|
||||||
|
// cat ./sometext.txt | openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 10000 -base64 -pass pass:somepassword
|
||||||
|
// openssl enc -p -aes-256-cbc -S 8302F586FAB491EC -pbkdf2 -iter 10000 -base64 -pass pass:somepassword -in ./sometext.txt
|
||||||
|
const opensslBase64Res =
|
||||||
|
"U2FsdGVkX1+DAvWG+rSR7MSa+yJav1zCE7SSXiBooqwI5Q+LMpIthpk/pXkLj+25";
|
||||||
|
// we output base32, so we need some transformation
|
||||||
|
const opensslBase32Res = base64ToBase32(opensslBase64Res);
|
||||||
|
|
||||||
|
expect(enc).equal(opensslBase32Res);
|
||||||
|
});
|
||||||
|
});
|
||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"0.0.2": "0.12.15"
|
"0.0.7": "0.12.15"
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-21
@@ -1,4 +1,5 @@
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
const webpack = require("webpack");
|
||||||
const TerserPlugin = require("terser-webpack-plugin");
|
const TerserPlugin = require("terser-webpack-plugin");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
@@ -9,6 +10,16 @@ module.exports = {
|
|||||||
path: __dirname,
|
path: __dirname,
|
||||||
libraryTarget: "commonjs",
|
libraryTarget: "commonjs",
|
||||||
},
|
},
|
||||||
|
plugins: [
|
||||||
|
// Work around for Buffer is undefined:
|
||||||
|
// https://github.com/webpack/changelog-v5/issues/10
|
||||||
|
new webpack.ProvidePlugin({
|
||||||
|
Buffer: ["buffer", "Buffer"],
|
||||||
|
}),
|
||||||
|
new webpack.ProvidePlugin({
|
||||||
|
process: "process/browser",
|
||||||
|
}),
|
||||||
|
],
|
||||||
module: {
|
module: {
|
||||||
rules: [
|
rules: [
|
||||||
{
|
{
|
||||||
@@ -20,31 +31,31 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
extensions: [".tsx", ".ts", ".js"],
|
extensions: [".tsx", ".ts", ".js"],
|
||||||
mainFields: ["module", "main"],
|
mainFields: ["browser", "module", "main"],
|
||||||
fallback: {
|
fallback: {
|
||||||
assert: require.resolve("assert"),
|
// assert: require.resolve("assert"),
|
||||||
buffer: require.resolve("buffer/"),
|
// buffer: require.resolve("buffer/"),
|
||||||
console: require.resolve("console-browserify"),
|
// console: require.resolve("console-browserify"),
|
||||||
constants: require.resolve("constants-browserify"),
|
// constants: require.resolve("constants-browserify"),
|
||||||
crypto: require.resolve("crypto-browserify"),
|
// crypto: require.resolve("crypto-browserify"),
|
||||||
domain: require.resolve("domain-browser"),
|
// domain: require.resolve("domain-browser"),
|
||||||
events: require.resolve("events"),
|
// events: require.resolve("events"),
|
||||||
http: require.resolve("stream-http"),
|
// http: require.resolve("stream-http"),
|
||||||
https: require.resolve("https-browserify"),
|
// https: require.resolve("https-browserify"),
|
||||||
os: require.resolve("os-browserify/browser"),
|
// os: require.resolve("os-browserify/browser"),
|
||||||
path: require.resolve("path-browserify"),
|
path: require.resolve("path-browserify"),
|
||||||
punycode: require.resolve("punycode"),
|
// punycode: require.resolve("punycode"),
|
||||||
process: require.resolve("process/browser"),
|
process: require.resolve("process/browser"),
|
||||||
querystring: require.resolve("querystring-es3"),
|
// querystring: require.resolve("querystring-es3"),
|
||||||
stream: require.resolve("stream-browserify"),
|
stream: require.resolve("stream-browserify"),
|
||||||
string_decoder: require.resolve("string_decoder"),
|
// string_decoder: require.resolve("string_decoder"),
|
||||||
sys: require.resolve("util"),
|
// sys: require.resolve("util"),
|
||||||
timers: require.resolve("timers-browserify"),
|
// timers: require.resolve("timers-browserify"),
|
||||||
tty: require.resolve("tty-browserify"),
|
// tty: require.resolve("tty-browserify"),
|
||||||
url: require.resolve("url"),
|
// url: require.resolve("url"),
|
||||||
util: require.resolve("util"),
|
// util: require.resolve("util"),
|
||||||
vm: require.resolve("vm-browserify"),
|
// vm: require.resolve("vm-browserify"),
|
||||||
zlib: require.resolve("browserify-zlib"),
|
// zlib: require.resolve("browserify-zlib"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
externals: {
|
externals: {
|
||||||
|
|||||||
Reference in New Issue
Block a user