1
0
Fork 0
peertube/src/utils.js

203 lines
6.1 KiB
JavaScript
Raw Normal View History

2015-06-09 11:41:40 -04:00
;(function () {
'use strict'
var async = require('async')
2015-06-09 11:41:40 -04:00
var config = require('config')
2015-11-16 07:54:00 -05:00
var crypto = require('crypto')
2015-06-09 11:41:40 -04:00
var fs = require('fs')
var openssl = require('openssl-wrapper')
2015-11-16 07:54:00 -05:00
var request = require('request')
var replay = require('request-replay')
var ursa = require('ursa')
2015-06-09 11:41:40 -04:00
var constants = require('./constants')
2015-06-09 11:41:40 -04:00
var logger = require('./logger')
2015-11-16 07:54:00 -05:00
var utils = {}
2015-06-09 11:41:40 -04:00
var http = config.get('webserver.https') ? 'https' : 'http'
var host = config.get('webserver.host')
var port = config.get('webserver.port')
var algorithm = 'aes-256-ctr'
// ----------- Private functions ----------
function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
// Append the signature
if (signature) {
params.json.signature = {
url: from_url,
signature: signature
}
}
2015-12-06 15:36:57 -05:00
logger.debug('Make retry requests to %s.', to_pod.url)
2015-06-09 11:41:40 -04:00
replay(
request.post(params, function (err, response, body) {
callbackEach(err, response, body, params.url, to_pod)
2015-06-09 11:41:40 -04:00
}),
{
retries: constants.REQUEST_RETRIES,
2015-06-09 11:41:40 -04:00
factor: 3,
maxTimeout: Infinity,
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
}
).on('replay', function (replay) {
logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
})
}
// ----------- Public attributes ----------
utils.certDir = __dirname + '/../' + config.get('storage.certs')
// { path, data }
2015-11-16 07:54:00 -05:00
utils.makeMultipleRetryRequest = function (all_data, pods, callbackEach, callback) {
2015-06-09 11:41:40 -04:00
if (!callback) {
callback = callbackEach
callbackEach = null
2015-06-09 11:41:40 -04:00
}
var url = http + '://' + host + ':' + port
var signature
2015-11-16 07:54:00 -05:00
// Add signature if it is specified in the params
if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem'))
2015-06-09 11:41:40 -04:00
signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex')
}
// Make a request for each pod
async.each(pods, function (pod, callback_each_async) {
function callbackEachRetryRequest (err, response, body, url, pod) {
if (callbackEach !== null) {
callbackEach(err, response, body, url, pod, function () {
callback_each_async()
})
} else {
callback_each_async()
}
}
2015-06-09 11:41:40 -04:00
var params = {
2015-11-16 07:54:00 -05:00
url: pod.url + all_data.path,
method: all_data.method
2015-06-09 11:41:40 -04:00
}
// Add data with POST requst ?
2015-11-16 07:54:00 -05:00
if (all_data.method === 'POST' && all_data.data) {
2015-06-09 11:41:40 -04:00
// Encrypt data ?
2015-11-16 07:54:00 -05:00
if (all_data.encrypt === true) {
2015-06-09 11:41:40 -04:00
var crt = ursa.createPublicKey(pod.publicKey)
// TODO: ES6 with let
;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) {
2015-11-16 07:54:00 -05:00
utils.symetricEncrypt(JSON.stringify(all_data.data), function (err, dataEncrypted) {
2015-06-09 11:41:40 -04:00
if (err) throw err
var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex')
copy_params.json = {
data: dataEncrypted.crypted,
key: passwordEncrypted
}
makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
2015-06-09 11:41:40 -04:00
})
})(crt, params, url, pod, signature)
} else {
2015-11-16 07:54:00 -05:00
params.json = { data: all_data.data }
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
2015-06-09 11:41:40 -04:00
}
} else {
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
2015-06-09 11:41:40 -04:00
}
}, callback)
2015-06-09 11:41:40 -04:00
}
utils.certsExist = function (callback) {
fs.exists(utils.certDir + 'peertube.key.pem', function (exists) {
2015-06-09 11:41:40 -04:00
return callback(exists)
})
}
utils.createCerts = function (callback) {
utils.certsExist(function (exist) {
if (exist === true) {
var string = 'Certs already exist.'
logger.warning(string)
return callback(new Error(string))
}
2015-11-16 07:54:00 -05:00
logger.info('Generating a RSA key...')
Add travis support Squashed commit of the following: commit f9a7a1fd51dd2c857fc52923be8f2e98f059f923 Author: Chocobozzz <florian.bigard@gmail.com> Date: Fri Oct 30 20:05:20 2015 +0100 This is electron-prebuilt and not just electron commit a2361482942abe4443c68e61fdcfea9bf35ede61 Author: Chocobozzz <florian.bigard@gmail.com> Date: Fri Oct 30 19:58:56 2015 +0100 Let's try to change this option... commit 7ee8bfa9bf34b0165a7ef51bc9397bf86ff54878 Author: Chocobozzz <florian.bigard@gmail.com> Date: Fri Oct 30 19:39:23 2015 +0100 Explicitly say to generate a rsa key of length 2048 commit d3905aef92b537c395a9111d5d0c6f4da8b322ba Author: Chocobozzz <florian.bigard@gmail.com> Date: Fri Oct 30 19:22:46 2015 +0100 typo commit 602cb774848da906ec693999484b618618f70846 Author: Chocobozzz <florian.bigard@gmail.com> Date: Fri Oct 30 19:21:26 2015 +0100 Add after_failure, cache for grunt and electron commit 79f7c7f7b7a9a0726de0b7b1af1cda69a4d401cc Author: Chocobozzz <florian.bigard@gmail.com> Date: Fri Oct 30 19:10:25 2015 +0100 increase timewait when uploading a video (for the federation) commit 3c8dd590dd53ab2b069d6a52cd8e954156814304 Author: Chocobozzz <florian.bigard@gmail.com> Date: Fri Oct 30 19:00:26 2015 +0100 install grunt-cli commit 1b5d02fb067a7b4cde62a6dd5073b76e5f1ba269 Author: Chocobozzz <florian.bigard@gmail.com> Date: Fri Oct 30 18:55:23 2015 +0100 Trigger travis build commit ede965e1186c4dc25270c384f669f0d6749d2a9d Author: Chocobozzz <florian.bigard@gmail.com> Date: Fri Oct 30 18:52:08 2015 +0100 let's try travis
2015-10-30 15:08:46 -04:00
openssl.exec('genrsa', { 'out': utils.certDir + 'peertube.key.pem', '2048': false }, function (err) {
2015-06-09 11:41:40 -04:00
if (err) {
logger.error('Cannot create private key on this pod.', { error: err })
return callback(err)
}
2015-11-16 07:54:00 -05:00
logger.info('RSA key generated.')
2015-06-09 11:41:40 -04:00
2015-11-16 07:54:00 -05:00
logger.info('Manage public key...')
openssl.exec('rsa', { 'in': utils.certDir + 'peertube.key.pem', 'pubout': true, 'out': utils.certDir + 'peertube.pub' }, function (err) {
2015-06-09 11:41:40 -04:00
if (err) {
logger.error('Cannot create public key on this pod .', { error: err })
return callback(err)
}
2015-11-16 07:54:00 -05:00
logger.info('Public key managed.')
2015-06-09 11:41:40 -04:00
return callback(null)
})
})
})
}
utils.createCertsIfNotExist = function (callback) {
utils.certsExist(function (exist) {
if (exist === true) {
return callback(null)
}
utils.createCerts(function (err) {
return callback(err)
})
})
}
utils.generatePassword = function (callback) {
crypto.randomBytes(32, function (err, buf) {
if (err) {
return callback(err)
}
callback(null, buf.toString('utf8'))
})
}
utils.symetricEncrypt = function (text, callback) {
utils.generatePassword(function (err, password) {
if (err) {
return callback(err)
}
var cipher = crypto.createCipher(algorithm, password)
var crypted = cipher.update(text, 'utf8', 'hex')
crypted += cipher.final('hex')
callback(null, { crypted: crypted, password: password })
})
}
utils.symetricDecrypt = function (text, password) {
var decipher = crypto.createDecipher(algorithm, password)
var dec = decipher.update(text, 'hex', 'utf8')
dec += decipher.final('utf8')
return dec
}
utils.cleanForExit = function (webtorrent_process) {
logger.info('Gracefully exiting')
process.kill(-webtorrent_process.pid)
}
2015-06-09 11:41:40 -04:00
module.exports = utils
})()