2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-25 22:39:21 -04:00
|
|
|
const log = require('./log');
|
|
|
|
const rp = require('request-promise');
|
|
|
|
const sql = require('./sql');
|
2018-04-02 21:25:20 -04:00
|
|
|
const sqlInit = require('./sql_init');
|
2018-04-01 21:27:46 -04:00
|
|
|
const optionService = require('./options');
|
2017-10-26 21:16:21 -04:00
|
|
|
const utils = require('./utils');
|
2018-04-01 21:27:46 -04:00
|
|
|
const sourceIdService = require('./source_id');
|
2018-04-02 20:46:46 -04:00
|
|
|
const dateUtils = require('./date_utils');
|
2018-04-01 21:27:46 -04:00
|
|
|
const syncUpdateService = require('./sync_update');
|
|
|
|
const contentHashService = require('./content_hash');
|
|
|
|
const eventLogService = require('./event_log');
|
2017-12-01 20:39:48 -05:00
|
|
|
const fs = require('fs');
|
2018-04-01 21:27:46 -04:00
|
|
|
const appInfo = require('./app_info');
|
|
|
|
const messagingService = require('./messaging');
|
|
|
|
const syncSetup = require('./sync_setup');
|
|
|
|
const syncMutexService = require('./sync_mutex');
|
2018-03-28 23:41:22 -04:00
|
|
|
const cls = require('./cls');
|
2017-10-25 22:39:21 -04:00
|
|
|
|
2017-11-16 20:08:04 -05:00
|
|
|
let proxyToggle = true;
|
2017-12-01 20:39:48 -05:00
|
|
|
let syncServerCertificate = null;
|
2017-10-25 22:39:21 -04:00
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
async function sync() {
|
2017-10-29 14:55:48 -04:00
|
|
|
try {
|
2018-04-01 21:27:46 -04:00
|
|
|
await syncMutexService.doExclusively(async () => {
|
2018-04-02 21:25:20 -04:00
|
|
|
if (!await sqlInit.isDbUpToDate()) {
|
2018-01-13 22:51:39 -05:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "DB not up to date"
|
|
|
|
};
|
|
|
|
}
|
2017-10-31 19:34:58 -04:00
|
|
|
|
2018-01-13 22:51:39 -05:00
|
|
|
const syncContext = await login();
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-01-13 22:51:39 -05:00
|
|
|
await pushSync(syncContext);
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-01-13 22:51:39 -05:00
|
|
|
await pullSync(syncContext);
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-01-13 22:51:39 -05:00
|
|
|
await pushSync(syncContext);
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-01-13 22:51:39 -05:00
|
|
|
await checkContentHash(syncContext);
|
|
|
|
});
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
return {
|
|
|
|
success: true
|
|
|
|
};
|
2017-10-29 14:55:48 -04:00
|
|
|
}
|
|
|
|
catch (e) {
|
2017-11-16 20:08:04 -05:00
|
|
|
proxyToggle = !proxyToggle;
|
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
if (e.message.indexOf('ECONNREFUSED') !== -1) {
|
|
|
|
log.info("No connection to sync server.");
|
2017-10-26 20:31:31 -04:00
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: "No connection to sync server."
|
|
|
|
};
|
2017-10-31 19:34:58 -04:00
|
|
|
}
|
2017-11-09 20:52:47 -05:00
|
|
|
else {
|
|
|
|
log.info("sync failed: " + e.stack);
|
|
|
|
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
message: e.message
|
|
|
|
}
|
2017-10-31 19:34:58 -04:00
|
|
|
}
|
2017-11-09 20:52:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function login() {
|
2018-04-02 20:46:46 -04:00
|
|
|
const timestamp = dateUtils.nowDate();
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-04-02 21:47:46 -04:00
|
|
|
const documentSecret = await optionService.getOption('documentSecret');
|
2017-11-09 20:52:47 -05:00
|
|
|
const hash = utils.hmac(documentSecret, timestamp);
|
|
|
|
|
|
|
|
const syncContext = { cookieJar: rp.jar() };
|
|
|
|
|
2017-11-09 23:25:23 -05:00
|
|
|
const resp = await syncRequest(syncContext, 'POST', '/api/login/sync', {
|
2017-11-09 20:52:47 -05:00
|
|
|
timestamp: timestamp,
|
2018-04-02 21:47:46 -04:00
|
|
|
dbVersion: appInfo.dbVersion,
|
2017-11-09 20:52:47 -05:00
|
|
|
hash: hash
|
|
|
|
});
|
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
if (sourceIdService.isLocalSourceId(resp.sourceId)) {
|
2017-12-22 06:48:24 -05:00
|
|
|
throw new Error(`Sync server has source ID ${resp.sourceId} which is also local. Try restarting sync server.`);
|
|
|
|
}
|
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
syncContext.sourceId = resp.sourceId;
|
|
|
|
|
|
|
|
return syncContext;
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:11:27 -05:00
|
|
|
async function getLastSyncedPull() {
|
2018-04-02 21:47:46 -04:00
|
|
|
return parseInt(await optionService.getOption('lastSyncedPull'));
|
2017-11-21 22:11:27 -05:00
|
|
|
}
|
|
|
|
|
2017-11-28 20:52:38 -05:00
|
|
|
async function setLastSyncedPull(syncId) {
|
2018-04-02 21:47:46 -04:00
|
|
|
await optionService.setOption('lastSyncedPull', syncId);
|
2017-11-28 20:52:38 -05:00
|
|
|
}
|
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
async function pullSync(syncContext) {
|
2017-11-21 22:11:27 -05:00
|
|
|
const lastSyncedPull = await getLastSyncedPull();
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2017-11-16 21:50:00 -05:00
|
|
|
const changesUri = '/api/sync/changed?lastSyncId=' + lastSyncedPull;
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-04-07 21:53:42 -04:00
|
|
|
const rows = await syncRequest(syncContext, 'GET', changesUri);
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-04-07 21:53:42 -04:00
|
|
|
log.info("Pulled " + rows.length + " changes from " + changesUri);
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-04-07 21:53:42 -04:00
|
|
|
for (const {sync, entity} of rows) {
|
2018-04-01 21:27:46 -04:00
|
|
|
if (sourceIdService.isLocalSourceId(sync.sourceId)) {
|
2018-01-28 19:30:14 -05:00
|
|
|
log.info(`Skipping pull #${sync.id} ${sync.entityName} ${sync.entityId} because ${sync.sourceId} is a local source id.`);
|
2017-11-16 21:50:00 -05:00
|
|
|
|
2017-11-28 20:52:38 -05:00
|
|
|
await setLastSyncedPull(sync.id);
|
|
|
|
|
2017-11-16 21:50:00 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-07 21:53:42 -04:00
|
|
|
if (!entity) {
|
2018-01-28 19:30:14 -05:00
|
|
|
log.error(`Empty response to pull for sync #${sync.id} ${sync.entityName}, id=${sync.entityId}`);
|
2017-11-23 23:54:54 -05:00
|
|
|
}
|
2017-10-31 20:09:07 -04:00
|
|
|
else {
|
2018-04-07 22:25:28 -04:00
|
|
|
await syncUpdateService.updateEntity(sync.entityName, entity, syncContext.sourceId);
|
2017-10-31 20:09:07 -04:00
|
|
|
}
|
|
|
|
|
2017-11-28 20:52:38 -05:00
|
|
|
await setLastSyncedPull(sync.id);
|
2017-10-26 20:31:31 -04:00
|
|
|
}
|
2017-10-31 19:34:58 -04:00
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
log.info("Finished pull");
|
2017-10-26 20:31:31 -04:00
|
|
|
}
|
2017-10-25 22:39:21 -04:00
|
|
|
|
2017-11-21 22:11:27 -05:00
|
|
|
async function getLastSyncedPush() {
|
2018-04-02 21:47:46 -04:00
|
|
|
return parseInt(await optionService.getOption('lastSyncedPush'));
|
2017-11-21 22:11:27 -05:00
|
|
|
}
|
|
|
|
|
2017-11-29 20:47:01 -05:00
|
|
|
async function setLastSyncedPush(lastSyncedPush) {
|
2018-04-02 21:47:46 -04:00
|
|
|
await optionService.setOption('lastSyncedPush', lastSyncedPush);
|
2017-11-29 20:47:01 -05:00
|
|
|
}
|
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
async function pushSync(syncContext) {
|
2017-11-21 22:11:27 -05:00
|
|
|
let lastSyncedPush = await getLastSyncedPush();
|
2017-10-25 22:39:21 -04:00
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
while (true) {
|
2018-04-07 22:25:28 -04:00
|
|
|
const syncs = await sql.getRows('SELECT * FROM sync WHERE id > ? LIMIT 1000', [lastSyncedPush]);
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
const filteredSyncs = syncs.filter(sync => {
|
|
|
|
if (sync.sourceId === syncContext.sourceId) {
|
|
|
|
log.info(`Skipping push #${sync.id} ${sync.entityName} ${sync.entityId} because it originates from sync target`);
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
// this may set lastSyncedPush beyond what's actually sent (because of size limit)
|
|
|
|
// so this is applied to the database only if there's no actual update
|
|
|
|
// TODO: it would be better to simplify this somehow
|
|
|
|
lastSyncedPush = sync.id;
|
2017-11-09 20:52:47 -05:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2017-10-29 22:22:30 -04:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
if (filteredSyncs.length === 0) {
|
|
|
|
// nothing to sync
|
2017-11-01 23:16:21 -04:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
log.info("Nothing to push");
|
2018-02-18 22:55:36 -05:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
await setLastSyncedPush(lastSyncedPush);
|
2018-01-06 15:56:00 -05:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
break;
|
2018-01-06 15:56:00 -05:00
|
|
|
}
|
2017-11-01 23:16:21 -04:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
const syncRecords = await getSyncRecords(filteredSyncs);
|
2017-11-04 22:10:41 -04:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
await syncRequest(syncContext, 'PUT', '/api/sync/update', {
|
|
|
|
sourceId: sourceIdService.getCurrentSourceId(),
|
|
|
|
entities: syncRecords
|
|
|
|
});
|
2017-11-01 23:16:21 -04:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
lastSyncedPush = syncRecords[syncRecords.length - 1].sync.id;
|
2017-10-29 22:22:30 -04:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
await setLastSyncedPush(lastSyncedPush);
|
|
|
|
}
|
2017-11-09 20:52:47 -05:00
|
|
|
}
|
2017-10-28 22:17:00 -04:00
|
|
|
|
2018-02-18 22:55:36 -05:00
|
|
|
function serializeNoteContentBuffer(note) {
|
|
|
|
if (note.type === 'file') {
|
|
|
|
note.content = note.content.toString("binary");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 22:11:27 -05:00
|
|
|
async function checkContentHash(syncContext) {
|
2017-12-16 12:30:37 -05:00
|
|
|
const resp = await syncRequest(syncContext, 'GET', '/api/sync/check');
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
if (await getLastSyncedPull() < resp.maxSyncId) {
|
2017-12-16 12:30:37 -05:00
|
|
|
log.info("There are some outstanding pulls, skipping content check.");
|
2017-11-21 22:11:27 -05:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-16 12:30:37 -05:00
|
|
|
const lastSyncedPush = await getLastSyncedPush();
|
2018-01-29 17:41:59 -05:00
|
|
|
const notPushedSyncs = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2017-12-16 12:30:37 -05:00
|
|
|
if (notPushedSyncs > 0) {
|
|
|
|
log.info("There's " + notPushedSyncs + " outstanding pushes, skipping content check.");
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2017-12-15 21:14:10 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
const hashes = await contentHashService.getHashes();
|
2017-12-15 21:14:10 -05:00
|
|
|
let allChecksPassed = true;
|
|
|
|
|
|
|
|
for (const key in hashes) {
|
|
|
|
if (hashes[key] !== resp.hashes[key]) {
|
2017-12-15 21:36:21 -05:00
|
|
|
allChecksPassed = false;
|
2017-11-21 22:11:27 -05:00
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
await eventLogService.addEvent(`Content hash check for ${key} FAILED. Local is ${hashes[key]}, remote is ${resp.hashes[key]}`);
|
2017-12-15 21:14:10 -05:00
|
|
|
|
|
|
|
if (key !== 'recent_notes') {
|
|
|
|
// let's not get alarmed about recent notes which get updated often and can cause failures in race conditions
|
2018-04-01 21:27:46 -04:00
|
|
|
await messagingService.sendMessageToAllClients({type: 'sync-hash-check-failed'});
|
2017-12-15 21:14:10 -05:00
|
|
|
}
|
|
|
|
}
|
2017-11-21 22:11:27 -05:00
|
|
|
}
|
2017-12-12 23:47:17 -05:00
|
|
|
|
2017-12-15 21:14:10 -05:00
|
|
|
if (allChecksPassed) {
|
|
|
|
log.info("Content hash checks PASSED");
|
2017-11-21 22:11:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 20:52:47 -05:00
|
|
|
async function syncRequest(syncContext, method, uri, body) {
|
2018-04-01 21:27:46 -04:00
|
|
|
const fullUri = syncSetup.SYNC_SERVER + uri;
|
2017-10-28 22:17:00 -04:00
|
|
|
|
2017-10-29 14:55:48 -04:00
|
|
|
try {
|
2017-11-13 19:45:13 -05:00
|
|
|
const options = {
|
2017-11-09 20:52:47 -05:00
|
|
|
method: method,
|
|
|
|
uri: fullUri,
|
|
|
|
jar: syncContext.cookieJar,
|
2017-10-29 14:55:48 -04:00
|
|
|
json: true,
|
2017-11-09 20:52:47 -05:00
|
|
|
body: body,
|
2018-04-01 21:27:46 -04:00
|
|
|
timeout: syncSetup.SYNC_TIMEOUT
|
2017-11-13 19:45:13 -05:00
|
|
|
};
|
|
|
|
|
2017-12-01 20:39:48 -05:00
|
|
|
if (syncServerCertificate) {
|
|
|
|
options.ca = syncServerCertificate;
|
|
|
|
}
|
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
if (syncSetup.SYNC_PROXY && proxyToggle) {
|
|
|
|
options.proxy = syncSetup.SYNC_PROXY;
|
2017-11-13 19:45:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return await rp(options);
|
2017-10-25 22:39:21 -04:00
|
|
|
}
|
|
|
|
catch (e) {
|
2017-12-15 21:49:46 -05:00
|
|
|
throw new Error(`Request to ${method} ${fullUri} failed, inner exception: ${e.stack}`);
|
2017-11-05 00:16:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 22:25:28 -04:00
|
|
|
const primaryKeys = {
|
|
|
|
"notes": "noteId",
|
|
|
|
"branches": "branchId",
|
|
|
|
"note_revisions": "noteRevisionId",
|
|
|
|
"option": "name",
|
|
|
|
"recent_notes": "branchId",
|
|
|
|
"images": "imageId",
|
|
|
|
"note_images": "noteImageId",
|
|
|
|
"labels": "labelId",
|
|
|
|
"api_tokens": "apiTokenId"
|
|
|
|
};
|
|
|
|
|
|
|
|
async function getEntityRow(entityName, entityId) {
|
|
|
|
if (entityName === 'note_reordering') {
|
|
|
|
return await getNoteReordering(entityId);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const primaryKey = primaryKeys[entityName];
|
|
|
|
|
|
|
|
if (!primaryKey) {
|
|
|
|
throw new Error("Unknown entity " + entityName);
|
|
|
|
}
|
|
|
|
|
|
|
|
const entityRow = await sql.getRow(`SELECT * FROM ${entityName} WHERE ${primaryKey} = ?`, [entityId]);
|
|
|
|
|
|
|
|
if (entityName === 'notes') {
|
|
|
|
serializeNoteContentBuffer(entityRow);
|
|
|
|
}
|
|
|
|
else if (entityName === 'images') {
|
|
|
|
entityRow.data = entityRow.data.toString('base64');
|
|
|
|
}
|
|
|
|
|
|
|
|
return entityRow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getSyncRecords(syncs) {
|
|
|
|
const records = [];
|
|
|
|
let length = 0;
|
|
|
|
|
|
|
|
for (const sync of syncs) {
|
|
|
|
const record = {
|
|
|
|
sync: sync,
|
|
|
|
entity: await getEntityRow(sync.entityName, sync.entityId)
|
|
|
|
};
|
|
|
|
|
|
|
|
records.push(record);
|
|
|
|
|
|
|
|
length += JSON.stringify(record).length;
|
|
|
|
|
|
|
|
if (length > 1000000) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return records;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getNoteReordering(parentNoteId) {
|
|
|
|
return await sql.getMap("SELECT branchId, notePosition FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId])
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:25:20 -04:00
|
|
|
sqlInit.dbReady.then(() => {
|
2018-04-01 21:27:46 -04:00
|
|
|
if (syncSetup.isSyncSetup) {
|
|
|
|
log.info("Setting up sync to " + syncSetup.SYNC_SERVER + " with timeout " + syncSetup.SYNC_TIMEOUT);
|
2017-10-25 22:39:21 -04:00
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
if (syncSetup.SYNC_PROXY) {
|
|
|
|
log.info("Sync proxy: " + syncSetup.SYNC_PROXY);
|
2017-12-03 19:18:33 -05:00
|
|
|
}
|
2017-11-14 21:54:12 -05:00
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
if (syncSetup.SYNC_CERT_PATH) {
|
|
|
|
log.info('Sync certificate: ' + syncSetup.SYNC_CERT_PATH);
|
2017-12-01 20:39:48 -05:00
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
syncServerCertificate = fs.readFileSync(syncSetup.SYNC_CERT_PATH);
|
2017-12-03 19:18:33 -05:00
|
|
|
}
|
2017-12-01 20:39:48 -05:00
|
|
|
|
2018-03-28 23:41:22 -04:00
|
|
|
setInterval(cls.wrap(sync), 60000);
|
2017-10-26 23:21:31 -04:00
|
|
|
|
2017-12-03 19:18:33 -05:00
|
|
|
// kickoff initial sync immediately
|
2018-03-28 23:41:22 -04:00
|
|
|
setTimeout(cls.wrap(sync), 1000);
|
2017-12-03 19:18:33 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
log.info("Sync server not configured, sync timer not running.")
|
|
|
|
}
|
|
|
|
});
|
2017-10-26 21:16:21 -04:00
|
|
|
|
|
|
|
module.exports = {
|
2018-02-18 22:55:36 -05:00
|
|
|
sync,
|
2018-04-07 22:25:28 -04:00
|
|
|
serializeNoteContentBuffer,
|
|
|
|
getEntityRow,
|
|
|
|
getSyncRecords
|
2017-10-26 21:16:21 -04:00
|
|
|
};
|