aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/customValidators.js32
-rw-r--r--server/helpers/logger.js40
-rw-r--r--server/helpers/peertubeCrypto.js147
-rw-r--r--server/helpers/requests.js109
-rw-r--r--server/helpers/utils.js16
5 files changed, 344 insertions, 0 deletions
diff --git a/server/helpers/customValidators.js b/server/helpers/customValidators.js
new file mode 100644
index 000000000..20c41f5da
--- /dev/null
+++ b/server/helpers/customValidators.js
@@ -0,0 +1,32 @@
1'use strict'
2
3var validator = require('validator')
4
5var customValidators = {
6 eachIsRemoteVideosAddValid: eachIsRemoteVideosAddValid,
7 eachIsRemoteVideosRemoveValid: eachIsRemoteVideosRemoveValid,
8 isArray: isArray
9}
10
11function eachIsRemoteVideosAddValid (values) {
12 return values.every(function (val) {
13 return validator.isLength(val.name, 1, 50) &&
14 validator.isLength(val.description, 1, 50) &&
15 validator.isLength(val.magnetUri, 10) &&
16 validator.isURL(val.podUrl)
17 })
18}
19
20function eachIsRemoteVideosRemoveValid (values) {
21 return values.every(function (val) {
22 return validator.isLength(val.magnetUri, 10)
23 })
24}
25
26function isArray (value) {
27 return Array.isArray(value)
28}
29
30// ---------------------------------------------------------------------------
31
32module.exports = customValidators
diff --git a/server/helpers/logger.js b/server/helpers/logger.js
new file mode 100644
index 000000000..67f69a875
--- /dev/null
+++ b/server/helpers/logger.js
@@ -0,0 +1,40 @@
1// Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
2'use strict'
3
4var config = require('config')
5var path = require('path')
6var winston = require('winston')
7winston.emitErrs = true
8
9var logDir = path.join(__dirname, '..', config.get('storage.logs'))
10var logger = new winston.Logger({
11 transports: [
12 new winston.transports.File({
13 level: 'debug',
14 filename: path.join(logDir, 'all-logs.log'),
15 handleExceptions: true,
16 json: true,
17 maxsize: 5242880,
18 maxFiles: 5,
19 colorize: false
20 }),
21 new winston.transports.Console({
22 level: 'debug',
23 handleExceptions: true,
24 humanReadableUnhandledException: true,
25 json: false,
26 colorize: true
27 })
28 ],
29 exitOnError: true
30})
31
32logger.stream = {
33 write: function (message, encoding) {
34 logger.info(message)
35 }
36}
37
38// ---------------------------------------------------------------------------
39
40module.exports = logger
diff --git a/server/helpers/peertubeCrypto.js b/server/helpers/peertubeCrypto.js
new file mode 100644
index 000000000..29b9d79c9
--- /dev/null
+++ b/server/helpers/peertubeCrypto.js
@@ -0,0 +1,147 @@
1'use strict'
2
3var config = require('config')
4var crypto = require('crypto')
5var fs = require('fs')
6var openssl = require('openssl-wrapper')
7var path = require('path')
8var ursa = require('ursa')
9
10var logger = require('./logger')
11
12var certDir = path.join(__dirname, '..', config.get('storage.certs'))
13var algorithm = 'aes-256-ctr'
14
15var peertubeCrypto = {
16 checkSignature: checkSignature,
17 createCertsIfNotExist: createCertsIfNotExist,
18 decrypt: decrypt,
19 encrypt: encrypt,
20 getCertDir: getCertDir,
21 sign: sign
22}
23
24function checkSignature (public_key, raw_data, hex_signature) {
25 var crt = ursa.createPublicKey(public_key)
26 var is_valid = crt.hashAndVerify('sha256', new Buffer(raw_data).toString('hex'), hex_signature, 'hex')
27 return is_valid
28}
29
30function createCertsIfNotExist (callback) {
31 certsExist(function (exist) {
32 if (exist === true) {
33 return callback(null)
34 }
35
36 createCerts(function (err) {
37 return callback(err)
38 })
39 })
40}
41
42function decrypt (key, data, callback) {
43 fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
44 if (err) return callback(err)
45
46 var my_private_key = ursa.createPrivateKey(file)
47 var decrypted_key = my_private_key.decrypt(key, 'hex', 'utf8')
48 var decrypted_data = symetricDecrypt(data, decrypted_key)
49
50 return callback(null, decrypted_data)
51 })
52}
53
54function encrypt (public_key, data, callback) {
55 var crt = ursa.createPublicKey(public_key)
56
57 symetricEncrypt(data, function (err, dataEncrypted) {
58 if (err) return callback(err)
59
60 var key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
61 var encrypted = {
62 data: dataEncrypted.crypted,
63 key: key
64 }
65
66 callback(null, encrypted)
67 })
68}
69
70function getCertDir () {
71 return certDir
72}
73
74function sign (data) {
75 var myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
76 var signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
77
78 return signature
79}
80
81// ---------------------------------------------------------------------------
82
83module.exports = peertubeCrypto
84
85// ---------------------------------------------------------------------------
86
87function certsExist (callback) {
88 fs.exists(certDir + 'peertube.key.pem', function (exists) {
89 return callback(exists)
90 })
91}
92
93function createCerts (callback) {
94 certsExist(function (exist) {
95 if (exist === true) {
96 var string = 'Certs already exist.'
97 logger.warning(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)
106 }
107 logger.info('RSA key generated.')
108
109 logger.info('Manage public key...')
110 openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
111 if (err) {
112 logger.error('Cannot create public key on this pod.')
113 return callback(err)
114 }
115
116 logger.info('Public key managed.')
117 return callback(null)
118 })
119 })
120 })
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/server/helpers/requests.js b/server/helpers/requests.js
new file mode 100644
index 000000000..e19afa5ca
--- /dev/null
+++ b/server/helpers/requests.js
@@ -0,0 +1,109 @@
1'use strict'
2
3var async = require('async')
4var config = require('config')
5var request = require('request')
6var replay = require('request-replay')
7
8var constants = require('../initializers/constants')
9var logger = require('./logger')
10var peertubeCrypto = require('./peertubeCrypto')
11
12var http = config.get('webserver.https') ? 'https' : 'http'
13var host = config.get('webserver.host')
14var port = config.get('webserver.port')
15
16var requests = {
17 makeMultipleRetryRequest: makeMultipleRetryRequest
18}
19
20function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) {
21 if (!callback) {
22 callback = callbackEach
23 callbackEach = null
24 }
25
26 var url = http + '://' + host + ':' + port
27 var signature
28
29 // Add signature if it is specified in the params
30 if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
31 signature = peertubeCrypto.sign(url)
32 }
33
34 // Make a request for each pod
35 async.each(pods, function (pod, callback_each_async) {
36 function callbackEachRetryRequest (err, response, body, url, pod) {
37 if (callbackEach !== null) {
38 callbackEach(err, response, body, url, pod, function () {
39 callback_each_async()
40 })
41 } else {
42 callback_each_async()
43 }
44 }
45
46 var params = {
47 url: pod.url + all_data.path,
48 method: all_data.method
49 }
50
51 // Add data with POST requst ?
52 if (all_data.method === 'POST' && all_data.data) {
53 // Encrypt data ?
54 if (all_data.encrypt === true) {
55 // TODO: ES6 with let
56 ;(function (copy_params, copy_url, copy_pod, copy_signature) {
57 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
58 if (err) return callback(err)
59
60 copy_params.json = {
61 data: encrypted.data,
62 key: encrypted.key
63 }
64
65 makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
66 })
67 })(params, url, pod, signature)
68 } else {
69 params.json = { data: all_data.data }
70 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
71 }
72 } else {
73 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
74 }
75 }, callback)
76}
77
78// ---------------------------------------------------------------------------
79
80module.exports = requests
81
82// ---------------------------------------------------------------------------
83
84function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
85 // Append the signature
86 if (signature) {
87 params.json.signature = {
88 url: from_url,
89 signature: signature
90 }
91 }
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/server/helpers/utils.js b/server/helpers/utils.js
new file mode 100644
index 000000000..d2c9ad8b2
--- /dev/null
+++ b/server/helpers/utils.js
@@ -0,0 +1,16 @@
1'use strict'
2
3var logger = require('./logger')
4
5var utils = {
6 cleanForExit: cleanForExit
7}
8
9function cleanForExit (webtorrent_process) {
10 logger.info('Gracefully exiting.')
11 process.kill(-webtorrent_process.pid)
12}
13
14// ---------------------------------------------------------------------------
15
16module.exports = utils