From b9a3e09ad5a7673f64556d1dba122ed4c4fac980 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 7 Mar 2016 11:33:59 +0100 Subject: Prepare folders structure for angular app --- helpers/customValidators.js | 32 ---------- helpers/logger.js | 40 ------------ helpers/peertubeCrypto.js | 147 -------------------------------------------- helpers/requests.js | 109 -------------------------------- helpers/utils.js | 16 ----- 5 files changed, 344 deletions(-) delete mode 100644 helpers/customValidators.js delete mode 100644 helpers/logger.js delete mode 100644 helpers/peertubeCrypto.js delete mode 100644 helpers/requests.js delete mode 100644 helpers/utils.js (limited to 'helpers') diff --git a/helpers/customValidators.js b/helpers/customValidators.js deleted file mode 100644 index 20c41f5da..000000000 --- a/helpers/customValidators.js +++ /dev/null @@ -1,32 +0,0 @@ -'use strict' - -var validator = require('validator') - -var customValidators = { - eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid, - eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid, - isArray: isArray -} - -function eachIsRemoteVideosAddValid (values) { - return values.every(function (val) { - return validator.isLength(val.name, 1, 50) && - validator.isLength(val.description, 1, 50) && - validator.isLength(val.magnetUri, 10) && - validator.isURL(val.podUrl) - }) -} - -function eachIsRemoteVideosRemoveValid (values) { - return values.every(function (val) { - return validator.isLength(val.magnetUri, 10) - }) -} - -function isArray (value) { - return Array.isArray(value) -} - -// --------------------------------------------------------------------------- - -module.exports = customValidators diff --git a/helpers/logger.js b/helpers/logger.js deleted file mode 100644 index 67f69a875..000000000 --- a/helpers/logger.js +++ /dev/null @@ -1,40 +0,0 @@ -// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ -'use strict' - -var config = require('config') -var path = require('path') -var winston = require('winston') -winston.emitErrs = true - -var logDir = path.join(__dirname, '..', config.get('storage.logs')) -var logger = new winston.Logger({ - transports: [ - new winston.transports.File({ - level: 'debug', - filename: path.join(logDir, 'all-logs.log'), - handleExceptions: true, - json: true, - maxsize: 5242880, - maxFiles: 5, - colorize: false - }), - new winston.transports.Console({ - level: 'debug', - handleExceptions: true, - humanReadableUnhandledException: true, - json: false, - colorize: true - }) - ], - exitOnError: true -}) - -logger.stream = { - write: function (message, encoding) { - logger.info(message) - } -} - -// --------------------------------------------------------------------------- - -module.exports = logger diff --git a/helpers/peertubeCrypto.js b/helpers/peertubeCrypto.js deleted file mode 100644 index 29b9d79c9..000000000 --- a/helpers/peertubeCrypto.js +++ /dev/null @@ -1,147 +0,0 @@ -'use strict' - -var config = require('config') -var crypto = require('crypto') -var fs = require('fs') -var openssl = require('openssl-wrapper') -var path = require('path') -var ursa = require('ursa') - -var logger = require('./logger') - -var certDir = path.join(__dirname, '..', config.get('storage.certs')) -var algorithm = 'aes-256-ctr' - -var peertubeCrypto = { - checkSignature: checkSignature, - createCertsIfNotExist: createCertsIfNotExist, - decrypt: decrypt, - encrypt: encrypt, - getCertDir: getCertDir, - sign: sign -} - -function checkSignature (public_key, raw_data, hex_signature) { - var crt = ursa.createPublicKey(public_key) - var is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex') - return is_valid -} - -function createCertsIfNotExist (callback) { - certsExist(function (exist) { - if (exist === true) { - return callback(null) - } - - createCerts(function (err) { - return callback(err) - }) - }) -} - -function decrypt (key, data, callback) { - fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { - if (err) return callback(err) - - var my_private_key = ursa.createPrivateKey(file) - var decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8') - var decrypted_data = symetricDecrypt(data, decrypted_key) - - return callback(null, decrypted_data) - }) -} - -function encrypt (public_key, data, callback) { - var crt = ursa.createPublicKey(public_key) - - symetricEncrypt(data, function (err, dataEncrypted) { - if (err) return callback(err) - - var key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex') - var encrypted = { - data: dataEncrypted.crypted, - key: key - } - - callback(null, encrypted) - }) -} - -function getCertDir () { - return certDir -} - -function sign (data) { - var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) - var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') - - return signature -} - -// --------------------------------------------------------------------------- - -module.exports = peertubeCrypto - -// --------------------------------------------------------------------------- - -function certsExist (callback) { - fs.exists(certDir + 'peertube.key.pem', function (exists) { - return callback(exists) - }) -} - -function createCerts (callback) { - certsExist(function (exist) { - if (exist === true) { - var string = 'Certs already exist.' - logger.warning(string) - return callback(new Error(string)) - } - - logger.info('Generating a RSA key...') - openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { - if (err) { - logger.error('Cannot create private key on this pod.') - return callback(err) - } - logger.info('RSA key generated.') - - logger.info('Manage public key...') - openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { - if (err) { - logger.error('Cannot create public key on this pod.') - return callback(err) - } - - logger.info('Public key managed.') - return callback(null) - }) - }) - }) -} - -function generatePassword (callback) { - crypto.randomBytes(32, function (err, buf) { - if (err) return callback(err) - - callback(null, buf.toString('utf8')) - }) -} - -function symetricDecrypt (text, password) { - var decipher = crypto.createDecipher(algorithm, password) - var dec = decipher.update(text, 'hex', 'utf8') - dec += decipher.final('utf8') - return dec -} - -function symetricEncrypt (text, callback) { - 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 }) - }) -} diff --git a/helpers/requests.js b/helpers/requests.js deleted file mode 100644 index e19afa5ca..000000000 --- a/helpers/requests.js +++ /dev/null @@ -1,109 +0,0 @@ -'use strict' - -var async = require('async') -var config = require('config') -var request = require('request') -var replay = require('request-replay') - -var constants = require('../initializers/constants') -var logger = require('./logger') -var peertubeCrypto = require('./peertubeCrypto') - -var http = config.get('webserver.https') ? 'https' : 'http' -var host = config.get('webserver.host') -var port = config.get('webserver.port') - -var requests = { - makeMultipleRetryRequest: makeMultipleRetryRequest -} - -function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) { - if (!callback) { - callback = callbackEach - callbackEach = null - } - - var url = http + '://' + host + ':' + port - var signature - - // Add signature if it is specified in the params - if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { - signature = peertubeCrypto.sign(url) - } - - // 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() - } - } - - var params = { - url: pod.url + all_data.path, - method: all_data.method - } - - // Add data with POST requst ? - if (all_data.method === 'POST' && all_data.data) { - // Encrypt data ? - if (all_data.encrypt === true) { - // TODO: ES6 with let - ;(function (copy_params, copy_url, copy_pod, copy_signature) { - peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) { - if (err) return callback(err) - - copy_params.json = { - data: encrypted.data, - key: encrypted.key - } - - makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest) - }) - })(params, url, pod, signature) - } else { - params.json = { data: all_data.data } - makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) - } - } else { - makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) - } - }, callback) -} - -// --------------------------------------------------------------------------- - -module.exports = requests - -// --------------------------------------------------------------------------- - -function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { - // Append the signature - if (signature) { - params.json.signature = { - url: from_url, - signature: signature - } - } - - logger.debug('Make retry requests to %s.', to_pod.url) - - replay( - request.post(params, function (err, response, body) { - callbackEach(err, response, body, params.url, to_pod) - }), - { - retries: constants.REQUEST_RETRIES, - 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) - }) -} diff --git a/helpers/utils.js b/helpers/utils.js deleted file mode 100644 index d2c9ad8b2..000000000 --- a/helpers/utils.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict' - -var logger = require('./logger') - -var utils = { - cleanForExit: cleanForExit -} - -function cleanForExit (webtorrent_process) { - logger.info('Gracefully exiting.') - process.kill(-webtorrent_process.pid) -} - -// --------------------------------------------------------------------------- - -module.exports = utils -- cgit v1.2.3