From 8729a87024fc837f7dd6f13afeec90cf6dda1c06 Mon Sep 17 00:00:00 2001 From: smilekison <24309222+smilekison@users.noreply.github.com> Date: Wed, 25 Aug 2021 06:08:37 -0700 Subject: Support proxies for PeerTube (#4346) * Updated with latest code * Updated Support proxies for PeerTube * Support Proxies for PeerTube (Updated with change request) * Cleanup proxy PR Co-authored-by: Chocobozzz --- server/helpers/requests.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) (limited to 'server/helpers/requests.ts') diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index 36e69458e..e09e23086 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, 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 @@ -29,6 +31,8 @@ type PeerTubeRequestOptions = { } & Pick const peertubeGot = got.extend({ + ...getAgent(), + headers: { 'user-agent': getUserAgent() }, @@ -153,6 +157,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})` } -- cgit v1.2.3