mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 21:11:30 +08:00 
			
		
		
		
	more complete support for network-less electron frontend-backend communication including protected session
This commit is contained in:
		
							parent
							
								
									8bd76721ad
								
							
						
					
					
						commit
						d0a0366b05
					
				
							
								
								
									
										12
									
								
								app.js
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								app.js
									
									
									
									
									
								
							| @ -80,16 +80,24 @@ if (utils.isElectron()) { | |||||||
|         req.url = arg.url; |         req.url = arg.url; | ||||||
|         req.method = arg.method; |         req.method = arg.method; | ||||||
|         req.body = arg.data; |         req.body = arg.data; | ||||||
|         req.headers = {}; |         req.headers = arg.headers; | ||||||
|  | 
 | ||||||
|  |         const res = { | ||||||
|  |             statusCode: 200 | ||||||
|  |         }; | ||||||
| 
 | 
 | ||||||
|         const res = {}; |  | ||||||
|         res.setHeader = function() { |         res.setHeader = function() { | ||||||
| 
 | 
 | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|  |         res.status = function(statusCode) { | ||||||
|  |             res.statusCode = statusCode; | ||||||
|  |         }; | ||||||
|  | 
 | ||||||
|         res.send = function(obj) { |         res.send = function(obj) { | ||||||
|             event.sender.send('server-response', { |             event.sender.send('server-response', { | ||||||
|                 requestId: arg.requestId, |                 requestId: arg.requestId, | ||||||
|  |                 statusCode: res.statusCode, | ||||||
|                 body: obj |                 body: obj | ||||||
|             }); |             }); | ||||||
|         }; |         }; | ||||||
|  | |||||||
| @ -24,13 +24,13 @@ $(document).bind('keydown', 'alt+t', () => { | |||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| $(document).bind('keydown', 'f5', () => { | $(document).bind('keydown', 'f5', () => { | ||||||
|     window.location.reload(true); |     reloadApp(); | ||||||
| 
 | 
 | ||||||
|     return false; |     return false; | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| $(document).bind('keydown', 'ctrl+r', () => { | $(document).bind('keydown', 'ctrl+r', () => { | ||||||
|     window.location.reload(true); |     reloadApp(); | ||||||
| 
 | 
 | ||||||
|     return false; |     return false; | ||||||
| }); | }); | ||||||
|  | |||||||
| @ -625,6 +625,6 @@ const noteTree = (function() { | |||||||
|         createNewTopLevelNote, |         createNewTopLevelNote, | ||||||
|         createNote, |         createNote, | ||||||
|         setPrefix, |         setPrefix, | ||||||
|         getNotePathTitle | 
 | ||||||
|     }; |     }; | ||||||
| })(); | })(); | ||||||
| @ -10,8 +10,8 @@ const protected_session = (function() { | |||||||
|     let protectedSessionTimeout = null; |     let protectedSessionTimeout = null; | ||||||
|     let protectedSessionId = null; |     let protectedSessionId = null; | ||||||
| 
 | 
 | ||||||
|     server.get('settings/all').then(settings => { |     $(document).ready(() => { | ||||||
|         protectedSessionTimeout = settings.protected_session_timeout; |         server.get('settings/all').then(settings => protectedSessionTimeout = settings.protected_session_timeout); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     function setProtectedSessionTimeout(encSessTimeout) { |     function setProtectedSessionTimeout(encSessTimeout) { | ||||||
| @ -57,7 +57,6 @@ const protected_session = (function() { | |||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         protectedSessionId = response.protectedSessionId; |         protectedSessionId = response.protectedSessionId; | ||||||
|         server.initAjax(); |  | ||||||
| 
 | 
 | ||||||
|         dialogEl.dialog("close"); |         dialogEl.dialog("close"); | ||||||
| 
 | 
 | ||||||
| @ -96,11 +95,9 @@ const protected_session = (function() { | |||||||
|     function resetProtectedSession() { |     function resetProtectedSession() { | ||||||
|         protectedSessionId = null; |         protectedSessionId = null; | ||||||
| 
 | 
 | ||||||
|         server.initAjax(); |  | ||||||
| 
 |  | ||||||
|         // most secure solution - guarantees nothing remained in memory
 |         // most secure solution - guarantees nothing remained in memory
 | ||||||
|         // since this expires because user doesn't use the app, it shouldn't be disruptive
 |         // since this expires because user doesn't use the app, it shouldn't be disruptive
 | ||||||
|         window.location.reload(true); |         reloadApp(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     function isProtectedSessionAvailable() { |     function isProtectedSessionAvailable() { | ||||||
|  | |||||||
| @ -1,10 +1,8 @@ | |||||||
| const server = (function() { | const server = (function() { | ||||||
|     function initAjax() { |     function getHeaders() { | ||||||
|         $.ajaxSetup({ |         return { | ||||||
|             headers: { |             'x-protected-session-id': protected_session.getProtectedSessionId() | ||||||
|                 'x-protected-session-id': typeof protected_session !== 'undefined' ? protected_session.getProtectedSessionId() : null |         }; | ||||||
|             } |  | ||||||
|         }); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async function get(url) { |     async function get(url) { | ||||||
| @ -34,8 +32,11 @@ const server = (function() { | |||||||
|             return new Promise((resolve, reject) => { |             return new Promise((resolve, reject) => { | ||||||
|                 reqResolves[requestId] = resolve; |                 reqResolves[requestId] = resolve; | ||||||
| 
 | 
 | ||||||
|  |                 console.log("Request #" + requestId + " to " + method + " " + url); | ||||||
|  | 
 | ||||||
|                 ipc.send('server-request', { |                 ipc.send('server-request', { | ||||||
|                     requestId: requestId, |                     requestId: requestId, | ||||||
|  |                     headers: getHeaders(), | ||||||
|                     method: method, |                     method: method, | ||||||
|                     url: "/" + baseApiUrl + url, |                     url: "/" + baseApiUrl + url, | ||||||
|                     data: data |                     data: data | ||||||
| @ -51,14 +52,19 @@ const server = (function() { | |||||||
|         const ipc = require('electron').ipcRenderer; |         const ipc = require('electron').ipcRenderer; | ||||||
| 
 | 
 | ||||||
|         ipc.on('server-response', (event, arg) => { |         ipc.on('server-response', (event, arg) => { | ||||||
|  |             console.log("Response #" + arg.requestId + ": " + arg.statusCode); | ||||||
|  | 
 | ||||||
|             reqResolves[arg.requestId](arg.body); |             reqResolves[arg.requestId](arg.body); | ||||||
|  | 
 | ||||||
|  |             delete reqResolves[arg.requestId]; | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     async function ajax(url, method, data) { |     async function ajax(url, method, data) { | ||||||
|         const options = { |         const options = { | ||||||
|             url: baseApiUrl + url, |             url: baseApiUrl + url, | ||||||
|             type: method |             type: method, | ||||||
|  |             headers: getHeaders() | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|         if (data) { |         if (data) { | ||||||
| @ -71,14 +77,10 @@ const server = (function() { | |||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     initAjax(); |  | ||||||
| 
 |  | ||||||
|     return { |     return { | ||||||
|         get, |         get, | ||||||
|         post, |         post, | ||||||
|         put, |         put, | ||||||
|         remove, |         remove | ||||||
|         initAjax |  | ||||||
|     } |     } | ||||||
| })(); | })(); | ||||||
| @ -1,5 +1,9 @@ | |||||||
| "use strict"; | "use strict"; | ||||||
| 
 | 
 | ||||||
|  | function reloadApp() { | ||||||
|  |     window.location.reload(true); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| function showMessage(message) { | function showMessage(message) { | ||||||
|     console.log("message: ", message); |     console.log("message: ", message); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,12 +1,13 @@ | |||||||
| "use strict"; | "use strict"; | ||||||
| 
 | 
 | ||||||
| const utils = require('./utils'); | const utils = require('./utils'); | ||||||
|  | const session = {}; | ||||||
| 
 | 
 | ||||||
| function setDataKey(req, decryptedDataKey) { | function setDataKey(req, decryptedDataKey) { | ||||||
|     req.session.decryptedDataKey = Array.from(decryptedDataKey); // can't store buffer in session
 |     session.decryptedDataKey = Array.from(decryptedDataKey); // can't store buffer in session
 | ||||||
|     req.session.protectedSessionId = utils.randomSecureToken(32); |     session.protectedSessionId = utils.randomSecureToken(32); | ||||||
| 
 | 
 | ||||||
|     return req.session.protectedSessionId; |     return session.protectedSessionId; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function getProtectedSessionId(req) { | function getProtectedSessionId(req) { | ||||||
| @ -16,8 +17,8 @@ function getProtectedSessionId(req) { | |||||||
| function getDataKey(req) { | function getDataKey(req) { | ||||||
|     const protectedSessionId = getProtectedSessionId(req); |     const protectedSessionId = getProtectedSessionId(req); | ||||||
| 
 | 
 | ||||||
|     if (protectedSessionId && req.session.protectedSessionId === protectedSessionId) { |     if (protectedSessionId && session.protectedSessionId === protectedSessionId) { | ||||||
|         return req.session.decryptedDataKey; |         return session.decryptedDataKey; | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         return null; |         return null; | ||||||
| @ -27,7 +28,7 @@ function getDataKey(req) { | |||||||
| function isProtectedSessionAvailable(req) { | function isProtectedSessionAvailable(req) { | ||||||
|     const protectedSessionId = getProtectedSessionId(req); |     const protectedSessionId = getProtectedSessionId(req); | ||||||
| 
 | 
 | ||||||
|     return protectedSessionId && req.session.protectedSessionId === protectedSessionId; |     return protectedSessionId && session.protectedSessionId === protectedSessionId; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner