mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
sync with electron net API (for system proxy support) - working, but WIP
This commit is contained in:
parent
5b6d15acb3
commit
b942163748
@ -29,7 +29,7 @@ async function loginSync(req) {
|
|||||||
const syncVersion = req.body.syncVersion;
|
const syncVersion = req.body.syncVersion;
|
||||||
|
|
||||||
if (syncVersion !== appInfo.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');
|
const documentSecret = await options.getOption('documentSecret');
|
||||||
|
@ -84,7 +84,7 @@ async function doLogin() {
|
|||||||
const documentSecret = await optionService.getOption('documentSecret');
|
const documentSecret = await optionService.getOption('documentSecret');
|
||||||
const hash = utils.hmac(documentSecret, timestamp);
|
const hash = utils.hmac(documentSecret, timestamp);
|
||||||
|
|
||||||
const syncContext = { cookieJar: rp.jar() };
|
const syncContext = { cookieJar: {} };
|
||||||
|
|
||||||
const resp = await syncRequest(syncContext, 'POST', '/api/login/sync', {
|
const resp = await syncRequest(syncContext, 'POST', '/api/login/sync', {
|
||||||
timestamp: timestamp,
|
timestamp: timestamp,
|
||||||
@ -111,6 +111,10 @@ async function pullSync(syncContext) {
|
|||||||
const resp = await syncRequest(syncContext, 'GET', changesUri);
|
const resp = await syncRequest(syncContext, 'GET', changesUri);
|
||||||
stats.outstandingPulls = resp.maxSyncId - lastSyncedPull;
|
stats.outstandingPulls = resp.maxSyncId - lastSyncedPull;
|
||||||
|
|
||||||
|
if (stats.outstandingPulls < 0) {
|
||||||
|
stats.outstandingPulls = 0;
|
||||||
|
}
|
||||||
|
|
||||||
const rows = resp.syncs;
|
const rows = resp.syncs;
|
||||||
|
|
||||||
if (rows.length === 0) {
|
if (rows.length === 0) {
|
||||||
@ -215,26 +219,62 @@ async function checkContentHash(syncContext) {
|
|||||||
async function syncRequest(syncContext, method, uri, body) {
|
async function syncRequest(syncContext, method, uri, body) {
|
||||||
const fullUri = await syncOptions.getSyncServerHost() + uri;
|
const fullUri = await syncOptions.getSyncServerHost() + uri;
|
||||||
|
|
||||||
try {
|
if (utils.isElectron()) {
|
||||||
const options = {
|
return new Promise((resolve, reject) => {
|
||||||
method: method,
|
try {
|
||||||
uri: fullUri,
|
const { net } = require('electron');
|
||||||
jar: syncContext.cookieJar,
|
|
||||||
json: true,
|
|
||||||
body: body,
|
|
||||||
timeout: await syncOptions.getSyncTimeout()
|
|
||||||
};
|
|
||||||
|
|
||||||
const syncProxy = await syncOptions.getSyncProxy();
|
const request = net.request({
|
||||||
|
method,
|
||||||
|
url: fullUri,
|
||||||
|
headers: {
|
||||||
|
Cookie: syncContext.cookieJar.header || "",
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (syncProxy && proxyToggle) {
|
request.on('response', response => {
|
||||||
options.proxy = syncProxy;
|
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) {
|
else {
|
||||||
throw new Error(`Request to ${method} ${fullUri} failed, error: ${e.message}`);
|
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}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user