From 9d95d8f702e5c7aacdcf5f488c892c464a821e07 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 20 Jun 2023 14:04:22 +0200 Subject: [PATCH 1/6] [_]:(fix) Connect correctly to notifications websocket --- src/main/realtime.ts | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/src/main/realtime.ts b/src/main/realtime.ts index 880033e4c..b888ad68e 100644 --- a/src/main/realtime.ts +++ b/src/main/realtime.ts @@ -12,42 +12,23 @@ function cleanAndStartRemoteNotifications() { stopRemoteNotifications(); socket = io(process.env.NOTIFICATIONS_URL, { + transports: ['websocket', 'polling'], auth: { token: obtainToken('bearerToken'), }, withCredentials: true, }); - socket.io.on('open', () => { - socket?.io.engine.transport.on('pollComplete', () => { - const request = socket?.io.engine.transport.pollXhr.xhr; - const cookieHeader = request.getResponseHeader('set-cookie'); - if (!cookieHeader) { - return; - } - cookieHeader.forEach((cookieString: string) => { - if (cookieString.includes('INGRESSCOOKIE=')) { - const cookie = cookieString.split(';')[0]; - if (socket) { - socket.io.opts.extraHeaders = { - cookie, - }; - } - } - }); - }); - }); - socket.on('connect', () => { - logger.log('Remote notifications connected'); + logger.log('✅ Remote notifications connected'); }); socket.on('disconnect', (reason) => { - logger.log('Remote notifications disconnected, reason: ', reason); + logger.log('❌ Remote notifications disconnected, reason: ', reason); }); socket.on('connect_error', (error) => { - logger.error('Remote notifications connect error: ', error); + logger.error('❌ Remote notifications connect error: ', error); }); socket.on('event', (data) => { From 4b8bf5d0c60c11d72201e6400f7cf7b88166d117 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 21 Jun 2023 09:20:53 +0200 Subject: [PATCH 2/6] [_]:(fix) Use ingress cookie when polling --- src/main/realtime.ts | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/realtime.ts b/src/main/realtime.ts index b888ad68e..a786d4dd2 100644 --- a/src/main/realtime.ts +++ b/src/main/realtime.ts @@ -4,6 +4,9 @@ import { obtainToken } from './auth/service'; import eventBus from './event-bus'; import { broadcastToWindows } from './windows'; +type XHRRequest = { + getResponseHeader: (headerName: string) => string[] | null; +}; // REMOTE TRIGGER let socket: Socket | undefined; @@ -14,11 +17,35 @@ function cleanAndStartRemoteNotifications() { socket = io(process.env.NOTIFICATIONS_URL, { transports: ['websocket', 'polling'], auth: { - token: obtainToken('bearerToken'), + token: obtainToken('newToken'), }, withCredentials: true, }); + socket.on('open', () => { + socket?.io.engine.transport.on('pollComplete', () => { + const xhr = ( + socket?.io.engine.transport as unknown as { + pollXhr: { xhr: XHRRequest }; + } + ).pollXhr.xhr; + + const cookieHeader = xhr.getResponseHeader('set-cookie'); + if (!cookieHeader) { + return; + } + cookieHeader.forEach((cookieString: string) => { + if (cookieString.includes('INGRESSCOOKIE=')) { + const cookie = cookieString.split(';')[0]; + if (socket) { + socket.io.opts.extraHeaders = { + cookie, + }; + } + } + }); + }); + }); socket.on('connect', () => { logger.log('✅ Remote notifications connected'); }); From 32e2b58310f4b7d101dfc4b47914dcebdb5a6300 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 21 Jun 2023 09:36:01 +0200 Subject: [PATCH 3/6] [_]:(fix) Use bearerToken instead of newToken --- src/main/realtime.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/realtime.ts b/src/main/realtime.ts index a786d4dd2..6308eb039 100644 --- a/src/main/realtime.ts +++ b/src/main/realtime.ts @@ -17,7 +17,7 @@ function cleanAndStartRemoteNotifications() { socket = io(process.env.NOTIFICATIONS_URL, { transports: ['websocket', 'polling'], auth: { - token: obtainToken('newToken'), + token: obtainToken('bearerToken'), }, withCredentials: true, }); From d0526a24dd61d3b1da1e31f0bb3b4d55f62b268c Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 21 Jun 2023 09:55:25 +0200 Subject: [PATCH 4/6] [_]:(fix) Wait before checking for new changes via RemoteSyncManager --- src/main/remote-sync/handlers.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/remote-sync/handlers.ts b/src/main/remote-sync/handlers.ts index 9b63ea754..4b162d965 100644 --- a/src/main/remote-sync/handlers.ts +++ b/src/main/remote-sync/handlers.ts @@ -7,6 +7,7 @@ import { getNewTokenClient } from '../../shared/HttpClient/main-process-client'; import Logger from 'electron-log'; import { ipcMain } from 'electron'; import { reportError } from '../bug-report/service'; +import { sleep } from '../util'; const driveFilesCollection = new DriveFilesCollection(); const driveFoldersCollection = new DriveFoldersCollection(); @@ -54,6 +55,10 @@ ipcMain.handle('START_REMOTE_SYNC', async () => { }); eventBus.on('RECEIVED_REMOTE_CHANGES', async () => { + // Wait 1s before checking for updates, could be possible + // that we received the notification, but if we check + // for new data we don't receive it + await sleep(500); await remoteSyncManager.startRemoteSync(); }); eventBus.on('USER_LOGGED_IN', () => { From d0c722af4567c11339adc66b5da890c16d8d77b3 Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 21 Jun 2023 09:56:09 +0200 Subject: [PATCH 5/6] [_]:(fix) Wait before checking for new changes via RemoteSyncManager --- src/main/remote-sync/handlers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/remote-sync/handlers.ts b/src/main/remote-sync/handlers.ts index 4b162d965..7316fadb5 100644 --- a/src/main/remote-sync/handlers.ts +++ b/src/main/remote-sync/handlers.ts @@ -55,7 +55,7 @@ ipcMain.handle('START_REMOTE_SYNC', async () => { }); eventBus.on('RECEIVED_REMOTE_CHANGES', async () => { - // Wait 1s before checking for updates, could be possible + // Wait before checking for updates, could be possible // that we received the notification, but if we check // for new data we don't receive it await sleep(500); From 91559df257d3b6ef398f9631ca6864f2dd3c412c Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 21 Jun 2023 11:01:27 +0200 Subject: [PATCH 6/6] [_]:(fix) Remove polling --- src/main/realtime.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/realtime.ts b/src/main/realtime.ts index 6308eb039..3c24695a0 100644 --- a/src/main/realtime.ts +++ b/src/main/realtime.ts @@ -15,7 +15,7 @@ function cleanAndStartRemoteNotifications() { stopRemoteNotifications(); socket = io(process.env.NOTIFICATIONS_URL, { - transports: ['websocket', 'polling'], + transports: ['websocket'], auth: { token: obtainToken('bearerToken'), }, @@ -46,6 +46,7 @@ function cleanAndStartRemoteNotifications() { }); }); }); + socket.on('connect', () => { logger.log('✅ Remote notifications connected'); });