X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Frequests.ts;h=5354780170da2852f6287bbc6e23ebcdc8c5909b;hb=dbe91db73e8b7992b414bdc0d3088dc73dde40b6;hp=fd2a56f30c717e6ce892b1fd90deca6af228a327;hpb=a786d8a08bf99f339bf16808f46e160404497ae2;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index fd2a56f30..535478017 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts @@ -1,19 +1,21 @@ import { createWriteStream, remove } from 'fs-extra' import got, { CancelableRequest, Options as GotOptions, RequestError } from 'got' +import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent' import { join } from 'path' import { CONFIG } from '../initializers/config' -import { ACTIVITY_PUB, PEERTUBE_VERSION, WEBSERVER } from '../initializers/constants' +import { ACTIVITY_PUB, PEERTUBE_VERSION, REQUEST_TIMEOUT, WEBSERVER } from '../initializers/constants' import { pipelinePromise } from './core-utils' import { processImage } from './image-utils' import { logger } from './logger' +import { getProxy, isProxyEnabled } from './proxy' + +const httpSignature = require('http-signature') export interface PeerTubeRequestError extends Error { statusCode?: number responseBody?: any } -const httpSignature = require('http-signature') - type PeerTubeRequestOptions = { activityPub?: boolean bodyKBLimit?: number // 1MB @@ -28,6 +30,8 @@ type PeerTubeRequestOptions = { } & Pick const peertubeGot = got.extend({ + ...getAgent(), + headers: { 'user-agent': getUserAgent() }, @@ -92,6 +96,10 @@ const peertubeGot = got.extend({ path }, httpSignatureOptions) } + }, + + (options: GotOptions) => { + options.timeout = REQUEST_TIMEOUT } ] } @@ -148,6 +156,30 @@ async function downloadImage (url: string, destDir: string, destName: string, si } } +function getAgent () { + if (!isProxyEnabled()) return {} + + const proxy = getProxy() + + logger.info('Using proxy %s.', proxy) + + const proxyAgentOptions = { + keepAlive: true, + keepAliveMsecs: 1000, + maxSockets: 256, + maxFreeSockets: 256, + scheduling: 'lifo' as 'lifo', + proxy + } + + return { + agent: { + http: new HttpProxyAgent(proxyAgentOptions), + https: new HttpsProxyAgent(proxyAgentOptions) + } + } +} + function getUserAgent () { return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})` } @@ -180,6 +212,7 @@ function buildGotOptions (options: PeerTubeRequestOptions) { return { method: options.method, + dnsCache: true, json: options.json, searchParams: options.searchParams, headers,