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 = {
38 function makeSecureRequest (params: MakeSecureRequestParams) {
39 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
40 const requestParams = {
41 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
45 if (params.method !== 'POST') {
46 return rej(new Error('Cannot make a secure request with a non POST method.'))
49 const host = CONFIG.WEBSERVER.HOST
53 dataToSign = params.data
55 // We do not have data to sign so we just take our host
56 // It is not ideal but the connection should be in HTTPS
60 sign(dataToSign).then(signature => {
61 requestParams.json['signature'] = {
62 host, // Which host we pretend to be
66 // If there are data informations
68 requestParams.json['data'] = params.data
71 request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body }))
76 // ---------------------------------------------------------------------------