Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/socket/tunnelbroker.js
- This file was added.
// @flow | |||||||||||
import WebSocket from 'ws'; | |||||||||||
import { type TBKeyserverConnectionInitializationMessage } from 'lib/types/tunnelbroker-messages.js'; | |||||||||||
import sleep from 'lib/utils/sleep.js'; | |||||||||||
import { fetchOlmAccount } from '../updaters/olm-account-updater.js'; | |||||||||||
import type { IdentityInfo } from '../user/identity.js'; | |||||||||||
async function createAndMaintainTunnelbrokerWebsocket( | |||||||||||
identityInfo: IdentityInfo, | |||||||||||
) { | |||||||||||
const accountInfo = await fetchOlmAccount('content'); | |||||||||||
const deviceId = JSON.parse(accountInfo.account.identity_keys()).curve25519; | |||||||||||
ashoat: Please apply this feedback to every place in the diff that it applies to | |||||||||||
await openTunnelbrokerConnection( | |||||||||||
ashoatUnsubmitted Done Inline ActionsYou should not be awaiting something that doesn't return a promise ashoat: You should not be `await`ing something that doesn't return a promise | |||||||||||
deviceId, | |||||||||||
identityInfo.userId, | |||||||||||
identityInfo.accessToken, | |||||||||||
); | |||||||||||
} | |||||||||||
function openTunnelbrokerConnection( | |||||||||||
deviceId: string, | |||||||||||
userId: string, | |||||||||||
accessToken: string, | |||||||||||
) { | |||||||||||
try { | |||||||||||
const tunnelbrokerSocket = new WebSocket('ws://localhost:51001'); | |||||||||||
tunnelbrokerSocket.on('open', () => { | |||||||||||
const message: TBKeyserverConnectionInitializationMessage = { | |||||||||||
type: 'sessionRequest', | |||||||||||
accessToken, | |||||||||||
deviceId, | |||||||||||
deviceType: 'keyserver', | |||||||||||
userId, | |||||||||||
}; | |||||||||||
tunnelbrokerSocket.send(JSON.stringify(message)); | |||||||||||
console.info('Connection to tunnelbroker established'); | |||||||||||
ashoatUnsubmitted Done Inline Actions
ashoat: | |||||||||||
}); | |||||||||||
tunnelbrokerSocket.on('close', async () => { | |||||||||||
console.warn('Connection to tunnelbroker closed'); | |||||||||||
ashoatUnsubmitted Done Inline Actions
ashoat: | |||||||||||
await sleep(1000); | |||||||||||
console.info('Attempting to re-establish tunnelbroker connection'); | |||||||||||
ashoatUnsubmitted Done Inline Actions
Please apply this feedback everywhere ashoat: Please apply this feedback everywhere | |||||||||||
openTunnelbrokerConnection(deviceId, userId, accessToken); | |||||||||||
}); | |||||||||||
tunnelbrokerSocket.on('error', (error: Error) => { | |||||||||||
console.error('Tunnelbroker socket error: ' + error.message); | |||||||||||
ashoatUnsubmitted Done Inline Actions
ashoat: | |||||||||||
}); | |||||||||||
} catch (err) { | |||||||||||
ashoatUnsubmitted Done Inline Actions
If we're not doing anything with err, we can omit it ashoat: If we're not doing anything with `err`, we can omit it | |||||||||||
console.log('Failed to open connection with tunnelbroker'); | |||||||||||
} | |||||||||||
} | |||||||||||
export { createAndMaintainTunnelbrokerWebsocket }; |
Please apply this feedback to every place in the diff that it applies to