From b942163748d145b7e4f6afc4441b223f46f60d60 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 16 Dec 2018 21:19:12 +0100 Subject: [PATCH] sync with electron net API (for system proxy support) - working, but WIP --- src/routes/api/login.js | 2 +- src/services/sync.js | 74 +++++++++++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/routes/api/login.js b/src/routes/api/login.js index cbd30f3b4..fbc0886b3 100644 --- a/src/routes/api/login.js +++ b/src/routes/api/login.js @@ -29,7 +29,7 @@ async function loginSync(req) { const syncVersion = req.body.syncVersion; if (syncVersion !== appInfo.syncVersion) { - return [400, { message: 'Non-matching sync versions, local is version ' + appInfo.syncVersion }]; + return [400, { message: `Non-matching sync versions, local is version ${appInfo.syncVersion}, remote is ${syncVersion}` }]; } const documentSecret = await options.getOption('documentSecret'); diff --git a/src/services/sync.js b/src/services/sync.js index 73178f9c7..ee6d047ad 100644 --- a/src/services/sync.js +++ b/src/services/sync.js @@ -84,7 +84,7 @@ async function doLogin() { const documentSecret = await optionService.getOption('documentSecret'); const hash = utils.hmac(documentSecret, timestamp); - const syncContext = { cookieJar: rp.jar() }; + const syncContext = { cookieJar: {} }; const resp = await syncRequest(syncContext, 'POST', '/api/login/sync', { timestamp: timestamp, @@ -111,6 +111,10 @@ async function pullSync(syncContext) { const resp = await syncRequest(syncContext, 'GET', changesUri); stats.outstandingPulls = resp.maxSyncId - lastSyncedPull; + if (stats.outstandingPulls < 0) { + stats.outstandingPulls = 0; + } + const rows = resp.syncs; if (rows.length === 0) { @@ -215,26 +219,62 @@ async function checkContentHash(syncContext) { async function syncRequest(syncContext, method, uri, body) { const fullUri = await syncOptions.getSyncServerHost() + uri; - try { - const options = { - method: method, - uri: fullUri, - jar: syncContext.cookieJar, - json: true, - body: body, - timeout: await syncOptions.getSyncTimeout() - }; + if (utils.isElectron()) { + return new Promise((resolve, reject) => { + try { + const { net } = require('electron'); - const syncProxy = await syncOptions.getSyncProxy(); + const request = net.request({ + method, + url: fullUri, + headers: { + Cookie: syncContext.cookieJar.header || "", + 'Content-Type': 'application/json' + } + }); - if (syncProxy && proxyToggle) { - options.proxy = syncProxy; - } + request.on('response', response => { + if (response.headers['set-cookie']) { + syncContext.cookieJar.header = response.headers['set-cookie']; + } - return await rp(options); + let data = ''; + + response.on('data', chunk => data += chunk); + + response.on('end', () => resolve(data.trim() ? JSON.parse(data) : null)); + }); + + request.end(JSON.stringify(body)); + } + catch (e) { + console.log(e); + + reject(e.message); + } + }); } - catch (e) { - throw new Error(`Request to ${method} ${fullUri} failed, error: ${e.message}`); + else { + try { + const options = { + method: method, + uri: fullUri, + jar: syncContext.cookieJar, + json: true, + body: body, + timeout: await syncOptions.getSyncTimeout() + }; + + const syncProxy = await syncOptions.getSyncProxy(); + + if (syncProxy && proxyToggle) { + options.proxy = syncProxy; + } + + return await rp(options); + } catch (e) { + throw new Error(`Request to ${method} ${fullUri} failed, error: ${e.message}`); + } } }