1 import * as replay from 'request-replay'
2 import * as request from 'request'
3 import * as Promise from 'bluebird'
9 } from '../initializers'
10 import { PodInstance } from '../models'
11 import { sign } from './peertube-crypto'
13 type MakeRetryRequestParams = {
18 function makeRetryRequest (params: MakeRetryRequestParams) {
19 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
21 request(params, (err, response, body) => err ? rej(err) : res({ response, body })),
23 retries: RETRY_REQUESTS,
26 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
32 type MakeSecureRequestParams = {
39 function makeSecureRequest (params: MakeSecureRequestParams) {
40 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
41 const requestParams = {
42 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
46 if (params.method !== 'POST') {
47 return rej(new Error('Cannot make a secure request with a non POST method.'))
50 // Add signature if it is specified in the params
51 if (params.sign === true) {
52 const host = CONFIG.WEBSERVER.HOST
56 dataToSign = params.data
58 // We do not have data to sign so we just take our host
59 // It is not ideal but the connection should be in HTTPS
63 requestParams.json['signature'] = {
64 host, // Which host we pretend to be
65 signature: sign(dataToSign)
69 // If there are data informations
71 requestParams.json['data'] = params.data
74 request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body }))
78 // ---------------------------------------------------------------------------