2019-02-21 11:19:16 -05:00
|
|
|
import { createWriteStream, remove } from 'fs-extra'
|
2021-11-16 05:17:52 -05:00
|
|
|
import got, { CancelableRequest, NormalizedOptions, Options as GotOptions, RequestError, Response } from 'got'
|
2021-08-25 09:08:37 -04:00
|
|
|
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'
|
2021-03-08 08:24:11 -05:00
|
|
|
import { join } from 'path'
|
|
|
|
import { CONFIG } from '../initializers/config'
|
2021-11-29 09:45:02 -05:00
|
|
|
import { ACTIVITY_PUB, BINARY_CONTENT_TYPES, PEERTUBE_VERSION, REQUEST_TIMEOUTS, WEBSERVER } from '../initializers/constants'
|
2021-03-08 08:24:11 -05:00
|
|
|
import { pipelinePromise } from './core-utils'
|
2018-11-16 10:48:17 -05:00
|
|
|
import { processImage } from './image-utils'
|
2021-11-16 04:49:03 -05:00
|
|
|
import { logger, loggerTagsFactory } from './logger'
|
2021-08-25 09:08:37 -04:00
|
|
|
import { getProxy, isProxyEnabled } from './proxy'
|
|
|
|
|
2021-11-16 04:49:03 -05:00
|
|
|
const lTags = loggerTagsFactory('request')
|
|
|
|
|
2021-10-26 09:08:58 -04:00
|
|
|
const httpSignature = require('@peertube/http-signature')
|
2017-12-28 05:16:08 -05:00
|
|
|
|
2021-03-09 08:01:44 -05:00
|
|
|
export interface PeerTubeRequestError extends Error {
|
|
|
|
statusCode?: number
|
|
|
|
responseBody?: any
|
2021-11-16 05:17:52 -05:00
|
|
|
responseHeaders?: any
|
2021-03-09 08:01:44 -05:00
|
|
|
}
|
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
type PeerTubeRequestOptions = {
|
2021-11-29 09:45:02 -05:00
|
|
|
timeout?: number
|
2021-03-08 08:24:11 -05:00
|
|
|
activityPub?: boolean
|
|
|
|
bodyKBLimit?: number // 1MB
|
|
|
|
httpSignature?: {
|
|
|
|
algorithm: string
|
|
|
|
authorizationHeaderName: string
|
|
|
|
keyId: string
|
|
|
|
key: string
|
|
|
|
headers: string[]
|
|
|
|
}
|
|
|
|
jsonResponse?: boolean
|
|
|
|
} & Pick<GotOptions, 'headers' | 'json' | 'method' | 'searchParams'>
|
|
|
|
|
|
|
|
const peertubeGot = got.extend({
|
2021-08-25 09:08:37 -04:00
|
|
|
...getAgent(),
|
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
headers: {
|
|
|
|
'user-agent': getUserAgent()
|
|
|
|
},
|
|
|
|
|
|
|
|
handlers: [
|
|
|
|
(options, next) => {
|
|
|
|
const promiseOrStream = next(options) as CancelableRequest<any>
|
2021-03-09 03:58:08 -05:00
|
|
|
const bodyKBLimit = options.context?.bodyKBLimit as number
|
2021-03-08 08:24:11 -05:00
|
|
|
if (!bodyKBLimit) throw new Error('No KB limit for this request')
|
|
|
|
|
2021-03-09 03:58:08 -05:00
|
|
|
const bodyLimit = bodyKBLimit * 1000
|
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
/* eslint-disable @typescript-eslint/no-floating-promises */
|
|
|
|
promiseOrStream.on('downloadProgress', progress => {
|
2021-03-09 03:58:08 -05:00
|
|
|
if (progress.transferred > bodyLimit && progress.percent !== 1) {
|
|
|
|
const message = `Exceeded the download limit of ${bodyLimit} B`
|
2021-11-16 04:49:03 -05:00
|
|
|
logger.warn(message, lTags())
|
2021-03-09 03:58:08 -05:00
|
|
|
|
|
|
|
// CancelableRequest
|
|
|
|
if (promiseOrStream.cancel) {
|
|
|
|
promiseOrStream.cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream
|
|
|
|
(promiseOrStream as any).destroy()
|
2021-03-08 08:24:11 -05:00
|
|
|
}
|
|
|
|
})
|
2019-07-16 08:52:24 -04:00
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 05:17:52 -05:00
|
|
|
],
|
|
|
|
|
|
|
|
beforeRetry: [
|
|
|
|
(_options: NormalizedOptions, error: RequestError, retryCount: number) => {
|
|
|
|
logger.debug('Retrying request to %s.', error.request.requestUrl, { retryCount, error: buildRequestError(error), ...lTags() })
|
|
|
|
}
|
2021-03-08 08:24:11 -05:00
|
|
|
]
|
2017-12-28 05:16:08 -05:00
|
|
|
}
|
2021-03-08 08:24:11 -05:00
|
|
|
})
|
2017-11-09 11:51:58 -05:00
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
function doRequest (url: string, options: PeerTubeRequestOptions = {}) {
|
|
|
|
const gotOptions = buildGotOptions(options)
|
|
|
|
|
|
|
|
return peertubeGot(url, gotOptions)
|
|
|
|
.catch(err => { throw buildRequestError(err) })
|
|
|
|
}
|
|
|
|
|
|
|
|
function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions = {}) {
|
|
|
|
const gotOptions = buildGotOptions(options)
|
|
|
|
|
|
|
|
return peertubeGot<T>(url, { ...gotOptions, responseType: 'json' })
|
|
|
|
.catch(err => { throw buildRequestError(err) })
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
async function doRequestAndSaveToFile (
|
|
|
|
url: string,
|
2019-02-21 11:19:16 -05:00
|
|
|
destPath: string,
|
2021-03-08 08:24:11 -05:00
|
|
|
options: PeerTubeRequestOptions = {}
|
2019-02-21 11:19:16 -05:00
|
|
|
) {
|
2021-11-29 09:45:02 -05:00
|
|
|
const gotOptions = buildGotOptions({ ...options, timeout: options.timeout ?? REQUEST_TIMEOUTS.FILE })
|
2018-02-15 12:40:24 -05:00
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
const outFile = createWriteStream(destPath)
|
2019-02-21 11:19:16 -05:00
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
try {
|
|
|
|
await pipelinePromise(
|
|
|
|
peertubeGot.stream(url, gotOptions),
|
|
|
|
outFile
|
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
remove(destPath)
|
2021-11-16 04:49:03 -05:00
|
|
|
.catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err, ...lTags() }))
|
2019-02-21 11:19:16 -05:00
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
throw buildRequestError(err)
|
|
|
|
}
|
2017-11-10 08:34:45 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 10:02:49 -05:00
|
|
|
async function downloadImage (url: string, destDir: string, destName: string, size: { width: number, height: number }) {
|
|
|
|
const tmpPath = join(CONFIG.STORAGE.TMP_DIR, 'pending-' + destName)
|
2021-03-08 08:24:11 -05:00
|
|
|
await doRequestAndSaveToFile(url, tmpPath)
|
2018-11-16 10:48:17 -05:00
|
|
|
|
2018-12-04 10:02:49 -05:00
|
|
|
const destPath = join(destDir, destName)
|
2019-04-15 09:04:02 -04:00
|
|
|
|
|
|
|
try {
|
2019-04-24 03:56:25 -04:00
|
|
|
await processImage(tmpPath, destPath, size)
|
2019-04-15 09:04:02 -04:00
|
|
|
} catch (err) {
|
|
|
|
await remove(tmpPath)
|
|
|
|
|
|
|
|
throw err
|
|
|
|
}
|
2018-11-16 10:48:17 -05:00
|
|
|
}
|
|
|
|
|
2021-08-25 09:08:37 -04:00
|
|
|
function getAgent () {
|
|
|
|
if (!isProxyEnabled()) return {}
|
|
|
|
|
|
|
|
const proxy = getProxy()
|
|
|
|
|
2021-11-16 04:49:03 -05:00
|
|
|
logger.info('Using proxy %s.', proxy, lTags())
|
2021-08-25 09:08:37 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 08:52:24 -04:00
|
|
|
function getUserAgent () {
|
2019-07-17 04:03:55 -04:00
|
|
|
return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})`
|
2019-07-16 08:52:24 -04:00
|
|
|
}
|
|
|
|
|
2021-10-21 10:28:39 -04:00
|
|
|
function isBinaryResponse (result: Response<any>) {
|
|
|
|
return BINARY_CONTENT_TYPES.has(result.headers['content-type'])
|
|
|
|
}
|
|
|
|
|
2021-11-05 09:11:19 -04:00
|
|
|
async function findLatestRedirection (url: string, options: PeerTubeRequestOptions, iteration = 1) {
|
|
|
|
if (iteration > 10) throw new Error('Too much iterations to find final URL ' + url)
|
|
|
|
|
|
|
|
const { headers } = await peertubeGot(url, { followRedirect: false, ...buildGotOptions(options) })
|
|
|
|
|
|
|
|
if (headers.location) return findLatestRedirection(headers.location, options, iteration + 1)
|
|
|
|
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
2017-11-09 11:51:58 -05:00
|
|
|
doRequest,
|
2021-03-08 08:24:11 -05:00
|
|
|
doJSONRequest,
|
2018-11-16 10:48:17 -05:00
|
|
|
doRequestAndSaveToFile,
|
2021-10-21 10:28:39 -04:00
|
|
|
isBinaryResponse,
|
2021-10-15 02:32:06 -04:00
|
|
|
downloadImage,
|
2022-05-09 05:49:25 -04:00
|
|
|
getAgent,
|
2021-11-05 09:11:19 -04:00
|
|
|
findLatestRedirection,
|
2021-10-15 02:32:06 -04:00
|
|
|
peertubeGot
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
2019-02-21 11:19:16 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
function buildGotOptions (options: PeerTubeRequestOptions) {
|
|
|
|
const { activityPub, bodyKBLimit = 1000 } = options
|
2019-02-21 11:19:16 -05:00
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
const context = { bodyKBLimit, httpSignature: options.httpSignature }
|
2019-02-21 11:19:16 -05:00
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
let headers = options.headers || {}
|
|
|
|
|
2021-03-10 05:17:20 -05:00
|
|
|
if (!headers.date) {
|
|
|
|
headers = { ...headers, date: new Date().toUTCString() }
|
|
|
|
}
|
2021-03-08 08:24:11 -05:00
|
|
|
|
2021-03-10 05:17:20 -05:00
|
|
|
if (activityPub && !headers.accept) {
|
2021-03-08 08:24:11 -05:00
|
|
|
headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER }
|
2019-02-21 11:19:16 -05:00
|
|
|
}
|
2021-03-08 08:24:11 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
method: options.method,
|
2021-07-02 06:58:25 -04:00
|
|
|
dnsCache: true,
|
2021-11-29 09:45:02 -05:00
|
|
|
timeout: options.timeout ?? REQUEST_TIMEOUTS.DEFAULT,
|
2021-03-08 08:24:11 -05:00
|
|
|
json: options.json,
|
|
|
|
searchParams: options.searchParams,
|
2021-11-16 04:49:03 -05:00
|
|
|
retry: 2,
|
2021-03-08 08:24:11 -05:00
|
|
|
headers,
|
|
|
|
context
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 08:01:44 -05:00
|
|
|
function buildRequestError (error: RequestError) {
|
|
|
|
const newError: PeerTubeRequestError = new Error(error.message)
|
2021-03-08 08:24:11 -05:00
|
|
|
newError.name = error.name
|
|
|
|
newError.stack = error.stack
|
|
|
|
|
2021-03-09 08:01:44 -05:00
|
|
|
if (error.response) {
|
|
|
|
newError.responseBody = error.response.body
|
2021-11-16 05:17:52 -05:00
|
|
|
newError.responseHeaders = error.response.headers
|
2021-03-09 08:01:44 -05:00
|
|
|
newError.statusCode = error.response.statusCode
|
2021-03-08 08:24:11 -05:00
|
|
|
}
|
|
|
|
|
2021-03-09 08:01:44 -05:00
|
|
|
return newError
|
2019-02-21 11:19:16 -05:00
|
|
|
}
|