mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 15:11:31 +08: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;
 | 
			
		||||
 | 
			
		||||
    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');
 | 
			
		||||
 | 
			
		||||
@ -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}`);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user