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 config = require('config')
|
|
|
|
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')
|
|
|
|
const peertubeCrypto = require('./peertubeCrypto')
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const http = config.get('webserver.https') ? 'https' : 'http'
|
|
|
|
const host = config.get('webserver.host')
|
|
|
|
const port = config.get('webserver.port')
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const requests = {
|
2016-06-18 10:13:54 -04:00
|
|
|
makeRetryRequest: makeRetryRequest,
|
|
|
|
makeSecureRequest: 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 myUrl = http + '://' + host + ':' + port
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-06-18 10:13:54 -04:00
|
|
|
const requestParams = {
|
|
|
|
url: params.toPod.url + params.path
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-06-18 10:13:54 -04:00
|
|
|
// Add data with POST requst ?
|
|
|
|
if (params.method === 'POST') {
|
|
|
|
requestParams.json = {}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-06-18 10:13:54 -04:00
|
|
|
// Add signature if it is specified in the params
|
|
|
|
if (params.sign === true) {
|
|
|
|
requestParams.json.signature = {
|
|
|
|
url: myUrl,
|
|
|
|
signature: peertubeCrypto.sign(myUrl)
|
|
|
|
}
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-02-05 12:03:20 -05:00
|
|
|
|
2016-06-18 10:13:54 -04:00
|
|
|
// If there are data informations
|
|
|
|
if (params.data) {
|
|
|
|
// Encrypt data
|
|
|
|
if (params.encrypt === true) {
|
|
|
|
peertubeCrypto.encrypt(params.toPod.publicKey, JSON.stringify(params.data), function (err, encrypted) {
|
2016-03-16 17:29:27 -04:00
|
|
|
if (err) return callback(err)
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-06-18 10:13:54 -04:00
|
|
|
requestParams.json.data = encrypted.data
|
|
|
|
requestParams.json.key = encrypted.key
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-06-18 10:13:54 -04:00
|
|
|
request.post(requestParams, callback)
|
2016-03-16 17:29:27 -04:00
|
|
|
})
|
2016-02-05 12:03:20 -05:00
|
|
|
} else {
|
2016-06-18 10:13:54 -04:00
|
|
|
// No encryption
|
|
|
|
requestParams.json.data = params.data
|
|
|
|
request.post(requestParams, callback)
|
2016-02-05 12:03:20 -05:00
|
|
|
}
|
2016-02-07 05:23:23 -05:00
|
|
|
} else {
|
2016-06-18 10:13:54 -04:00
|
|
|
// No data
|
|
|
|
request.post(requestParams, callback)
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-06-18 10:13:54 -04:00
|
|
|
} else {
|
|
|
|
request.get(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
|