diff options
Diffstat (limited to 'src/utils.js')
-rw-r--r-- | src/utils.js | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/src/utils.js b/src/utils.js index 8ce1789f9..d6b26db4b 100644 --- a/src/utils.js +++ b/src/utils.js | |||
@@ -1,23 +1,23 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | var request = require('request') | ||
5 | var replay = require('request-replay') | ||
6 | var ursa = require('ursa') | ||
7 | var config = require('config') | 4 | var config = require('config') |
5 | var crypto = require('crypto') | ||
8 | var fs = require('fs') | 6 | var fs = require('fs') |
9 | var openssl = require('openssl-wrapper') | 7 | var openssl = require('openssl-wrapper') |
10 | var crypto = require('crypto') | 8 | var request = require('request') |
9 | var replay = require('request-replay') | ||
10 | var ursa = require('ursa') | ||
11 | 11 | ||
12 | var logger = require('./logger') | 12 | var logger = require('./logger') |
13 | 13 | ||
14 | var utils = {} | ||
15 | |||
14 | var http = config.get('webserver.https') ? 'https' : 'http' | 16 | var http = config.get('webserver.https') ? 'https' : 'http' |
15 | var host = config.get('webserver.host') | 17 | var host = config.get('webserver.host') |
16 | var port = config.get('webserver.port') | 18 | var port = config.get('webserver.port') |
17 | var algorithm = 'aes-256-ctr' | 19 | var algorithm = 'aes-256-ctr' |
18 | 20 | ||
19 | var utils = {} | ||
20 | |||
21 | // ----------- Private functions ---------- | 21 | // ----------- Private functions ---------- |
22 | 22 | ||
23 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { | 23 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { |
@@ -29,7 +29,7 @@ | |||
29 | } | 29 | } |
30 | } | 30 | } |
31 | 31 | ||
32 | logger.debug('Sending informations to %s', to_pod.url, { params: params }) | 32 | logger.debug('Sending informations to %s.', to_pod.url, { params: params }) |
33 | 33 | ||
34 | // Replay 15 times, with factor 3 | 34 | // Replay 15 times, with factor 3 |
35 | replay( | 35 | replay( |
@@ -52,7 +52,7 @@ | |||
52 | utils.certDir = __dirname + '/../' + config.get('storage.certs') | 52 | utils.certDir = __dirname + '/../' + config.get('storage.certs') |
53 | 53 | ||
54 | // { path, data } | 54 | // { path, data } |
55 | utils.makeMultipleRetryRequest = function (all, pods, callbackEach, callback) { | 55 | utils.makeMultipleRetryRequest = function (all_data, pods, callbackEach, callback) { |
56 | if (!callback) { | 56 | if (!callback) { |
57 | callback = callbackEach | 57 | callback = callbackEach |
58 | callbackEach = function () {} | 58 | callbackEach = function () {} |
@@ -61,8 +61,8 @@ | |||
61 | var url = http + '://' + host + ':' + port | 61 | var url = http + '://' + host + ':' + port |
62 | var signature | 62 | var signature |
63 | 63 | ||
64 | // Signature ? | 64 | // Add signature if it is specified in the params |
65 | if (all.method === 'POST' && all.data && all.sign === true) { | 65 | if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { |
66 | var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem')) | 66 | var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem')) |
67 | signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex') | 67 | signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex') |
68 | } | 68 | } |
@@ -70,22 +70,21 @@ | |||
70 | // Make a request for each pod | 70 | // Make a request for each pod |
71 | for (var pod of pods) { | 71 | for (var pod of pods) { |
72 | var params = { | 72 | var params = { |
73 | url: pod.url + all.path, | 73 | url: pod.url + all_data.path, |
74 | method: all.method | 74 | method: all_data.method |
75 | } | 75 | } |
76 | 76 | ||
77 | // Add data with POST requst ? | 77 | // Add data with POST requst ? |
78 | if (all.method === 'POST' && all.data) { | 78 | if (all_data.method === 'POST' && all_data.data) { |
79 | logger.debug('Make a POST request.') | 79 | logger.debug('Make a POST request.') |
80 | 80 | ||
81 | // Encrypt data ? | 81 | // Encrypt data ? |
82 | if (all.encrypt === true) { | 82 | if (all_data.encrypt === true) { |
83 | logger.debug(pod.publicKey) | ||
84 | var crt = ursa.createPublicKey(pod.publicKey) | 83 | var crt = ursa.createPublicKey(pod.publicKey) |
85 | 84 | ||
86 | // TODO: ES6 with let | 85 | // TODO: ES6 with let |
87 | ;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) { | 86 | ;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) { |
88 | utils.symetricEncrypt(JSON.stringify(all.data), function (err, dataEncrypted) { | 87 | utils.symetricEncrypt(JSON.stringify(all_data.data), function (err, dataEncrypted) { |
89 | if (err) throw err | 88 | if (err) throw err |
90 | 89 | ||
91 | var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex') | 90 | var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex') |
@@ -98,7 +97,7 @@ | |||
98 | }) | 97 | }) |
99 | })(crt, params, url, pod, signature) | 98 | })(crt, params, url, pod, signature) |
100 | } else { | 99 | } else { |
101 | params.json = { data: all.data } | 100 | params.json = { data: all_data.data } |
102 | makeRetryRequest(params, url, pod, signature, callbackEach) | 101 | makeRetryRequest(params, url, pod, signature, callbackEach) |
103 | } | 102 | } |
104 | } else { | 103 | } else { |
@@ -124,20 +123,22 @@ | |||
124 | return callback(new Error(string)) | 123 | return callback(new Error(string)) |
125 | } | 124 | } |
126 | 125 | ||
127 | logger.debug('Gen RSA keys...') | 126 | logger.info('Generating a RSA key...') |
128 | openssl.exec('genrsa', { 'out': utils.certDir + 'peertube.key.pem', '2048': false }, function (err) { | 127 | openssl.exec('genrsa', { 'out': utils.certDir + 'peertube.key.pem', '2048': false }, function (err) { |
129 | if (err) { | 128 | if (err) { |
130 | logger.error('Cannot create private key on this pod.', { error: err }) | 129 | logger.error('Cannot create private key on this pod.', { error: err }) |
131 | return callback(err) | 130 | return callback(err) |
132 | } | 131 | } |
132 | logger.info('RSA key generated.') | ||
133 | 133 | ||
134 | logger.debug('Manage public key...') | 134 | logger.info('Manage public key...') |
135 | openssl.exec('rsa', { 'in': utils.certDir + 'peertube.key.pem', 'pubout': true, 'out': utils.certDir + 'peertube.pub' }, function (err) { | 135 | openssl.exec('rsa', { 'in': utils.certDir + 'peertube.key.pem', 'pubout': true, 'out': utils.certDir + 'peertube.pub' }, function (err) { |
136 | if (err) { | 136 | if (err) { |
137 | logger.error('Cannot create public key on this pod .', { error: err }) | 137 | logger.error('Cannot create public key on this pod .', { error: err }) |
138 | return callback(err) | 138 | return callback(err) |
139 | } | 139 | } |
140 | 140 | ||
141 | logger.info('Public key managed.') | ||
141 | return callback(null) | 142 | return callback(null) |
142 | }) | 143 | }) |
143 | }) | 144 | }) |