From e861452fb26553177ad4e32bfb18b4fd8a5b1816 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Aug 2016 21:34:51 +0200 Subject: Server: put config in constants --- server/helpers/logger.js | 10 +++++----- server/helpers/peertube-crypto.js | 29 ++++++++++++++++------------- server/helpers/requests.js | 11 ++--------- 3 files changed, 23 insertions(+), 27 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/logger.js b/server/helpers/logger.js index 8ae90a4b2..590ceaeb6 100644 --- a/server/helpers/logger.js +++ b/server/helpers/logger.js @@ -1,23 +1,23 @@ // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ 'use strict' -const config = require('config') const mkdirp = require('mkdirp') const path = require('path') const winston = require('winston') winston.emitErrs = true -const logDir = path.join(__dirname, '..', '..', config.get('storage.logs')) -const label = config.get('webserver.host') + ':' + config.get('webserver.port') +const constants = require('../initializers/constants') + +const label = constants.CONFIG.WEBSERVER.HOST + ':' + constants.CONFIG.WEBSERVER.PORT // Create the directory if it does not exist -mkdirp.sync(logDir) +mkdirp.sync(constants.CONFIG.STORAGE.LOG_DIR) const logger = new winston.Logger({ transports: [ new winston.transports.File({ level: 'debug', - filename: path.join(logDir, 'all-logs.log'), + filename: path.join(constants.CONFIG.STORAGE.LOG_DIR, 'all-logs.log'), handleExceptions: true, json: true, maxsize: 5242880, diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js index 46dff8d03..ef130ea5c 100644 --- a/server/helpers/peertube-crypto.js +++ b/server/helpers/peertube-crypto.js @@ -1,15 +1,13 @@ 'use strict' -const config = require('config') const crypto = require('crypto') const fs = require('fs') const openssl = require('openssl-wrapper') -const path = require('path') const ursa = require('ursa') +const constants = require('../initializers/constants') const logger = require('./logger') -const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) const algorithm = 'aes-256-ctr' const peertubeCrypto = { @@ -17,7 +15,6 @@ const peertubeCrypto = { createCertsIfNotExist: createCertsIfNotExist, decrypt: decrypt, encrypt: encrypt, - getCertDir: getCertDir, sign: sign } @@ -40,7 +37,7 @@ function createCertsIfNotExist (callback) { } function decrypt (key, data, callback) { - fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { + fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) { if (err) return callback(err) const myPrivateKey = ursa.createPrivateKey(file) @@ -67,12 +64,8 @@ function encrypt (publicKey, data, callback) { }) } -function getCertDir () { - return certDir -} - function sign (data) { - const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) + const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem')) const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') return signature @@ -85,7 +78,7 @@ module.exports = peertubeCrypto // --------------------------------------------------------------------------- function certsExist (callback) { - fs.exists(certDir + 'peertube.key.pem', function (exists) { + fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { return callback(exists) }) } @@ -99,15 +92,25 @@ function createCerts (callback) { } logger.info('Generating a RSA key...') - openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { + + let options = { + 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + '2048': false + } + openssl.exec('genrsa', options, function (err) { if (err) { logger.error('Cannot create private key on this pod.') return callback(err) } logger.info('RSA key generated.') + options = { + 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', + 'pubout': true, + 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' + } logger.info('Manage public key...') - openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { + openssl.exec('rsa', options, function (err) { if (err) { logger.error('Cannot create public key on this pod.') return callback(err) diff --git a/server/helpers/requests.js b/server/helpers/requests.js index 547230adc..f76ff3473 100644 --- a/server/helpers/requests.js +++ b/server/helpers/requests.js @@ -1,16 +1,11 @@ 'use strict' -const config = require('config') const replay = require('request-replay') const request = require('request') const constants = require('../initializers/constants') const peertubeCrypto = require('./peertube-crypto') -const http = config.get('webserver.https') ? 'https' : 'http' -const host = config.get('webserver.host') -const port = config.get('webserver.port') - const requests = { makeRetryRequest: makeRetryRequest, makeSecureRequest: makeSecureRequest @@ -29,8 +24,6 @@ function makeRetryRequest (params, callback) { } function makeSecureRequest (params, callback) { - const myUrl = http + '://' + host + ':' + port - const requestParams = { url: params.toPod.url + params.path } @@ -42,8 +35,8 @@ function makeSecureRequest (params, callback) { // Add signature if it is specified in the params if (params.sign === true) { requestParams.json.signature = { - url: myUrl, - signature: peertubeCrypto.sign(myUrl) + url: constants.CONFIG.WEBSERVER.URL, + signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL) } } -- cgit v1.2.3