]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - helpers/utils.js
4 var async
= require('async')
5 var config
= require('config')
6 var crypto
= require('crypto')
8 var openssl
= require('openssl-wrapper')
9 var request
= require('request')
10 var replay
= require('request-replay')
11 var ursa
= require('ursa')
13 var constants
= require('../initializers/constants')
14 var logger
= require('./logger')
16 var certDir
= __dirname
+ '/../' + config
.get('storage.certs')
17 var http
= config
.get('webserver.https') ? 'https' : 'http'
18 var host
= config
.get('webserver.host')
19 var port
= config
.get('webserver.port')
20 var algorithm
= 'aes-256-ctr'
23 getCertDir: getCertDir
,
24 certsExist: certsExist
,
25 cleanForExit: cleanForExit
,
26 createCerts: createCerts
,
27 createCertsIfNotExist: createCertsIfNotExist
,
28 generatePassword: generatePassword
,
29 makeMultipleRetryRequest: makeMultipleRetryRequest
,
30 symetricEncrypt: symetricEncrypt
,
31 symetricDecrypt: symetricDecrypt
34 function getCertDir () {
38 function makeMultipleRetryRequest (all_data
, pods
, callbackEach
, callback
) {
40 callback
= callbackEach
44 var url
= http
+ '://' + host
+ ':' + port
47 // Add signature if it is specified in the params
48 if (all_data
.method
=== 'POST' && all_data
.data
&& all_data
.sign
=== true) {
49 var myKey
= ursa
.createPrivateKey(fs
.readFileSync(certDir
+ 'peertube.key.pem'))
50 signature
= myKey
.hashAndSign('sha256', url
, 'utf8', 'hex')
53 // Make a request for each pod
54 async
.each(pods
, function (pod
, callback_each_async
) {
55 function callbackEachRetryRequest (err
, response
, body
, url
, pod
) {
56 if (callbackEach
!== null) {
57 callbackEach(err
, response
, body
, url
, pod
, function () {
66 url: pod
.url
+ all_data
.path
,
67 method: all_data
.method
70 // Add data with POST requst ?
71 if (all_data
.method
=== 'POST' && all_data
.data
) {
73 if (all_data
.encrypt
=== true) {
74 var crt
= ursa
.createPublicKey(pod
.publicKey
)
77 ;(function (crt_copy
, copy_params
, copy_url
, copy_pod
, copy_signature
) {
78 symetricEncrypt(JSON
.stringify(all_data
.data
), function (err
, dataEncrypted
) {
81 var passwordEncrypted
= crt_copy
.encrypt(dataEncrypted
.password
, 'utf8', 'hex')
83 data: dataEncrypted
.crypted
,
84 key: passwordEncrypted
87 makeRetryRequest(copy_params
, copy_url
, copy_pod
, copy_signature
, callbackEachRetryRequest
)
89 })(crt
, params
, url
, pod
, signature
)
91 params
.json
= { data: all_data
.data
}
92 makeRetryRequest(params
, url
, pod
, signature
, callbackEachRetryRequest
)
95 makeRetryRequest(params
, url
, pod
, signature
, callbackEachRetryRequest
)
100 function certsExist (callback
) {
101 fs
.exists(certDir
+ 'peertube.key.pem', function (exists
) {
102 return callback(exists
)
106 function createCerts (callback
) {
107 certsExist(function (exist
) {
108 if (exist
=== true) {
109 var string
= 'Certs already exist.'
110 logger
.warning(string
)
111 return callback(new Error(string
))
114 logger
.info('Generating a RSA key...')
115 openssl
.exec('genrsa', { 'out': certDir
+ 'peertube.key.pem', '2048': false }, function (err
) {
117 logger
.error('Cannot create private key on this pod.', { error: err
})
120 logger
.info('RSA key generated.')
122 logger
.info('Manage public key...')
123 openssl
.exec('rsa', { 'in': certDir
+ 'peertube.key.pem', 'pubout': true, 'out': certDir
+ 'peertube.pub' }, function (err
) {
125 logger
.error('Cannot create public key on this pod .', { error: err
})
129 logger
.info('Public key managed.')
130 return callback(null)
136 function createCertsIfNotExist (callback
) {
137 certsExist(function (exist
) {
138 if (exist
=== true) {
139 return callback(null)
142 createCerts(function (err
) {
148 function generatePassword (callback
) {
149 crypto
.randomBytes(32, function (err
, buf
) {
154 callback(null, buf
.toString('utf8'))
158 function symetricEncrypt (text
, callback
) {
159 generatePassword(function (err
, password
) {
164 var cipher
= crypto
.createCipher(algorithm
, password
)
165 var crypted
= cipher
.update(text
, 'utf8', 'hex')
166 crypted
+= cipher
.final('hex')
167 callback(null, { crypted: crypted
, password: password
})
171 function symetricDecrypt (text
, password
) {
172 var decipher
= crypto
.createDecipher(algorithm
, password
)
173 var dec
= decipher
.update(text
, 'hex', 'utf8')
174 dec
+= decipher
.final('utf8')
178 function cleanForExit (webtorrent_process
) {
179 logger
.info('Gracefully exiting')
180 process
.kill(-webtorrent_process
.pid
)
183 // ---------------------------------------------------------------------------
185 module
.exports
= utils
187 // ---------------------------------------------------------------------------
189 function makeRetryRequest (params
, from_url
, to_pod
, signature
, callbackEach
) {
190 // Append the signature
192 params
.json
.signature
= {
198 logger
.debug('Make retry requests to %s.', to_pod
.url
)
201 request
.post(params
, function (err
, response
, body
) {
202 callbackEach(err
, response
, body
, params
.url
, to_pod
)
205 retries: constants
.REQUEST_RETRIES
,
207 maxTimeout: Infinity
,
208 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
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
)