]>
Commit | Line | Data |
---|---|---|
1 | import replay = require('request-replay') | |
2 | import request = require('request') | |
3 | ||
4 | import { | |
5 | RETRY_REQUESTS, | |
6 | REMOTE_SCHEME, | |
7 | CONFIG | |
8 | } from '../initializers' | |
9 | import { sign } from './peertube-crypto' | |
10 | ||
11 | function makeRetryRequest (params, callback) { | |
12 | replay( | |
13 | request(params, callback), | |
14 | { | |
15 | retries: RETRY_REQUESTS, | |
16 | factor: 3, | |
17 | maxTimeout: Infinity, | |
18 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | |
19 | } | |
20 | ) | |
21 | } | |
22 | ||
23 | function makeSecureRequest (params, callback) { | |
24 | const requestParams = { | |
25 | url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, | |
26 | json: {} | |
27 | } | |
28 | ||
29 | if (params.method !== 'POST') { | |
30 | return callback(new Error('Cannot make a secure request with a non POST method.')) | |
31 | } | |
32 | ||
33 | // Add signature if it is specified in the params | |
34 | if (params.sign === true) { | |
35 | const host = CONFIG.WEBSERVER.HOST | |
36 | ||
37 | let dataToSign | |
38 | if (params.data) { | |
39 | dataToSign = params.data | |
40 | } else { | |
41 | // We do not have data to sign so we just take our host | |
42 | // It is not ideal but the connection should be in HTTPS | |
43 | dataToSign = host | |
44 | } | |
45 | ||
46 | requestParams.json['signature'] = { | |
47 | host, // Which host we pretend to be | |
48 | signature: sign(dataToSign) | |
49 | } | |
50 | } | |
51 | ||
52 | // If there are data informations | |
53 | if (params.data) { | |
54 | requestParams.json['data'] = params.data | |
55 | } | |
56 | ||
57 | request.post(requestParams, callback) | |
58 | } | |
59 | ||
60 | // --------------------------------------------------------------------------- | |
61 | ||
62 | export { | |
63 | makeRetryRequest, | |
64 | makeSecureRequest | |
65 | } |