2016-02-07 11:23:23 +01:00
|
|
|
'use strict'
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const async = require('async')
|
|
|
|
const config = require('config')
|
|
|
|
const request = require('request')
|
|
|
|
const replay = require('request-replay')
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const constants = require('../initializers/constants')
|
|
|
|
const logger = require('./logger')
|
|
|
|
const peertubeCrypto = require('./peertubeCrypto')
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const http = config.get('webserver.https') ? 'https' : 'http'
|
|
|
|
const host = config.get('webserver.host')
|
|
|
|
const port = config.get('webserver.port')
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const requests = {
|
2016-02-07 11:23:23 +01:00
|
|
|
makeMultipleRetryRequest: makeMultipleRetryRequest
|
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-05-11 21:19:34 +02:00
|
|
|
function makeMultipleRetryRequest (allData, pods, callbackEach, callback) {
|
2016-02-07 11:23:23 +01:00
|
|
|
if (!callback) {
|
|
|
|
callback = callbackEach
|
|
|
|
callbackEach = null
|
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const url = http + '://' + host + ':' + port
|
|
|
|
let signature
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// Add signature if it is specified in the params
|
2016-05-11 21:19:34 +02:00
|
|
|
if (allData.method === 'POST' && allData.data && allData.sign === true) {
|
2016-02-07 11:23:23 +01:00
|
|
|
signature = peertubeCrypto.sign(url)
|
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// Make a request for each pod
|
2016-05-11 21:19:34 +02:00
|
|
|
async.each(pods, function (pod, callbackEachAsync) {
|
2016-02-07 11:23:23 +01:00
|
|
|
function callbackEachRetryRequest (err, response, body, url, pod) {
|
|
|
|
if (callbackEach !== null) {
|
|
|
|
callbackEach(err, response, body, url, pod, function () {
|
2016-05-11 21:19:34 +02:00
|
|
|
callbackEachAsync()
|
2016-02-07 11:23:23 +01:00
|
|
|
})
|
|
|
|
} else {
|
2016-05-11 21:19:34 +02:00
|
|
|
callbackEachAsync()
|
2016-02-05 18:03:20 +01:00
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
const params = {
|
2016-05-11 21:19:34 +02:00
|
|
|
url: pod.url + allData.path,
|
|
|
|
method: allData.method
|
2016-02-07 11:23:23 +01:00
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// Add data with POST requst ?
|
2016-05-11 21:19:34 +02:00
|
|
|
if (allData.method === 'POST' && allData.data) {
|
2016-02-07 11:23:23 +01:00
|
|
|
// Encrypt data ?
|
2016-05-11 21:19:34 +02:00
|
|
|
if (allData.encrypt === true) {
|
|
|
|
peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(allData.data), function (err, encrypted) {
|
2016-03-16 22:29:27 +01:00
|
|
|
if (err) return callback(err)
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
params.json = {
|
|
|
|
data: encrypted.data,
|
|
|
|
key: encrypted.key
|
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2016-03-16 22:29:27 +01:00
|
|
|
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
|
|
|
|
})
|
2016-02-05 18:03:20 +01:00
|
|
|
} else {
|
2016-05-11 21:19:34 +02:00
|
|
|
params.json = { data: allData.data }
|
2016-02-05 18:03:20 +01:00
|
|
|
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
|
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
} else {
|
|
|
|
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
|
|
|
|
}
|
|
|
|
}, callback)
|
|
|
|
}
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
module.exports = requests
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-02-07 11:23:23 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-05 18:03:20 +01:00
|
|
|
|
2016-05-11 21:19:34 +02:00
|
|
|
function makeRetryRequest (params, fromUrl, toPod, signature, callbackEach) {
|
2016-02-07 11:23:23 +01:00
|
|
|
// Append the signature
|
|
|
|
if (signature) {
|
|
|
|
params.json.signature = {
|
2016-05-11 21:19:34 +02:00
|
|
|
url: fromUrl,
|
2016-02-07 11:23:23 +01:00
|
|
|
signature: signature
|
2016-02-05 18:03:20 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-07 11:23:23 +01:00
|
|
|
|
2016-05-11 21:19:34 +02:00
|
|
|
logger.debug('Make retry requests to %s.', toPod.url)
|
2016-02-07 11:23:23 +01:00
|
|
|
|
|
|
|
replay(
|
|
|
|
request.post(params, function (err, response, body) {
|
2016-05-11 21:19:34 +02:00
|
|
|
callbackEach(err, response, body, params.url, toPod)
|
2016-02-07 11:23:23 +01:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
retries: constants.REQUEST_RETRIES,
|
|
|
|
factor: 3,
|
|
|
|
maxTimeout: Infinity,
|
|
|
|
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
|
|
|
|
}
|
|
|
|
).on('replay', function (replay) {
|
|
|
|
logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
|
|
|
|
params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
|
|
|
|
})
|
|
|
|
}
|