aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-08-19 21:34:51 +0200
committerChocobozzz <florian.bigard@gmail.com>2016-08-19 21:34:51 +0200
commite861452fb26553177ad4e32bfb18b4fd8a5b1816 (patch)
tree7c0cfd464709243a452b431665f5107a973df682 /server/helpers
parent5c39adb7313e0696aabb4b71196ab7b0b378c359 (diff)
downloadPeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.tar.gz
PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.tar.zst
PeerTube-e861452fb26553177ad4e32bfb18b4fd8a5b1816.zip
Server: put config in constants
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/logger.js10
-rw-r--r--server/helpers/peertube-crypto.js29
-rw-r--r--server/helpers/requests.js11
3 files changed, 23 insertions, 27 deletions
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 @@
1// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ 1// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2'use strict' 2'use strict'
3 3
4const config = require('config')
5const mkdirp = require('mkdirp') 4const mkdirp = require('mkdirp')
6const path = require('path') 5const path = require('path')
7const winston = require('winston') 6const winston = require('winston')
8winston.emitErrs = true 7winston.emitErrs = true
9 8
10const logDir = path.join(__dirname, '..', '..', config.get('storage.logs')) 9const constants = require('../initializers/constants')
11const label = config.get('webserver.host') + ':' + config.get('webserver.port') 10
11const label = constants.CONFIG.WEBSERVER.HOST + ':' + constants.CONFIG.WEBSERVER.PORT
12 12
13// Create the directory if it does not exist 13// Create the directory if it does not exist
14mkdirp.sync(logDir) 14mkdirp.sync(constants.CONFIG.STORAGE.LOG_DIR)
15 15
16const logger = new winston.Logger({ 16const logger = new winston.Logger({
17 transports: [ 17 transports: [
18 new winston.transports.File({ 18 new winston.transports.File({
19 level: 'debug', 19 level: 'debug',
20 filename: path.join(logDir, 'all-logs.log'), 20 filename: path.join(constants.CONFIG.STORAGE.LOG_DIR, 'all-logs.log'),
21 handleExceptions: true, 21 handleExceptions: true,
22 json: true, 22 json: true,
23 maxsize: 5242880, 23 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 @@
1'use strict' 1'use strict'
2 2
3const config = require('config')
4const crypto = require('crypto') 3const crypto = require('crypto')
5const fs = require('fs') 4const fs = require('fs')
6const openssl = require('openssl-wrapper') 5const openssl = require('openssl-wrapper')
7const path = require('path')
8const ursa = require('ursa') 6const ursa = require('ursa')
9 7
8const constants = require('../initializers/constants')
10const logger = require('./logger') 9const logger = require('./logger')
11 10
12const certDir = path.join(__dirname, '..', '..', config.get('storage.certs'))
13const algorithm = 'aes-256-ctr' 11const algorithm = 'aes-256-ctr'
14 12
15const peertubeCrypto = { 13const peertubeCrypto = {
@@ -17,7 +15,6 @@ const peertubeCrypto = {
17 createCertsIfNotExist: createCertsIfNotExist, 15 createCertsIfNotExist: createCertsIfNotExist,
18 decrypt: decrypt, 16 decrypt: decrypt,
19 encrypt: encrypt, 17 encrypt: encrypt,
20 getCertDir: getCertDir,
21 sign: sign 18 sign: sign
22} 19}
23 20
@@ -40,7 +37,7 @@ function createCertsIfNotExist (callback) {
40} 37}
41 38
42function decrypt (key, data, callback) { 39function decrypt (key, data, callback) {
43 fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { 40 fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (err, file) {
44 if (err) return callback(err) 41 if (err) return callback(err)
45 42
46 const myPrivateKey = ursa.createPrivateKey(file) 43 const myPrivateKey = ursa.createPrivateKey(file)
@@ -67,12 +64,8 @@ function encrypt (publicKey, data, callback) {
67 }) 64 })
68} 65}
69 66
70function getCertDir () {
71 return certDir
72}
73
74function sign (data) { 67function sign (data) {
75 const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) 68 const myKey = ursa.createPrivateKey(fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem'))
76 const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') 69 const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
77 70
78 return signature 71 return signature
@@ -85,7 +78,7 @@ module.exports = peertubeCrypto
85// --------------------------------------------------------------------------- 78// ---------------------------------------------------------------------------
86 79
87function certsExist (callback) { 80function certsExist (callback) {
88 fs.exists(certDir + 'peertube.key.pem', function (exists) { 81 fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) {
89 return callback(exists) 82 return callback(exists)
90 }) 83 })
91} 84}
@@ -99,15 +92,25 @@ function createCerts (callback) {
99 } 92 }
100 93
101 logger.info('Generating a RSA key...') 94 logger.info('Generating a RSA key...')
102 openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { 95
96 let options = {
97 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem',
98 '2048': false
99 }
100 openssl.exec('genrsa', options, function (err) {
103 if (err) { 101 if (err) {
104 logger.error('Cannot create private key on this pod.') 102 logger.error('Cannot create private key on this pod.')
105 return callback(err) 103 return callback(err)
106 } 104 }
107 logger.info('RSA key generated.') 105 logger.info('RSA key generated.')
108 106
107 options = {
108 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem',
109 'pubout': true,
110 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub'
111 }
109 logger.info('Manage public key...') 112 logger.info('Manage public key...')
110 openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) { 113 openssl.exec('rsa', options, function (err) {
111 if (err) { 114 if (err) {
112 logger.error('Cannot create public key on this pod.') 115 logger.error('Cannot create public key on this pod.')
113 return callback(err) 116 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 @@
1'use strict' 1'use strict'
2 2
3const config = require('config')
4const replay = require('request-replay') 3const replay = require('request-replay')
5const request = require('request') 4const request = require('request')
6 5
7const constants = require('../initializers/constants') 6const constants = require('../initializers/constants')
8const peertubeCrypto = require('./peertube-crypto') 7const peertubeCrypto = require('./peertube-crypto')
9 8
10const http = config.get('webserver.https') ? 'https' : 'http'
11const host = config.get('webserver.host')
12const port = config.get('webserver.port')
13
14const requests = { 9const requests = {
15 makeRetryRequest: makeRetryRequest, 10 makeRetryRequest: makeRetryRequest,
16 makeSecureRequest: makeSecureRequest 11 makeSecureRequest: makeSecureRequest
@@ -29,8 +24,6 @@ function makeRetryRequest (params, callback) {
29} 24}
30 25
31function makeSecureRequest (params, callback) { 26function makeSecureRequest (params, callback) {
32 const myUrl = http + '://' + host + ':' + port
33
34 const requestParams = { 27 const requestParams = {
35 url: params.toPod.url + params.path 28 url: params.toPod.url + params.path
36 } 29 }
@@ -42,8 +35,8 @@ function makeSecureRequest (params, callback) {
42 // Add signature if it is specified in the params 35 // Add signature if it is specified in the params
43 if (params.sign === true) { 36 if (params.sign === true) {
44 requestParams.json.signature = { 37 requestParams.json.signature = {
45 url: myUrl, 38 url: constants.CONFIG.WEBSERVER.URL,
46 signature: peertubeCrypto.sign(myUrl) 39 signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL)
47 } 40 }
48 } 41 }
49 42