aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/checker.js45
-rw-r--r--src/constants.js37
-rw-r--r--src/customValidators.js29
-rw-r--r--src/database.js61
-rw-r--r--src/logger.js40
-rw-r--r--src/pods.js274
-rw-r--r--src/poolRequests.js206
-rw-r--r--src/utils.js202
-rw-r--r--src/videos.js272
-rw-r--r--src/webTorrentNode.js160
-rw-r--r--src/webtorrent.js91
11 files changed, 0 insertions, 1417 deletions
diff --git a/src/checker.js b/src/checker.js
deleted file mode 100644
index 7a3a53616..000000000
--- a/src/checker.js
+++ /dev/null
@@ -1,45 +0,0 @@
1;(function () {
2 'use strict'
3
4 var config = require('config')
5 var mkdirp = require('mkdirp')
6
7 var checker = {}
8
9 // Check the config files
10 checker.checkConfig = function () {
11 var required = [ 'listen.port',
12 'webserver.https', 'webserver.host', 'webserver.port',
13 'database.host', 'database.port', 'database.suffix',
14 'storage.certs', 'storage.uploads', 'storage.logs',
15 'network.friends' ]
16 var miss = []
17
18 for (var key of required) {
19 if (!config.has(key)) {
20 miss.push(key)
21 }
22 }
23
24 return miss
25 }
26
27 // Create directories for the storage if it doesn't exist
28 checker.createDirectoriesIfNotExist = function () {
29 var storages = config.get('storage')
30
31 for (var key of Object.keys(storages)) {
32 var path = storages[key]
33 try {
34 mkdirp.sync(__dirname + '/../' + path)
35 } catch (error) {
36 // Do not use logger
37 console.error('Cannot create ' + path + ':' + error)
38 process.exit(0)
39 }
40 }
41 }
42
43 // ----------- Export -----------
44 module.exports = checker
45})()
diff --git a/src/constants.js b/src/constants.js
deleted file mode 100644
index 00b713961..000000000
--- a/src/constants.js
+++ /dev/null
@@ -1,37 +0,0 @@
1;(function () {
2 'use strict'
3
4 var constants = {}
5
6 function isTestInstance () {
7 return (process.env.NODE_ENV === 'test')
8 }
9
10 // API version of our pod
11 constants.API_VERSION = 'v1'
12
13 // Score a pod has when we create it as a friend
14 constants.FRIEND_BASE_SCORE = 100
15
16 // Time to wait between requests to the friends
17 constants.INTERVAL = 60000
18
19 // Number of points we add/remove from a friend after a successful/bad request
20 constants.PODS_SCORE = {
21 MALUS: -10,
22 BONUS: 10
23 }
24
25 // Number of retries we make for the make retry requests (to friends...)
26 constants.REQUEST_RETRIES = 10
27
28 // Special constants for a test instance
29 if (isTestInstance() === true) {
30 constants.FRIEND_BASE_SCORE = 20
31 constants.INTERVAL = 10000
32 constants.REQUEST_RETRIES = 2
33 }
34
35 // ----------- Export -----------
36 module.exports = constants
37})()
diff --git a/src/customValidators.js b/src/customValidators.js
deleted file mode 100644
index 73c2f8461..000000000
--- a/src/customValidators.js
+++ /dev/null
@@ -1,29 +0,0 @@
1;(function () {
2 'use strict'
3
4 var validator = require('validator')
5
6 var customValidators = {}
7
8 customValidators.eachIsRemoteVideosAddValid = function (values) {
9 return values.every(function (val) {
10 return validator.isLength(val.name, 1, 50) &&
11 validator.isLength(val.description, 1, 50) &&
12 validator.isLength(val.magnetUri, 10) &&
13 validator.isURL(val.podUrl)
14 })
15 }
16
17 customValidators.eachIsRemoteVideosRemoveValid = function (values) {
18 return values.every(function (val) {
19 return validator.isLength(val.magnetUri, 10)
20 })
21 }
22
23 customValidators.isArray = function (value) {
24 return Array.isArray(value)
25 }
26
27 // ----------- Export -----------
28 module.exports = customValidators
29})()
diff --git a/src/database.js b/src/database.js
deleted file mode 100644
index e03176b31..000000000
--- a/src/database.js
+++ /dev/null
@@ -1,61 +0,0 @@
1;(function () {
2 'use strict'
3
4 var config = require('config')
5 var mongoose = require('mongoose')
6
7 var constants = require('./constants')
8 var logger = require('./logger')
9
10 var dbname = 'peertube' + config.get('database.suffix')
11 var host = config.get('database.host')
12 var port = config.get('database.port')
13
14 // ----------- Videos -----------
15 var videosSchema = mongoose.Schema({
16 name: String,
17 namePath: String,
18 description: String,
19 magnetUri: String,
20 podUrl: String
21 })
22
23 var VideosDB = mongoose.model('videos', videosSchema)
24
25 // ----------- Pods -----------
26 var podsSchema = mongoose.Schema({
27 url: String,
28 publicKey: String,
29 score: { type: Number, max: constants.FRIEND_BASE_SCORE }
30 })
31
32 var PodsDB = mongoose.model('pods', podsSchema)
33
34 // ----------- PoolRequests -----------
35 var poolRequestsSchema = mongoose.Schema({
36 type: String,
37 id: String, // Special id to find duplicates (video created we want to remove...)
38 request: mongoose.Schema.Types.Mixed
39 })
40
41 var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema)
42
43 // ----------- Connection -----------
44
45 mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname)
46 mongoose.connection.on('error', function () {
47 logger.error('Mongodb connection error.')
48 process.exit(0)
49 })
50
51 mongoose.connection.on('open', function () {
52 logger.info('Connected to mongodb.')
53 })
54
55 // ----------- Export -----------
56 module.exports = {
57 VideosDB: VideosDB,
58 PodsDB: PodsDB,
59 PoolRequestsDB: PoolRequestsDB
60 }
61})()
diff --git a/src/logger.js b/src/logger.js
deleted file mode 100644
index 850af10cb..000000000
--- a/src/logger.js
+++ /dev/null
@@ -1,40 +0,0 @@
1;(function () {
2 // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
3 'use strict'
4
5 var config = require('config')
6 var winston = require('winston')
7
8 var logDir = __dirname + '/../' + config.get('storage.logs')
9
10 winston.emitErrs = true
11
12 var logger = new winston.Logger({
13 transports: [
14 new winston.transports.File({
15 level: 'debug',
16 filename: logDir + '/all-logs.log',
17 handleExceptions: true,
18 json: true,
19 maxsize: 5242880,
20 maxFiles: 5,
21 colorize: false
22 }),
23 new winston.transports.Console({
24 level: 'debug',
25 handleExceptions: true,
26 humanReadableUnhandledException: true,
27 json: false,
28 colorize: true
29 })
30 ],
31 exitOnError: true
32 })
33
34 module.exports = logger
35 module.exports.stream = {
36 write: function (message, encoding) {
37 logger.info(message)
38 }
39 }
40})()
diff --git a/src/pods.js b/src/pods.js
deleted file mode 100644
index defa9b1c1..000000000
--- a/src/pods.js
+++ /dev/null
@@ -1,274 +0,0 @@
1;(function () {
2 'use strict'
3
4 var async = require('async')
5 var config = require('config')
6 var fs = require('fs')
7 var request = require('request')
8
9 var constants = require('./constants')
10 var logger = require('./logger')
11 var PodsDB = require('./database').PodsDB
12 var poolRequests = require('./poolRequests')
13 var utils = require('./utils')
14
15 var pods = {}
16
17 var http = config.get('webserver.https') ? 'https' : 'http'
18 var host = config.get('webserver.host')
19 var port = config.get('webserver.port')
20
21 // ----------- Private functions -----------
22
23 function getForeignPodsList (url, callback) {
24 var path = '/api/' + constants.API_VERSION + '/pods'
25
26 request.get(url + path, function (err, response, body) {
27 if (err) throw err
28 callback(JSON.parse(body))
29 })
30 }
31
32 // ----------- Public functions -----------
33
34 pods.list = function (callback) {
35 PodsDB.find(function (err, pods_list) {
36 if (err) {
37 logger.error('Cannot get the list of the pods.', { error: err })
38 return callback(err)
39 }
40
41 return callback(null, pods_list)
42 })
43 }
44
45 // { url }
46 // TODO: check if the pod is not already a friend
47 pods.add = function (data, callback) {
48 var videos = require('./videos')
49 logger.info('Adding pod: %s', data.url)
50
51 var params = {
52 url: data.url,
53 publicKey: data.publicKey,
54 score: constants.FRIEND_BASE_SCORE
55 }
56
57 PodsDB.create(params, function (err, pod) {
58 if (err) {
59 logger.error('Cannot insert the pod.', { error: err })
60 return callback(err)
61 }
62
63 videos.addRemotes(data.videos)
64
65 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
66 if (err) {
67 logger.error('Cannot read cert file.', { error: err })
68 return callback(err)
69 }
70
71 videos.listOwned(function (err, videos_list) {
72 if (err) {
73 logger.error('Cannot get the list of owned videos.', { error: err })
74 return callback(err)
75 }
76
77 return callback(null, { cert: cert, videos: videos_list })
78 })
79 })
80 })
81 }
82
83 pods.remove = function (url, callback) {
84 var videos = require('./videos')
85 logger.info('Removing %s pod.', url)
86
87 videos.removeAllRemotesOf(url, function (err) {
88 if (err) logger.error('Cannot remove all remote videos of %s.', url)
89
90 PodsDB.remove({ url: url }, function (err) {
91 if (err) return callback(err)
92
93 logger.info('%s pod removed.', url)
94 callback(null)
95 })
96 })
97 }
98
99 pods.addVideoToFriends = function (video) {
100 // To avoid duplicates
101 var id = video.name + video.magnetUri
102 poolRequests.addToPoolRequests(id, 'add', video)
103 }
104
105 pods.removeVideoToFriends = function (video) {
106 // To avoid duplicates
107 var id = video.name + video.magnetUri
108 poolRequests.addToPoolRequests(id, 'remove', video)
109 }
110
111 pods.makeFriends = function (callback) {
112 var videos = require('./videos')
113 var pods_score = {}
114
115 logger.info('Make friends!')
116 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
117 if (err) {
118 logger.error('Cannot read public cert.', { error: err })
119 return callback(err)
120 }
121
122 var urls = config.get('network.friends')
123
124 async.each(urls, computeForeignPodsList, function () {
125 logger.debug('Pods scores computed.', { pods_score: pods_score })
126 var pods_list = computeWinningPods(urls, pods_score)
127 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
128
129 makeRequestsToWinningPods(cert, pods_list)
130 })
131 })
132
133 // -----------------------------------------------------------------------
134
135 function computeForeignPodsList (url, callback) {
136 // Let's give 1 point to the pod we ask the friends list
137 pods_score[url] = 1
138
139 getForeignPodsList(url, function (foreign_pods_list) {
140 if (foreign_pods_list.length === 0) return callback()
141
142 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
143 var foreign_url = foreign_pod.url
144
145 if (pods_score[foreign_url]) pods_score[foreign_url]++
146 else pods_score[foreign_url] = 1
147
148 callback_each()
149 }, function () {
150 callback()
151 })
152 })
153 }
154
155 function computeWinningPods (urls, pods_score) {
156 // Build the list of pods to add
157 // Only add a pod if it exists in more than a half base pods
158 var pods_list = []
159 var base_score = urls.length / 2
160 Object.keys(pods_score).forEach(function (pod) {
161 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
162 })
163
164 return pods_list
165 }
166
167 function makeRequestsToWinningPods (cert, pods_list) {
168 // Stop pool requests
169 poolRequests.deactivate()
170 // Flush pool requests
171 poolRequests.forceSend()
172
173 // Get the list of our videos to send to our new friends
174 videos.listOwned(function (err, videos_list) {
175 if (err) throw err
176
177 var data = {
178 url: http + '://' + host + ':' + port,
179 publicKey: cert,
180 videos: videos_list
181 }
182
183 utils.makeMultipleRetryRequest(
184 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
185
186 pods_list,
187
188 function eachRequest (err, response, body, url, pod, callback_each_request) {
189 // We add the pod if it responded correctly with its public certificate
190 if (!err && response.statusCode === 200) {
191 pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
192 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
193
194 videos.addRemotes(body.videos, function (err) {
195 if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err })
196
197 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
198 return callback_each_request()
199 })
200 })
201 } else {
202 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
203 return callback_each_request()
204 }
205 },
206
207 function endRequests (err) {
208 // Now we made new friends, we can re activate the pool of requests
209 poolRequests.activate()
210
211 if (err) {
212 logger.error('There was some errors when we wanted to make friends.', { error: err })
213 return callback(err)
214 }
215
216 logger.debug('makeRequestsToWinningPods finished.')
217 return callback(null)
218 }
219 )
220 })
221 }
222 }
223
224 pods.quitFriends = function (callback) {
225 // Stop pool requests
226 poolRequests.deactivate()
227 // Flush pool requests
228 poolRequests.forceSend()
229
230 PodsDB.find(function (err, pods) {
231 if (err) return callback(err)
232
233 var request = {
234 method: 'POST',
235 path: '/api/' + constants.API_VERSION + '/pods/remove',
236 sign: true,
237 encrypt: true,
238 data: {
239 url: 'me' // Fake data
240 }
241 }
242
243 // Announce we quit them
244 utils.makeMultipleRetryRequest(request, pods, function () {
245 PodsDB.remove(function (err) {
246 poolRequests.activate()
247
248 if (err) return callback(err)
249
250 logger.info('Broke friends, so sad :(')
251
252 var videos = require('./videos')
253 videos.removeAllRemotes(function (err) {
254 if (err) return callback(err)
255
256 logger.info('Removed all remote videos.')
257 callback(null)
258 })
259 })
260 })
261 })
262 }
263
264 pods.hasFriends = function (callback) {
265 PodsDB.count(function (err, count) {
266 if (err) return callback(err)
267
268 var has_friends = (count !== 0)
269 callback(null, has_friends)
270 })
271 }
272
273 module.exports = pods
274})()
diff --git a/src/poolRequests.js b/src/poolRequests.js
deleted file mode 100644
index 7f422f372..000000000
--- a/src/poolRequests.js
+++ /dev/null
@@ -1,206 +0,0 @@
1;(function () {
2 'use strict'
3
4 var async = require('async')
5
6 var constants = require('./constants')
7 var logger = require('./logger')
8 var database = require('./database')
9 var pluck = require('lodash-node/compat/collection/pluck')
10 var PoolRequestsDB = database.PoolRequestsDB
11 var PodsDB = database.PodsDB
12 var utils = require('./utils')
13 var VideosDB = database.VideosDB
14
15 var poolRequests = {}
16
17 // ----------- Private -----------
18 var timer = null
19
20 function removePoolRequestsFromDB (ids) {
21 PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) {
22 if (err) {
23 logger.error('Cannot remove requests from the pool requests database.', { error: err })
24 return
25 }
26
27 logger.info('Pool requests flushed.')
28 })
29 }
30
31 function makePoolRequests () {
32 logger.info('Making pool requests to friends.')
33
34 PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, function (err, pool_requests) {
35 if (err) throw err
36
37 if (pool_requests.length === 0) return
38
39 var requests = {
40 add: {
41 ids: [],
42 requests: []
43 },
44 remove: {
45 ids: [],
46 requests: []
47 }
48 }
49
50 async.each(pool_requests, function (pool_request, callback_each) {
51 if (pool_request.type === 'add') {
52 requests.add.requests.push(pool_request.request)
53 requests.add.ids.push(pool_request._id)
54 } else if (pool_request.type === 'remove') {
55 requests.remove.requests.push(pool_request.request)
56 requests.remove.ids.push(pool_request._id)
57 } else {
58 throw new Error('Unkown pool request type.')
59 }
60
61 callback_each()
62 }, function () {
63 // Send the add requests
64 if (requests.add.requests.length !== 0) {
65 makePoolRequest('add', requests.add.requests, function (err) {
66 if (err) logger.error('Errors when sent add pool requests.', { error: err })
67
68 removePoolRequestsFromDB(requests.add.ids)
69 })
70 }
71
72 // Send the remove requests
73 if (requests.remove.requests.length !== 0) {
74 makePoolRequest('remove', requests.remove.requests, function (err) {
75 if (err) logger.error('Errors when sent remove pool requests.', { error: err })
76
77 removePoolRequestsFromDB(requests.remove.ids)
78 })
79 }
80 })
81 })
82 }
83
84 function updatePodsScore (good_pods, bad_pods) {
85 logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
86
87 PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec()
88 PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) {
89 if (err) throw err
90 removeBadPods()
91 })
92 }
93
94 function removeBadPods () {
95 PodsDB.find({ score: 0 }, { _id: 1, url: 1 }, function (err, pods) {
96 if (err) throw err
97
98 if (pods.length === 0) return
99
100 var urls = pluck(pods, 'url')
101 var ids = pluck(pods, '_id')
102
103 VideosDB.remove({ podUrl: { $in: urls } }, function (err, r) {
104 if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err })
105 var videos_removed = r.result.n
106 logger.info('Removed %d videos.', videos_removed)
107
108 PodsDB.remove({ _id: { $in: ids } }, function (err, r) {
109 if (err) logger.error('Cannot remove bad pods.', { error: err })
110
111 var pods_removed = r.result.n
112 logger.info('Removed %d pods.', pods_removed)
113 })
114 })
115 })
116 }
117
118 function makePoolRequest (type, requests, callback) {
119 if (!callback) callback = function () {}
120
121 PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) {
122 if (err) throw err
123
124 var params = {
125 encrypt: true,
126 sign: true,
127 method: 'POST',
128 path: null,
129 data: requests
130 }
131
132 if (type === 'add') {
133 params.path = '/api/' + constants.API_VERSION + '/remotevideos/add'
134 } else if (type === 'remove') {
135 params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove'
136 } else {
137 throw new Error('Unkown pool request type.')
138 }
139
140 var bad_pods = []
141 var good_pods = []
142
143 utils.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished)
144
145 function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) {
146 if (err || (response.statusCode !== 200 && response.statusCode !== 204)) {
147 bad_pods.push(pod._id)
148 logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') })
149 } else {
150 good_pods.push(pod._id)
151 }
152
153 return callback_each_pod_finished()
154 }
155
156 function callbackAllPodsFinished (err) {
157 if (err) return callback(err)
158
159 updatePodsScore(good_pods, bad_pods)
160 callback(null)
161 }
162 })
163 }
164
165 // ----------- Public -----------
166 poolRequests.activate = function () {
167 logger.info('Pool requests activated.')
168 timer = setInterval(makePoolRequests, constants.INTERVAL)
169 }
170
171 poolRequests.addToPoolRequests = function (id, type, request) {
172 logger.debug('Add request to the pool requests.', { id: id, type: type, request: request })
173
174 PoolRequestsDB.findOne({ id: id }, function (err, entity) {
175 if (err) logger.error(err)
176
177 if (entity) {
178 if (entity.type === type) {
179 logger.error(new Error('Cannot insert two same requests.'))
180 return
181 }
182
183 // Remove the request of the other type
184 PoolRequestsDB.remove({ id: id }, function (err) {
185 if (err) logger.error(err)
186 })
187 } else {
188 PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) {
189 if (err) logger.error(err)
190 })
191 }
192 })
193 }
194
195 poolRequests.deactivate = function () {
196 logger.info('Pool requests deactivated.')
197 clearInterval(timer)
198 }
199
200 poolRequests.forceSend = function () {
201 logger.info('Force pool requests sending.')
202 makePoolRequests()
203 }
204
205 module.exports = poolRequests
206})()
diff --git a/src/utils.js b/src/utils.js
deleted file mode 100644
index 176648a31..000000000
--- a/src/utils.js
+++ /dev/null
@@ -1,202 +0,0 @@
1;(function () {
2 'use strict'
3
4 var async = require('async')
5 var config = require('config')
6 var crypto = require('crypto')
7 var fs = require('fs')
8 var openssl = require('openssl-wrapper')
9 var request = require('request')
10 var replay = require('request-replay')
11 var ursa = require('ursa')
12
13 var constants = require('./constants')
14 var logger = require('./logger')
15
16 var utils = {}
17
18 var http = config.get('webserver.https') ? 'https' : 'http'
19 var host = config.get('webserver.host')
20 var port = config.get('webserver.port')
21 var algorithm = 'aes-256-ctr'
22
23 // ----------- Private functions ----------
24
25 function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
26 // Append the signature
27 if (signature) {
28 params.json.signature = {
29 url: from_url,
30 signature: signature
31 }
32 }
33
34 logger.debug('Make retry requests to %s.', to_pod.url)
35
36 replay(
37 request.post(params, function (err, response, body) {
38 callbackEach(err, response, body, params.url, to_pod)
39 }),
40 {
41 retries: constants.REQUEST_RETRIES,
42 factor: 3,
43 maxTimeout: Infinity,
44 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
45 }
46 ).on('replay', function (replay) {
47 logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
48 params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
49 })
50 }
51
52 // ----------- Public attributes ----------
53 utils.certDir = __dirname + '/../' + config.get('storage.certs')
54
55 // { path, data }
56 utils.makeMultipleRetryRequest = function (all_data, pods, callbackEach, callback) {
57 if (!callback) {
58 callback = callbackEach
59 callbackEach = null
60 }
61
62 var url = http + '://' + host + ':' + port
63 var signature
64
65 // Add signature if it is specified in the params
66 if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
67 var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem'))
68 signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex')
69 }
70
71 // Make a request for each pod
72 async.each(pods, function (pod, callback_each_async) {
73 function callbackEachRetryRequest (err, response, body, url, pod) {
74 if (callbackEach !== null) {
75 callbackEach(err, response, body, url, pod, function () {
76 callback_each_async()
77 })
78 } else {
79 callback_each_async()
80 }
81 }
82
83 var params = {
84 url: pod.url + all_data.path,
85 method: all_data.method
86 }
87
88 // Add data with POST requst ?
89 if (all_data.method === 'POST' && all_data.data) {
90 // Encrypt data ?
91 if (all_data.encrypt === true) {
92 var crt = ursa.createPublicKey(pod.publicKey)
93
94 // TODO: ES6 with let
95 ;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) {
96 utils.symetricEncrypt(JSON.stringify(all_data.data), function (err, dataEncrypted) {
97 if (err) throw err
98
99 var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex')
100 copy_params.json = {
101 data: dataEncrypted.crypted,
102 key: passwordEncrypted
103 }
104
105 makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
106 })
107 })(crt, params, url, pod, signature)
108 } else {
109 params.json = { data: all_data.data }
110 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
111 }
112 } else {
113 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
114 }
115 }, callback)
116 }
117
118 utils.certsExist = function (callback) {
119 fs.exists(utils.certDir + 'peertube.key.pem', function (exists) {
120 return callback(exists)
121 })
122 }
123
124 utils.createCerts = function (callback) {
125 utils.certsExist(function (exist) {
126 if (exist === true) {
127 var string = 'Certs already exist.'
128 logger.warning(string)
129 return callback(new Error(string))
130 }
131
132 logger.info('Generating a RSA key...')
133 openssl.exec('genrsa', { 'out': utils.certDir + 'peertube.key.pem', '2048': false }, function (err) {
134 if (err) {
135 logger.error('Cannot create private key on this pod.', { error: err })
136 return callback(err)
137 }
138 logger.info('RSA key generated.')
139
140 logger.info('Manage public key...')
141 openssl.exec('rsa', { 'in': utils.certDir + 'peertube.key.pem', 'pubout': true, 'out': utils.certDir + 'peertube.pub' }, function (err) {
142 if (err) {
143 logger.error('Cannot create public key on this pod .', { error: err })
144 return callback(err)
145 }
146
147 logger.info('Public key managed.')
148 return callback(null)
149 })
150 })
151 })
152 }
153
154 utils.createCertsIfNotExist = function (callback) {
155 utils.certsExist(function (exist) {
156 if (exist === true) {
157 return callback(null)
158 }
159
160 utils.createCerts(function (err) {
161 return callback(err)
162 })
163 })
164 }
165
166 utils.generatePassword = function (callback) {
167 crypto.randomBytes(32, function (err, buf) {
168 if (err) {
169 return callback(err)
170 }
171
172 callback(null, buf.toString('utf8'))
173 })
174 }
175
176 utils.symetricEncrypt = function (text, callback) {
177 utils.generatePassword(function (err, password) {
178 if (err) {
179 return callback(err)
180 }
181
182 var cipher = crypto.createCipher(algorithm, password)
183 var crypted = cipher.update(text, 'utf8', 'hex')
184 crypted += cipher.final('hex')
185 callback(null, { crypted: crypted, password: password })
186 })
187 }
188
189 utils.symetricDecrypt = function (text, password) {
190 var decipher = crypto.createDecipher(algorithm, password)
191 var dec = decipher.update(text, 'hex', 'utf8')
192 dec += decipher.final('utf8')
193 return dec
194 }
195
196 utils.cleanForExit = function (webtorrent_process) {
197 logger.info('Gracefully exiting')
198 process.kill(-webtorrent_process.pid)
199 }
200
201 module.exports = utils
202})()
diff --git a/src/videos.js b/src/videos.js
deleted file mode 100644
index 90821fdf6..000000000
--- a/src/videos.js
+++ /dev/null
@@ -1,272 +0,0 @@
1;(function () {
2 'use strict'
3
4 var async = require('async')
5 var config = require('config')
6 var dz = require('dezalgo')
7 var fs = require('fs')
8 var webtorrent = require('./webTorrentNode')
9
10 var logger = require('./logger')
11 var pods = require('./pods')
12 var VideosDB = require('./database').VideosDB
13
14 var videos = {}
15
16 var http = config.get('webserver.https') === true ? 'https' : 'http'
17 var host = config.get('webserver.host')
18 var port = config.get('webserver.port')
19
20 // ----------- Private functions -----------
21 function seedVideo (path, callback) {
22 logger.info('Seeding %s...', path)
23
24 webtorrent.seed(path, function (torrent) {
25 logger.info('%s seeded (%s).', path, torrent.magnetURI)
26
27 return callback(null, torrent)
28 })
29 }
30
31 // ----------- Public attributes ----------
32 videos.uploadDir = __dirname + '/../' + config.get('storage.uploads')
33
34 // ----------- Public functions -----------
35 videos.list = function (callback) {
36 VideosDB.find(function (err, videos_list) {
37 if (err) {
38 logger.error('Cannot get list of the videos.', { error: err })
39 return callback(err)
40 }
41
42 return callback(null, videos_list)
43 })
44 }
45
46 videos.listOwned = function (callback) {
47 // If namePath is not null this is *our* video
48 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
49 if (err) {
50 logger.error('Cannot get list of the videos.', { error: err })
51 return callback(err)
52 }
53
54 return callback(null, videos_list)
55 })
56 }
57
58 videos.add = function (data, callback) {
59 var video_file = data.video
60 var video_data = data.data
61
62 logger.info('Adding %s video.', video_file.path)
63 seedVideo(video_file.path, function (err, torrent) {
64 if (err) {
65 logger.error('Cannot seed this video.', { error: err })
66 return callback(err)
67 }
68
69 var params = {
70 name: video_data.name,
71 namePath: video_file.filename,
72 description: video_data.description,
73 magnetUri: torrent.magnetURI,
74 podUrl: http + '://' + host + ':' + port
75 }
76
77 VideosDB.create(params, function (err, video) {
78 if (err) {
79 logger.error('Cannot insert this video.', { error: err })
80 return callback(err)
81 }
82
83 // Now we'll add the video's meta data to our friends
84 params.namePath = null
85
86 pods.addVideoToFriends(params)
87 callback(null)
88 })
89 })
90 }
91
92 videos.remove = function (id, callback) {
93 // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process
94 function removeTorrent (magnetUri, callback) {
95 try {
96 webtorrent.remove(magnetUri, callback)
97 } catch (err) {
98 logger.warn('Cannot remove the torrent from WebTorrent', { err: err })
99 return callback(null)
100 }
101 }
102
103 VideosDB.findById(id, function (err, video) {
104 if (err || !video) {
105 if (!err) err = new Error('Cannot find this video.')
106 logger.error('Cannot find this video.', { error: err })
107 return callback(err)
108 }
109
110 if (video.namePath === null) {
111 var error_string = 'Cannot remove the video of another pod.'
112 logger.error(error_string)
113 return callback(new Error(error_string))
114 }
115
116 logger.info('Removing %s video', video.name)
117
118 removeTorrent(video.magnetUri, function () {
119 VideosDB.findByIdAndRemove(id, function (err) {
120 if (err) {
121 logger.error('Cannot remove the torrent.', { error: err })
122 return callback(err)
123 }
124
125 fs.unlink(videos.uploadDir + video.namePath, function (err) {
126 if (err) {
127 logger.error('Cannot remove this video file.', { error: err })
128 return callback(err)
129 }
130
131 var params = {
132 name: video.name,
133 magnetUri: video.magnetUri
134 }
135
136 pods.removeVideoToFriends(params)
137 callback(null)
138 })
139 })
140 })
141 })
142 }
143
144 // Use the magnet Uri because the _id field is not the same on different servers
145 videos.removeRemotes = function (fromUrl, magnetUris, callback) {
146 if (callback === undefined) callback = function () {}
147
148 VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) {
149 if (err || !videos) {
150 logger.error('Cannot find the torrent URI of these remote videos.')
151 return callback(err)
152 }
153
154 var to_remove = []
155 async.each(videos, function (video, callback_async) {
156 callback_async = dz(callback_async)
157
158 if (video.podUrl !== fromUrl) {
159 logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl)
160 } else {
161 to_remove.push(video._id)
162 }
163
164 callback_async()
165 }, function () {
166 VideosDB.remove({ _id: { $in: to_remove } }, function (err) {
167 if (err) {
168 logger.error('Cannot remove the remote videos.')
169 return callback(err)
170 }
171
172 logger.info('Removed remote videos from %s.', fromUrl)
173 callback(null)
174 })
175 })
176 })
177 }
178
179 videos.removeAllRemotes = function (callback) {
180 VideosDB.remove({ namePath: null }, function (err) {
181 if (err) return callback(err)
182
183 callback(null)
184 })
185 }
186
187 videos.removeAllRemotesOf = function (fromUrl, callback) {
188 VideosDB.remove({ podUrl: fromUrl }, function (err) {
189 if (err) return callback(err)
190
191 callback(null)
192 })
193 }
194
195 // { name, magnetUri, podUrl }
196 // TODO: avoid doublons
197 videos.addRemotes = function (videos, callback) {
198 if (callback === undefined) callback = function () {}
199
200 var to_add = []
201
202 async.each(videos, function (video, callback_each) {
203 callback_each = dz(callback_each)
204 logger.debug('Add remote video from pod: %s', video.podUrl)
205
206 var params = {
207 name: video.name,
208 namePath: null,
209 description: video.description,
210 magnetUri: video.magnetUri,
211 podUrl: video.podUrl
212 }
213
214 to_add.push(params)
215
216 callback_each()
217 }, function () {
218 VideosDB.create(to_add, function (err, videos) {
219 if (err) {
220 logger.error('Cannot insert this remote video.', { error: err })
221 return callback(err)
222 }
223
224 return callback(null, videos)
225 })
226 })
227 }
228
229 videos.get = function (id, callback) {
230 VideosDB.findById(id, function (err, video) {
231 if (err) {
232 logger.error('Cannot get this video.', { error: err })
233 return callback(err)
234 }
235
236 return callback(null, video)
237 })
238 }
239
240 videos.search = function (name, callback) {
241 VideosDB.find({ name: new RegExp(name) }, function (err, videos) {
242 if (err) {
243 logger.error('Cannot search the videos.', { error: err })
244 return callback(err)
245 }
246
247 return callback(null, videos)
248 })
249 }
250
251 videos.seedAll = function (callback) {
252 VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) {
253 if (err) {
254 logger.error('Cannot get list of the videos to seed.', { error: err })
255 return callback(err)
256 }
257
258 async.each(videos_list, function (video, each_callback) {
259 seedVideo(videos.uploadDir + video.namePath, function (err) {
260 if (err) {
261 logger.error('Cannot seed this video.', { error: err })
262 return callback(err)
263 }
264
265 each_callback(null)
266 })
267 }, callback)
268 })
269 }
270
271 module.exports = videos
272})()
diff --git a/src/webTorrentNode.js b/src/webTorrentNode.js
deleted file mode 100644
index d6801d0fb..000000000
--- a/src/webTorrentNode.js
+++ /dev/null
@@ -1,160 +0,0 @@
1;(function () {
2 'use strict'
3
4 var config = require('config')
5 var ipc = require('node-ipc')
6 var pathUtils = require('path')
7 var spawn = require('electron-spawn')
8
9 var logger = require('./logger')
10
11 var host = config.get('webserver.host')
12 var port = config.get('webserver.port')
13
14 var nodeKey = 'webtorrentnode' + port
15 var processKey = 'webtorrent' + port
16
17 ipc.config.silent = true
18 ipc.config.id = nodeKey
19
20 var webtorrentnode = {}
21
22 // Useful for beautiful tests
23 webtorrentnode.silent = false
24
25 // Useful to kill it
26 webtorrentnode.app = null
27
28 webtorrentnode.create = function (options, callback) {
29 if (typeof options === 'function') {
30 callback = options
31 options = {}
32 }
33
34 // Override options
35 if (options.host) host = options.host
36 if (options.port) {
37 port = options.port
38 nodeKey = 'webtorrentnode' + port
39 processKey = 'webtorrent' + port
40 ipc.config.id = nodeKey
41 }
42
43 ipc.serve(function () {
44 if (!webtorrentnode.silent) logger.info('IPC server ready.')
45
46 // Run a timeout of 30s after which we exit the process
47 var timeout_webtorrent_process = setTimeout(function () {
48 logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.')
49 process.exit()
50 }, 30000)
51
52 ipc.server.on(processKey + '.ready', function () {
53 if (!webtorrentnode.silent) logger.info('Webtorrent process ready.')
54 clearTimeout(timeout_webtorrent_process)
55 callback()
56 })
57
58 ipc.server.on(processKey + '.exception', function (data) {
59 logger.error('Received exception error from webtorrent process.', { exception: data.exception })
60 process.exit()
61 })
62
63 var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true })
64 webtorrent_process.stderr.on('data', function (data) {
65 // logger.debug('Webtorrent process stderr: ', data.toString())
66 })
67
68 webtorrent_process.stdout.on('data', function (data) {
69 // logger.debug('Webtorrent process:', data.toString())
70 })
71
72 webtorrentnode.app = webtorrent_process
73 })
74
75 ipc.server.start()
76 }
77
78 webtorrentnode.seed = function (path, callback) {
79 var extension = pathUtils.extname(path)
80 var basename = pathUtils.basename(path, extension)
81 var data = {
82 _id: basename,
83 args: {
84 path: path
85 }
86 }
87
88 if (!webtorrentnode.silent) logger.debug('Node wants to seed %s.', data._id)
89
90 // Finish signal
91 var event_key = nodeKey + '.seedDone.' + data._id
92 ipc.server.on(event_key, function listener (received) {
93 if (!webtorrentnode.silent) logger.debug('Process seeded torrent %s.', received.magnetUri)
94
95 // This is a fake object, we just use the magnetUri in this project
96 var torrent = {
97 magnetURI: received.magnetUri
98 }
99
100 ipc.server.off(event_key)
101 callback(torrent)
102 })
103
104 ipc.server.broadcast(processKey + '.seed', data)
105 }
106
107 webtorrentnode.add = function (magnetUri, callback) {
108 var data = {
109 _id: magnetUri,
110 args: {
111 magnetUri: magnetUri
112 }
113 }
114
115 if (!webtorrentnode.silent) logger.debug('Node wants to add ' + data._id)
116
117 // Finish signal
118 var event_key = nodeKey + '.addDone.' + data._id
119 ipc.server.on(event_key, function (received) {
120 if (!webtorrentnode.silent) logger.debug('Process added torrent.')
121
122 // This is a fake object, we just use the magnetUri in this project
123 var torrent = {
124 files: received.files
125 }
126
127 ipc.server.off(event_key)
128 callback(torrent)
129 })
130
131 ipc.server.broadcast(processKey + '.add', data)
132 }
133
134 webtorrentnode.remove = function (magnetUri, callback) {
135 var data = {
136 _id: magnetUri,
137 args: {
138 magnetUri: magnetUri
139 }
140 }
141
142 if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding %s.', data._id)
143
144 // Finish signal
145 var event_key = nodeKey + '.removeDone.' + data._id
146 ipc.server.on(event_key, function (received) {
147 if (!webtorrentnode.silent) logger.debug('Process removed torrent %s.', data._id)
148
149 var err = null
150 if (received.err) err = received.err
151
152 ipc.server.off(event_key)
153 callback(err)
154 })
155
156 ipc.server.broadcast(processKey + '.remove', data)
157 }
158
159 module.exports = webtorrentnode
160})()
diff --git a/src/webtorrent.js b/src/webtorrent.js
deleted file mode 100644
index b72bc500d..000000000
--- a/src/webtorrent.js
+++ /dev/null
@@ -1,91 +0,0 @@
1;(function () {
2 'use strict'
3
4 module.exports = function (args) {
5 var WebTorrent = require('webtorrent')
6 var ipc = require('node-ipc')
7
8 if (args.length !== 3) {
9 console.log('Wrong arguments number: ' + args.length + '/3')
10 process.exit(-1)
11 }
12
13 var host = args[1]
14 var port = args[2]
15 var nodeKey = 'webtorrentnode' + port
16 var processKey = 'webtorrent' + port
17
18 ipc.config.silent = true
19 ipc.config.id = processKey
20
21 if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
22 else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
23 var wt = new WebTorrent({ dht: false })
24
25 function seed (data) {
26 var args = data.args
27 var path = args.path
28 var _id = data._id
29
30 wt.seed(path, { announceList: '' }, function (torrent) {
31 var to_send = {
32 magnetUri: torrent.magnetURI
33 }
34
35 ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send)
36 })
37 }
38
39 function add (data) {
40 var args = data.args
41 var magnetUri = args.magnetUri
42 var _id = data._id
43
44 wt.add(magnetUri, function (torrent) {
45 var to_send = {
46 files: []
47 }
48
49 torrent.files.forEach(function (file) {
50 to_send.files.push({ path: file.path })
51 })
52
53 ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send)
54 })
55 }
56
57 function remove (data) {
58 var args = data.args
59 var magnetUri = args.magnetUri
60 var _id = data._id
61
62 try {
63 wt.remove(magnetUri, callback)
64 } catch (err) {
65 console.log('Cannot remove the torrent from WebTorrent', { err: err })
66 return callback(null)
67 }
68
69 function callback () {
70 var to_send = {}
71 ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send)
72 }
73 }
74
75 console.log('Configuration: ' + host + ':' + port)
76 console.log('Connecting to IPC...')
77
78 ipc.connectTo(nodeKey, function () {
79 ipc.of[nodeKey].on(processKey + '.seed', seed)
80 ipc.of[nodeKey].on(processKey + '.add', add)
81 ipc.of[nodeKey].on(processKey + '.remove', remove)
82
83 ipc.of[nodeKey].emit(processKey + '.ready')
84 console.log('Ready.')
85 })
86
87 process.on('uncaughtException', function (e) {
88 ipc.of[nodeKey].emit(processKey + '.exception', { exception: e })
89 })
90 }
91})()