From 65fcc3119c334b75dd13bcfdebf186afdc580a8f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 15 May 2017 22:22:03 +0200 Subject: First typescript iteration --- server/helpers/requests.ts | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 server/helpers/requests.ts (limited to 'server/helpers/requests.ts') diff --git a/server/helpers/requests.ts b/server/helpers/requests.ts new file mode 100644 index 000000000..8ded52972 --- /dev/null +++ b/server/helpers/requests.ts @@ -0,0 +1,65 @@ +import replay = require('request-replay') +import request = require('request') + +import { + RETRY_REQUESTS, + REMOTE_SCHEME, + CONFIG +} from '../initializers' +import { sign } from './peertube-crypto' + +function makeRetryRequest (params, callback) { + replay( + request(params, callback), + { + retries: RETRY_REQUESTS, + factor: 3, + maxTimeout: Infinity, + errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] + } + ) +} + +function makeSecureRequest (params, callback) { + const requestParams = { + url: REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path, + json: {} + } + + if (params.method !== 'POST') { + return callback(new Error('Cannot make a secure request with a non POST method.')) + } + + // Add signature if it is specified in the params + if (params.sign === true) { + const host = CONFIG.WEBSERVER.HOST + + let dataToSign + if (params.data) { + dataToSign = params.data + } else { + // We do not have data to sign so we just take our host + // It is not ideal but the connection should be in HTTPS + dataToSign = host + } + + requestParams.json['signature'] = { + host, // Which host we pretend to be + signature: sign(dataToSign) + } + } + + // If there are data informations + if (params.data) { + requestParams.json['data'] = params.data + } + + request.post(requestParams, callback) +} + +// --------------------------------------------------------------------------- + +export { + makeRetryRequest, + makeSecureRequest +} -- cgit v1.2.3