]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
dac0a531 | 2 | |
f0f5567b | 3 | const replay = require('request-replay') |
528a9efa | 4 | const request = require('request') |
dac0a531 | 5 | |
f0f5567b | 6 | const constants = require('../initializers/constants') |
5f698b82 | 7 | const peertubeCrypto = require('./peertube-crypto') |
dac0a531 | 8 | |
f0f5567b | 9 | const requests = { |
c4403b29 C |
10 | makeRetryRequest, |
11 | makeSecureRequest | |
9f10b292 | 12 | } |
dac0a531 | 13 | |
528a9efa C |
14 | function 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 | 26 | function makeSecureRequest (params, callback) { |
528a9efa | 27 | const requestParams = { |
49abbbbe | 28 | url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path |
9f10b292 | 29 | } |
dac0a531 | 30 | |
bdfbd4f1 C |
31 | if (params.method !== 'POST') { |
32 | return callback(new Error('Cannot make a secure request with a non POST method.')) | |
33 | } | |
34 | ||
35 | requestParams.json = {} | |
dac0a531 | 36 | |
bdfbd4f1 C |
37 | // Add signature if it is specified in the params |
38 | if (params.sign === true) { | |
39 | const host = constants.CONFIG.WEBSERVER.HOST | |
40 | ||
41 | let dataToSign | |
528a9efa | 42 | if (params.data) { |
bdfbd4f1 | 43 | dataToSign = dataToSign = params.data |
9f10b292 | 44 | } else { |
bdfbd4f1 C |
45 | // We do not have data to sign so we just take our host |
46 | // It is not ideal but the connection should be in HTTPS | |
47 | dataToSign = host | |
9f10b292 | 48 | } |
bdfbd4f1 C |
49 | |
50 | requestParams.json.signature = { | |
51 | host, // Which host we pretend to be | |
52 | signature: peertubeCrypto.sign(dataToSign) | |
53 | } | |
54 | } | |
55 | ||
56 | // If there are data informations | |
57 | if (params.data) { | |
58 | requestParams.json.data = params.data | |
528a9efa | 59 | } |
bdfbd4f1 C |
60 | |
61 | request.post(requestParams, callback) | |
9f10b292 | 62 | } |
dac0a531 | 63 | |
9f10b292 | 64 | // --------------------------------------------------------------------------- |
dac0a531 | 65 | |
9f10b292 | 66 | module.exports = requests |