aboutsummaryrefslogtreecommitdiffhomepage
path: root/helpers
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2016-02-07 11:23:23 +0100
committerChocobozzz <florian.bigard@gmail.com>2016-02-07 11:23:23 +0100
commit9f10b2928df655c3672d9607e864e667d4bc903a (patch)
tree7743911b974b3a7fb0d4c7cec2a723942466b7f1 /helpers
parentd7c01e7793d813d804a3b5716d8288f9dcf71a16 (diff)
downloadPeerTube-9f10b2928df655c3672d9607e864e667d4bc903a.tar.gz
PeerTube-9f10b2928df655c3672d9607e864e667d4bc903a.tar.zst
PeerTube-9f10b2928df655c3672d9607e864e667d4bc903a.zip
Remove useless anonymous functions of files
Diffstat (limited to 'helpers')
-rw-r--r--helpers/customValidators.js52
-rw-r--r--helpers/logger.js70
-rw-r--r--helpers/peertubeCrypto.js252
-rw-r--r--helpers/requests.js178
-rw-r--r--helpers/utils.js24
5 files changed, 283 insertions, 293 deletions
diff --git a/helpers/customValidators.js b/helpers/customValidators.js
index c433e5c5d..20c41f5da 100644
--- a/helpers/customValidators.js
+++ b/helpers/customValidators.js
@@ -1,34 +1,32 @@
1;(function () { 1'use strict'
2 'use strict'
3 2
4 var validator = require('validator') 3var validator = require('validator')
5 4
6 var customValidators = { 5var customValidators = {
7 eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid, 6 eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid,
8 eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid, 7 eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid,
9 isArray: isArray 8 isArray: isArray
10 } 9}
11 10
12 function eachIsRemoteVideosAddValid (values) { 11function eachIsRemoteVideosAddValid (values) {
13 return values.every(function (val) { 12 return values.every(function (val) {
14 return validator.isLength(val.name, 1, 50) && 13 return validator.isLength(val.name, 1, 50) &&
15 validator.isLength(val.description, 1, 50) && 14 validator.isLength(val.description, 1, 50) &&
16 validator.isLength(val.magnetUri, 10) && 15 validator.isLength(val.magnetUri, 10) &&
17 validator.isURL(val.podUrl) 16 validator.isURL(val.podUrl)
18 }) 17 })
19 } 18}
20 19
21 function eachIsRemoteVideosRemoveValid (values) { 20function eachIsRemoteVideosRemoveValid (values) {
22 return values.every(function (val) { 21 return values.every(function (val) {
23 return validator.isLength(val.magnetUri, 10) 22 return validator.isLength(val.magnetUri, 10)
24 }) 23 })
25 } 24}
26 25
27 function isArray (value) { 26function isArray (value) {
28 return Array.isArray(value) 27 return Array.isArray(value)
29 } 28}
30 29
31 // --------------------------------------------------------------------------- 30// ---------------------------------------------------------------------------
32 31
33 module.exports = customValidators 32module.exports = customValidators
34})()
diff --git a/helpers/logger.js b/helpers/logger.js
index 436091730..67f69a875 100644
--- a/helpers/logger.js
+++ b/helpers/logger.js
@@ -1,42 +1,40 @@
1;(function () { 1// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2 // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ 2'use strict'
3 'use strict'
4 3
5 var config = require('config') 4var config = require('config')
6 var path = require('path') 5var path = require('path')
7 var winston = require('winston') 6var winston = require('winston')
8 winston.emitErrs = true 7winston.emitErrs = true
9 8
10 var logDir = path.join(__dirname, '..', config.get('storage.logs')) 9var logDir = path.join(__dirname, '..', config.get('storage.logs'))
11 var logger = new winston.Logger({ 10var logger = new winston.Logger({
12 transports: [ 11 transports: [
13 new winston.transports.File({ 12 new winston.transports.File({
14 level: 'debug', 13 level: 'debug',
15 filename: path.join(logDir, 'all-logs.log'), 14 filename: path.join(logDir, 'all-logs.log'),
16 handleExceptions: true, 15 handleExceptions: true,
17 json: true, 16 json: true,
18 maxsize: 5242880, 17 maxsize: 5242880,
19 maxFiles: 5, 18 maxFiles: 5,
20 colorize: false 19 colorize: false
21 }), 20 }),
22 new winston.transports.Console({ 21 new winston.transports.Console({
23 level: 'debug', 22 level: 'debug',
24 handleExceptions: true, 23 handleExceptions: true,
25 humanReadableUnhandledException: true, 24 humanReadableUnhandledException: true,
26 json: false, 25 json: false,
27 colorize: true 26 colorize: true
28 }) 27 })
29 ], 28 ],
30 exitOnError: true 29 exitOnError: true
31 }) 30})
32 31
33 logger.stream = { 32logger.stream = {
34 write: function (message, encoding) { 33 write: function (message, encoding) {
35 logger.info(message) 34 logger.info(message)
36 }
37 } 35 }
36}
38 37
39 // --------------------------------------------------------------------------- 38// ---------------------------------------------------------------------------
40 39
41 module.exports = logger 40module.exports = logger
42})()
diff --git a/helpers/peertubeCrypto.js b/helpers/peertubeCrypto.js
index 9031f6ae5..29b9d79c9 100644
--- a/helpers/peertubeCrypto.js
+++ b/helpers/peertubeCrypto.js
@@ -1,149 +1,147 @@
1;(function () { 1'use strict'
2 'use strict' 2
3 3var config = require('config')
4 var config = require('config') 4var crypto = require('crypto')
5 var crypto = require('crypto') 5var fs = require('fs')
6 var fs = require('fs') 6var openssl = require('openssl-wrapper')
7 var openssl = require('openssl-wrapper') 7var path = require('path')
8 var path = require('path') 8var ursa = require('ursa')
9 var ursa = require('ursa') 9
10 10var logger = require('./logger')
11 var logger = require('./logger') 11
12 12var certDir = path.join(__dirname, '..', config.get('storage.certs'))
13 var certDir = path.join(__dirname, '..', config.get('storage.certs')) 13var algorithm = 'aes-256-ctr'
14 var algorithm = 'aes-256-ctr' 14
15 15var peertubeCrypto = {
16 var peertubeCrypto = { 16 checkSignature: checkSignature,
17 checkSignature: checkSignature, 17 createCertsIfNotExist: createCertsIfNotExist,
18 createCertsIfNotExist: createCertsIfNotExist, 18 decrypt: decrypt,
19 decrypt: decrypt, 19 encrypt: encrypt,
20 encrypt: encrypt, 20 getCertDir: getCertDir,
21 getCertDir: getCertDir, 21 sign: sign
22 sign: sign 22}
23 } 23
24 24function checkSignature (public_key, raw_data, hex_signature) {
25 function checkSignature (public_key, raw_data, hex_signature) { 25 var crt = ursa.createPublicKey(public_key)
26 var crt = ursa.createPublicKey(public_key) 26 var is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex')
27 var is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex') 27 return is_valid
28 return is_valid 28}
29 } 29
30 30function createCertsIfNotExist (callback) {
31 function createCertsIfNotExist (callback) { 31 certsExist(function (exist) {
32 certsExist(function (exist) { 32 if (exist === true) {
33 if (exist === true) { 33 return callback(null)
34 return callback(null) 34 }
35 } 35
36 36 createCerts(function (err) {
37 createCerts(function (err) { 37 return callback(err)
38 return callback(err)
39 })
40 }) 38 })
41 } 39 })
40}
42 41
43 function decrypt (key, data, callback) { 42function decrypt (key, data, callback) {
44 fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) { 43 fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
45 if (err) return callback(err) 44 if (err) return callback(err)
46 45
47 var my_private_key = ursa.createPrivateKey(file) 46 var my_private_key = ursa.createPrivateKey(file)
48 var decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8') 47 var decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8')
49 var decrypted_data = symetricDecrypt(data, decrypted_key) 48 var decrypted_data = symetricDecrypt(data, decrypted_key)
50 49
51 return callback(null, decrypted_data) 50 return callback(null, decrypted_data)
52 }) 51 })
53 } 52}
54 53
55 function encrypt (public_key, data, callback) { 54function encrypt (public_key, data, callback) {
56 var crt = ursa.createPublicKey(public_key) 55 var crt = ursa.createPublicKey(public_key)
57 56
58 symetricEncrypt(data, function (err, dataEncrypted) { 57 symetricEncrypt(data, function (err, dataEncrypted) {
59 if (err) return callback(err) 58 if (err) return callback(err)
60 59
61 var key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex') 60 var key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
62 var encrypted = { 61 var encrypted = {
63 data: dataEncrypted.crypted, 62 data: dataEncrypted.crypted,
64 key: key 63 key: key
65 } 64 }
66 65
67 callback(null, encrypted) 66 callback(null, encrypted)
68 }) 67 })
69 } 68}
70 69
71 function getCertDir () { 70function getCertDir () {
72 return certDir 71 return certDir
73 } 72}
74 73
75 function sign (data) { 74function sign (data) {
76 var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem')) 75 var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
77 var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex') 76 var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
78 77
79 return signature 78 return signature
80 } 79}
81 80
82 // --------------------------------------------------------------------------- 81// ---------------------------------------------------------------------------
83 82
84 module.exports = peertubeCrypto 83module.exports = peertubeCrypto
85 84
86 // --------------------------------------------------------------------------- 85// ---------------------------------------------------------------------------
87 86
88 function certsExist (callback) { 87function certsExist (callback) {
89 fs.exists(certDir + 'peertube.key.pem', function (exists) { 88 fs.exists(certDir + 'peertube.key.pem', function (exists) {
90 return callback(exists) 89 return callback(exists)
91 }) 90 })
92 } 91}
93 92
94 function createCerts (callback) { 93function createCerts (callback) {
95 certsExist(function (exist) { 94 certsExist(function (exist) {
96 if (exist === true) { 95 if (exist === true) {
97 var string = 'Certs already exist.' 96 var string = 'Certs already exist.'
98 logger.warning(string) 97 logger.warning(string)
99 return callback(new Error(string)) 98 return callback(new Error(string))
99 }
100
101 logger.info('Generating a RSA key...')
102 openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
103 if (err) {
104 logger.error('Cannot create private key on this pod.')
105 return callback(err)
100 } 106 }
107 logger.info('RSA key generated.')
101 108
102 logger.info('Generating a RSA key...') 109 logger.info('Manage public key...')
103 openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) { 110 openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
104 if (err) { 111 if (err) {
105 logger.error('Cannot create private key on this pod.') 112 logger.error('Cannot create public key on this pod.')
106 return callback(err) 113 return callback(err)
107 } 114 }
108 logger.info('RSA key generated.')
109
110 logger.info('Manage public key...')
111 openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
112 if (err) {
113 logger.error('Cannot create public key on this pod.')
114 return callback(err)
115 }
116
117 logger.info('Public key managed.')
118 return callback(null)
119 })
120 })
121 })
122 }
123 115
124 function generatePassword (callback) { 116 logger.info('Public key managed.')
125 crypto.randomBytes(32, function (err, buf) { 117 return callback(null)
126 if (err) return callback(err) 118 })
127
128 callback(null, buf.toString('utf8'))
129 })
130 }
131
132 function symetricDecrypt (text, password) {
133 var decipher = crypto.createDecipher(algorithm, password)
134 var dec = decipher.update(text, 'hex', 'utf8')
135 dec += decipher.final('utf8')
136 return dec
137 }
138
139 function symetricEncrypt (text, callback) {
140 generatePassword(function (err, password) {
141 if (err) return callback(err)
142
143 var cipher = crypto.createCipher(algorithm, password)
144 var crypted = cipher.update(text, 'utf8', 'hex')
145 crypted += cipher.final('hex')
146 callback(null, { crypted: crypted, password: password })
147 }) 119 })
148 } 120 })
149})() 121}
122
123function generatePassword (callback) {
124 crypto.randomBytes(32, function (err, buf) {
125 if (err) return callback(err)
126
127 callback(null, buf.toString('utf8'))
128 })
129}
130
131function symetricDecrypt (text, password) {
132 var decipher = crypto.createDecipher(algorithm, password)
133 var dec = decipher.update(text, 'hex', 'utf8')
134 dec += decipher.final('utf8')
135 return dec
136}
137
138function symetricEncrypt (text, callback) {
139 generatePassword(function (err, password) {
140 if (err) return callback(err)
141
142 var cipher = crypto.createCipher(algorithm, password)
143 var crypted = cipher.update(text, 'utf8', 'hex')
144 crypted += cipher.final('hex')
145 callback(null, { crypted: crypted, password: password })
146 })
147}
diff --git a/helpers/requests.js b/helpers/requests.js
index 0e301da79..e19afa5ca 100644
--- a/helpers/requests.js
+++ b/helpers/requests.js
@@ -1,111 +1,109 @@
1;(function () { 1'use strict'
2 'use strict'
3 2
4 var async = require('async') 3var async = require('async')
5 var config = require('config') 4var config = require('config')
6 var request = require('request') 5var request = require('request')
7 var replay = require('request-replay') 6var replay = require('request-replay')
8 7
9 var constants = require('../initializers/constants') 8var constants = require('../initializers/constants')
10 var logger = require('./logger') 9var logger = require('./logger')
11 var peertubeCrypto = require('./peertubeCrypto') 10var peertubeCrypto = require('./peertubeCrypto')
12 11
13 var http = config.get('webserver.https') ? 'https' : 'http' 12var http = config.get('webserver.https') ? 'https' : 'http'
14 var host = config.get('webserver.host') 13var host = config.get('webserver.host')
15 var port = config.get('webserver.port') 14var port = config.get('webserver.port')
16 15
17 var requests = { 16var requests = {
18 makeMultipleRetryRequest: makeMultipleRetryRequest 17 makeMultipleRetryRequest: makeMultipleRetryRequest
19 } 18}
20 19
21 function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) { 20function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) {
22 if (!callback) { 21 if (!callback) {
23 callback = callbackEach 22 callback = callbackEach
24 callbackEach = null 23 callbackEach = null
25 } 24 }
26 25
27 var url = http + '://' + host + ':' + port 26 var url = http + '://' + host + ':' + port
28 var signature 27 var signature
29 28
30 // Add signature if it is specified in the params 29 // Add signature if it is specified in the params
31 if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { 30 if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
32 signature = peertubeCrypto.sign(url) 31 signature = peertubeCrypto.sign(url)
33 } 32 }
34 33
35 // Make a request for each pod 34 // Make a request for each pod
36 async.each(pods, function (pod, callback_each_async) { 35 async.each(pods, function (pod, callback_each_async) {
37 function callbackEachRetryRequest (err, response, body, url, pod) { 36 function callbackEachRetryRequest (err, response, body, url, pod) {
38 if (callbackEach !== null) { 37 if (callbackEach !== null) {
39 callbackEach(err, response, body, url, pod, function () { 38 callbackEach(err, response, body, url, pod, function () {
40 callback_each_async()
41 })
42 } else {
43 callback_each_async() 39 callback_each_async()
44 } 40 })
41 } else {
42 callback_each_async()
45 } 43 }
44 }
46 45
47 var params = { 46 var params = {
48 url: pod.url + all_data.path, 47 url: pod.url + all_data.path,
49 method: all_data.method 48 method: all_data.method
50 } 49 }
51 50
52 // Add data with POST requst ? 51 // Add data with POST requst ?
53 if (all_data.method === 'POST' && all_data.data) { 52 if (all_data.method === 'POST' && all_data.data) {
54 // Encrypt data ? 53 // Encrypt data ?
55 if (all_data.encrypt === true) { 54 if (all_data.encrypt === true) {
56 // TODO: ES6 with let 55 // TODO: ES6 with let
57 ;(function (copy_params, copy_url, copy_pod, copy_signature) { 56 ;(function (copy_params, copy_url, copy_pod, copy_signature) {
58 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) { 57 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
59 if (err) return callback(err) 58 if (err) return callback(err)
60 59
61 copy_params.json = { 60 copy_params.json = {
62 data: encrypted.data, 61 data: encrypted.data,
63 key: encrypted.key 62 key: encrypted.key
64 } 63 }
65 64
66 makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest) 65 makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
67 }) 66 })
68 })(params, url, pod, signature) 67 })(params, url, pod, signature)
69 } else {
70 params.json = { data: all_data.data }
71 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
72 }
73 } else { 68 } else {
69 params.json = { data: all_data.data }
74 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) 70 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
75 } 71 }
76 }, callback) 72 } else {
77 } 73 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
74 }
75 }, callback)
76}
78 77
79 // --------------------------------------------------------------------------- 78// ---------------------------------------------------------------------------
80 79
81 module.exports = requests 80module.exports = requests
82 81
83 // --------------------------------------------------------------------------- 82// ---------------------------------------------------------------------------
84 83
85 function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { 84function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
86 // Append the signature 85 // Append the signature
87 if (signature) { 86 if (signature) {
88 params.json.signature = { 87 params.json.signature = {
89 url: from_url, 88 url: from_url,
90 signature: signature 89 signature: signature
91 }
92 } 90 }
93
94 logger.debug('Make retry requests to %s.', to_pod.url)
95
96 replay(
97 request.post(params, function (err, response, body) {
98 callbackEach(err, response, body, params.url, to_pod)
99 }),
100 {
101 retries: constants.REQUEST_RETRIES,
102 factor: 3,
103 maxTimeout: Infinity,
104 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
105 }
106 ).on('replay', function (replay) {
107 logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
108 params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
109 })
110 } 91 }
111})() 92
93 logger.debug('Make retry requests to %s.', to_pod.url)
94
95 replay(
96 request.post(params, function (err, response, body) {
97 callbackEach(err, response, body, params.url, to_pod)
98 }),
99 {
100 retries: constants.REQUEST_RETRIES,
101 factor: 3,
102 maxTimeout: Infinity,
103 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
104 }
105 ).on('replay', function (replay) {
106 logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
107 params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
108 })
109}
diff --git a/helpers/utils.js b/helpers/utils.js
index 92684ea81..d2c9ad8b2 100644
--- a/helpers/utils.js
+++ b/helpers/utils.js
@@ -1,18 +1,16 @@
1;(function () { 1'use strict'
2 'use strict'
3 2
4 var logger = require('./logger') 3var logger = require('./logger')
5 4
6 var utils = { 5var utils = {
7 cleanForExit: cleanForExit 6 cleanForExit: cleanForExit
8 } 7}
9 8
10 function cleanForExit (webtorrent_process) { 9function cleanForExit (webtorrent_process) {
11 logger.info('Gracefully exiting.') 10 logger.info('Gracefully exiting.')
12 process.kill(-webtorrent_process.pid) 11 process.kill(-webtorrent_process.pid)
13 } 12}
14 13
15 // --------------------------------------------------------------------------- 14// ---------------------------------------------------------------------------
16 15
17 module.exports = utils 16module.exports = utils
18})()