diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2015-11-16 13:54:00 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2015-11-16 13:54:00 +0100 |
commit | a18603803ae37d7db8ac116113ede0bd979eddcd (patch) | |
tree | 50c6a00159b0a34cdd2568753c214babf48ba2ba | |
parent | 16357143aeef68768664c8961db686e65b2350e2 (diff) | |
download | PeerTube-a18603803ae37d7db8ac116113ede0bd979eddcd.tar.gz PeerTube-a18603803ae37d7db8ac116113ede0bd979eddcd.tar.zst PeerTube-a18603803ae37d7db8ac116113ede0bd979eddcd.zip |
Source refractoring
-rw-r--r-- | server.js | 22 | ||||
-rw-r--r-- | src/checker.js | 2 | ||||
-rw-r--r-- | src/logger.js | 3 | ||||
-rw-r--r-- | src/pods.js | 154 | ||||
-rw-r--r-- | src/utils.js | 39 | ||||
-rw-r--r-- | src/videos.js | 35 | ||||
-rw-r--r-- | src/webTorrentNode.js | 12 |
7 files changed, 152 insertions, 115 deletions
@@ -1,20 +1,22 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | // ----------- Constantes ----------- | 4 | // ----------- Constants ----------- |
5 | global.API_VERSION = 'v1' | 5 | global.API_VERSION = 'v1' |
6 | 6 | ||
7 | // ----------- Node modules ----------- | 7 | // ----------- Node modules ----------- |
8 | var bodyParser = require('body-parser') | ||
8 | var express = require('express') | 9 | var express = require('express') |
9 | var expressValidator = require('express-validator') | 10 | var expressValidator = require('express-validator') |
10 | var path = require('path') | 11 | var http = require('http') |
11 | var morgan = require('morgan') | 12 | var morgan = require('morgan') |
12 | var bodyParser = require('body-parser') | ||
13 | var multer = require('multer') | 13 | var multer = require('multer') |
14 | var path = require('path') | ||
14 | var TrackerServer = require('bittorrent-tracker').Server | 15 | var TrackerServer = require('bittorrent-tracker').Server |
15 | var WebSocketServer = require('ws').Server | 16 | var WebSocketServer = require('ws').Server |
17 | |||
18 | // Create our main app | ||
16 | var app = express() | 19 | var app = express() |
17 | var http = require('http') | ||
18 | 20 | ||
19 | // ----------- Checker ----------- | 21 | // ----------- Checker ----------- |
20 | var checker = require('./src/checker') | 22 | var checker = require('./src/checker') |
@@ -26,7 +28,7 @@ | |||
26 | process.exit(0) | 28 | process.exit(0) |
27 | } | 29 | } |
28 | 30 | ||
29 | checker.createDirectories() | 31 | checker.createDirectoriesIfNotExist() |
30 | 32 | ||
31 | // ----------- PeerTube modules ----------- | 33 | // ----------- PeerTube modules ----------- |
32 | var config = require('config') | 34 | var config = require('config') |
@@ -36,16 +38,22 @@ | |||
36 | var videos = require('./src/videos') | 38 | var videos = require('./src/videos') |
37 | var webtorrent = require('./src/webTorrentNode') | 39 | var webtorrent = require('./src/webTorrentNode') |
38 | 40 | ||
41 | // Get configurations | ||
39 | var port = config.get('listen.port') | 42 | var port = config.get('listen.port') |
40 | var uploads = config.get('storage.uploads') | 43 | var uploads = config.get('storage.uploads') |
41 | 44 | ||
42 | // ----------- Command line ----------- | 45 | // ----------- Command line ----------- |
43 | 46 | ||
44 | // ----------- App ----------- | 47 | // ----------- App ----------- |
48 | |||
49 | // For the logger | ||
45 | app.use(morgan('combined', { stream: logger.stream })) | 50 | app.use(morgan('combined', { stream: logger.stream })) |
51 | // For body requests | ||
46 | app.use(bodyParser.json()) | 52 | app.use(bodyParser.json()) |
53 | // For POST file requests | ||
47 | app.use(multer({ dest: uploads })) | 54 | app.use(multer({ dest: uploads })) |
48 | app.use(bodyParser.urlencoded({ extended: false })) | 55 | app.use(bodyParser.urlencoded({ extended: false })) |
56 | // Validate some params for the API | ||
49 | app.use(expressValidator()) | 57 | app.use(expressValidator()) |
50 | 58 | ||
51 | // ----------- Views, routes and static files ----------- | 59 | // ----------- Views, routes and static files ----------- |
@@ -55,15 +63,17 @@ | |||
55 | port: 35729 | 63 | port: 35729 |
56 | })) | 64 | })) |
57 | 65 | ||
66 | // Catch sefaults | ||
58 | require('segfault-handler').registerHandler() | 67 | require('segfault-handler').registerHandler() |
59 | 68 | ||
69 | // Static files | ||
60 | app.use(express.static(path.join(__dirname, '/public'), { maxAge: 0 })) | 70 | app.use(express.static(path.join(__dirname, '/public'), { maxAge: 0 })) |
61 | 71 | ||
62 | // Jade template from ./views directory | 72 | // Jade template from ./views directory |
63 | app.set('views', path.join(__dirname, '/views')) | 73 | app.set('views', path.join(__dirname, '/views')) |
64 | app.set('view engine', 'jade') | 74 | app.set('view engine', 'jade') |
65 | 75 | ||
66 | // API | 76 | // API routes |
67 | var api_route = '/api/' + global.API_VERSION | 77 | var api_route = '/api/' + global.API_VERSION |
68 | app.use(api_route, routes.api) | 78 | app.use(api_route, routes.api) |
69 | 79 | ||
diff --git a/src/checker.js b/src/checker.js index fd828ca8c..7a3a53616 100644 --- a/src/checker.js +++ b/src/checker.js | |||
@@ -25,7 +25,7 @@ | |||
25 | } | 25 | } |
26 | 26 | ||
27 | // Create directories for the storage if it doesn't exist | 27 | // Create directories for the storage if it doesn't exist |
28 | checker.createDirectories = function () { | 28 | checker.createDirectoriesIfNotExist = function () { |
29 | var storages = config.get('storage') | 29 | var storages = config.get('storage') |
30 | 30 | ||
31 | for (var key of Object.keys(storages)) { | 31 | for (var key of Object.keys(storages)) { |
diff --git a/src/logger.js b/src/logger.js index b1384e8b9..850af10cb 100644 --- a/src/logger.js +++ b/src/logger.js | |||
@@ -1,10 +1,9 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ | 2 | // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ |
3 | |||
4 | 'use strict' | 3 | 'use strict' |
5 | 4 | ||
6 | var winston = require('winston') | ||
7 | var config = require('config') | 5 | var config = require('config') |
6 | var winston = require('winston') | ||
8 | 7 | ||
9 | var logDir = __dirname + '/../' + config.get('storage.logs') | 8 | var logDir = __dirname + '/../' + config.get('storage.logs') |
10 | 9 | ||
diff --git a/src/pods.js b/src/pods.js index db159a466..b4325ebcf 100644 --- a/src/pods.js +++ b/src/pods.js | |||
@@ -1,16 +1,17 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | var fs = require('fs') | ||
5 | var config = require('config') | ||
6 | var async = require('async') | 4 | var async = require('async') |
5 | var config = require('config') | ||
6 | var fs = require('fs') | ||
7 | var request = require('request') | 7 | var request = require('request') |
8 | 8 | ||
9 | var logger = require('./logger') | 9 | var logger = require('./logger') |
10 | var utils = require('./utils') | ||
11 | var PodsDB = require('./database').PodsDB | 10 | var PodsDB = require('./database').PodsDB |
11 | var utils = require('./utils') | ||
12 | 12 | ||
13 | var pods = {} | 13 | var pods = {} |
14 | |||
14 | var http = config.get('webserver.https') ? 'https' : 'http' | 15 | var http = config.get('webserver.https') ? 'https' : 'http' |
15 | var host = config.get('webserver.host') | 16 | var host = config.get('webserver.host') |
16 | var port = config.get('webserver.port') | 17 | var port = config.get('webserver.port') |
@@ -27,6 +28,7 @@ | |||
27 | } | 28 | } |
28 | 29 | ||
29 | // ----------- Public functions ----------- | 30 | // ----------- Public functions ----------- |
31 | |||
30 | pods.list = function (callback) { | 32 | pods.list = function (callback) { |
31 | PodsDB.find(function (err, pods_list) { | 33 | PodsDB.find(function (err, pods_list) { |
32 | if (err) { | 34 | if (err) { |
@@ -73,18 +75,27 @@ | |||
73 | } | 75 | } |
74 | 76 | ||
75 | logger.debug('Make multiple requests.') | 77 | logger.debug('Make multiple requests.') |
78 | |||
79 | var params = { | ||
80 | encrypt: true, | ||
81 | sign: true, | ||
82 | method: data.method, | ||
83 | path: data.path, | ||
84 | data: data.data | ||
85 | } | ||
86 | |||
76 | utils.makeMultipleRetryRequest( | 87 | utils.makeMultipleRetryRequest( |
77 | { encrypt: true, sign: true, method: data.method, path: data.path, data: data.data }, | 88 | params, |
78 | 89 | ||
79 | urls, | 90 | urls, |
80 | 91 | ||
81 | function (err, response, body, url) { | 92 | function callbackEachPodFinished (err, response, body, url) { |
82 | if (err || response.statusCode !== 200) { | 93 | if (err || response.statusCode !== 200) { |
83 | logger.error('Error sending secure request to %s/%s pod.', url, data.path, { error: err }) | 94 | logger.error('Error sending secure request to %s/%s pod.', url, data.path, { error: err }) |
84 | } | 95 | } |
85 | }, | 96 | }, |
86 | 97 | ||
87 | function (err) { | 98 | function callbackAllPodsFinished (err) { |
88 | if (err) { | 99 | if (err) { |
89 | logger.error('There was some errors when sending the video meta data.', { error: err }) | 100 | logger.error('There was some errors when sending the video meta data.', { error: err }) |
90 | return callback(err) | 101 | return callback(err) |
@@ -98,7 +109,9 @@ | |||
98 | } | 109 | } |
99 | 110 | ||
100 | pods.makeFriends = function (callback) { | 111 | pods.makeFriends = function (callback) { |
101 | logger.debug('Read public key...') | 112 | var pods_score = {} |
113 | |||
114 | logger.info('Make friends!') | ||
102 | fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) { | 115 | fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) { |
103 | if (err) { | 116 | if (err) { |
104 | logger.error('Cannot read public cert.', { error: err }) | 117 | logger.error('Cannot read public cert.', { error: err }) |
@@ -106,71 +119,86 @@ | |||
106 | } | 119 | } |
107 | 120 | ||
108 | var urls = config.get('network.friends') | 121 | var urls = config.get('network.friends') |
109 | var pods_score = {} | ||
110 | 122 | ||
111 | async.each(urls, function (url, callback) { | 123 | async.each(urls, computeForeignPodsList, function () { |
112 | // Always add a trust pod | 124 | logger.debug('Pods scores computed.', { pods_score: pods_score }) |
113 | pods_score[url] = Infinity | 125 | var pods_list = computeWinningPods(urls, pods_score) |
126 | logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) | ||
127 | |||
128 | logger.debug('Make requests...') | ||
129 | makeRequestsToWinningPods(cert, pods_list) | ||
130 | }) | ||
131 | }) | ||
132 | |||
133 | // ----------------------------------------------------------------------- | ||
114 | 134 | ||
115 | getForeignPodsList(url, function (foreign_pods_list) { | 135 | function computeForeignPodsList (url, callback) { |
116 | if (foreign_pods_list.length === 0) return callback() | 136 | // Always add a trust pod |
137 | pods_score[url] = Infinity | ||
117 | 138 | ||
118 | async.each(foreign_pods_list, function (foreign_pod, callback) { | 139 | getForeignPodsList(url, function (foreign_pods_list) { |
119 | var foreign_url = foreign_pod.url | 140 | if (foreign_pods_list.length === 0) return callback() |
120 | if (pods_score[foreign_url]) pods_score[foreign_url]++ | 141 | |
121 | else pods_score[foreign_url] = 1 | 142 | async.each(foreign_pods_list, function (foreign_pod, callback_each) { |
122 | callback() | 143 | var foreign_url = foreign_pod.url |
123 | }, callback) | 144 | |
124 | }) | 145 | if (pods_score[foreign_url]) pods_score[foreign_url]++ |
125 | }, function () { | 146 | else pods_score[foreign_url] = 1 |
126 | logger.debug('Pods score', { pods_score: pods_score }) | 147 | |
127 | 148 | callback_each() | |
128 | // Build the list of pods to add | 149 | }, function () { |
129 | // Only add a pod if it exists in more than a half base pods | 150 | callback() |
130 | var pods_list = [] | ||
131 | var base_score = urls.length / 2 | ||
132 | Object.keys(pods_score).forEach(function (pod) { | ||
133 | if (pods_score[pod] > base_score) pods_list.push({ url: pod }) | ||
134 | }) | 151 | }) |
152 | }) | ||
153 | } | ||
135 | 154 | ||
136 | logger.debug('Pods that we keep', { pods: pods_list }) | 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 | }) | ||
137 | 163 | ||
138 | var data = { | 164 | return pods_list |
139 | url: http + '://' + host + ':' + port, | 165 | } |
140 | publicKey: cert | ||
141 | } | ||
142 | 166 | ||
143 | logger.debug('Make requests...') | 167 | function makeRequestsToWinningPods (cert, pods_list) { |
168 | var data = { | ||
169 | url: http + '://' + host + ':' + port, | ||
170 | publicKey: cert | ||
171 | } | ||
144 | 172 | ||
145 | utils.makeMultipleRetryRequest( | 173 | utils.makeMultipleRetryRequest( |
146 | { method: 'POST', path: '/api/' + global.API_VERSION + '/pods/', data: data }, | 174 | { method: 'POST', path: '/api/' + global.API_VERSION + '/pods/', data: data }, |
147 | 175 | ||
148 | pods_list, | 176 | pods_list, |
149 | 177 | ||
150 | function eachRequest (err, response, body, url) { | 178 | function eachRequest (err, response, body, url) { |
151 | if (!err && response.statusCode === 200) { | 179 | // We add the pod if it responded correctly with its public certificate |
152 | pods.add({ url: url, publicKey: body.cert }, function (err) { | 180 | if (!err && response.statusCode === 200) { |
153 | if (err) { | 181 | pods.add({ url: url, publicKey: body.cert }, function (err) { |
154 | logger.error('Error with adding %s pod.', url, { error: err }) | 182 | if (err) { |
155 | } | 183 | logger.error('Error with adding %s pod.', url, { error: err }) |
156 | }) | 184 | } |
157 | } else { | 185 | }) |
158 | logger.error('Error with adding %s pod.', url) | 186 | } else { |
159 | } | 187 | logger.error('Error with adding %s pod.', url, { error: err || new Error('Status not 200') }) |
160 | }, | ||
161 | |||
162 | function endRequests (err) { | ||
163 | if (err) { | ||
164 | logger.error('There was some errors when we wanted to make friends.', { error: err }) | ||
165 | return callback(err) | ||
166 | } | ||
167 | |||
168 | logger.debug('Finished') | ||
169 | callback(null) | ||
170 | } | 188 | } |
171 | ) | 189 | }, |
172 | }) | 190 | |
173 | }) | 191 | function endRequests (err) { |
192 | if (err) { | ||
193 | logger.error('There was some errors when we wanted to make friends.', { error: err }) | ||
194 | return callback(err) | ||
195 | } | ||
196 | |||
197 | logger.debug('makeRequestsToWinningPods finished.') | ||
198 | return callback(null) | ||
199 | } | ||
200 | ) | ||
201 | } | ||
174 | } | 202 | } |
175 | 203 | ||
176 | module.exports = pods | 204 | module.exports = pods |
diff --git a/src/utils.js b/src/utils.js index 8ce1789f9..d6b26db4b 100644 --- a/src/utils.js +++ b/src/utils.js | |||
@@ -1,23 +1,23 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | var request = require('request') | ||
5 | var replay = require('request-replay') | ||
6 | var ursa = require('ursa') | ||
7 | var config = require('config') | 4 | var config = require('config') |
5 | var crypto = require('crypto') | ||
8 | var fs = require('fs') | 6 | var fs = require('fs') |
9 | var openssl = require('openssl-wrapper') | 7 | var openssl = require('openssl-wrapper') |
10 | var crypto = require('crypto') | 8 | var request = require('request') |
9 | var replay = require('request-replay') | ||
10 | var ursa = require('ursa') | ||
11 | 11 | ||
12 | var logger = require('./logger') | 12 | var logger = require('./logger') |
13 | 13 | ||
14 | var utils = {} | ||
15 | |||
14 | var http = config.get('webserver.https') ? 'https' : 'http' | 16 | var http = config.get('webserver.https') ? 'https' : 'http' |
15 | var host = config.get('webserver.host') | 17 | var host = config.get('webserver.host') |
16 | var port = config.get('webserver.port') | 18 | var port = config.get('webserver.port') |
17 | var algorithm = 'aes-256-ctr' | 19 | var algorithm = 'aes-256-ctr' |
18 | 20 | ||
19 | var utils = {} | ||
20 | |||
21 | // ----------- Private functions ---------- | 21 | // ----------- Private functions ---------- |
22 | 22 | ||
23 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { | 23 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { |
@@ -29,7 +29,7 @@ | |||
29 | } | 29 | } |
30 | } | 30 | } |
31 | 31 | ||
32 | logger.debug('Sending informations to %s', to_pod.url, { params: params }) | 32 | logger.debug('Sending informations to %s.', to_pod.url, { params: params }) |
33 | 33 | ||
34 | // Replay 15 times, with factor 3 | 34 | // Replay 15 times, with factor 3 |
35 | replay( | 35 | replay( |
@@ -52,7 +52,7 @@ | |||
52 | utils.certDir = __dirname + '/../' + config.get('storage.certs') | 52 | utils.certDir = __dirname + '/../' + config.get('storage.certs') |
53 | 53 | ||
54 | // { path, data } | 54 | // { path, data } |
55 | utils.makeMultipleRetryRequest = function (all, pods, callbackEach, callback) { | 55 | utils.makeMultipleRetryRequest = function (all_data, pods, callbackEach, callback) { |
56 | if (!callback) { | 56 | if (!callback) { |
57 | callback = callbackEach | 57 | callback = callbackEach |
58 | callbackEach = function () {} | 58 | callbackEach = function () {} |
@@ -61,8 +61,8 @@ | |||
61 | var url = http + '://' + host + ':' + port | 61 | var url = http + '://' + host + ':' + port |
62 | var signature | 62 | var signature |
63 | 63 | ||
64 | // Signature ? | 64 | // Add signature if it is specified in the params |
65 | if (all.method === 'POST' && all.data && all.sign === true) { | 65 | if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { |
66 | var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem')) | 66 | var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem')) |
67 | signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex') | 67 | signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex') |
68 | } | 68 | } |
@@ -70,22 +70,21 @@ | |||
70 | // Make a request for each pod | 70 | // Make a request for each pod |
71 | for (var pod of pods) { | 71 | for (var pod of pods) { |
72 | var params = { | 72 | var params = { |
73 | url: pod.url + all.path, | 73 | url: pod.url + all_data.path, |
74 | method: all.method | 74 | method: all_data.method |
75 | } | 75 | } |
76 | 76 | ||
77 | // Add data with POST requst ? | 77 | // Add data with POST requst ? |
78 | if (all.method === 'POST' && all.data) { | 78 | if (all_data.method === 'POST' && all_data.data) { |
79 | logger.debug('Make a POST request.') | 79 | logger.debug('Make a POST request.') |
80 | 80 | ||
81 | // Encrypt data ? | 81 | // Encrypt data ? |
82 | if (all.encrypt === true) { | 82 | if (all_data.encrypt === true) { |
83 | logger.debug(pod.publicKey) | ||
84 | var crt = ursa.createPublicKey(pod.publicKey) | 83 | var crt = ursa.createPublicKey(pod.publicKey) |
85 | 84 | ||
86 | // TODO: ES6 with let | 85 | // TODO: ES6 with let |
87 | ;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) { | 86 | ;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) { |
88 | utils.symetricEncrypt(JSON.stringify(all.data), function (err, dataEncrypted) { | 87 | utils.symetricEncrypt(JSON.stringify(all_data.data), function (err, dataEncrypted) { |
89 | if (err) throw err | 88 | if (err) throw err |
90 | 89 | ||
91 | var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex') | 90 | var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex') |
@@ -98,7 +97,7 @@ | |||
98 | }) | 97 | }) |
99 | })(crt, params, url, pod, signature) | 98 | })(crt, params, url, pod, signature) |
100 | } else { | 99 | } else { |
101 | params.json = { data: all.data } | 100 | params.json = { data: all_data.data } |
102 | makeRetryRequest(params, url, pod, signature, callbackEach) | 101 | makeRetryRequest(params, url, pod, signature, callbackEach) |
103 | } | 102 | } |
104 | } else { | 103 | } else { |
@@ -124,20 +123,22 @@ | |||
124 | return callback(new Error(string)) | 123 | return callback(new Error(string)) |
125 | } | 124 | } |
126 | 125 | ||
127 | logger.debug('Gen RSA keys...') | 126 | logger.info('Generating a RSA key...') |
128 | openssl.exec('genrsa', { 'out': utils.certDir + 'peertube.key.pem', '2048': false }, function (err) { | 127 | openssl.exec('genrsa', { 'out': utils.certDir + 'peertube.key.pem', '2048': false }, function (err) { |
129 | if (err) { | 128 | if (err) { |
130 | logger.error('Cannot create private key on this pod.', { error: err }) | 129 | logger.error('Cannot create private key on this pod.', { error: err }) |
131 | return callback(err) | 130 | return callback(err) |
132 | } | 131 | } |
132 | logger.info('RSA key generated.') | ||
133 | 133 | ||
134 | logger.debug('Manage public key...') | 134 | logger.info('Manage public key...') |
135 | openssl.exec('rsa', { 'in': utils.certDir + 'peertube.key.pem', 'pubout': true, 'out': utils.certDir + 'peertube.pub' }, function (err) { | 135 | openssl.exec('rsa', { 'in': utils.certDir + 'peertube.key.pem', 'pubout': true, 'out': utils.certDir + 'peertube.pub' }, function (err) { |
136 | if (err) { | 136 | if (err) { |
137 | logger.error('Cannot create public key on this pod .', { error: err }) | 137 | logger.error('Cannot create public key on this pod .', { error: err }) |
138 | return callback(err) | 138 | return callback(err) |
139 | } | 139 | } |
140 | 140 | ||
141 | logger.info('Public key managed.') | ||
141 | return callback(null) | 142 | return callback(null) |
142 | }) | 143 | }) |
143 | }) | 144 | }) |
diff --git a/src/videos.js b/src/videos.js index f787ae49c..b95219c39 100644 --- a/src/videos.js +++ b/src/videos.js | |||
@@ -1,27 +1,27 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | var async = require('async') | ||
5 | var config = require('config') | ||
4 | var fs = require('fs') | 6 | var fs = require('fs') |
5 | var webtorrent = require('./webTorrentNode') | 7 | var webtorrent = require('./webTorrentNode') |
6 | var config = require('config') | ||
7 | var async = require('async') | ||
8 | 8 | ||
9 | var logger = require('./logger') | 9 | var logger = require('./logger') |
10 | var VideosDB = require('./database').VideosDB | ||
11 | var pods = require('./pods') | 10 | var pods = require('./pods') |
11 | var VideosDB = require('./database').VideosDB | ||
12 | 12 | ||
13 | var videos = {} | 13 | var videos = {} |
14 | // Public url | 14 | |
15 | var http = config.get('webserver.https') === true ? 'https' : 'http' | 15 | var http = config.get('webserver.https') === true ? 'https' : 'http' |
16 | var host = config.get('webserver.host') | 16 | var host = config.get('webserver.host') |
17 | var port = config.get('webserver.port') | 17 | var port = config.get('webserver.port') |
18 | 18 | ||
19 | // ----------- Private functions ----------- | 19 | // ----------- Private functions ----------- |
20 | function seedVideo (path, callback) { | 20 | function seedVideo (path, callback) { |
21 | logger.debug('Seeding : %s', path) | 21 | logger.info('Seeding %s...', path) |
22 | 22 | ||
23 | webtorrent.seed(path, function (torrent) { | 23 | webtorrent.seed(path, function (torrent) { |
24 | logger.debug('Seeded : %s', torrent.magnetURI) | 24 | logger.info('%s seeded (%s).', path, torrent.magnetURI) |
25 | 25 | ||
26 | return callback(null, torrent) | 26 | return callback(null, torrent) |
27 | }) | 27 | }) |
@@ -46,7 +46,7 @@ | |||
46 | var video_file = data.video | 46 | var video_file = data.video |
47 | var video_data = data.data | 47 | var video_data = data.data |
48 | 48 | ||
49 | logger.debug('Path: %s', video_file.path) | 49 | logger.info('Adding %s video.', video_file.path) |
50 | seedVideo(video_file.path, function (err, torrent) { | 50 | seedVideo(video_file.path, function (err, torrent) { |
51 | if (err) { | 51 | if (err) { |
52 | logger.error('Cannot seed this video.', { error: err }) | 52 | logger.error('Cannot seed this video.', { error: err }) |
@@ -70,7 +70,7 @@ | |||
70 | // Now we'll send the video's meta data | 70 | // Now we'll send the video's meta data |
71 | params.namePath = null | 71 | params.namePath = null |
72 | 72 | ||
73 | logger.debug('Sending this video Uri to friends...') | 73 | logger.info('Sending %s video to friends.', video_file.path) |
74 | 74 | ||
75 | var data = { | 75 | var data = { |
76 | path: '/api/' + global.API_VERSION + '/remotevideos/add', | 76 | path: '/api/' + global.API_VERSION + '/remotevideos/add', |
@@ -91,7 +91,7 @@ | |||
91 | } | 91 | } |
92 | 92 | ||
93 | videos.remove = function (id, callback) { | 93 | videos.remove = function (id, callback) { |
94 | // Maybe the torrent is not seeding, it doesn't have importance | 94 | // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process |
95 | function removeTorrent (magnetUri, callback) { | 95 | function removeTorrent (magnetUri, callback) { |
96 | try { | 96 | try { |
97 | webtorrent.remove(magnetUri, callback) | 97 | webtorrent.remove(magnetUri, callback) |
@@ -114,7 +114,7 @@ | |||
114 | return callback(new Error(error_string)) | 114 | return callback(new Error(error_string)) |
115 | } | 115 | } |
116 | 116 | ||
117 | logger.debug('Removing video %s', video.magnetUri) | 117 | logger.info('Removing %s video', video.name) |
118 | 118 | ||
119 | removeTorrent(video.magnetUri, function () { | 119 | removeTorrent(video.magnetUri, function () { |
120 | VideosDB.findByIdAndRemove(id, function (err) { | 120 | VideosDB.findByIdAndRemove(id, function (err) { |
@@ -154,16 +154,15 @@ | |||
154 | 154 | ||
155 | // Use the magnet Uri because the _id field is not the same on different servers | 155 | // Use the magnet Uri because the _id field is not the same on different servers |
156 | videos.removeRemote = function (fromUrl, magnetUri, callback) { | 156 | videos.removeRemote = function (fromUrl, magnetUri, callback) { |
157 | // TODO : check if the remote server has the rights to remove this video | ||
158 | |||
159 | VideosDB.findOne({ magnetUri: magnetUri }, function (err, video) { | 157 | VideosDB.findOne({ magnetUri: magnetUri }, function (err, video) { |
160 | if (err || !video) { | 158 | if (err || !video) { |
161 | logger.error('Cannot find the torrent URI of this remote video.') | 159 | logger.error('Cannot find the torrent URI of this remote video.') |
162 | return callback(err) | 160 | return callback(err) |
163 | } | 161 | } |
164 | 162 | ||
163 | // TODO: move to reqValidators middleware ? | ||
165 | if (video.podUrl !== fromUrl) { | 164 | if (video.podUrl !== fromUrl) { |
166 | logger.error('The pod has not rights on this video.') | 165 | logger.error('The pod has not the rights on this video.') |
167 | return callback(err) | 166 | return callback(err) |
168 | } | 167 | } |
169 | 168 | ||
@@ -222,23 +221,23 @@ | |||
222 | }) | 221 | }) |
223 | } | 222 | } |
224 | 223 | ||
225 | videos.seedAll = function (final_callback) { | 224 | videos.seedAll = function (callback) { |
226 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { | 225 | VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { |
227 | if (err) { | 226 | if (err) { |
228 | logger.error('Cannot get list of the videos to seed.', { error: err }) | 227 | logger.error('Cannot get list of the videos to seed.', { error: err }) |
229 | return final_callback(err) | 228 | return callback(err) |
230 | } | 229 | } |
231 | 230 | ||
232 | async.each(videos_list, function (video, callback) { | 231 | async.each(videos_list, function (video, each_callback) { |
233 | seedVideo(videos.uploadDir + video.namePath, function (err) { | 232 | seedVideo(videos.uploadDir + video.namePath, function (err) { |
234 | if (err) { | 233 | if (err) { |
235 | logger.error('Cannot seed this video.', { error: err }) | 234 | logger.error('Cannot seed this video.', { error: err }) |
236 | return callback(err) | 235 | return callback(err) |
237 | } | 236 | } |
238 | 237 | ||
239 | callback(null) | 238 | each_callback(null) |
240 | }) | 239 | }) |
241 | }, final_callback) | 240 | }, callback) |
242 | }) | 241 | }) |
243 | } | 242 | } |
244 | 243 | ||
diff --git a/src/webTorrentNode.js b/src/webTorrentNode.js index bc77872ce..d6801d0fb 100644 --- a/src/webTorrentNode.js +++ b/src/webTorrentNode.js | |||
@@ -1,10 +1,10 @@ | |||
1 | ;(function () { | 1 | ;(function () { |
2 | 'use strict' | 2 | 'use strict' |
3 | 3 | ||
4 | var spawn = require('electron-spawn') | ||
5 | var config = require('config') | 4 | var config = require('config') |
6 | var ipc = require('node-ipc') | 5 | var ipc = require('node-ipc') |
7 | var pathUtils = require('path') | 6 | var pathUtils = require('path') |
7 | var spawn = require('electron-spawn') | ||
8 | 8 | ||
9 | var logger = require('./logger') | 9 | var logger = require('./logger') |
10 | 10 | ||
@@ -85,12 +85,12 @@ | |||
85 | } | 85 | } |
86 | } | 86 | } |
87 | 87 | ||
88 | if (!webtorrentnode.silent) logger.debug('Node wants to seed ' + data._id) | 88 | if (!webtorrentnode.silent) logger.debug('Node wants to seed %s.', data._id) |
89 | 89 | ||
90 | // Finish signal | 90 | // Finish signal |
91 | var event_key = nodeKey + '.seedDone.' + data._id | 91 | var event_key = nodeKey + '.seedDone.' + data._id |
92 | ipc.server.on(event_key, function listener (received) { | 92 | ipc.server.on(event_key, function listener (received) { |
93 | if (!webtorrentnode.silent) logger.debug('Process seeded torrent ' + received.magnetUri) | 93 | if (!webtorrentnode.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) |
94 | 94 | ||
95 | // This is a fake object, we just use the magnetUri in this project | 95 | // This is a fake object, we just use the magnetUri in this project |
96 | var torrent = { | 96 | var torrent = { |
@@ -117,7 +117,7 @@ | |||
117 | // Finish signal | 117 | // Finish signal |
118 | var event_key = nodeKey + '.addDone.' + data._id | 118 | var event_key = nodeKey + '.addDone.' + data._id |
119 | ipc.server.on(event_key, function (received) { | 119 | ipc.server.on(event_key, function (received) { |
120 | if (!webtorrentnode.silent) logger.debug('Process added torrent') | 120 | if (!webtorrentnode.silent) logger.debug('Process added torrent.') |
121 | 121 | ||
122 | // This is a fake object, we just use the magnetUri in this project | 122 | // This is a fake object, we just use the magnetUri in this project |
123 | var torrent = { | 123 | var torrent = { |
@@ -139,12 +139,12 @@ | |||
139 | } | 139 | } |
140 | } | 140 | } |
141 | 141 | ||
142 | if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding ' + data._id) | 142 | if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding %s.', data._id) |
143 | 143 | ||
144 | // Finish signal | 144 | // Finish signal |
145 | var event_key = nodeKey + '.removeDone.' + data._id | 145 | var event_key = nodeKey + '.removeDone.' + data._id |
146 | ipc.server.on(event_key, function (received) { | 146 | ipc.server.on(event_key, function (received) { |
147 | if (!webtorrentnode.silent) logger.debug('Process removed torrent ' + data._id) | 147 | if (!webtorrentnode.silent) logger.debug('Process removed torrent %s.', data._id) |
148 | 148 | ||
149 | var err = null | 149 | var err = null |
150 | if (received.err) err = received.err | 150 | if (received.err) err = received.err |