]>
Commit | Line | Data |
---|---|---|
8c308c2b C |
1 | ;(function () { |
2 | 'use strict' | |
3 | ||
3bcb78b3 | 4 | var async = require('async') |
8c308c2b | 5 | var config = require('config') |
a1860380 | 6 | var crypto = require('crypto') |
8c308c2b C |
7 | var fs = require('fs') |
8 | var openssl = require('openssl-wrapper') | |
a1860380 C |
9 | var request = require('request') |
10 | var replay = require('request-replay') | |
11 | var ursa = require('ursa') | |
8c308c2b | 12 | |
656ea8f7 | 13 | var constants = require('./constants') |
8c308c2b C |
14 | var logger = require('./logger') |
15 | ||
a1860380 C |
16 | var utils = {} |
17 | ||
8c308c2b C |
18 | var http = config.get('webserver.https') ? 'https' : 'http' |
19 | var host = config.get('webserver.host') | |
20 | var port = config.get('webserver.port') | |
21 | var algorithm = 'aes-256-ctr' | |
22 | ||
8c308c2b C |
23 | // ----------- Private functions ---------- |
24 | ||
25 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { | |
26 | // Append the signature | |
27 | if (signature) { | |
28 | params.json.signature = { | |
29 | url: from_url, | |
30 | signature: signature | |
31 | } | |
32 | } | |
33 | ||
1cb4884d | 34 | logger.debug('Make retry requests to %s.', to_pod.url) |
8c308c2b | 35 | |
8c308c2b C |
36 | replay( |
37 | request.post(params, function (err, response, body) { | |
0b697522 | 38 | callbackEach(err, response, body, params.url, to_pod) |
8c308c2b C |
39 | }), |
40 | { | |
656ea8f7 | 41 | retries: constants.REQUEST_RETRIES, |
8c308c2b C |
42 | factor: 3, |
43 | maxTimeout: Infinity, | |
44 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | |
45 | } | |
46 | ).on('replay', function (replay) { | |
47 | logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.', | |
48 | params.url, replay.error.code, replay.error.message, replay.number, replay.delay) | |
49 | }) | |
50 | } | |
51 | ||
52 | // ----------- Public attributes ---------- | |
53 | utils.certDir = __dirname + '/../' + config.get('storage.certs') | |
54 | ||
55 | // { path, data } | |
a1860380 | 56 | utils.makeMultipleRetryRequest = function (all_data, pods, callbackEach, callback) { |
8c308c2b C |
57 | if (!callback) { |
58 | callback = callbackEach | |
59 | callbackEach = function () {} | |
60 | } | |
61 | ||
62 | var url = http + '://' + host + ':' + port | |
63 | var signature | |
64 | ||
a1860380 C |
65 | // Add signature if it is specified in the params |
66 | if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { | |
d148f3b9 | 67 | var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem')) |
8c308c2b C |
68 | signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex') |
69 | } | |
70 | ||
71 | // Make a request for each pod | |
3bcb78b3 | 72 | async.each(pods, function (pod, callback_each_async) { |
0b697522 C |
73 | function callbackEachRetryRequest (err, response, body, url, pod) { |
74 | callbackEach(err, response, body, url, pod, function () { | |
3bcb78b3 C |
75 | callback_each_async() |
76 | }) | |
77 | } | |
78 | ||
8c308c2b | 79 | var params = { |
a1860380 C |
80 | url: pod.url + all_data.path, |
81 | method: all_data.method | |
8c308c2b C |
82 | } |
83 | ||
84 | // Add data with POST requst ? | |
a1860380 | 85 | if (all_data.method === 'POST' && all_data.data) { |
8c308c2b | 86 | // Encrypt data ? |
a1860380 | 87 | if (all_data.encrypt === true) { |
8c308c2b C |
88 | var crt = ursa.createPublicKey(pod.publicKey) |
89 | ||
90 | // TODO: ES6 with let | |
91 | ;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) { | |
a1860380 | 92 | utils.symetricEncrypt(JSON.stringify(all_data.data), function (err, dataEncrypted) { |
8c308c2b C |
93 | if (err) throw err |
94 | ||
95 | var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex') | |
96 | copy_params.json = { | |
97 | data: dataEncrypted.crypted, | |
98 | key: passwordEncrypted | |
99 | } | |
100 | ||
3bcb78b3 | 101 | makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest) |
8c308c2b C |
102 | }) |
103 | })(crt, params, url, pod, signature) | |
104 | } else { | |
a1860380 | 105 | params.json = { data: all_data.data } |
3bcb78b3 | 106 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) |
8c308c2b C |
107 | } |
108 | } else { | |
3bcb78b3 | 109 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) |
8c308c2b | 110 | } |
3bcb78b3 | 111 | }, callback) |
8c308c2b C |
112 | } |
113 | ||
114 | utils.certsExist = function (callback) { | |
d148f3b9 | 115 | fs.exists(utils.certDir + 'peertube.key.pem', function (exists) { |
8c308c2b C |
116 | return callback(exists) |
117 | }) | |
118 | } | |
119 | ||
120 | utils.createCerts = function (callback) { | |
121 | utils.certsExist(function (exist) { | |
122 | if (exist === true) { | |
123 | var string = 'Certs already exist.' | |
124 | logger.warning(string) | |
125 | return callback(new Error(string)) | |
126 | } | |
127 | ||
a1860380 | 128 | logger.info('Generating a RSA key...') |
2696b9af | 129 | openssl.exec('genrsa', { 'out': utils.certDir + 'peertube.key.pem', '2048': false }, function (err) { |
8c308c2b C |
130 | if (err) { |
131 | logger.error('Cannot create private key on this pod.', { error: err }) | |
132 | return callback(err) | |
133 | } | |
a1860380 | 134 | logger.info('RSA key generated.') |
8c308c2b | 135 | |
a1860380 | 136 | logger.info('Manage public key...') |
d148f3b9 | 137 | openssl.exec('rsa', { 'in': utils.certDir + 'peertube.key.pem', 'pubout': true, 'out': utils.certDir + 'peertube.pub' }, function (err) { |
8c308c2b C |
138 | if (err) { |
139 | logger.error('Cannot create public key on this pod .', { error: err }) | |
140 | return callback(err) | |
141 | } | |
142 | ||
a1860380 | 143 | logger.info('Public key managed.') |
8c308c2b C |
144 | return callback(null) |
145 | }) | |
146 | }) | |
147 | }) | |
148 | } | |
149 | ||
150 | utils.createCertsIfNotExist = function (callback) { | |
151 | utils.certsExist(function (exist) { | |
152 | if (exist === true) { | |
153 | return callback(null) | |
154 | } | |
155 | ||
156 | utils.createCerts(function (err) { | |
157 | return callback(err) | |
158 | }) | |
159 | }) | |
160 | } | |
161 | ||
162 | utils.generatePassword = function (callback) { | |
163 | crypto.randomBytes(32, function (err, buf) { | |
164 | if (err) { | |
165 | return callback(err) | |
166 | } | |
167 | ||
168 | callback(null, buf.toString('utf8')) | |
169 | }) | |
170 | } | |
171 | ||
172 | utils.symetricEncrypt = function (text, callback) { | |
173 | utils.generatePassword(function (err, password) { | |
174 | if (err) { | |
175 | return callback(err) | |
176 | } | |
177 | ||
178 | var cipher = crypto.createCipher(algorithm, password) | |
179 | var crypted = cipher.update(text, 'utf8', 'hex') | |
180 | crypted += cipher.final('hex') | |
181 | callback(null, { crypted: crypted, password: password }) | |
182 | }) | |
183 | } | |
184 | ||
185 | utils.symetricDecrypt = function (text, password) { | |
186 | var decipher = crypto.createDecipher(algorithm, password) | |
187 | var dec = decipher.update(text, 'hex', 'utf8') | |
188 | dec += decipher.final('utf8') | |
189 | return dec | |
190 | } | |
191 | ||
0ae2e7f7 C |
192 | utils.cleanForExit = function (webtorrent_process) { |
193 | logger.info('Gracefully exiting') | |
194 | process.kill(-webtorrent_process.pid) | |
195 | } | |
196 | ||
8c308c2b C |
197 | module.exports = utils |
198 | })() |