diff --git a/keyserver/src/socket/tunnelbroker.js b/keyserver/src/socket/tunnelbroker.js --- a/keyserver/src/socket/tunnelbroker.js +++ b/keyserver/src/socket/tunnelbroker.js @@ -8,6 +8,7 @@ type TBMessage, } from 'lib/types/tunnelbroker-messages.js'; import { TBRefreshKeysValidator } from 'lib/types/tunnelbroker-messages.js'; +import { getCommConfig } from 'lib/utils/comm-config.js'; import { ServerError } from 'lib/utils/errors.js'; import sleep from 'lib/utils/sleep.js'; @@ -15,16 +16,38 @@ import { type IdentityInfo } from '../user/identity.js'; import { uploadNewOneTimeKeys } from '../utils/olm-utils.js'; +type TBConnectionInfo = { + +url: string, +}; + +async function getTBConnectionInfo(): Promise { + const tbConfig = await getCommConfig({ + folder: 'facts', + name: 'tunnelbroker', + }); + + if (tbConfig) { + return tbConfig; + } + + console.warn('Defaulting to local tunnelbroker instance'); + return { + url: 'ws://127.0.0.1:51001', + }; +} + async function createAndMaintainTunnelbrokerWebsocket( identityInfo: IdentityInfo, ) { const accountInfo = await fetchOlmAccount('content'); const deviceId = JSON.parse(accountInfo.account.identity_keys()).curve25519; + const tbConnectionInfo = await getTBConnectionInfo(); openTunnelbrokerConnection( deviceId, identityInfo.userId, identityInfo.accessToken, + tbConnectionInfo.url, ); } @@ -56,9 +79,10 @@ deviceID: string, userID: string, accessToken: string, + tbUrl: string, ) { try { - const tunnelbrokerSocket = new WebSocket('ws://127.0.0.1:51001'); + const tunnelbrokerSocket = new WebSocket(tbUrl); tunnelbrokerSocket.on('open', () => { const message: TBKeyserverConnectionInitializationMessage = { @@ -76,8 +100,11 @@ tunnelbrokerSocket.on('close', async () => { console.warn('Connection to Tunnelbroker closed'); await sleep(1000); - console.info('Attempting to re-establish Tunnelbroker connection'); - openTunnelbrokerConnection(deviceID, userID, accessToken); + console.info( + 'Attempting to re-establish Tunnelbroker connection to ', + tbUrl, + ); + openTunnelbrokerConnection(deviceID, userID, accessToken, tbUrl); }); tunnelbrokerSocket.on('error', (error: Error) => {