]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/requests.js
Pod URL -> pod host. HTTPS is required to make friends.
[github/Chocobozzz/PeerTube.git] / server / helpers / requests.js
CommitLineData
9f10b292 1'use strict'
dac0a531 2
f0f5567b 3const replay = require('request-replay')
528a9efa 4const request = require('request')
dac0a531 5
f0f5567b 6const constants = require('../initializers/constants')
5f698b82 7const peertubeCrypto = require('./peertube-crypto')
dac0a531 8
f0f5567b 9const requests = {
c4403b29
C
10 makeRetryRequest,
11 makeSecureRequest
9f10b292 12}
dac0a531 13
528a9efa
C
14function makeRetryRequest (params, callback) {
15 replay(
16 request(params, callback),
17 {
18 retries: constants.RETRY_REQUESTS,
19 factor: 3,
20 maxTimeout: Infinity,
21 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
22 }
23 )
24}
dac0a531 25
528a9efa 26function makeSecureRequest (params, callback) {
528a9efa 27 const requestParams = {
49abbbbe 28 url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path
9f10b292 29 }
dac0a531 30
528a9efa
C
31 // Add data with POST requst ?
32 if (params.method === 'POST') {
33 requestParams.json = {}
dac0a531 34
528a9efa
C
35 // Add signature if it is specified in the params
36 if (params.sign === true) {
49abbbbe
C
37 const host = constants.CONFIG.WEBSERVER.HOST
38
528a9efa 39 requestParams.json.signature = {
49abbbbe
C
40 host,
41 signature: peertubeCrypto.sign(host)
528a9efa 42 }
9f10b292 43 }
dac0a531 44
528a9efa
C
45 // If there are data informations
46 if (params.data) {
47 // Encrypt data
48 if (params.encrypt === true) {
49 peertubeCrypto.encrypt(params.toPod.publicKey, JSON.stringify(params.data), function (err, encrypted) {
f0f5567b 50 if (err) return callback(err)
9f10b292 51
528a9efa
C
52 requestParams.json.data = encrypted.data
53 requestParams.json.key = encrypted.key
9f10b292 54
528a9efa 55 request.post(requestParams, callback)
f0f5567b 56 })
dac0a531 57 } else {
528a9efa
C
58 // No encryption
59 requestParams.json.data = params.data
60 request.post(requestParams, callback)
dac0a531 61 }
9f10b292 62 } else {
528a9efa
C
63 // No data
64 request.post(requestParams, callback)
9f10b292 65 }
528a9efa
C
66 } else {
67 request.get(requestParams, callback)
68 }
9f10b292 69}
dac0a531 70
9f10b292 71// ---------------------------------------------------------------------------
dac0a531 72
9f10b292 73module.exports = requests