aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/requests.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/requests.ts')
-rw-r--r--server/helpers/requests.ts32
1 files changed, 30 insertions, 2 deletions
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 @@
1import { createWriteStream, remove } from 'fs-extra' 1import { createWriteStream, remove } from 'fs-extra'
2import got, { CancelableRequest, Options as GotOptions, RequestError } from 'got' 2import got, { CancelableRequest, Options as GotOptions, RequestError } from 'got'
3import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'
3import { join } from 'path' 4import { join } from 'path'
4import { CONFIG } from '../initializers/config' 5import { CONFIG } from '../initializers/config'
5import { ACTIVITY_PUB, PEERTUBE_VERSION, REQUEST_TIMEOUT, WEBSERVER } from '../initializers/constants' 6import { ACTIVITY_PUB, PEERTUBE_VERSION, REQUEST_TIMEOUT, WEBSERVER } from '../initializers/constants'
6import { pipelinePromise } from './core-utils' 7import { pipelinePromise } from './core-utils'
7import { processImage } from './image-utils' 8import { processImage } from './image-utils'
8import { logger } from './logger' 9import { logger } from './logger'
10import { getProxy, isProxyEnabled } from './proxy'
11
12const httpSignature = require('http-signature')
9 13
10export interface PeerTubeRequestError extends Error { 14export interface PeerTubeRequestError extends Error {
11 statusCode?: number 15 statusCode?: number
12 responseBody?: any 16 responseBody?: any
13} 17}
14 18
15const httpSignature = require('http-signature')
16
17type PeerTubeRequestOptions = { 19type PeerTubeRequestOptions = {
18 activityPub?: boolean 20 activityPub?: boolean
19 bodyKBLimit?: number // 1MB 21 bodyKBLimit?: number // 1MB
@@ -29,6 +31,8 @@ type PeerTubeRequestOptions = {
29} & Pick<GotOptions, 'headers' | 'json' | 'method' | 'searchParams'> 31} & Pick<GotOptions, 'headers' | 'json' | 'method' | 'searchParams'>
30 32
31const peertubeGot = got.extend({ 33const peertubeGot = got.extend({
34 ...getAgent(),
35
32 headers: { 36 headers: {
33 'user-agent': getUserAgent() 37 'user-agent': getUserAgent()
34 }, 38 },
@@ -153,6 +157,30 @@ async function downloadImage (url: string, destDir: string, destName: string, si
153 } 157 }
154} 158}
155 159
160function getAgent () {
161 if (!isProxyEnabled()) return {}
162
163 const proxy = getProxy()
164
165 logger.info('Using proxy %s.', proxy)
166
167 const proxyAgentOptions = {
168 keepAlive: true,
169 keepAliveMsecs: 1000,
170 maxSockets: 256,
171 maxFreeSockets: 256,
172 scheduling: 'lifo' as 'lifo',
173 proxy
174 }
175
176 return {
177 agent: {
178 http: new HttpProxyAgent(proxyAgentOptions),
179 https: new HttpsProxyAgent(proxyAgentOptions)
180 }
181 }
182}
183
156function getUserAgent () { 184function getUserAgent () {
157 return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})` 185 return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})`
158} 186}