X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Frequests.ts;h=799034b90dc0c1f2a24e16a9a8df215c1c58c99d;hb=5037e0e474044d7fc04092158784395a001e5c25;hp=b31074373e2c9c449db2a20c475ff0846a6ee455;hpb=6fcd19ba737f1f5614a56c6925adb882dea43b8d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts index b31074373..799034b90 100644 --- a/server/helpers/requests.ts +++ b/server/helpers/requests.ts @@ -1,83 +1,237 @@ -import * as replay from 'request-replay' -import * as request from 'request' -import * as Promise from 'bluebird' - -import { - RETRY_REQUESTS, - REMOTE_SCHEME, - CONFIG -} from '../initializers' -import { PodInstance } from '../models' -import { sign } from './peertube-crypto' - -type MakeRetryRequestParams = { - url: string, - method: 'GET'|'POST', - json: Object +import { createWriteStream, remove } from 'fs-extra' +import got, { CancelableRequest, Options as GotOptions, RequestError, Response } from 'got' +import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent' +import { join } from 'path' +import { CONFIG } from '../initializers/config' +import { ACTIVITY_PUB, BINARY_CONTENT_TYPES, 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('@peertube/http-signature') + +export interface PeerTubeRequestError extends Error { + statusCode?: number + responseBody?: any } -function makeRetryRequest (params: MakeRetryRequestParams) { - return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { - replay( - request(params, (err, response, body) => err ? rej(err) : res({ response, body })), - { - retries: RETRY_REQUESTS, - factor: 3, - maxTimeout: Infinity, - errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] + +type PeerTubeRequestOptions = { + activityPub?: boolean + bodyKBLimit?: number // 1MB + httpSignature?: { + algorithm: string + authorizationHeaderName: string + keyId: string + key: string + headers: string[] + } + jsonResponse?: boolean +} & Pick + +const peertubeGot = got.extend({ + ...getAgent(), + + headers: { + 'user-agent': getUserAgent() + }, + + handlers: [ + (options, next) => { + const promiseOrStream = next(options) as CancelableRequest + const bodyKBLimit = options.context?.bodyKBLimit as number + if (!bodyKBLimit) throw new Error('No KB limit for this request') + + const bodyLimit = bodyKBLimit * 1000 + + /* eslint-disable @typescript-eslint/no-floating-promises */ + promiseOrStream.on('downloadProgress', progress => { + if (progress.transferred > bodyLimit && progress.percent !== 1) { + const message = `Exceeded the download limit of ${bodyLimit} B` + logger.warn(message) + + // CancelableRequest + if (promiseOrStream.cancel) { + promiseOrStream.cancel() + return + } + + // Stream + (promiseOrStream as any).destroy() + } + }) + + return promiseOrStream + } + ], + + hooks: { + beforeRequest: [ + options => { + const headers = options.headers || {} + headers['host'] = options.url.host + }, + + options => { + const httpSignatureOptions = options.context?.httpSignature + + if (httpSignatureOptions) { + const method = options.method ?? 'GET' + const path = options.path ?? options.url.pathname + + if (!method || !path) { + throw new Error(`Cannot sign request without method (${method}) or path (${path}) ${options}`) + } + + httpSignature.signRequest({ + getHeader: function (header) { + return options.headers[header] + }, + + setHeader: function (header, value) { + options.headers[header] = value + }, + + method, + path + }, httpSignatureOptions) + } } + ] + } +}) + +function doRequest (url: string, options: PeerTubeRequestOptions = {}) { + const gotOptions = buildGotOptions(options) + + return peertubeGot(url, gotOptions) + .catch(err => { throw buildRequestError(err) }) +} + +function doJSONRequest (url: string, options: PeerTubeRequestOptions = {}) { + const gotOptions = buildGotOptions(options) + + return peertubeGot(url, { ...gotOptions, responseType: 'json' }) + .catch(err => { throw buildRequestError(err) }) +} + +async function doRequestAndSaveToFile ( + url: string, + destPath: string, + options: PeerTubeRequestOptions = {} +) { + const gotOptions = buildGotOptions(options) + + const outFile = createWriteStream(destPath) + + try { + await pipelinePromise( + peertubeGot.stream(url, gotOptions), + outFile ) - }) + } catch (err) { + remove(destPath) + .catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err })) + + throw buildRequestError(err) + } } -type MakeSecureRequestParams = { - method: 'GET'|'POST' - toPod: PodInstance - path: string - sign: boolean - data?: Object +async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) { + const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName) + await doRequestAndSaveToFile(url, tmpPath) + + const destPath = join(destDir, destName) + + try { + await processImage(tmpPath, destPath, size) + } catch (err) { + await remove(tmpPath) + + throw err + } } -function makeSecureRequest (params: MakeSecureRequestParams) { - return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => { - const requestParams = { - url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, - json: {} - } - if (params.method !== 'POST') { - return rej(new Error('Cannot make a secure request with a non POST method.')) - } +function getAgent () { + if (!isProxyEnabled()) return {} - // Add signature if it is specified in the params - if (params.sign === true) { - const host = CONFIG.WEBSERVER.HOST - - let dataToSign - if (params.data) { - dataToSign = params.data - } else { - // We do not have data to sign so we just take our host - // It is not ideal but the connection should be in HTTPS - dataToSign = host - } + const proxy = getProxy() - requestParams.json['signature'] = { - host, // Which host we pretend to be - signature: sign(dataToSign) - } - } + logger.info('Using proxy %s.', proxy) - // If there are data informations - if (params.data) { - requestParams.json['data'] = params.data + 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})` +} - request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body })) - }) +function isBinaryResponse (result: Response) { + return BINARY_CONTENT_TYPES.has(result.headers['content-type']) } // --------------------------------------------------------------------------- export { - makeRetryRequest, - makeSecureRequest + doRequest, + doJSONRequest, + doRequestAndSaveToFile, + isBinaryResponse, + downloadImage, + peertubeGot +} + +// --------------------------------------------------------------------------- + +function buildGotOptions (options: PeerTubeRequestOptions) { + const { activityPub, bodyKBLimit = 1000 } = options + + const context = { bodyKBLimit, httpSignature: options.httpSignature } + + let headers = options.headers || {} + + if (!headers.date) { + headers = { ...headers, date: new Date().toUTCString() } + } + + if (activityPub && !headers.accept) { + headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER } + } + + return { + method: options.method, + dnsCache: true, + timeout: REQUEST_TIMEOUT, + json: options.json, + searchParams: options.searchParams, + headers, + context + } +} + +function buildRequestError (error: RequestError) { + const newError: PeerTubeRequestError = new Error(error.message) + newError.name = error.name + newError.stack = error.stack + + if (error.response) { + newError.responseBody = error.response.body + newError.statusCode = error.response.statusCode + } + + return newError }