2017-11-25 17:43:05 -05:00
|
|
|
const WebSocket = require('ws');
|
2017-11-30 23:50:42 -05:00
|
|
|
const utils = require('./utils');
|
|
|
|
const log = require('./log');
|
2017-12-19 23:22:21 -05:00
|
|
|
const sql = require('./sql');
|
2020-01-31 22:32:24 +01:00
|
|
|
const cls = require('./cls');
|
2020-08-29 00:11:50 +02:00
|
|
|
const config = require('./config');
|
2019-10-22 21:59:51 +02:00
|
|
|
const syncMutexService = require('./sync_mutex');
|
2020-02-01 22:29:32 +01:00
|
|
|
const protectedSessionService = require('./protected_session');
|
2017-11-25 17:43:05 -05:00
|
|
|
|
|
|
|
let webSocketServer;
|
|
|
|
|
2017-11-30 23:50:42 -05:00
|
|
|
function init(httpServer, sessionParser) {
|
|
|
|
webSocketServer = new WebSocket.Server({
|
|
|
|
verifyClient: (info, done) => {
|
|
|
|
sessionParser(info.req, {}, () => {
|
2020-08-29 00:11:50 +02:00
|
|
|
const allowed = utils.isElectron()
|
|
|
|
|| info.req.session.loggedIn
|
|
|
|
|| (config.General && config.General.noAuthentication);
|
2017-11-30 23:50:42 -05:00
|
|
|
|
|
|
|
if (!allowed) {
|
|
|
|
log.error("WebSocket connection not allowed because session is neither electron nor logged in.");
|
|
|
|
}
|
|
|
|
|
|
|
|
done(allowed)
|
|
|
|
});
|
|
|
|
},
|
|
|
|
server: httpServer
|
|
|
|
});
|
|
|
|
|
2017-12-01 22:28:22 -05:00
|
|
|
webSocketServer.on('connection', (ws, req) => {
|
2019-12-02 22:27:06 +01:00
|
|
|
ws.id = utils.randomString(10);
|
|
|
|
|
|
|
|
console.log(`websocket client connected`);
|
2017-12-01 22:28:22 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
ws.on('message', async messageJson => {
|
2017-12-01 22:28:22 -05:00
|
|
|
const message = JSON.parse(messageJson);
|
|
|
|
|
|
|
|
if (message.type === 'log-error') {
|
2020-06-10 00:10:27 +02:00
|
|
|
log.info('JS Error: ' + message.error + '\r\nStack: ' + message.stack);
|
2017-12-01 22:28:22 -05:00
|
|
|
}
|
2017-12-19 23:22:21 -05:00
|
|
|
else if (message.type === 'ping') {
|
2020-06-20 12:31:38 +02:00
|
|
|
await syncMutexService.doExclusively(() => sendPing(ws));
|
2017-12-19 23:22:21 -05:00
|
|
|
}
|
2017-12-01 22:28:22 -05:00
|
|
|
else {
|
|
|
|
log.error('Unrecognized message: ');
|
|
|
|
log.error(message);
|
|
|
|
}
|
|
|
|
});
|
2017-11-25 17:43:05 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:42:22 +01:00
|
|
|
function sendMessage(client, message) {
|
2017-12-19 23:22:21 -05:00
|
|
|
const jsonStr = JSON.stringify(message);
|
|
|
|
|
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
client.send(jsonStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:42:22 +01:00
|
|
|
function sendMessageToAllClients(message) {
|
2017-11-25 17:43:05 -05:00
|
|
|
const jsonStr = JSON.stringify(message);
|
|
|
|
|
2019-01-02 22:36:06 +01:00
|
|
|
if (webSocketServer) {
|
|
|
|
log.info("Sending message to all clients: " + jsonStr);
|
2018-01-01 19:41:22 -05:00
|
|
|
|
2019-01-02 22:36:06 +01:00
|
|
|
webSocketServer.clients.forEach(function each(client) {
|
|
|
|
if (client.readyState === WebSocket.OPEN) {
|
|
|
|
client.send(jsonStr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-25 17:43:05 -05:00
|
|
|
}
|
|
|
|
|
2020-12-16 22:17:42 +01:00
|
|
|
function fillInAdditionalProperties(entityChange) {
|
2020-01-18 08:48:36 +01:00
|
|
|
// fill in some extra data needed by the frontend
|
2020-12-16 22:17:42 +01:00
|
|
|
if (entityChange.entityName === 'attributes') {
|
|
|
|
entityChange.entity = sql.getRow(`SELECT * FROM attributes WHERE attributeId = ?`, [entityChange.entityId]);
|
|
|
|
} else if (entityChange.entityName === 'branches') {
|
|
|
|
entityChange.entity = sql.getRow(`SELECT * FROM branches WHERE branchId = ?`, [entityChange.entityId]);
|
|
|
|
} else if (entityChange.entityName === 'notes') {
|
|
|
|
entityChange.entity = sql.getRow(`SELECT * FROM notes WHERE noteId = ?`, [entityChange.entityId]);
|
|
|
|
|
|
|
|
if (entityChange.entity.isProtected) {
|
|
|
|
entityChange.entity.title = protectedSessionService.decryptString(entityChange.entity.title);
|
2020-02-01 22:29:32 +01:00
|
|
|
}
|
2020-12-16 22:17:42 +01:00
|
|
|
} else if (entityChange.entityName === 'note_revisions') {
|
|
|
|
entityChange.noteId = sql.getValue(`SELECT noteId
|
2020-01-18 08:48:36 +01:00
|
|
|
FROM note_revisions
|
2020-12-16 22:17:42 +01:00
|
|
|
WHERE noteRevisionId = ?`, [entityChange.entityId]);
|
|
|
|
} else if (entityChange.entityName === 'note_reordering') {
|
|
|
|
entityChange.positions = sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [entityChange.entityId]);
|
2020-01-18 08:48:36 +01:00
|
|
|
}
|
2020-12-16 22:17:42 +01:00
|
|
|
else if (entityChange.entityName === 'options') {
|
|
|
|
entityChange.entity = sql.getRow(`SELECT * FROM options WHERE name = ?`, [entityChange.entityId]);
|
2020-02-05 22:08:45 +01:00
|
|
|
}
|
2020-01-18 08:48:36 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:17:51 +01:00
|
|
|
function sendPing(client, entityChanges = []) {
|
|
|
|
for (const sync of entityChanges) {
|
2020-01-18 08:48:36 +01:00
|
|
|
try {
|
2020-06-20 12:31:38 +02:00
|
|
|
fillInAdditionalProperties(sync);
|
2019-06-01 12:14:09 +02:00
|
|
|
}
|
2020-01-18 08:48:36 +01:00
|
|
|
catch (e) {
|
|
|
|
log.error("Could not fill additional properties for sync " + JSON.stringify(sync)
|
|
|
|
+ " because of error: " + e.message + ": " + e.stack);
|
2019-10-20 12:29:34 +02:00
|
|
|
}
|
2019-06-01 12:14:09 +02:00
|
|
|
}
|
|
|
|
|
2019-10-28 18:42:22 +01:00
|
|
|
sendMessage(client, {
|
2017-12-19 23:22:21 -05:00
|
|
|
type: 'sync',
|
2020-12-14 14:17:51 +01:00
|
|
|
data: entityChanges
|
2017-12-19 23:22:21 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:44:47 +02:00
|
|
|
function sendTransactionSyncsToAllClients() {
|
2019-12-02 22:27:06 +01:00
|
|
|
if (webSocketServer) {
|
2020-12-14 14:17:51 +01:00
|
|
|
const entityChanges = cls.getAndClearEntityChanges();
|
2020-06-21 13:44:47 +02:00
|
|
|
|
2021-03-12 23:48:14 +01:00
|
|
|
webSocketServer.clients.forEach(client => sendPing(client, entityChanges));
|
2019-12-02 22:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:42:22 +01:00
|
|
|
function syncPullInProgress() {
|
|
|
|
sendMessageToAllClients({ type: 'sync-pull-in-progress' });
|
2019-10-25 22:20:14 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 00:01:28 +01:00
|
|
|
function syncPushInProgress() {
|
|
|
|
sendMessageToAllClients({ type: 'sync-push-in-progress' });
|
|
|
|
}
|
|
|
|
|
|
|
|
function syncFinished() {
|
|
|
|
sendMessageToAllClients({ type: 'sync-finished' });
|
|
|
|
}
|
|
|
|
|
|
|
|
function syncFailed() {
|
|
|
|
sendMessageToAllClients({ type: 'sync-failed' });
|
2019-10-25 22:20:14 +02:00
|
|
|
}
|
|
|
|
|
2017-11-25 17:43:05 -05:00
|
|
|
module.exports = {
|
|
|
|
init,
|
2019-02-10 16:59:50 +01:00
|
|
|
sendMessageToAllClients,
|
2021-03-21 00:01:28 +01:00
|
|
|
syncPushInProgress,
|
2019-10-25 22:20:14 +02:00
|
|
|
syncPullInProgress,
|
2021-03-21 00:01:28 +01:00
|
|
|
syncFinished,
|
|
|
|
syncFailed,
|
2020-06-21 13:44:47 +02:00
|
|
|
sendTransactionSyncsToAllClients
|
2020-06-10 00:10:27 +02:00
|
|
|
};
|