aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/pods.js3
-rw-r--r--server/controllers/client.js2
-rw-r--r--server/helpers/peertube-crypto.js38
-rw-r--r--server/initializers/constants.js8
-rw-r--r--server/lib/friends.js9
-rw-r--r--server/models/video.js31
-rw-r--r--server/tests/api/single-pod.js4
-rw-r--r--server/tests/utils/servers.js2
8 files changed, 60 insertions, 37 deletions
diff --git a/server/controllers/api/pods.js b/server/controllers/api/pods.js
index 38702face..e1fe6fb5d 100644
--- a/server/controllers/api/pods.js
+++ b/server/controllers/api/pods.js
@@ -5,6 +5,7 @@ const waterfall = require('async/waterfall')
5 5
6const db = require('../../initializers/database') 6const db = require('../../initializers/database')
7const logger = require('../../helpers/logger') 7const logger = require('../../helpers/logger')
8const peertubeCrypto = require('../../helpers/peertube-crypto')
8const utils = require('../../helpers/utils') 9const utils = require('../../helpers/utils')
9const friends = require('../../lib/friends') 10const friends = require('../../lib/friends')
10const middlewares = require('../../middlewares') 11const middlewares = require('../../middlewares')
@@ -67,7 +68,7 @@ function addPods (req, res, next) {
67 }, 68 },
68 69
69 function fetchMyCertificate (callback) { 70 function fetchMyCertificate (callback) {
70 friends.getMyCertificate(function (err, cert) { 71 peertubeCrypto.getMyPublicCert(function (err, cert) {
71 if (err) { 72 if (err) {
72 logger.error('Cannot read cert file.') 73 logger.error('Cannot read cert file.')
73 return callback(err) 74 return callback(err)
diff --git a/server/controllers/client.js b/server/controllers/client.js
index 8c242af07..83243a4f7 100644
--- a/server/controllers/client.js
+++ b/server/controllers/client.js
@@ -12,7 +12,7 @@ const db = require('../initializers/database')
12const router = express.Router() 12const router = express.Router()
13 13
14const opengraphComment = '<!-- opengraph tags -->' 14const opengraphComment = '<!-- opengraph tags -->'
15const distPath = path.join(__dirname, '../../client/dist') 15const distPath = path.join(__dirname, '..', '..', 'client/dist')
16const embedPath = path.join(distPath, 'standalone/videos/embed.html') 16const embedPath = path.join(distPath, 'standalone/videos/embed.html')
17const indexPath = path.join(distPath, 'index.html') 17const indexPath = path.join(distPath, 'index.html')
18 18
diff --git a/server/helpers/peertube-crypto.js b/server/helpers/peertube-crypto.js
index 0f1e02ad6..ef6808d5c 100644
--- a/server/helpers/peertube-crypto.js
+++ b/server/helpers/peertube-crypto.js
@@ -4,6 +4,7 @@ const crypto = require('crypto')
4const bcrypt = require('bcrypt') 4const bcrypt = require('bcrypt')
5const fs = require('fs') 5const fs = require('fs')
6const openssl = require('openssl-wrapper') 6const openssl = require('openssl-wrapper')
7const pathUtils = require('path')
7 8
8const constants = require('../initializers/constants') 9const constants = require('../initializers/constants')
9const logger = require('./logger') 10const logger = require('./logger')
@@ -13,6 +14,8 @@ const peertubeCrypto = {
13 comparePassword, 14 comparePassword,
14 createCertsIfNotExist, 15 createCertsIfNotExist,
15 cryptPassword, 16 cryptPassword,
17 getMyPrivateCert,
18 getMyPublicCert,
16 sign 19 sign
17} 20}
18 21
@@ -55,7 +58,8 @@ function sign (data) {
55 sign.update(dataString, 'utf8') 58 sign.update(dataString, 'utf8')
56 59
57 // TODO: make async 60 // TODO: make async
58 const myKey = fs.readFileSync(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem') 61 const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
62 const myKey = fs.readFileSync(certPath)
59 const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING) 63 const signature = sign.sign(myKey, constants.SIGNATURE_ENCODING)
60 64
61 return signature 65 return signature
@@ -91,6 +95,16 @@ function cryptPassword (password, callback) {
91 }) 95 })
92} 96}
93 97
98function getMyPrivateCert (callback) {
99 const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
100 fs.readFile(certPath, 'utf8', callback)
101}
102
103function getMyPublicCert (callback) {
104 const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PUBLIC_CERT_NAME)
105 fs.readFile(certPath, 'utf8', callback)
106}
107
94// --------------------------------------------------------------------------- 108// ---------------------------------------------------------------------------
95 109
96module.exports = peertubeCrypto 110module.exports = peertubeCrypto
@@ -98,7 +112,8 @@ module.exports = peertubeCrypto
98// --------------------------------------------------------------------------- 112// ---------------------------------------------------------------------------
99 113
100function certsExist (callback) { 114function certsExist (callback) {
101 fs.exists(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', function (exists) { 115 const certPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
116 fs.exists(certPath, function (exists) {
102 return callback(exists) 117 return callback(exists)
103 }) 118 })
104} 119}
@@ -113,24 +128,27 @@ function createCerts (callback) {
113 128
114 logger.info('Generating a RSA key...') 129 logger.info('Generating a RSA key...')
115 130
116 let options = { 131 const privateCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, constants.PRIVATE_CERT_NAME)
117 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', 132 const genRsaOptions = {
133 'out': privateCertPath,
118 '2048': false 134 '2048': false
119 } 135 }
120 openssl.exec('genrsa', options, function (err) { 136 openssl.exec('genrsa', genRsaOptions, function (err) {
121 if (err) { 137 if (err) {
122 logger.error('Cannot create private key on this pod.') 138 logger.error('Cannot create private key on this pod.')
123 return callback(err) 139 return callback(err)
124 } 140 }
141
125 logger.info('RSA key generated.') 142 logger.info('RSA key generated.')
143 logger.info('Managing public key...')
126 144
127 options = { 145 const publicCertPath = pathUtils.join(constants.CONFIG.STORAGE.CERT_DIR, 'peertube.pub')
128 'in': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.key.pem', 146 const rsaOptions = {
147 'in': privateCertPath,
129 'pubout': true, 148 'pubout': true,
130 'out': constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub' 149 'out': publicCertPath
131 } 150 }
132 logger.info('Manage public key...') 151 openssl.exec('rsa', rsaOptions, function (err) {
133 openssl.exec('rsa', options, function (err) {
134 if (err) { 152 if (err) {
135 logger.error('Cannot create public key on this pod.') 153 logger.error('Cannot create public key on this pod.')
136 return callback(err) 154 return callback(err)
diff --git a/server/initializers/constants.js b/server/initializers/constants.js
index 0c080ccd2..90adbf406 100644
--- a/server/initializers/constants.js
+++ b/server/initializers/constants.js
@@ -134,6 +134,8 @@ const REMOTE_SCHEME = {
134 134
135// --------------------------------------------------------------------------- 135// ---------------------------------------------------------------------------
136 136
137const PRIVATE_CERT_NAME = 'peertube.key.pem'
138const PUBLIC_CERT_NAME = 'peertube.pub'
137const SIGNATURE_ALGORITHM = 'RSA-SHA256' 139const SIGNATURE_ALGORITHM = 'RSA-SHA256'
138const SIGNATURE_ENCODING = 'hex' 140const SIGNATURE_ENCODING = 'hex'
139 141
@@ -189,13 +191,15 @@ module.exports = {
189 PAGINATION_COUNT_DEFAULT, 191 PAGINATION_COUNT_DEFAULT,
190 PODS_SCORE, 192 PODS_SCORE,
191 PREVIEWS_SIZE, 193 PREVIEWS_SIZE,
194 PRIVATE_CERT_NAME,
195 PUBLIC_CERT_NAME,
192 REMOTE_SCHEME, 196 REMOTE_SCHEME,
193 REQUEST_ENDPOINTS,
194 REQUEST_ENDPOINT_ACTIONS, 197 REQUEST_ENDPOINT_ACTIONS,
198 REQUEST_ENDPOINTS,
195 REQUESTS_IN_PARALLEL, 199 REQUESTS_IN_PARALLEL,
196 REQUESTS_INTERVAL, 200 REQUESTS_INTERVAL,
197 REQUESTS_LIMIT_PODS,
198 REQUESTS_LIMIT_PER_POD, 201 REQUESTS_LIMIT_PER_POD,
202 REQUESTS_LIMIT_PODS,
199 RETRY_REQUESTS, 203 RETRY_REQUESTS,
200 SEARCHABLE_COLUMNS, 204 SEARCHABLE_COLUMNS,
201 SIGNATURE_ALGORITHM, 205 SIGNATURE_ALGORITHM,
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 1e8037c37..2ea837c99 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -3,13 +3,13 @@
3const each = require('async/each') 3const each = require('async/each')
4const eachLimit = require('async/eachLimit') 4const eachLimit = require('async/eachLimit')
5const eachSeries = require('async/eachSeries') 5const eachSeries = require('async/eachSeries')
6const fs = require('fs')
7const request = require('request') 6const request = require('request')
8const waterfall = require('async/waterfall') 7const waterfall = require('async/waterfall')
9 8
10const constants = require('../initializers/constants') 9const constants = require('../initializers/constants')
11const db = require('../initializers/database') 10const db = require('../initializers/database')
12const logger = require('../helpers/logger') 11const logger = require('../helpers/logger')
12const peertubeCrypto = require('../helpers/peertube-crypto')
13const requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
14 14
15const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] 15const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
@@ -19,7 +19,6 @@ const friends = {
19 updateVideoToFriends, 19 updateVideoToFriends,
20 reportAbuseVideoToFriend, 20 reportAbuseVideoToFriend,
21 hasFriends, 21 hasFriends,
22 getMyCertificate,
23 makeFriends, 22 makeFriends,
24 quitFriends, 23 quitFriends,
25 removeVideoToFriends, 24 removeVideoToFriends,
@@ -74,15 +73,11 @@ function hasFriends (callback) {
74 }) 73 })
75} 74}
76 75
77function getMyCertificate (callback) {
78 fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
79}
80
81function makeFriends (hosts, callback) { 76function makeFriends (hosts, callback) {
82 const podsScore = {} 77 const podsScore = {}
83 78
84 logger.info('Make friends!') 79 logger.info('Make friends!')
85 getMyCertificate(function (err, cert) { 80 peertubeCrypto.getMyPublicCert(function (err, cert) {
86 if (err) { 81 if (err) {
87 logger.error('Cannot read public cert.') 82 logger.error('Cannot read public cert.')
88 return callback(err) 83 return callback(err)
diff --git a/server/models/video.js b/server/models/video.js
index 17eff6428..742150d69 100644
--- a/server/models/video.js
+++ b/server/models/video.js
@@ -157,8 +157,7 @@ function beforeCreate (video, options, next) {
157 const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) 157 const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
158 158
159 tasks.push( 159 tasks.push(
160 // TODO: refractoring 160 function createVideoTorrent (callback) {
161 function (callback) {
162 const options = { 161 const options = {
163 announceList: [ 162 announceList: [
164 [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ] 163 [ constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT + '/tracker/socket' ]
@@ -171,7 +170,8 @@ function beforeCreate (video, options, next) {
171 createTorrent(videoPath, options, function (err, torrent) { 170 createTorrent(videoPath, options, function (err, torrent) {
172 if (err) return callback(err) 171 if (err) return callback(err)
173 172
174 fs.writeFile(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), torrent, function (err) { 173 const filePath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
174 fs.writeFile(filePath, torrent, function (err) {
175 if (err) return callback(err) 175 if (err) return callback(err)
176 176
177 const parsedTorrent = parseTorrent(torrent) 177 const parsedTorrent = parseTorrent(torrent)
@@ -180,10 +180,12 @@ function beforeCreate (video, options, next) {
180 }) 180 })
181 }) 181 })
182 }, 182 },
183 function (callback) { 183
184 function createVideoThumbnail (callback) {
184 createThumbnail(video, videoPath, callback) 185 createThumbnail(video, videoPath, callback)
185 }, 186 },
186 function (callback) { 187
188 function createVIdeoPreview (callback) {
187 createPreview(video, videoPath, callback) 189 createPreview(video, videoPath, callback)
188 } 190 }
189 ) 191 )
@@ -205,19 +207,19 @@ function afterDestroy (video, options, next) {
205 207
206 if (video.isOwned()) { 208 if (video.isOwned()) {
207 tasks.push( 209 tasks.push(
208 function (callback) { 210 function removeVideoFile (callback) {
209 removeFile(video, callback) 211 removeFile(video, callback)
210 }, 212 },
211 213
212 function (callback) { 214 function removeVideoTorrent (callback) {
213 removeTorrent(video, callback) 215 removeTorrent(video, callback)
214 }, 216 },
215 217
216 function (callback) { 218 function removeVideoPreview (callback) {
217 removePreview(video, callback) 219 removePreview(video, callback)
218 }, 220 },
219 221
220 function (callback) { 222 function removeVideoToFriends (callback) {
221 const params = { 223 const params = {
222 remoteId: video.id 224 remoteId: video.id
223 } 225 }
@@ -395,7 +397,7 @@ function generateThumbnailFromData (video, thumbnailData, callback) {
395 // Creating the thumbnail for a remote video 397 // Creating the thumbnail for a remote video
396 398
397 const thumbnailName = video.getThumbnailName() 399 const thumbnailName = video.getThumbnailName()
398 const thumbnailPath = constants.CONFIG.STORAGE.THUMBNAILS_DIR + thumbnailName 400 const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, thumbnailName)
399 fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) { 401 fs.writeFile(thumbnailPath, Buffer.from(thumbnailData, 'binary'), function (err) {
400 if (err) return callback(err) 402 if (err) return callback(err)
401 403
@@ -596,15 +598,18 @@ function searchAndPopulateAuthorAndPodAndTags (value, field, start, count, sort,
596// --------------------------------------------------------------------------- 598// ---------------------------------------------------------------------------
597 599
598function removeThumbnail (video, callback) { 600function removeThumbnail (video, callback) {
599 fs.unlink(constants.CONFIG.STORAGE.THUMBNAILS_DIR + video.getThumbnailName(), callback) 601 const thumbnailPath = pathUtils.join(constants.CONFIG.STORAGE.THUMBNAILS_DIR, video.getThumbnailName())
602 fs.unlink(thumbnailPath, callback)
600} 603}
601 604
602function removeFile (video, callback) { 605function removeFile (video, callback) {
603 fs.unlink(constants.CONFIG.STORAGE.VIDEOS_DIR + video.getVideoFilename(), callback) 606 const filePath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
607 fs.unlink(filePath, callback)
604} 608}
605 609
606function removeTorrent (video, callback) { 610function removeTorrent (video, callback) {
607 fs.unlink(constants.CONFIG.STORAGE.TORRENTS_DIR + video.getTorrentName(), callback) 611 const torrenPath = pathUtils.join(constants.CONFIG.STORAGE.TORRENTS_DIR, video.getTorrentName())
612 fs.unlink(torrenPath, callback)
608} 613}
609 614
610function removePreview (video, callback) { 615function removePreview (video, callback) {
diff --git a/server/tests/api/single-pod.js b/server/tests/api/single-pod.js
index 2db60448f..83a2b4411 100644
--- a/server/tests/api/single-pod.js
+++ b/server/tests/api/single-pod.js
@@ -251,12 +251,12 @@ describe('Test a single pod', function () {
251 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) { 251 videosUtils.removeVideo(server.url, server.accessToken, videoId, function (err) {
252 if (err) throw err 252 if (err) throw err
253 253
254 fs.readdir(pathUtils.join(__dirname, '../../../test1/videos/'), function (err, files) { 254 fs.readdir(pathUtils.join(__dirname, '..', '..', '..', 'test1/videos/'), function (err, files) {
255 if (err) throw err 255 if (err) throw err
256 256
257 expect(files.length).to.equal(0) 257 expect(files.length).to.equal(0)
258 258
259 fs.readdir(pathUtils.join(__dirname, '../../../test1/thumbnails/'), function (err, files) { 259 fs.readdir(pathUtils.join(__dirname, '..', '..', '..', 'test1/thumbnails/'), function (err, files) {
260 if (err) throw err 260 if (err) throw err
261 261
262 expect(files.length).to.equal(0) 262 expect(files.length).to.equal(0)
diff --git a/server/tests/utils/servers.js b/server/tests/utils/servers.js
index 1946ef49a..c07db4c40 100644
--- a/server/tests/utils/servers.js
+++ b/server/tests/utils/servers.js
@@ -81,7 +81,7 @@ function runServer (number, callback) {
81 detached: true 81 detached: true
82 } 82 }
83 83
84 server.app = fork(pathUtils.join(__dirname, '../../../server.js'), [], options) 84 server.app = fork(pathUtils.join(__dirname, '..', '..', '..', 'server.js'), [], options)
85 server.app.stdout.on('data', function onStdout (data) { 85 server.app.stdout.on('data', function onStdout (data) {
86 let dontContinue = false 86 let dontContinue = false
87 87