Move websocket preconnect earlier into startup
Co-authored-by: trevor-signal <131492920+trevor-signal@users.noreply.github.com>
This commit is contained in:
parent
cc4470bb6f
commit
f4185e65dc
5 changed files with 102 additions and 83 deletions
|
@ -15,9 +15,7 @@ import { v4 as getGuid } from 'uuid';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import type { Readable } from 'stream';
|
import type { Readable } from 'stream';
|
||||||
|
|
||||||
import { Net } from '@signalapp/libsignal-client';
|
|
||||||
import { assertDev, strictAssert } from '../util/assert';
|
import { assertDev, strictAssert } from '../util/assert';
|
||||||
import { drop } from '../util/drop';
|
|
||||||
import * as durations from '../util/durations';
|
import * as durations from '../util/durations';
|
||||||
import type { ExplodePromiseResultType } from '../util/explodePromise';
|
import type { ExplodePromiseResultType } from '../util/explodePromise';
|
||||||
import { explodePromise } from '../util/explodePromise';
|
import { explodePromise } from '../util/explodePromise';
|
||||||
|
@ -69,8 +67,8 @@ import * as log from '../logging/log';
|
||||||
import { maybeParseUrl, urlPathFromComponents } from '../util/url';
|
import { maybeParseUrl, urlPathFromComponents } from '../util/url';
|
||||||
import { HOUR, MINUTE, SECOND } from '../util/durations';
|
import { HOUR, MINUTE, SECOND } from '../util/durations';
|
||||||
import { safeParseNumber } from '../util/numbers';
|
import { safeParseNumber } from '../util/numbers';
|
||||||
import { isStagingServer } from '../util/isStagingServer';
|
|
||||||
import type { IWebSocketResource } from './WebsocketResources';
|
import type { IWebSocketResource } from './WebsocketResources';
|
||||||
|
import { getLibsignalNet } from './preconnect';
|
||||||
import type { GroupSendToken } from '../types/GroupSendEndorsements';
|
import type { GroupSendToken } from '../types/GroupSendEndorsements';
|
||||||
import { parseUnknown, safeParseUnknown } from '../util/schemas';
|
import { parseUnknown, safeParseUnknown } from '../util/schemas';
|
||||||
import type {
|
import type {
|
||||||
|
@ -78,8 +76,6 @@ import type {
|
||||||
ProfileFetchUnauthRequestOptions,
|
ProfileFetchUnauthRequestOptions,
|
||||||
} from '../services/profiles';
|
} from '../services/profiles';
|
||||||
import { isMockServer } from '../util/isMockServer';
|
import { isMockServer } from '../util/isMockServer';
|
||||||
import { getMockServerPort } from '../util/getMockServerPort';
|
|
||||||
import { pemToDer } from '../util/pemToDer';
|
|
||||||
import { ToastType } from '../types/Toast';
|
import { ToastType } from '../types/Toast';
|
||||||
import { isProduction } from '../util/version';
|
import { isProduction } from '../util/version';
|
||||||
import type { ServerAlert } from '../util/handleServerAlerts';
|
import type { ServerAlert } from '../util/handleServerAlerts';
|
||||||
|
@ -91,43 +87,6 @@ import { isAbortError } from '../util/isAbortError';
|
||||||
const DEBUG = false;
|
const DEBUG = false;
|
||||||
const DEFAULT_TIMEOUT = 30 * SECOND;
|
const DEFAULT_TIMEOUT = 30 * SECOND;
|
||||||
|
|
||||||
// Libsignal has internally configured values for domain names
|
|
||||||
// (and other connectivity params) of the services.
|
|
||||||
function resolveLibsignalNet(
|
|
||||||
url: string,
|
|
||||||
version: string,
|
|
||||||
certificateAuthority?: string
|
|
||||||
): Net.Net {
|
|
||||||
const userAgent = getUserAgent(version);
|
|
||||||
log.info(`libsignal net url: ${url}`);
|
|
||||||
if (isStagingServer(url)) {
|
|
||||||
log.info('libsignal net environment resolved to staging');
|
|
||||||
return new Net.Net({
|
|
||||||
env: Net.Environment.Staging,
|
|
||||||
userAgent,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isMockServer(url) && certificateAuthority !== undefined) {
|
|
||||||
const DISCARD_PORT = 9; // Reserved by RFC 863.
|
|
||||||
log.info('libsignal net environment resolved to mock');
|
|
||||||
return new Net.Net({
|
|
||||||
localTestServer: true,
|
|
||||||
userAgent,
|
|
||||||
TESTING_localServer_chatPort: parseInt(getMockServerPort(url), 10),
|
|
||||||
TESTING_localServer_cdsiPort: DISCARD_PORT,
|
|
||||||
TESTING_localServer_svr2Port: DISCARD_PORT,
|
|
||||||
TESTING_localServer_rootCertificateDer: pemToDer(certificateAuthority),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
log.info('libsignal net environment resolved to prod');
|
|
||||||
return new Net.Net({
|
|
||||||
env: Net.Environment.Production,
|
|
||||||
userAgent,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function _createRedactor(
|
function _createRedactor(
|
||||||
...toReplace: ReadonlyArray<string | undefined | null>
|
...toReplace: ReadonlyArray<string | undefined | null>
|
||||||
): RedactUrl {
|
): RedactUrl {
|
||||||
|
@ -1760,45 +1719,7 @@ export type TopLevelType = {
|
||||||
|
|
||||||
type InflightCallback = (error: Error) => unknown;
|
type InflightCallback = (error: Error) => unknown;
|
||||||
|
|
||||||
// `libsignalNet` is an instance of a class from libsignal that is responsible
|
const libsignalNet = getLibsignalNet();
|
||||||
// for providing network layer API and related functionality.
|
|
||||||
// It's important to have a single instance of this class as it holds
|
|
||||||
// resources that are shared across all other use cases.
|
|
||||||
let libsignalNet: Net.Net;
|
|
||||||
|
|
||||||
// Not definied in tests
|
|
||||||
if (window.SignalContext.config?.serverUrl) {
|
|
||||||
const { config } = window.SignalContext;
|
|
||||||
|
|
||||||
libsignalNet = resolveLibsignalNet(
|
|
||||||
config.serverUrl,
|
|
||||||
config.version,
|
|
||||||
config.certificateAuthority
|
|
||||||
);
|
|
||||||
|
|
||||||
libsignalNet.setIpv6Enabled(!config.disableIPv6);
|
|
||||||
if (config.proxyUrl) {
|
|
||||||
log.info('WebAPI: Setting libsignal proxy');
|
|
||||||
try {
|
|
||||||
libsignalNet.setProxyFromUrl(config.proxyUrl);
|
|
||||||
} catch (error) {
|
|
||||||
log.error(`WebAPI: Failed to set proxy: ${error}`);
|
|
||||||
libsignalNet.clearProxy();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
drop(
|
|
||||||
(async () => {
|
|
||||||
try {
|
|
||||||
log.info('WebAPI: preconnect start');
|
|
||||||
await libsignalNet.preconnectChat();
|
|
||||||
log.info('WebAPI: preconnect done');
|
|
||||||
} catch (error) {
|
|
||||||
log.error(`WebAPI: Failed to preconnect: ${toLogFormat(error)}`);
|
|
||||||
}
|
|
||||||
})()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// We first set up the data that won't change during this session of the app
|
// We first set up the data that won't change during this session of the app
|
||||||
export function initialize({
|
export function initialize({
|
||||||
|
|
94
ts/textsecure/preconnect.ts
Normal file
94
ts/textsecure/preconnect.ts
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
// Copyright 2025 Signal Messenger, LLC
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
import { Net } from '@signalapp/libsignal-client';
|
||||||
|
|
||||||
|
import { getUserAgent } from '../util/getUserAgent';
|
||||||
|
import { isStagingServer } from '../util/isStagingServer';
|
||||||
|
import { getMockServerPort } from '../util/getMockServerPort';
|
||||||
|
import { isMockServer } from '../util/isMockServer';
|
||||||
|
import { pemToDer } from '../util/pemToDer';
|
||||||
|
import { drop } from '../util/drop';
|
||||||
|
import { toLogFormat } from '../types/errors';
|
||||||
|
import * as log from '../logging/log';
|
||||||
|
|
||||||
|
// Libsignal has internally configured values for domain names
|
||||||
|
// (and other connectivity params) of the services.
|
||||||
|
function resolveLibsignalNet(
|
||||||
|
url: string,
|
||||||
|
version: string,
|
||||||
|
certificateAuthority?: string
|
||||||
|
): Net.Net {
|
||||||
|
const userAgent = getUserAgent(version);
|
||||||
|
log.info(`libsignal net url: ${url}`);
|
||||||
|
if (isStagingServer(url)) {
|
||||||
|
log.info('libsignal net environment resolved to staging');
|
||||||
|
return new Net.Net({
|
||||||
|
env: Net.Environment.Staging,
|
||||||
|
userAgent,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isMockServer(url) && certificateAuthority !== undefined) {
|
||||||
|
const DISCARD_PORT = 9; // Reserved by RFC 863.
|
||||||
|
log.info('libsignal net environment resolved to mock');
|
||||||
|
return new Net.Net({
|
||||||
|
localTestServer: true,
|
||||||
|
userAgent,
|
||||||
|
TESTING_localServer_chatPort: parseInt(getMockServerPort(url), 10),
|
||||||
|
TESTING_localServer_cdsiPort: DISCARD_PORT,
|
||||||
|
TESTING_localServer_svr2Port: DISCARD_PORT,
|
||||||
|
TESTING_localServer_rootCertificateDer: pemToDer(certificateAuthority),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info('libsignal net environment resolved to prod');
|
||||||
|
return new Net.Net({
|
||||||
|
env: Net.Environment.Production,
|
||||||
|
userAgent,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// `libsignalNet` is an instance of a class from libsignal that is responsible
|
||||||
|
// for providing network layer API and related functionality.
|
||||||
|
// It's important to have a single instance of this class as it holds
|
||||||
|
// resources that are shared across all other use cases.
|
||||||
|
let libsignalNet: Net.Net;
|
||||||
|
|
||||||
|
export function getLibsignalNet(): Net.Net {
|
||||||
|
return libsignalNet;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not defined in tests
|
||||||
|
if (window.SignalContext.config?.serverUrl) {
|
||||||
|
const { config } = window.SignalContext;
|
||||||
|
|
||||||
|
libsignalNet = resolveLibsignalNet(
|
||||||
|
config.serverUrl,
|
||||||
|
config.version,
|
||||||
|
config.certificateAuthority
|
||||||
|
);
|
||||||
|
|
||||||
|
libsignalNet.setIpv6Enabled(!config.disableIPv6);
|
||||||
|
if (config.proxyUrl) {
|
||||||
|
log.info('WebAPI: Setting libsignal proxy');
|
||||||
|
try {
|
||||||
|
libsignalNet.setProxyFromUrl(config.proxyUrl);
|
||||||
|
} catch (error) {
|
||||||
|
log.error(`WebAPI: Failed to set proxy: ${error}`);
|
||||||
|
libsignalNet.clearProxy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drop(
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
log.info('WebAPI: preconnect start');
|
||||||
|
await libsignalNet.preconnectChat();
|
||||||
|
log.info('WebAPI: preconnect done');
|
||||||
|
} catch (error) {
|
||||||
|
log.error(`WebAPI: Failed to preconnect: ${toLogFormat(error)}`);
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
);
|
||||||
|
}
|
|
@ -10,7 +10,7 @@ export function isStagingServer(
|
||||||
if (getEnvironment() === Environment.Staging) {
|
if (getEnvironment() === Environment.Staging) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (isStaging(window.getVersion())) {
|
if (isStaging(window.SignalContext.getVersion())) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return /staging/i.test(serverUrl);
|
return /staging/i.test(serverUrl);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
// Copyright 2025 Signal Messenger, LLC
|
// Copyright 2025 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
import '../context';
|
|
||||||
import { Environment, getEnvironment } from '../../environment';
|
import { Environment, getEnvironment } from '../../environment';
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|
|
@ -6,6 +6,11 @@ import { contextBridge } from 'electron';
|
||||||
|
|
||||||
import * as log from '../../logging/log';
|
import * as log from '../../logging/log';
|
||||||
|
|
||||||
|
import '../context';
|
||||||
|
|
||||||
|
// Connect websocket early
|
||||||
|
import '../../textsecure/preconnect';
|
||||||
|
|
||||||
import './phase0-devtools';
|
import './phase0-devtools';
|
||||||
import './phase1-ipc';
|
import './phase1-ipc';
|
||||||
import '../preload';
|
import '../preload';
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue