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