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 { PodSignature } from '../../shared'
12 import { sign } from './peertube-crypto'
14 type MakeRetryRequestParams = {
19 function makeRetryRequest (params: MakeRetryRequestParams) {
20 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
22 request(params, (err, response, body) => err ? rej(err) : res({ response, body })),
24 retries: RETRY_REQUESTS,
27 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
33 type MakeSecureRequestParams = {
39 function makeSecureRequest (params: MakeSecureRequestParams) {
40 return new Promise<{ response: request.RequestResponse, body: any }>((res, rej) => {
41 const requestParams: {
44 signature: PodSignature,
48 url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path,
55 if (params.method !== 'POST') {
56 return rej(new Error('Cannot make a secure request with a non POST method.'))
59 const host = CONFIG.WEBSERVER.HOST
63 dataToSign = params.data
65 // We do not have data to sign so we just take our host
66 // It is not ideal but the connection should be in HTTPS
70 sign(dataToSign).then(signature => {
71 requestParams.json.signature = {
72 host, // Which host we pretend to be
76 // If there are data informations
78 requestParams.json.data = params.data
81 request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body }))
86 // ---------------------------------------------------------------------------