Use createHTTPSAgent for all requests

This commit is contained in:
Fedor Indutny 2023-12-12 23:57:09 +01:00 committed by GitHub
parent 15e1d6e441
commit f8c4e392fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 42 deletions

View file

@ -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 {

View file

@ -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;

View file

@ -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');