diff options
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/customValidators.js | 15 | ||||
-rw-r--r-- | helpers/logger.js | 11 | ||||
-rw-r--r-- | helpers/utils.js | 111 |
3 files changed, 78 insertions, 59 deletions
diff --git a/helpers/customValidators.js b/helpers/customValidators.js index 73c2f8461..c433e5c5d 100644 --- a/helpers/customValidators.js +++ b/helpers/customValidators.js | |||
@@ -3,9 +3,13 @@ | |||
3 | 3 | ||
4 | var validator = require('validator') | 4 | var validator = require('validator') |
5 | 5 | ||
6 | var customValidators = {} | 6 | var customValidators = { |
7 | eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid, | ||
8 | eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid, | ||
9 | isArray: isArray | ||
10 | } | ||
7 | 11 | ||
8 | customValidators.eachIsRemoteVideosAddValid = function (values) { | 12 | function eachIsRemoteVideosAddValid (values) { |
9 | return values.every(function (val) { | 13 | return values.every(function (val) { |
10 | return validator.isLength(val.name, 1, 50) && | 14 | return validator.isLength(val.name, 1, 50) && |
11 | validator.isLength(val.description, 1, 50) && | 15 | validator.isLength(val.description, 1, 50) && |
@@ -14,16 +18,17 @@ | |||
14 | }) | 18 | }) |
15 | } | 19 | } |
16 | 20 | ||
17 | customValidators.eachIsRemoteVideosRemoveValid = function (values) { | 21 | function eachIsRemoteVideosRemoveValid (values) { |
18 | return values.every(function (val) { | 22 | return values.every(function (val) { |
19 | return validator.isLength(val.magnetUri, 10) | 23 | return validator.isLength(val.magnetUri, 10) |
20 | }) | 24 | }) |
21 | } | 25 | } |
22 | 26 | ||
23 | customValidators.isArray = function (value) { | 27 | function isArray (value) { |
24 | return Array.isArray(value) | 28 | return Array.isArray(value) |
25 | } | 29 | } |
26 | 30 | ||
27 | // ----------- Export ----------- | 31 | // --------------------------------------------------------------------------- |
32 | |||
28 | module.exports = customValidators | 33 | module.exports = customValidators |
29 | })() | 34 | })() |
diff --git a/helpers/logger.js b/helpers/logger.js index 850af10cb..fa5b53708 100644 --- a/helpers/logger.js +++ b/helpers/logger.js | |||
@@ -4,11 +4,9 @@ | |||
4 | 4 | ||
5 | var config = require('config') | 5 | var config = require('config') |
6 | var winston = require('winston') | 6 | var winston = require('winston') |
7 | |||
8 | var logDir = __dirname + '/../' + config.get('storage.logs') | ||
9 | |||
10 | winston.emitErrs = true | 7 | winston.emitErrs = true |
11 | 8 | ||
9 | var logDir = __dirname + '/../' + config.get('storage.logs') | ||
12 | var logger = new winston.Logger({ | 10 | var logger = new winston.Logger({ |
13 | transports: [ | 11 | transports: [ |
14 | new winston.transports.File({ | 12 | new winston.transports.File({ |
@@ -31,10 +29,13 @@ | |||
31 | exitOnError: true | 29 | exitOnError: true |
32 | }) | 30 | }) |
33 | 31 | ||
34 | module.exports = logger | 32 | logger.stream = { |
35 | module.exports.stream = { | ||
36 | write: function (message, encoding) { | 33 | write: function (message, encoding) { |
37 | logger.info(message) | 34 | logger.info(message) |
38 | } | 35 | } |
39 | } | 36 | } |
37 | |||
38 | // --------------------------------------------------------------------------- | ||
39 | |||
40 | module.exports = logger | ||
40 | })() | 41 | })() |
diff --git a/helpers/utils.js b/helpers/utils.js index 7cdb2600d..ec46631b1 100644 --- a/helpers/utils.js +++ b/helpers/utils.js | |||
@@ -13,47 +13,29 @@ | |||
13 | var constants = require('../initializers/constants') | 13 | var constants = require('../initializers/constants') |
14 | var logger = require('./logger') | 14 | var logger = require('./logger') |
15 | 15 | ||
16 | var utils = {} | 16 | var certDir = __dirname + '/../' + config.get('storage.certs') |
17 | |||
18 | var http = config.get('webserver.https') ? 'https' : 'http' | 17 | var http = config.get('webserver.https') ? 'https' : 'http' |
19 | var host = config.get('webserver.host') | 18 | var host = config.get('webserver.host') |
20 | var port = config.get('webserver.port') | 19 | var port = config.get('webserver.port') |
21 | var algorithm = 'aes-256-ctr' | 20 | var algorithm = 'aes-256-ctr' |
22 | 21 | ||
23 | // ----------- Private functions ---------- | 22 | var utils = { |
24 | 23 | getCertDir: getCertDir, | |
25 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { | 24 | certsExist: certsExist, |
26 | // Append the signature | 25 | cleanForExit: cleanForExit, |
27 | if (signature) { | 26 | createCerts: createCerts, |
28 | params.json.signature = { | 27 | createCertsIfNotExist: createCertsIfNotExist, |
29 | url: from_url, | 28 | generatePassword: generatePassword, |
30 | signature: signature | 29 | makeMultipleRetryRequest: makeMultipleRetryRequest, |
31 | } | 30 | symetricEncrypt: symetricEncrypt, |
32 | } | 31 | symetricDecrypt: symetricDecrypt |
33 | |||
34 | logger.debug('Make retry requests to %s.', to_pod.url) | ||
35 | |||
36 | replay( | ||
37 | request.post(params, function (err, response, body) { | ||
38 | callbackEach(err, response, body, params.url, to_pod) | ||
39 | }), | ||
40 | { | ||
41 | retries: constants.REQUEST_RETRIES, | ||
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 | } | 32 | } |
51 | 33 | ||
52 | // ----------- Public attributes ---------- | 34 | function getCertDir () { |
53 | utils.certDir = __dirname + '/../' + config.get('storage.certs') | 35 | return certDir |
36 | } | ||
54 | 37 | ||
55 | // { path, data } | 38 | function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) { |
56 | utils.makeMultipleRetryRequest = function (all_data, pods, callbackEach, callback) { | ||
57 | if (!callback) { | 39 | if (!callback) { |
58 | callback = callbackEach | 40 | callback = callbackEach |
59 | callbackEach = null | 41 | callbackEach = null |
@@ -64,7 +46,7 @@ | |||
64 | 46 | ||
65 | // Add signature if it is specified in the params | 47 | // Add signature if it is specified in the params |
66 | if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { | 48 | if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { |
67 | var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem')) | 49 | var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) |
68 | signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex') | 50 | signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex') |
69 | } | 51 | } |
70 | 52 | ||
@@ -93,7 +75,7 @@ | |||
93 | 75 | ||
94 | // TODO: ES6 with let | 76 | // TODO: ES6 with let |
95 | ;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) { | 77 | ;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) { |
96 | utils.symetricEncrypt(JSON.stringify(all_data.data), function (err, dataEncrypted) { | 78 | symetricEncrypt(JSON.stringify(all_data.data), function (err, dataEncrypted) { |
97 | if (err) throw err | 79 | if (err) throw err |
98 | 80 | ||
99 | var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex') | 81 | var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex') |
@@ -115,14 +97,14 @@ | |||
115 | }, callback) | 97 | }, callback) |
116 | } | 98 | } |
117 | 99 | ||
118 | utils.certsExist = function (callback) { | 100 | function certsExist (callback) { |
119 | fs.exists(utils.certDir + 'peertube.key.pem', function (exists) { | 101 | fs.exists(certDir + 'peertube.key.pem', function (exists) { |
120 | return callback(exists) | 102 | return callback(exists) |
121 | }) | 103 | }) |
122 | } | 104 | } |
123 | 105 | ||
124 | utils.createCerts = function (callback) { | 106 | function createCerts (callback) { |
125 | utils.certsExist(function (exist) { | 107 | certsExist(function (exist) { |
126 | if (exist === true) { | 108 | if (exist === true) { |
127 | var string = 'Certs already exist.' | 109 | var string = 'Certs already exist.' |
128 | logger.warning(string) | 110 | logger.warning(string) |
@@ -130,7 +112,7 @@ | |||
130 | } | 112 | } |
131 | 113 | ||
132 | logger.info('Generating a RSA key...') | 114 | logger.info('Generating a RSA key...') |
133 | openssl.exec('genrsa', { 'out': utils.certDir + 'peertube.key.pem', '2048': false }, function (err) { | 115 | openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { |
134 | if (err) { | 116 | if (err) { |
135 | logger.error('Cannot create private key on this pod.', { error: err }) | 117 | logger.error('Cannot create private key on this pod.', { error: err }) |
136 | return callback(err) | 118 | return callback(err) |
@@ -138,7 +120,7 @@ | |||
138 | logger.info('RSA key generated.') | 120 | logger.info('RSA key generated.') |
139 | 121 | ||
140 | logger.info('Manage public key...') | 122 | logger.info('Manage public key...') |
141 | openssl.exec('rsa', { 'in': utils.certDir + 'peertube.key.pem', 'pubout': true, 'out': utils.certDir + 'peertube.pub' }, function (err) { | 123 | openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { |
142 | if (err) { | 124 | if (err) { |
143 | logger.error('Cannot create public key on this pod .', { error: err }) | 125 | logger.error('Cannot create public key on this pod .', { error: err }) |
144 | return callback(err) | 126 | return callback(err) |
@@ -151,19 +133,19 @@ | |||
151 | }) | 133 | }) |
152 | } | 134 | } |
153 | 135 | ||
154 | utils.createCertsIfNotExist = function (callback) { | 136 | function createCertsIfNotExist (callback) { |
155 | utils.certsExist(function (exist) { | 137 | certsExist(function (exist) { |
156 | if (exist === true) { | 138 | if (exist === true) { |
157 | return callback(null) | 139 | return callback(null) |
158 | } | 140 | } |
159 | 141 | ||
160 | utils.createCerts(function (err) { | 142 | createCerts(function (err) { |
161 | return callback(err) | 143 | return callback(err) |
162 | }) | 144 | }) |
163 | }) | 145 | }) |
164 | } | 146 | } |
165 | 147 | ||
166 | utils.generatePassword = function (callback) { | 148 | function generatePassword (callback) { |
167 | crypto.randomBytes(32, function (err, buf) { | 149 | crypto.randomBytes(32, function (err, buf) { |
168 | if (err) { | 150 | if (err) { |
169 | return callback(err) | 151 | return callback(err) |
@@ -173,8 +155,8 @@ | |||
173 | }) | 155 | }) |
174 | } | 156 | } |
175 | 157 | ||
176 | utils.symetricEncrypt = function (text, callback) { | 158 | function symetricEncrypt (text, callback) { |
177 | utils.generatePassword(function (err, password) { | 159 | generatePassword(function (err, password) { |
178 | if (err) { | 160 | if (err) { |
179 | return callback(err) | 161 | return callback(err) |
180 | } | 162 | } |
@@ -186,17 +168,48 @@ | |||
186 | }) | 168 | }) |
187 | } | 169 | } |
188 | 170 | ||
189 | utils.symetricDecrypt = function (text, password) { | 171 | function symetricDecrypt (text, password) { |
190 | var decipher = crypto.createDecipher(algorithm, password) | 172 | var decipher = crypto.createDecipher(algorithm, password) |
191 | var dec = decipher.update(text, 'hex', 'utf8') | 173 | var dec = decipher.update(text, 'hex', 'utf8') |
192 | dec += decipher.final('utf8') | 174 | dec += decipher.final('utf8') |
193 | return dec | 175 | return dec |
194 | } | 176 | } |
195 | 177 | ||
196 | utils.cleanForExit = function (webtorrent_process) { | 178 | function cleanForExit (webtorrent_process) { |
197 | logger.info('Gracefully exiting') | 179 | logger.info('Gracefully exiting') |
198 | process.kill(-webtorrent_process.pid) | 180 | process.kill(-webtorrent_process.pid) |
199 | } | 181 | } |
200 | 182 | ||
183 | // --------------------------------------------------------------------------- | ||
184 | |||
201 | module.exports = utils | 185 | module.exports = utils |
186 | |||
187 | // --------------------------------------------------------------------------- | ||
188 | |||
189 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { | ||
190 | // Append the signature | ||
191 | if (signature) { | ||
192 | params.json.signature = { | ||
193 | url: from_url, | ||
194 | signature: signature | ||
195 | } | ||
196 | } | ||
197 | |||
198 | logger.debug('Make retry requests to %s.', to_pod.url) | ||
199 | |||
200 | replay( | ||
201 | request.post(params, function (err, response, body) { | ||
202 | callbackEach(err, response, body, params.url, to_pod) | ||
203 | }), | ||
204 | { | ||
205 | retries: constants.REQUEST_RETRIES, | ||
206 | factor: 3, | ||
207 | maxTimeout: Infinity, | ||
208 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | ||
209 | } | ||
210 | ).on('replay', function (replay) { | ||
211 | logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.', | ||
212 | params.url, replay.error.code, replay.error.message, replay.number, replay.delay) | ||
213 | }) | ||
214 | } | ||
202 | })() | 215 | })() |