2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2022-02-24 21:01:41 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type { StrictOptions as GotOptions } from 'got';
|
|
|
|
import config from 'config';
|
2023-05-30 23:57:16 +00:00
|
|
|
import { Agent as HTTPAgent } from 'http';
|
2022-02-24 21:01:41 +00:00
|
|
|
|
|
|
|
import * as packageJson from '../../package.json';
|
|
|
|
import { getUserAgent } from '../util/getUserAgent';
|
2022-03-02 19:48:07 +00:00
|
|
|
import * as durations from '../util/durations';
|
2023-05-30 23:57:16 +00:00
|
|
|
import { createHTTPSAgent } from '../util/createHTTPSAgent';
|
2023-08-29 23:58:48 +00:00
|
|
|
import { createProxyAgent } from '../util/createProxyAgent';
|
2022-02-24 21:01:41 +00:00
|
|
|
|
2022-04-27 23:02:41 +00:00
|
|
|
export const GOT_CONNECT_TIMEOUT = durations.MINUTE;
|
|
|
|
export const GOT_LOOKUP_TIMEOUT = durations.MINUTE;
|
|
|
|
export const GOT_SOCKET_TIMEOUT = durations.MINUTE;
|
|
|
|
const GOT_RETRY_LIMIT = 3;
|
2022-02-24 21:01:41 +00:00
|
|
|
|
|
|
|
export function getProxyUrl(): string | undefined {
|
|
|
|
return process.env.HTTPS_PROXY || process.env.https_proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCertificateAuthority(): string {
|
|
|
|
return config.get('certificateAuthority');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getGotOptions(): GotOptions {
|
|
|
|
const certificateAuthority = getCertificateAuthority();
|
|
|
|
const proxyUrl = getProxyUrl();
|
|
|
|
const agent = proxyUrl
|
|
|
|
? {
|
2023-08-29 23:58:48 +00:00
|
|
|
http: createProxyAgent(proxyUrl),
|
|
|
|
https: createProxyAgent(proxyUrl),
|
2022-02-24 21:01:41 +00:00
|
|
|
}
|
2023-05-30 23:57:16 +00:00
|
|
|
: {
|
|
|
|
http: new HTTPAgent(),
|
|
|
|
https: createHTTPSAgent(),
|
|
|
|
};
|
2022-02-24 21:01:41 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
agent,
|
|
|
|
https: {
|
|
|
|
certificateAuthority,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
'User-Agent': getUserAgent(packageJson.version),
|
|
|
|
},
|
|
|
|
timeout: {
|
|
|
|
connect: GOT_CONNECT_TIMEOUT,
|
|
|
|
lookup: GOT_LOOKUP_TIMEOUT,
|
|
|
|
|
|
|
|
// This timeout is reset whenever we get new data on the socket
|
|
|
|
socket: GOT_SOCKET_TIMEOUT,
|
|
|
|
},
|
2022-04-27 23:02:41 +00:00
|
|
|
retry: {
|
|
|
|
limit: GOT_RETRY_LIMIT,
|
|
|
|
errorCodes: [
|
|
|
|
'ETIMEDOUT',
|
|
|
|
'ECONNRESET',
|
|
|
|
'ECONNREFUSED',
|
|
|
|
'EPIPE',
|
|
|
|
'ENOTFOUND',
|
|
|
|
'ENETUNREACH',
|
|
|
|
'EAI_AGAIN',
|
|
|
|
],
|
|
|
|
methods: ['GET', 'HEAD'],
|
|
|
|
statusCodes: [413, 429, 503],
|
|
|
|
},
|
2022-02-24 21:01:41 +00:00
|
|
|
};
|
|
|
|
}
|