2023-07-31 14:34:36 +02:00
|
|
|
import httpSignature from '@peertube/http-signature'
|
2024-08-14 15:32:25 +02:00
|
|
|
import { CONFIG } from '@server/initializers/config.js'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { createWriteStream } from 'fs'
|
|
|
|
import { remove } from 'fs-extra/esm'
|
|
|
|
import got, { CancelableRequest, OptionsInit, OptionsOfTextResponseBody, OptionsOfUnknownResponseBody, RequestError, Response } from 'got'
|
2024-08-14 15:32:25 +02:00
|
|
|
import { gotSsrf } from 'got-ssrf'
|
2021-08-25 15:08:37 +02:00
|
|
|
import { HttpProxyAgent, HttpsProxyAgent } from 'hpagent'
|
2023-07-31 14:34:36 +02:00
|
|
|
import { ACTIVITY_PUB, BINARY_CONTENT_TYPES, PEERTUBE_VERSION, REQUEST_TIMEOUTS, WEBSERVER } from '../initializers/constants.js'
|
|
|
|
import { pipelinePromise } from './core-utils.js'
|
|
|
|
import { logger, loggerTagsFactory } from './logger.js'
|
|
|
|
import { getProxy, isProxyEnabled } from './proxy.js'
|
2021-08-25 15:08:37 +02:00
|
|
|
|
2021-11-16 10:49:03 +01:00
|
|
|
const lTags = loggerTagsFactory('request')
|
|
|
|
|
2021-03-09 14:01:44 +01:00
|
|
|
export interface PeerTubeRequestError extends Error {
|
|
|
|
statusCode?: number
|
|
|
|
responseBody?: any
|
2021-11-16 11:17:52 +01:00
|
|
|
responseHeaders?: any
|
2023-07-25 14:21:01 +02:00
|
|
|
requestHeaders?: any
|
2021-03-09 14:01:44 +01:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
export type PeerTubeRequestOptions = {
|
2021-11-29 15:45:02 +01:00
|
|
|
timeout?: number
|
2021-03-08 14:24:11 +01:00
|
|
|
activityPub?: boolean
|
|
|
|
bodyKBLimit?: number // 1MB
|
2023-07-27 17:01:15 +02:00
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
httpSignature?: {
|
|
|
|
algorithm: string
|
|
|
|
authorizationHeaderName: string
|
|
|
|
keyId: string
|
|
|
|
key: string
|
|
|
|
headers: string[]
|
|
|
|
}
|
2023-07-27 17:01:15 +02:00
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
jsonResponse?: boolean
|
2023-07-27 17:01:15 +02:00
|
|
|
|
|
|
|
followRedirect?: boolean
|
2023-07-31 14:34:36 +02:00
|
|
|
} & Pick<OptionsInit, 'headers' | 'json' | 'method' | 'searchParams'>
|
2021-03-08 14:24:11 +01:00
|
|
|
|
2024-08-14 15:32:25 +02:00
|
|
|
export const unsafeSSRFGot = got.extend({
|
|
|
|
...getProxyAgent(),
|
2021-08-25 15:08:37 +02:00
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
headers: {
|
|
|
|
'user-agent': getUserAgent()
|
|
|
|
},
|
|
|
|
|
|
|
|
handlers: [
|
|
|
|
(options, next) => {
|
|
|
|
const promiseOrStream = next(options) as CancelableRequest<any>
|
2021-03-09 09:58:08 +01:00
|
|
|
const bodyKBLimit = options.context?.bodyKBLimit as number
|
2021-03-08 14:24:11 +01:00
|
|
|
if (!bodyKBLimit) throw new Error('No KB limit for this request')
|
|
|
|
|
2021-03-09 09:58:08 +01:00
|
|
|
const bodyLimit = bodyKBLimit * 1000
|
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-floating-promises */
|
|
|
|
promiseOrStream.on('downloadProgress', progress => {
|
2021-03-09 09:58:08 +01:00
|
|
|
if (progress.transferred > bodyLimit && progress.percent !== 1) {
|
|
|
|
const message = `Exceeded the download limit of ${bodyLimit} B`
|
2021-11-16 10:49:03 +01:00
|
|
|
logger.warn(message, lTags())
|
2021-03-09 09:58:08 +01:00
|
|
|
|
|
|
|
// CancelableRequest
|
|
|
|
if (promiseOrStream.cancel) {
|
|
|
|
promiseOrStream.cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stream
|
|
|
|
(promiseOrStream as any).destroy()
|
2021-03-08 14:24:11 +01:00
|
|
|
}
|
|
|
|
})
|
2019-07-16 14:52:24 +02:00
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
return promiseOrStream
|
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
hooks: {
|
|
|
|
beforeRequest: [
|
|
|
|
options => {
|
|
|
|
const headers = options.headers || {}
|
2023-07-31 14:34:36 +02:00
|
|
|
headers['host'] = buildUrl(options.url).host
|
2021-03-08 14:24:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
options => {
|
|
|
|
const httpSignatureOptions = options.context?.httpSignature
|
|
|
|
|
|
|
|
if (httpSignatureOptions) {
|
|
|
|
const method = options.method ?? 'GET'
|
2023-07-31 14:34:36 +02:00
|
|
|
const path = buildUrl(options.url).pathname
|
2021-03-08 14:24:11 +01:00
|
|
|
|
|
|
|
if (!method || !path) {
|
|
|
|
throw new Error(`Cannot sign request without method (${method}) or path (${path}) ${options}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpSignature.signRequest({
|
2022-07-15 11:06:27 +02:00
|
|
|
getHeader: function (header: string) {
|
|
|
|
const value = options.headers[header.toLowerCase()]
|
|
|
|
|
|
|
|
if (!value) logger.warn('Unknown header requested by http-signature.', { headers: options.headers, header })
|
|
|
|
return value
|
2021-03-08 14:24:11 +01:00
|
|
|
},
|
|
|
|
|
2022-07-15 11:06:27 +02:00
|
|
|
setHeader: function (header: string, value: string) {
|
2021-03-08 14:24:11 +01:00
|
|
|
options.headers[header] = value
|
|
|
|
},
|
|
|
|
|
|
|
|
method,
|
|
|
|
path
|
|
|
|
}, httpSignatureOptions)
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 11:17:52 +01:00
|
|
|
],
|
|
|
|
|
|
|
|
beforeRetry: [
|
2023-07-31 14:34:36 +02:00
|
|
|
(error: RequestError, retryCount: number) => {
|
2021-11-16 11:17:52 +01:00
|
|
|
logger.debug('Retrying request to %s.', error.request.requestUrl, { retryCount, error: buildRequestError(error), ...lTags() })
|
|
|
|
}
|
2021-03-08 14:24:11 +01:00
|
|
|
]
|
2017-12-28 11:16:08 +01:00
|
|
|
}
|
2021-03-08 14:24:11 +01:00
|
|
|
})
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2024-08-14 15:32:25 +02:00
|
|
|
export const peertubeGot = CONFIG.FEDERATION.PREVENT_SSRF
|
|
|
|
? got.extend(gotSsrf, unsafeSSRFGot)
|
|
|
|
: unsafeSSRFGot
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2024-08-14 16:14:06 +02:00
|
|
|
export function doRequest (url: string, options: PeerTubeRequestOptions & { preventSSRF?: false } = {}) {
|
2023-07-31 14:34:36 +02:00
|
|
|
const gotOptions = buildGotOptions(options) as OptionsOfTextResponseBody
|
2021-03-08 14:24:11 +01:00
|
|
|
|
2024-08-14 16:14:06 +02:00
|
|
|
const gotInstance = options.preventSSRF === false
|
|
|
|
? unsafeSSRFGot
|
|
|
|
: peertubeGot
|
|
|
|
|
|
|
|
return gotInstance(url, gotOptions)
|
2021-03-08 14:24:11 +01:00
|
|
|
.catch(err => { throw buildRequestError(err) })
|
|
|
|
}
|
|
|
|
|
2024-08-14 15:32:25 +02:00
|
|
|
export function doJSONRequest <T> (url: string, options: PeerTubeRequestOptions & { preventSSRF?: false } = {}) {
|
2021-03-08 14:24:11 +01:00
|
|
|
const gotOptions = buildGotOptions(options)
|
|
|
|
|
2024-08-14 15:32:25 +02:00
|
|
|
const gotInstance = options.preventSSRF === false
|
|
|
|
? unsafeSSRFGot
|
|
|
|
: peertubeGot
|
|
|
|
|
|
|
|
return gotInstance<T>(url, { ...gotOptions, responseType: 'json' })
|
2021-03-08 14:24:11 +01:00
|
|
|
.catch(err => { throw buildRequestError(err) })
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
export async function doRequestAndSaveToFile (url: string, destPath: string, options: PeerTubeRequestOptions = {}) {
|
2021-11-29 15:45:02 +01:00
|
|
|
const gotOptions = buildGotOptions({ ...options, timeout: options.timeout ?? REQUEST_TIMEOUTS.FILE })
|
2018-02-15 18:40:24 +01:00
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
const outFile = createWriteStream(destPath)
|
2019-02-21 17:19:16 +01:00
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
try {
|
|
|
|
await pipelinePromise(
|
2023-07-31 14:34:36 +02:00
|
|
|
peertubeGot.stream(url, { ...gotOptions, isStream: true }),
|
2021-03-08 14:24:11 +01:00
|
|
|
outFile
|
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
remove(destPath)
|
2021-11-16 10:49:03 +01:00
|
|
|
.catch(err => logger.error('Cannot remove %s after request failure.', destPath, { err, ...lTags() }))
|
2019-02-21 17:19:16 +01:00
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
throw buildRequestError(err)
|
|
|
|
}
|
2017-11-10 14:34:45 +01:00
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
export function generateRequestStream (url: string, options: PeerTubeRequestOptions = {}) {
|
|
|
|
const gotOptions = buildGotOptions({ ...options, timeout: options.timeout ?? REQUEST_TIMEOUTS.DEFAULT })
|
|
|
|
|
|
|
|
return peertubeGot.stream(url, { ...gotOptions, isStream: true })
|
|
|
|
}
|
|
|
|
|
2024-08-14 15:32:25 +02:00
|
|
|
export function getProxyAgent () {
|
2021-08-25 15:08:37 +02:00
|
|
|
if (!isProxyEnabled()) return {}
|
|
|
|
|
|
|
|
const proxy = getProxy()
|
|
|
|
|
2021-11-16 10:49:03 +01:00
|
|
|
logger.info('Using proxy %s.', proxy, lTags())
|
2021-08-25 15:08:37 +02: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-23 16:38:51 +02:00
|
|
|
export function isBinaryResponse (result: Response<any>) {
|
2021-10-21 16:28:39 +02:00
|
|
|
return BINARY_CONTENT_TYPES.has(result.headers['content-type'])
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2024-07-23 16:38:51 +02:00
|
|
|
// Private
|
2019-02-21 17:19:16 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2024-08-14 15:32:25 +02:00
|
|
|
function getUserAgent () {
|
|
|
|
return `PeerTube/${PEERTUBE_VERSION} (+${WEBSERVER.URL})`
|
|
|
|
}
|
|
|
|
|
2023-07-31 14:34:36 +02:00
|
|
|
function buildGotOptions (options: PeerTubeRequestOptions): OptionsOfUnknownResponseBody {
|
2024-08-08 10:33:41 +02:00
|
|
|
const { activityPub, bodyKBLimit = 3000 } = options
|
2019-02-21 17:19:16 +01:00
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
const context = { bodyKBLimit, httpSignature: options.httpSignature }
|
2019-02-21 17:19:16 +01:00
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
let headers = options.headers || {}
|
|
|
|
|
2021-03-10 11:17:20 +01:00
|
|
|
if (!headers.date) {
|
|
|
|
headers = { ...headers, date: new Date().toUTCString() }
|
|
|
|
}
|
2021-03-08 14:24:11 +01:00
|
|
|
|
2021-03-10 11:17:20 +01:00
|
|
|
if (activityPub && !headers.accept) {
|
2021-03-08 14:24:11 +01:00
|
|
|
headers = { ...headers, accept: ACTIVITY_PUB.ACCEPT_HEADER }
|
2019-02-21 17:19:16 +01:00
|
|
|
}
|
2021-03-08 14:24:11 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
method: options.method,
|
2021-07-02 12:58:25 +02:00
|
|
|
dnsCache: true,
|
2023-07-31 14:34:36 +02:00
|
|
|
timeout: {
|
|
|
|
request: options.timeout ?? REQUEST_TIMEOUTS.DEFAULT
|
|
|
|
},
|
2021-03-08 14:24:11 +01:00
|
|
|
json: options.json,
|
|
|
|
searchParams: options.searchParams,
|
2023-07-27 17:01:15 +02:00
|
|
|
followRedirect: options.followRedirect,
|
2023-07-31 14:34:36 +02:00
|
|
|
retry: {
|
|
|
|
limit: 2
|
|
|
|
},
|
2021-03-08 14:24:11 +01:00
|
|
|
headers,
|
|
|
|
context
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:01:44 +01:00
|
|
|
function buildRequestError (error: RequestError) {
|
|
|
|
const newError: PeerTubeRequestError = new Error(error.message)
|
2021-03-08 14:24:11 +01:00
|
|
|
newError.name = error.name
|
|
|
|
newError.stack = error.stack
|
|
|
|
|
2021-03-09 14:01:44 +01:00
|
|
|
if (error.response) {
|
|
|
|
newError.responseBody = error.response.body
|
2021-11-16 11:17:52 +01:00
|
|
|
newError.responseHeaders = error.response.headers
|
2021-03-09 14:01:44 +01:00
|
|
|
newError.statusCode = error.response.statusCode
|
2021-03-08 14:24:11 +01:00
|
|
|
}
|
|
|
|
|
2023-07-25 14:21:01 +02:00
|
|
|
if (error.options) {
|
|
|
|
newError.requestHeaders = error.options.headers
|
|
|
|
}
|
|
|
|
|
2021-03-09 14:01:44 +01:00
|
|
|
return newError
|
2019-02-21 17:19:16 +01:00
|
|
|
}
|
2023-07-31 14:34:36 +02:00
|
|
|
|
|
|
|
function buildUrl (url: string | URL) {
|
|
|
|
if (typeof url === 'string') {
|
|
|
|
return new URL(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
return url
|
|
|
|
}
|