diff --git a/ts/textsecure/WebAPI.ts b/ts/textsecure/WebAPI.ts index 37c761c2a..80d2d956c 100644 --- a/ts/textsecure/WebAPI.ts +++ b/ts/textsecure/WebAPI.ts @@ -1367,13 +1367,17 @@ export function initialize({ log.warn(`${logId}: Done`); } - let fetchForLinkPreviews: linkPreviewFetch.FetchFn; + let fetchAgent: Agent; if (proxyUrl) { - const agent = createProxyAgent(proxyUrl); - fetchForLinkPreviews = (href, init) => fetch(href, { ...init, agent }); + fetchAgent = createProxyAgent(proxyUrl); } else { - fetchForLinkPreviews = fetch; + fetchAgent = createHTTPSAgent({ + keepAlive: false, + maxCachedSessions: 0, + }); } + const fetchForLinkPreviews: linkPreviewFetch.FetchFn = (href, init) => + fetch(href, { ...init, agent: fetchAgent }); // Thanks, function hoisting! return { diff --git a/ts/util/createHTTPSAgent.ts b/ts/util/createHTTPSAgent.ts index d6eac2fa3..eef8c848f 100644 --- a/ts/util/createHTTPSAgent.ts +++ b/ts/util/createHTTPSAgent.ts @@ -33,6 +33,28 @@ const CONNECT_TIMEOUT_MS = 10 * SECOND; const electronLookup = promisify(electronLookupWithCb); +const HOST_LOG_ALLOWLIST = new Set([ + // Production + 'chat.signal.org', + 'storage.signal.org', + 'cdsi.signal.org', + 'cdn.signal.org', + 'cdn2.signal.org', + 'create.signal.art', + + // Staging + 'chat.staging.signal.org', + 'storage-staging.signal.org', + 'cdsi.staging.signal.org', + 'cdn-staging.signal.org', + 'cdn2-staging.signal.org', + 'create.staging.signal.art', + + // Common + 'updates2.signal.org', + 'sfu.voip.signal.org', +]); + export class Agent extends HTTPSAgent { constructor(options: AgentOptions = {}) { super({ @@ -65,16 +87,18 @@ export class Agent extends HTTPSAgent { }, }); - const duration = Date.now() - start; - const logLine = - `createHTTPSAgent.createConnection(${host}): connected to ` + - `IPv${address.family} addr after ${duration}ms ` + - `(attempts v4=${v4Attempts} v6=${v6Attempts})`; + if (HOST_LOG_ALLOWLIST.has(host)) { + const duration = Date.now() - start; + const logLine = + `createHTTPSAgent.createConnection(${host}): connected to ` + + `IPv${address.family} addr after ${duration}ms ` + + `(attempts v4=${v4Attempts} v6=${v6Attempts})`; - if (v4Attempts + v6Attempts > 1 || duration > CONNECT_THRESHOLD_MS) { - log.warn(logLine); - } else { - log.info(logLine); + if (v4Attempts + v6Attempts > 1 || duration > CONNECT_THRESHOLD_MS) { + log.warn(logLine); + } else { + log.info(logLine); + } } return socket; diff --git a/ts/util/dns.ts b/ts/util/dns.ts index f1e5cdfb2..a835d2327 100644 --- a/ts/util/dns.ts +++ b/ts/util/dns.ts @@ -1,36 +1,18 @@ // Copyright 2023 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -import type { LookupOneOptions, LookupAllOptions, LookupAddress } from 'dns'; -import { lookup as nodeLookup } from 'dns'; +import type { + LookupOneOptions, + LookupAllOptions, + LookupAddress, + lookup as nodeLookup, +} from 'dns'; import { ipcRenderer, net } from 'electron'; import type { ResolvedHost } from 'electron'; import { strictAssert } from './assert'; import { drop } from './drop'; -const HOST_ALLOWLIST = new Set([ - // Production - 'chat.signal.org', - 'storage.signal.org', - 'cdsi.signal.org', - 'cdn.signal.org', - 'cdn2.signal.org', - 'create.signal.art', - - // Staging - 'chat.staging.signal.org', - 'storage-staging.signal.org', - 'cdsi.staging.signal.org', - 'cdn-staging.signal.org', - 'cdn2-staging.signal.org', - 'create.staging.signal.art', - - // Common - 'updates2.signal.org', - 'sfu.voip.signal.org', -]); - function lookupAll( hostname: string, opts: LookupOneOptions | LookupAllOptions, @@ -40,11 +22,6 @@ function lookupAll( family?: number ) => void ): void { - if (!HOST_ALLOWLIST.has(hostname)) { - nodeLookup(hostname, opts, callback); - return; - } - // Node.js support various signatures, but we only support one. strictAssert(typeof opts === 'object', 'missing options'); strictAssert(typeof callback === 'function', 'missing callback');