2016-02-07 05:23:23 -05:00
|
|
|
'use strict'
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const replay = require('request-replay')
|
2016-06-18 10:13:54 -04:00
|
|
|
const request = require('request')
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const constants = require('../initializers/constants')
|
2016-07-01 10:22:36 -04:00
|
|
|
const peertubeCrypto = require('./peertube-crypto')
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const requests = {
|
2016-10-02 06:19:02 -04:00
|
|
|
makeRetryRequest,
|
|
|
|
makeSecureRequest
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-06-18 10:13:54 -04:00
|
|
|
function makeRetryRequest (params, callback) {
|
|
|
|
replay(
|
|
|
|
request(params, callback),
|
|
|
|
{
|
|
|
|
retries: constants.RETRY_REQUESTS,
|
|
|
|
factor: 3,
|
|
|
|
maxTimeout: Infinity,
|
|
|
|
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-06-18 10:13:54 -04:00
|
|
|
function makeSecureRequest (params, callback) {
|
|
|
|
const requestParams = {
|
2016-11-14 14:03:04 -05:00
|
|
|
url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2017-01-04 16:23:07 -05:00
|
|
|
if (params.method !== 'POST') {
|
|
|
|
return callback(new Error('Cannot make a secure request with a non POST method.'))
|
|
|
|
}
|
|
|
|
|
|
|
|
requestParams.json = {}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2017-01-04 16:23:07 -05:00
|
|
|
// Add signature if it is specified in the params
|
|
|
|
if (params.sign === true) {
|
|
|
|
const host = constants.CONFIG.WEBSERVER.HOST
|
|
|
|
|
|
|
|
let dataToSign
|
2016-06-18 10:13:54 -04:00
|
|
|
if (params.data) {
|
2017-01-04 16:23:07 -05:00
|
|
|
dataToSign = dataToSign = params.data
|
2016-02-07 05:23:23 -05:00
|
|
|
} else {
|
2017-01-04 16:23:07 -05:00
|
|
|
// We do not have data to sign so we just take our host
|
|
|
|
// It is not ideal but the connection should be in HTTPS
|
|
|
|
dataToSign = host
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2017-01-04 16:23:07 -05:00
|
|
|
|
|
|
|
requestParams.json.signature = {
|
|
|
|
host, // Which host we pretend to be
|
|
|
|
signature: peertubeCrypto.sign(dataToSign)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are data informations
|
|
|
|
if (params.data) {
|
|
|
|
requestParams.json.data = params.data
|
2016-06-18 10:13:54 -04:00
|
|
|
}
|
2017-01-04 16:23:07 -05:00
|
|
|
|
2017-02-21 15:35:59 -05:00
|
|
|
console.log(requestParams.json.data)
|
|
|
|
|
2017-01-04 16:23:07 -05:00
|
|
|
request.post(requestParams, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
module.exports = requests
|