diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-08-19 21:34:51 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-08-19 21:34:51 +0200 |
commit | e861452fb26553177ad4e32bfb18b4fd8a5b1816 (patch) | |
tree | 7c0cfd464709243a452b431665f5107a973df682 /server/helpers | |
parent | 5c39adb7313e0696aabb4b71196ab7b0b378c359 (diff) | |
download | PeerTube-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.js | 10 | ||||
-rw-r--r-- | server/helpers/peertube-crypto.js | 29 | ||||
-rw-r--r-- | server/helpers/requests.js | 11 |
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 | ||
4 | const config = require('config') | ||
5 | const mkdirp = require('mkdirp') | 4 | const mkdirp = require('mkdirp') |
6 | const path = require('path') | 5 | const path = require('path') |
7 | const winston = require('winston') | 6 | const winston = require('winston') |
8 | winston.emitErrs = true | 7 | winston.emitErrs = true |
9 | 8 | ||
10 | const logDir = path.join(__dirname, '..', '..', config.get('storage.logs')) | 9 | const constants = require('../initializers/constants') |
11 | const label = config.get('webserver.host') + ':' + config.get('webserver.port') | 10 | |
11 | const 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 |
14 | mkdirp.sync(logDir) | 14 | mkdirp.sync(constants.CONFIG.STORAGE.LOG_DIR) |
15 | 15 | ||
16 | const logger = new winston.Logger({ | 16 | const 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 | ||
3 | const config = require('config') | ||
4 | const crypto = require('crypto') | 3 | const crypto = require('crypto') |
5 | const fs = require('fs') | 4 | const fs = require('fs') |
6 | const openssl = require('openssl-wrapper') | 5 | const openssl = require('openssl-wrapper') |
7 | const path = require('path') | ||
8 | const ursa = require('ursa') | 6 | const ursa = require('ursa') |
9 | 7 | ||
8 | const constants = require('../initializers/constants') | ||
10 | const logger = require('./logger') | 9 | const logger = require('./logger') |
11 | 10 | ||
12 | const certDir = path.join(__dirname, '..', '..', config.get('storage.certs')) | ||
13 | const algorithm = 'aes-256-ctr' | 11 | const algorithm = 'aes-256-ctr' |
14 | 12 | ||
15 | const peertubeCrypto = { | 13 | const 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 | ||
42 | function decrypt (key, data, callback) { | 39 | function 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 | ||
70 | function getCertDir () { | ||
71 | return certDir | ||
72 | } | ||
73 | |||
74 | function sign (data) { | 67 | function 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 | ||
87 | function certsExist (callback) { | 80 | function 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 | ||
3 | const config = require('config') | ||
4 | const replay = require('request-replay') | 3 | const replay = require('request-replay') |
5 | const request = require('request') | 4 | const request = require('request') |
6 | 5 | ||
7 | const constants = require('../initializers/constants') | 6 | const constants = require('../initializers/constants') |
8 | const peertubeCrypto = require('./peertube-crypto') | 7 | const peertubeCrypto = require('./peertube-crypto') |
9 | 8 | ||
10 | const http = config.get('webserver.https') ? 'https' : 'http' | ||
11 | const host = config.get('webserver.host') | ||
12 | const port = config.get('webserver.port') | ||
13 | |||
14 | const requests = { | 9 | const 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 | ||
31 | function makeSecureRequest (params, callback) { | 26 | function 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 | ||