diff --git a/packages/libs/js-lib/src/core/WebsocketData/WebsocketProvider.ts b/packages/libs/js-lib/src/core/WebsocketData/WebsocketProvider.ts index 364da39be..6b74bf63d 100644 --- a/packages/libs/js-lib/src/core/WebsocketData/WebsocketProvider.ts +++ b/packages/libs/js-lib/src/core/WebsocketData/WebsocketProvider.ts @@ -242,7 +242,7 @@ export const Unsubscribe = ( } }; -export const Notify = async (command: WebsocketCommand | EstablishConnectionRequest) => { +export const Notify = async (command: WebsocketCommand) => { if (!webSocketClient) throw new Error('No active websocket to message across'); if (isDebug) console.debug(`[WebsocketProvider] Send command (${JSON.stringify(command)})`); diff --git a/packages/libs/js-lib/src/core/WebsocketData/WebsocketTypes.ts b/packages/libs/js-lib/src/core/WebsocketData/WebsocketTypes.ts index 2cb113391..fa225fb9b 100644 --- a/packages/libs/js-lib/src/core/WebsocketData/WebsocketTypes.ts +++ b/packages/libs/js-lib/src/core/WebsocketData/WebsocketTypes.ts @@ -81,6 +81,13 @@ export type TypedConnectionNotification = | ReactionNotification; export interface WebsocketCommand { - command: string; + command: WebSocketCommands; data: string; } + +type WebSocketCommands = + | 'establishConnectionRequest' + | 'processTransitInstructions' + | 'processInbox' + | 'ping' + | 'whoIsOnline'; diff --git a/packages/libs/js-lib/src/peer/WebsocketData/WebsocketProviderOverPeer.ts b/packages/libs/js-lib/src/peer/WebsocketData/WebsocketProviderOverPeer.ts index 700d0793c..36fc59eca 100644 --- a/packages/libs/js-lib/src/peer/WebsocketData/WebsocketProviderOverPeer.ts +++ b/packages/libs/js-lib/src/peer/WebsocketData/WebsocketProviderOverPeer.ts @@ -92,17 +92,6 @@ const ConnectSocket = async ( sharedSecret: activeSs, }); - // we need to preauth before we can connect - await directGuestClient - .createAxiosClient() - .post('/notify/peer/preauth', undefined, { - validateStatus: () => true, - }) - .catch((error) => { - console.error({ error }); - reject('[WebsocketProviderOverPeer] Preauth failed'); - }); - const url = `wss://${directGuestClient.getRoot().split('//')[1]}/api/guest/v1/notify/peer/ws`; // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -119,10 +108,13 @@ const ConnectSocket = async ( batchSize: 1, }; - NotifyOverPeer({ - command: 'establishConnectionRequest', - data: JSON.stringify(establishConnectionRequest), - }); + EstablishConnectionOverPeer( + { + command: 'establishConnectionRequest', + data: JSON.stringify(establishConnectionRequest), + }, + tokenToConnectOverPeer.authenticationToken64 + ); }; const setupPing = () => { @@ -285,7 +277,26 @@ export const UnsubscribeOverPeer = ( } }; -export const NotifyOverPeer = async (command: WebsocketCommand | EstablishConnectionRequest) => { +const EstablishConnectionOverPeer = async ( + command: WebsocketCommand, + clientAuthenticationToken64: string +) => { + if (!webSocketClient) throw new Error('No active websocket to message across'); + if (isDebug) + console.debug(`[WebsocketProviderOverPeer] Send command (${JSON.stringify(command)})`); + + const json = jsonStringify64(command); + const establishConnectionMessage = await encryptData(json, getRandomIv(), activeSs); + + webSocketClient.send( + JSON.stringify({ + sharedSecretEncryptedOptions: establishConnectionMessage, + clientAuthToken64: clientAuthenticationToken64, + }) + ); +}; + +export const NotifyOverPeer = async (command: WebsocketCommand) => { if (!webSocketClient) throw new Error('No active websocket to message across'); if (isDebug) console.debug(`[WebsocketProviderOverPeer] Send command (${JSON.stringify(command)})`);