Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: build scripts + frontend UI for encrypted notebook (stage 6) #18034

Daniel545d0e0bcfe8
diff --git a/app/src/config/tabs/accessTab.ts b/app/src/config/tabs/accessTab.ts
index 2f73764863..c5f04d4364 100644
--- a/app/src/config/tabs/accessTab.ts
+++ b/app/src/config/tabs/accessTab.ts
@@ -315,6 +315,151 @@ const mountPublishAuthAccounts = (root: HTMLElement) => {
export const registerAccessTab = (tab: SettingTabBuilder) => {
registerAccessAuthGroup(tab);
+ registerEncryptedNotebookGroup(tab);
registerAccessServerGroup(tab);
registerAccessPublishGroup(tab);
};
+
+const registerEncryptedNotebookGroup = (tab: SettingTabBuilder) => {
+ if (window.siyuan.config.readonly) {
+ return;
+ }
+ const group = tab.group("encryptedNotebook", window.siyuan.languages.encryptedNotebook);
+ group.slot({
+ key: "encryptedNotebookStatus",
+ html: () => `<div class="fn__flex b3-label config-item config-wrap">
+ <div class="fn__flex-1 fn__flex-center">
+ <div class="ft__on-surface ft__smaller" id="encryptedNotebookDesc">${window.siyuan.languages.encryptedNotebookTip}</div>
+ <div class="fn__hr--b"></div>
+ <div id="encryptedNotebookActions"></div>
+ </div>
+ <div class="fn__space"></div>
+ <div class="fn__flex-center">
+ <input type="checkbox" id="encryptedNotebookSwitch" class="b3-switch">
+ </div>
+</div>`,
+ afterMount: mountEncryptedNotebook,
+ });
+};
+
+const mountEncryptedNotebook = (root: HTMLElement) => {
+ const switchElement = root.querySelector("#encryptedNotebookSwitch") as HTMLInputElement;
+ const actionsElement = root.querySelector("#encryptedNotebookActions");
+ const refresh = () => {
+ fetchPost("/api/notebook/getEncryptedNotebookStatus", {}, (response) => {
+ const enabled = response.data.enabled;
+ switchElement.checked = enabled;
+ actionsElement.innerHTML = enabled
+ ? `<button class="b3-button b3-button--white" id="changeMasterPasswordBtn">${window.siyuan.languages.changeMasterPassword}</button>`
+ : "";
+ const changeBtn = root.querySelector("#changeMasterPasswordBtn");
+ if (changeBtn) {
+ changeBtn.addEventListener("click", () => openChangeMasterPasswordDialog());
+ }
+ });
+ };
+ refresh();
+
+ switchElement.addEventListener("change", () => {
+ if (switchElement.checked) {
+ // 切到 ON:弹设密码框
+ openEnableEncryptedDialog(() => {
+ refresh();
+ }, () => {
+ switchElement.checked = false; // 取消则恢复
+ });
+ } else {
+ // 切到 OFF:暂不支持禁用(需要确认所有加密笔记本已删除)
+ showMessage(window.siyuan.languages.encryptedNotebookTip, 4000);
+ switchElement.checked = true;
+ }
+ });
+};
+
+const openEnableEncryptedDialog = (onSuccess: () => void, onCancel: () => void) => {
+ const dialog = new Dialog({
+ title: window.siyuan.languages.setMasterPassword,
+ content: `<div class="b3-dialog__content">
+ <input type="password" placeholder="${window.siyuan.languages.masterPassword}" class="b3-text-field fn__block">
+ <div class="fn__hr"></div>
+ <input type="password" placeholder="${window.siyuan.languages.confirmMasterPassword}" class="b3-text-field fn__block">
+ <div class="fn__hr--b"></div>
+ <label class="b3-label__text"><input type="checkbox" id="encRiskConfirm"> ${window.siyuan.languages.encryptedNotebookRiskTip}</label>
+</div>
+<div class="b3-dialog__action">
+ <button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
+ <button class="b3-button b3-button--text" disabled>${window.siyuan.languages.confirm}</button>
+</div>`,
+ width: isMobile() ? "92vw" : "520px",
+ });
+ const btnsElement = dialog.element.querySelectorAll(".b3-button");
+ const inputs = dialog.element.querySelectorAll("input");
+ const confirmBtn = btnsElement[1] as HTMLButtonElement;
+ const riskCheckbox = dialog.element.querySelector("#encRiskConfirm") as HTMLInputElement;
+ riskCheckbox.addEventListener("change", () => {
+ confirmBtn.disabled = !riskCheckbox.checked;
+ });
+ btnsElement[0].addEventListener("click", () => {
+ dialog.destroy();
+ onCancel();
+ });
+ confirmBtn.addEventListener("click", () => {
+ const pwd1 = (inputs[0] as HTMLInputElement).value;
+ const pwd2 = (inputs[1] as HTMLInputElement).value;
+ if (!pwd1) {
+ showMessage(window.siyuan.languages.masterPassword);
+ return;
+ }
+ if (pwd1 !== pwd2) {
+ showMessage(window.siyuan.languages.passwordNoMatch);
+ return;
+ }
+ fetchPost("/api/notebook/enableEncryptedNotebooks", {password: pwd1}, () => {
+ showMessage(window.siyuan.languages.encryptedNotebookEnabled);
+ dialog.destroy();
+ onSuccess();
+ });
+ });
+};
+
+const openChangeMasterPasswordDialog = () => {
+ const dialog = new Dialog({
+ title: window.siyuan.languages.changeMasterPassword,
+ content: `<div class="b3-dialog__content">
+ <input type="password" placeholder="${window.siyuan.languages.oldMasterPassword}" class="b3-text-field fn__block">
+ <div class="fn__hr"></div>
+ <input type="password" placeholder="${window.siyuan.languages.newMasterPassword}" class="b3-text-field fn__block">
+ <div class="fn__hr"></div>
+ <input type="password" placeholder="${window.siyuan.languages.confirmMasterPassword}" class="b3-text-field fn__block">
+</div>
+<div class="b3-dialog__action">
+ <button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
+ <button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
+</div>`,
+ width: isMobile() ? "92vw" : "520px",
+ });
+ const btnsElement = dialog.element.querySelectorAll(".b3-button");
+ const inputs = dialog.element.querySelectorAll("input");
+ btnsElement[0].addEventListener("click", () => {
+ dialog.destroy();
+ });
+ btnsElement[1].addEventListener("click", () => {
+ const oldPwd = (inputs[0] as HTMLInputElement).value;
+ const newPwd = (inputs[1] as HTMLInputElement).value;
+ const confirmPwd = (inputs[2] as HTMLInputElement).value;
+ if (!oldPwd || !newPwd) {
+ return;
+ }
+ if (newPwd !== confirmPwd) {
+ showMessage(window.siyuan.languages.passwordNoMatch);
+ return;
+ }
+ fetchPost("/api/notebook/changeMasterPassword", {
+ oldPassword: oldPwd,
+ newPassword: newPwd
+ }, () => {
+ showMessage(window.siyuan.languages.changeMasterPassword);
+ dialog.destroy();
+ });
+ });
+};
diff --git a/app/src/layout/dock/Files.ts b/app/src/layout/dock/Files.ts
index ad9d661681..bdeaa75d2d 100644
--- a/app/src/layout/dock/Files.ts
+++ b/app/src/layout/dock/Files.ts
@@ -16,7 +16,7 @@ import {
} from "../../protyle/util/publishAccess";
import {fetchPost, fetchSyncPost} from "../../util/fetch";
import {openEmojiPanel, unicode2Emoji} from "../../emoji";
-import {mountHelp, newNotebook} from "../../util/mount";
+import {mountHelp, newEncryptedNotebook, newNotebook, openEncryptedNotebook} from "../../util/mount";
import {isNotCtrl, isOnlyMeta, setStorageVal, updateHotkeyAfterTip} from "../../protyle/util/compatibility";
import {openFileById} from "../../editor/util";
import {
@@ -109,9 +109,16 @@ export class Files extends Model {
event.preventDefault();
break;
} else if (type === "open") {
- fetchPost("/api/notebook/openNotebook", {
- notebook: target.getAttribute("data-url")
- });
+ const notebookId = target.getAttribute("data-url");
+ const liElement = target.closest("li");
+ // 加密笔记本关闭(锁定)时点"打开"先弹解锁框,解锁成功后再挂载
+ if (liElement && liElement.getAttribute("data-encrypted") === "true") {
+ openEncryptedNotebook(app, notebookId, liElement.querySelector(".b3-list-item__text").textContent);
+ } else {
+ fetchPost("/api/notebook/openNotebook", {
+ notebook: notebookId
+ });
+ }
window.siyuan.menus.menu.remove();
event.stopPropagation();
event.preventDefault();
@@ -931,10 +938,14 @@ export class Files extends Model {
private genNotebook(item: INotebook) {
const editingPublishAccess = this.element.classList.contains("file-tree__publish-access--active");
- const emojiHTML = `<span class="b3-list-item__icon b3-tooltips b3-tooltips__e${editingPublishAccess ? " fn__none" : ""}" aria-label="${window.siyuan.languages.changeIcon}">${unicode2Emoji(item.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].note)}</span>`;
+ // 加密笔记本关闭(锁定)时用锁图标替换 emoji,提示需解锁;打开(解锁)后恢复正常 emoji
+ const iconContent = (item.encrypted && item.closed)
+ ? "<svg style=\"width:14px;height:14px\"><use xlink:href=\"#iconLock\"></use></svg>"
+ : unicode2Emoji(item.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].note);
+ const emojiHTML = `<span class="b3-list-item__icon b3-tooltips b3-tooltips__e${editingPublishAccess ? " fn__none" : ""}" aria-label="${window.siyuan.languages.changeIcon}">${iconContent}</span>`;
const switchHTML = `<span class="b3-list-item__switch b3-tooltips b3-tooltips__e${editingPublishAccess ? "" : " fn__none"}" aria-label="${window.siyuan.languages.publishAccess}">${getPublishAccessOptionByLevel("public").iconHTML}</span>`;
if (item.closed) {
- return `<li data-url="${item.id}" class="b3-list-item b3-list-item--hide-action">
+ return `<li data-url="${item.id}" class="b3-list-item b3-list-item--hide-action"${item.encrypted ? ' data-encrypted="true"' : ""}>
<span class="b3-list-item__toggle fn__hidden">
<svg class="b3-list-item__arrow"><use xlink:href="#iconRight"></use></svg>
</span>
@@ -1430,6 +1441,13 @@ aria-label="${ariaLabel}">${getDocDisplayName(item.name, item.titleEmpty, true)}
newNotebook();
}
}).element);
+ window.siyuan.menus.menu.append(new MenuItem({
+ icon: "iconLock",
+ label: window.siyuan.languages.newEncryptedNotebook,
+ click: () => {
+ newEncryptedNotebook();
+ }
+ }).element);
}
window.siyuan.menus.menu.append(new MenuItem({
icon: "iconRefresh",
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index cbaf70c392..bf50b76b6b 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -517,6 +517,7 @@ interface INotebook {
newFlashcardCount?: string;
flashcardCount?: string;
sortMode: number;
+ encrypted?: boolean;
}
interface ISiyuan {
diff --git a/app/src/util/mount.ts b/app/src/util/mount.ts
index 4de60ca888..8f27b8a63d 100644
--- a/app/src/util/mount.ts
+++ b/app/src/util/mount.ts
@@ -134,3 +134,95 @@ export const newNotebook = () => {
dialog.destroy();
});
};
+
+export const newEncryptedNotebook = () => {
+ // 先检查加密功能是否已启用;未启用则提示去设置页启用
+ fetchPost("/api/notebook/getEncryptedNotebookStatus", {}, (response) => {
+ if (!response.data.enabled) {
+ showMessage(window.siyuan.languages.encryptedNotebookTip, 6000);
+ return;
+ }
+ const dialog = new Dialog({
+ title: window.siyuan.languages.newEncryptedNotebook,
+ content: `<div class="b3-dialog__content">
+ <input placeholder="${window.siyuan.languages.notebookName}" class="b3-text-field fn__block">
+ <div class="fn__hr"></div>
+ <input type="password" placeholder="${window.siyuan.languages.masterPassword}" class="b3-text-field fn__block">
+</div>
+<div class="b3-dialog__action">
+ <button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
+ <button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
+</div>`,
+ width: isMobile() ? "92vw" : "520px"
+ });
+ const btnsElement = dialog.element.querySelectorAll(".b3-button");
+ const inputs = dialog.element.querySelectorAll("input");
+ dialog.bindInput(inputs[0], () => {
+ btnsElement[1].dispatchEvent(new CustomEvent("click"));
+ });
+ btnsElement[0].addEventListener("click", () => {
+ dialog.destroy();
+ });
+ btnsElement[1].addEventListener("click", () => {
+ const name = inputs[0].value;
+ const password = inputs[1].value;
+ if (!validateName(name)) {
+ return false;
+ }
+ if (!password) {
+ showMessage(window.siyuan.languages.masterPassword);
+ return false;
+ }
+ fetchPost("/api/notebook/createEncryptedNotebook", {
+ name: replaceFileName(name),
+ password
+ }, () => {
+ // 创建后是锁定状态,引导用户解锁
+ showMessage(window.siyuan.languages.encryptedNotebook, 3000);
+ });
+ dialog.destroy();
+ });
+ });
+};
+
+export const openEncryptedNotebook = (app: App, notebookId: string, name: string) => {
+ const dialog = new Dialog({
+ title: window.siyuan.languages.unlockEncryptedNotebook.replace("${x}", name),
+ content: `<div class="b3-dialog__content">
+ <input type="password" placeholder="${window.siyuan.languages.masterPassword}" class="b3-text-field fn__block">
+</div>
+<div class="b3-dialog__action">
+ <button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
+ <button class="b3-button b3-button--text">${window.siyuan.languages.unlockEncryptedNotebook.replace("${x}", name)}</button>
+</div>`,
+ width: isMobile() ? "92vw" : "520px"
+ });
+ const btnsElement = dialog.element.querySelectorAll(".b3-button");
+ const inputElement = dialog.element.querySelector("input");
+ dialog.bindInput(inputElement, () => {
+ btnsElement[1].dispatchEvent(new CustomEvent("click"));
+ });
+ btnsElement[0].addEventListener("click", () => {
+ dialog.destroy();
+ });
+ btnsElement[1].addEventListener("click", () => {
+ const password = inputElement.value;
+ if (!password) {
+ return false;
+ }
+ btnsElement[1].setAttribute("disabled", "disabled");
+ // 先解锁(派生 KEK + 解 DEK + 打开加密 db),成功后再挂载
+ fetchPost("/api/notebook/unlockBox", {
+ notebook: notebookId,
+ password
+ }, () => {
+ fetchPost("/api/notebook/openNotebook", {
+ notebook: notebookId
+ });
+ dialog.destroy();
+ }, (err) => {
+ btnsElement[1].removeAttribute("disabled");
+ showMessage(err.msg || window.siyuan.languages.incorrectMasterPassword);
+ });
+ });
+};
diff --git a/scripts/darwin-build.sh b/scripts/darwin-build.sh
index e62a5ea456..2b5c5a57ff 100755
--- a/scripts/darwin-build.sh
+++ b/scripts/darwin-build.sh
@@ -71,13 +71,13 @@ if [[ "$TARGET" == 'amd64' || "$TARGET" == 'all' ]]; then
echo
echo 'Building Kernel amd64'
export GOARCH=amd64
- go build -tags fts5 -o "../app/kernel-darwin/SiYuan-Kernel" -ldflags "-s -w" .
+ go build -tags "fts5 sqlcipher" -o "../app/kernel-darwin/SiYuan-Kernel" -ldflags "-s -w" .
fi
if [[ "$TARGET" == 'arm64' || "$TARGET" == 'all' ]]; then
echo
echo 'Building Kernel arm64'
export GOARCH=arm64
- go build -tags fts5 -o "../app/kernel-darwin-arm64/SiYuan-Kernel" -ldflags "-s -w" .
+ go build -tags "fts5 sqlcipher" -o "../app/kernel-darwin-arm64/SiYuan-Kernel" -ldflags "-s -w" .
fi
echo
diff --git a/scripts/linux-build.sh b/scripts/linux-build.sh
index c867a02b3c..ea05868a32 100755
--- a/scripts/linux-build.sh
+++ b/scripts/linux-build.sh
@@ -106,14 +106,14 @@ if [[ "$TARGET" == 'amd64' || "$TARGET" == 'all' ]]; then
echo 'Building Kernel amd64'
export GOARCH=amd64
setup_cc amd64
- go build -buildmode=pie -tags fts5 -o "../app/kernel-linux/SiYuan-Kernel" -ldflags "-s -w -extldflags -static-pie" .
+ go build -buildmode=pie -tags "fts5 sqlcipher" -o "../app/kernel-linux/SiYuan-Kernel" -ldflags "-s -w -extldflags -static-pie" .
fi
if [[ "$TARGET" == 'arm64' || "$TARGET" == 'all' ]]; then
echo
echo 'Building Kernel arm64'
export GOARCH=arm64
setup_cc arm64
- go build -buildmode=pie -tags fts5 -o "../app/kernel-linux-arm64/SiYuan-Kernel" -ldflags "-s -w -extldflags -static-pie" .
+ go build -buildmode=pie -tags "fts5 sqlcipher" -o "../app/kernel-linux-arm64/SiYuan-Kernel" -ldflags "-s -w -extldflags -static-pie" .
fi
echo
diff --git a/scripts/win-build.bat b/scripts/win-build.bat
index 6eb650d30c..181bc8c4d3 100755
--- a/scripts/win-build.bat
+++ b/scripts/win-build.bat
@@ -109,7 +109,7 @@ if defined BUILD_AMD64 (
echo.
echo Building Kernel amd64
set GOARCH=amd64
- go build -tags fts5 -o "%PROJECT_ROOT%\app\kernel\SiYuan-Kernel.exe" -ldflags "-s -w" .
+ go build -tags "fts5 sqlcipher" -o "%PROJECT_ROOT%\app\kernel\SiYuan-Kernel.exe" -ldflags "-s -w" .
if errorlevel 1 (
exit /b %errorlevel%
)
@@ -121,7 +121,7 @@ if defined BUILD_ARM64 (
set GOARCH=arm64
@REM if you want to build arm64, you need to install aarch64-w64-mingw32-gcc
set CC="D:/Program Files/llvm-mingw-20240518-ucrt-x86_64/bin/aarch64-w64-mingw32-gcc.exe"
- go build -tags fts5 -o "%PROJECT_ROOT%\app\kernel-arm64\SiYuan-Kernel.exe" -ldflags "-s -w" .
+ go build -tags "fts5 sqlcipher" -o "%PROJECT_ROOT%\app\kernel-arm64\SiYuan-Kernel.exe" -ldflags "-s -w" .
if errorlevel 1 (
exit /b %errorlevel%
)