1 import * as replay from 'request-replay'
2 import * as request from 'request'
8 } from '../initializers'
9 import { PodInstance } from '../models'
10 import { sign } from './peertube-crypto'
12 type MakeRetryRequestParams = {
17 function makeRetryRequest (params: MakeRetryRequestParams, callback: request.RequestCallback) {
19 request(params, callback),
21 retries: RETRY_REQUESTS,
24 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
29 type MakeSecureRequestParams = {
36 function makeSecureRequest (params: MakeSecureRequestParams, callback: request.RequestCallback) {
37 const requestParams = {
38 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
42 if (params.method !== 'POST') {
43 return callback(new Error('Cannot make a secure request with a non POST method.'), null, null)
46 // Add signature if it is specified in the params
47 if (params.sign === true) {
48 const host = CONFIG.WEBSERVER.HOST
52 dataToSign = params.data
54 // We do not have data to sign so we just take our host
55 // It is not ideal but the connection should be in HTTPS
59 requestParams.json['signature'] = {
60 host, // Which host we pretend to be
61 signature: sign(dataToSign)
65 // If there are data informations
67 requestParams.json['data'] = params.data
70 request.post(requestParams, callback)
73 // ---------------------------------------------------------------------------