Skip to content

Commit

Permalink
Fix lazy creation of ProxyAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal committed Apr 17, 2024
1 parent 968d8d8 commit 97f5430
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions ts/textsecure/SocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class SocketManager extends EventListener {

private credentials?: WebAPICredentials;

private proxyAgent?: ProxyAgent;
private lazyProxyAgent?: Promise<ProxyAgent>;

private status = SocketStatus.CLOSED;

Expand Down Expand Up @@ -164,6 +164,7 @@ export class SocketManager extends EventListener {
name: AUTHENTICATED_CHANNEL_NAME,
path: '/v1/websocket/',
query: { login: username, password },
proxyAgent: await this.getProxyAgent(),
resourceOptions: {
name: AUTHENTICATED_CHANNEL_NAME,
keepalive: { path: '/v1/keepalive' },
Expand Down Expand Up @@ -321,6 +322,7 @@ export class SocketManager extends EventListener {
return this.connectResource({
name: 'provisioning',
path: '/v1/websocket/provisioning/',
proxyAgent: await this.getProxyAgent(),
resourceOptions: {
name: 'provisioning',
handleRequest: (req: IncomingWebSocketRequest): void => {
Expand All @@ -339,16 +341,13 @@ export class SocketManager extends EventListener {
url: string;
extraHeaders?: Record<string, string>;
}): Promise<WebSocket> {
// Create proxy agent lazily
if (this.options.proxyUrl && !this.proxyAgent) {
this.proxyAgent = await createProxyAgent(this.options.proxyUrl);
}
const proxyAgent = await this.getProxyAgent();

return connectWebSocket({
name: 'art-creator-provisioning',
url,
version: this.options.version,
proxyAgent: this.proxyAgent,
proxyAgent,
extraHeaders,

createResource(socket: WebSocket): WebSocket {
Expand Down Expand Up @@ -525,15 +524,11 @@ export class SocketManager extends EventListener {
}
}

private transportOption(): TransportOption {
private transportOption(proxyAgent: ProxyAgent | undefined): TransportOption {
const { hostname } = URL.parse(this.options.url);

// transport experiment doesn't support proxy
if (
this.proxyAgent ||
hostname == null ||
!hostname.endsWith('signal.org')
) {
if (proxyAgent || hostname == null || !hostname.endsWith('signal.org')) {
return TransportOption.Original;
}

Expand Down Expand Up @@ -590,7 +585,9 @@ export class SocketManager extends EventListener {

log.info('SocketManager: connecting unauthenticated socket');

const transportOption = this.transportOption();
const proxyAgent = await this.getProxyAgent();

const transportOption = this.transportOption(proxyAgent);
log.info(
`SocketManager: connecting unauthenticated socket, transport option [${transportOption}]`
);
Expand All @@ -603,6 +600,7 @@ export class SocketManager extends EventListener {
const process = this.connectResource({
name: UNAUTHENTICATED_CHANNEL_NAME,
path: '/v1/websocket/',
proxyAgent,
resourceOptions: {
name: UNAUTHENTICATED_CHANNEL_NAME,
keepalive: { path: '/v1/keepalive' },
Expand Down Expand Up @@ -646,12 +644,14 @@ export class SocketManager extends EventListener {
private connectResource({
name,
path,
proxyAgent,
resourceOptions,
query = {},
extraHeaders = {},
}: {
name: string;
path: string;
proxyAgent: ProxyAgent | undefined;
resourceOptions: WebSocketResourceOptions;
query?: Record<string, string>;
extraHeaders?: Record<string, string>;
Expand All @@ -671,7 +671,7 @@ export class SocketManager extends EventListener {
url,
version,
certificateAuthority: this.options.certificateAuthority,
proxyAgent: this.proxyAgent,
proxyAgent,

extraHeaders,

Expand Down Expand Up @@ -876,6 +876,14 @@ export class SocketManager extends EventListener {
);
}

private async getProxyAgent(): Promise<ProxyAgent | undefined> {
if (this.options.proxyUrl && !this.lazyProxyAgent) {
// Cache the promise so that we don't import concurrently.
this.lazyProxyAgent = createProxyAgent(this.options.proxyUrl);
}
return this.lazyProxyAgent;
}

// EventEmitter types

public override on(
Expand Down

0 comments on commit 97f5430

Please sign in to comment.