Skip to content

Build your personal knowledge base with Trilium Notes

more complete support for network-less electron frontend-backend communication including protected session

azivnerd0a0366b05d1
diff --git a/app.js b/app.js
index 77165068a1..661f7dffda 100644
--- a/app.js
+++ b/app.js
@@ -80,16 +80,24 @@ if (utils.isElectron()) {
req.url = arg.url;
req.method = arg.method;
req.body = arg.data;
- req.headers = {};
+ req.headers = arg.headers;
+
+ const res = {
+ statusCode: 200
+ };
- const res = {};
res.setHeader = function() {
};
+ res.status = function(statusCode) {
+ res.statusCode = statusCode;
+ };
+
res.send = function(obj) {
event.sender.send('server-response', {
requestId: arg.requestId,
+ statusCode: res.statusCode,
body: obj
});
};
diff --git a/public/javascripts/init.js b/public/javascripts/init.js
index b51872028f..1f3ddccc7c 100644
--- a/public/javascripts/init.js
+++ b/public/javascripts/init.js
@@ -24,13 +24,13 @@ $(document).bind('keydown', 'alt+t', () => {
});
$(document).bind('keydown', 'f5', () => {
- window.location.reload(true);
+ reloadApp();
return false;
});
$(document).bind('keydown', 'ctrl+r', () => {
- window.location.reload(true);
+ reloadApp();
return false;
});
diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js
index a600da6c7c..00b28fce10 100644
--- a/public/javascripts/note_tree.js
+++ b/public/javascripts/note_tree.js
@@ -625,6 +625,6 @@ const noteTree = (function() {
createNewTopLevelNote,
createNote,
setPrefix,
- getNotePathTitle
+
};
})();
\ No newline at end of file
diff --git a/public/javascripts/protected_session.js b/public/javascripts/protected_session.js
index a00a677a9d..2cdb21ab76 100644
--- a/public/javascripts/protected_session.js
+++ b/public/javascripts/protected_session.js
@@ -10,8 +10,8 @@ const protected_session = (function() {
let protectedSessionTimeout = null;
let protectedSessionId = null;
- server.get('settings/all').then(settings => {
- protectedSessionTimeout = settings.protected_session_timeout;
+ $(document).ready(() => {
+ server.get('settings/all').then(settings => protectedSessionTimeout = settings.protected_session_timeout);
});
function setProtectedSessionTimeout(encSessTimeout) {
@@ -57,7 +57,6 @@ const protected_session = (function() {
}
protectedSessionId = response.protectedSessionId;
- server.initAjax();
dialogEl.dialog("close");
@@ -96,11 +95,9 @@ const protected_session = (function() {
function resetProtectedSession() {
protectedSessionId = null;
- server.initAjax();
-
// most secure solution - guarantees nothing remained in memory
// since this expires because user doesn't use the app, it shouldn't be disruptive
- window.location.reload(true);
+ reloadApp();
}
function isProtectedSessionAvailable() {
diff --git a/public/javascripts/server.js b/public/javascripts/server.js
index 05ac046cbf..e04ca25959 100644
--- a/public/javascripts/server.js
+++ b/public/javascripts/server.js
@@ -1,10 +1,8 @@
const server = (function() {
- function initAjax() {
- $.ajaxSetup({
- headers: {
- 'x-protected-session-id': typeof protected_session !== 'undefined' ? protected_session.getProtectedSessionId() : null
- }
- });
+ function getHeaders() {
+ return {
+ 'x-protected-session-id': protected_session.getProtectedSessionId()
+ };
}
async function get(url) {
@@ -34,8 +32,11 @@ const server = (function() {
return new Promise((resolve, reject) => {
reqResolves[requestId] = resolve;
+ console.log("Request #" + requestId + " to " + method + " " + url);
+
ipc.send('server-request', {
requestId: requestId,
+ headers: getHeaders(),
method: method,
url: "/" + baseApiUrl + url,
data: data
@@ -51,14 +52,19 @@ const server = (function() {
const ipc = require('electron').ipcRenderer;
ipc.on('server-response', (event, arg) => {
+ console.log("Response #" + arg.requestId + ": " + arg.statusCode);
+
reqResolves[arg.requestId](arg.body);
+
+ delete reqResolves[arg.requestId];
});
}
async function ajax(url, method, data) {
const options = {
url: baseApiUrl + url,
- type: method
+ type: method,
+ headers: getHeaders()
};
if (data) {
@@ -71,14 +77,10 @@ const server = (function() {
});
}
-
- initAjax();
-
return {
get,
post,
put,
- remove,
- initAjax
+ remove
}
})();
\ No newline at end of file
diff --git a/public/javascripts/utils.js b/public/javascripts/utils.js
index 72e2c661ea..7495ebf501 100644
--- a/public/javascripts/utils.js
+++ b/public/javascripts/utils.js
@@ -1,5 +1,9 @@
"use strict";
+function reloadApp() {
+ window.location.reload(true);
+}
+
function showMessage(message) {
console.log("message: ", message);
diff --git a/services/protected_session.js b/services/protected_session.js
index d1db0cb42e..c32ded5055 100644
--- a/services/protected_session.js
+++ b/services/protected_session.js
@@ -1,12 +1,13 @@
"use strict";
const utils = require('./utils');
+const session = {};
function setDataKey(req, decryptedDataKey) {
- req.session.decryptedDataKey = Array.from(decryptedDataKey); // can't store buffer in session
- req.session.protectedSessionId = utils.randomSecureToken(32);
+ session.decryptedDataKey = Array.from(decryptedDataKey); // can't store buffer in session
+ session.protectedSessionId = utils.randomSecureToken(32);
- return req.session.protectedSessionId;
+ return session.protectedSessionId;
}
function getProtectedSessionId(req) {
@@ -16,8 +17,8 @@ function getProtectedSessionId(req) {
function getDataKey(req) {
const protectedSessionId = getProtectedSessionId(req);
- if (protectedSessionId && req.session.protectedSessionId === protectedSessionId) {
- return req.session.decryptedDataKey;
+ if (protectedSessionId && session.protectedSessionId === protectedSessionId) {
+ return session.decryptedDataKey;
}
else {
return null;
@@ -27,7 +28,7 @@ function getDataKey(req) {
function isProtectedSessionAvailable(req) {
const protectedSessionId = getProtectedSessionId(req);
- return protectedSessionId && req.session.protectedSessionId === protectedSessionId;
+ return protectedSessionId && session.protectedSessionId === protectedSessionId;
}
module.exports = {