diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/api/pods.js | 16 | ||||
-rw-r--r-- | server/controllers/api/remote.js | 12 | ||||
-rw-r--r-- | server/helpers/custom-validators/pods.js | 12 | ||||
-rw-r--r-- | server/helpers/custom-validators/videos.js | 6 | ||||
-rw-r--r-- | server/helpers/requests.js | 8 | ||||
-rw-r--r-- | server/initializers/constants.js | 3 | ||||
-rw-r--r-- | server/lib/friends.js | 59 | ||||
-rw-r--r-- | server/middlewares/pods.js | 50 | ||||
-rw-r--r-- | server/middlewares/secure.js | 14 | ||||
-rw-r--r-- | server/middlewares/validators/pods.js | 4 | ||||
-rw-r--r-- | server/middlewares/validators/remote.js | 4 | ||||
-rw-r--r-- | server/models/pods.js | 17 | ||||
-rw-r--r-- | server/models/request.js | 2 | ||||
-rw-r--r-- | server/models/video.js | 32 |
14 files changed, 110 insertions, 129 deletions
diff --git a/server/controllers/api/pods.js b/server/controllers/api/pods.js index 853e0705b..7857fcee0 100644 --- a/server/controllers/api/pods.js +++ b/server/controllers/api/pods.js | |||
@@ -20,14 +20,14 @@ const Pod = mongoose.model('Pod') | |||
20 | router.get('/', listPods) | 20 | router.get('/', listPods) |
21 | router.post('/', | 21 | router.post('/', |
22 | validators.podsAdd, | 22 | validators.podsAdd, |
23 | podsMiddleware.setBodyUrlPort, | 23 | podsMiddleware.setBodyHostPort, |
24 | addPods | 24 | addPods |
25 | ) | 25 | ) |
26 | router.post('/makefriends', | 26 | router.post('/makefriends', |
27 | oAuth.authenticate, | 27 | oAuth.authenticate, |
28 | admin.ensureIsAdmin, | 28 | admin.ensureIsAdmin, |
29 | validators.makeFriends, | 29 | validators.makeFriends, |
30 | podsMiddleware.setBodyUrlsPort, | 30 | podsMiddleware.setBodyHostsPort, |
31 | makeFriends | 31 | makeFriends |
32 | ) | 32 | ) |
33 | router.get('/quitfriends', | 33 | router.get('/quitfriends', |
@@ -84,17 +84,17 @@ function addPods (req, res, next) { | |||
84 | } | 84 | } |
85 | 85 | ||
86 | function listPods (req, res, next) { | 86 | function listPods (req, res, next) { |
87 | Pod.list(function (err, podsUrlList) { | 87 | Pod.list(function (err, podsList) { |
88 | if (err) return next(err) | 88 | if (err) return next(err) |
89 | 89 | ||
90 | res.json(getFormatedPods(podsUrlList)) | 90 | res.json(getFormatedPods(podsList)) |
91 | }) | 91 | }) |
92 | } | 92 | } |
93 | 93 | ||
94 | function makeFriends (req, res, next) { | 94 | function makeFriends (req, res, next) { |
95 | const urls = req.body.urls | 95 | const hosts = req.body.hosts |
96 | 96 | ||
97 | friends.makeFriends(urls, function (err) { | 97 | friends.makeFriends(hosts, function (err) { |
98 | if (err) { | 98 | if (err) { |
99 | logger.error('Could not make friends.', { error: err }) | 99 | logger.error('Could not make friends.', { error: err }) |
100 | return | 100 | return |
@@ -107,11 +107,11 @@ function makeFriends (req, res, next) { | |||
107 | } | 107 | } |
108 | 108 | ||
109 | function removePods (req, res, next) { | 109 | function removePods (req, res, next) { |
110 | const url = req.body.signature.url | 110 | const host = req.body.signature.host |
111 | 111 | ||
112 | waterfall([ | 112 | waterfall([ |
113 | function loadPod (callback) { | 113 | function loadPod (callback) { |
114 | Pod.loadByUrl(url, callback) | 114 | Pod.loadByHost(host, callback) |
115 | }, | 115 | }, |
116 | 116 | ||
117 | function removePod (pod, callback) { | 117 | function removePod (pod, callback) { |
diff --git a/server/controllers/api/remote.js b/server/controllers/api/remote.js index 94808693d..4085deb2d 100644 --- a/server/controllers/api/remote.js +++ b/server/controllers/api/remote.js | |||
@@ -30,7 +30,7 @@ module.exports = router | |||
30 | 30 | ||
31 | function remoteVideos (req, res, next) { | 31 | function remoteVideos (req, res, next) { |
32 | const requests = req.body.data | 32 | const requests = req.body.data |
33 | const fromUrl = req.body.signature.url | 33 | const fromHost = req.body.signature.host |
34 | 34 | ||
35 | // We need to process in the same order to keep consistency | 35 | // We need to process in the same order to keep consistency |
36 | // TODO: optimization | 36 | // TODO: optimization |
@@ -40,7 +40,7 @@ function remoteVideos (req, res, next) { | |||
40 | if (request.type === 'add') { | 40 | if (request.type === 'add') { |
41 | addRemoteVideo(videoData, callbackEach) | 41 | addRemoteVideo(videoData, callbackEach) |
42 | } else if (request.type === 'remove') { | 42 | } else if (request.type === 'remove') { |
43 | removeRemoteVideo(videoData, fromUrl, callbackEach) | 43 | removeRemoteVideo(videoData, fromHost, callbackEach) |
44 | } else { | 44 | } else { |
45 | logger.error('Unkown remote request type %s.', request.type) | 45 | logger.error('Unkown remote request type %s.', request.type) |
46 | } | 46 | } |
@@ -62,16 +62,16 @@ function addRemoteVideo (videoToCreateData, callback) { | |||
62 | video.save(callback) | 62 | video.save(callback) |
63 | } | 63 | } |
64 | 64 | ||
65 | function removeRemoteVideo (videoToRemoveData, fromUrl, callback) { | 65 | function removeRemoteVideo (videoToRemoveData, fromHost, callback) { |
66 | // We need the list because we have to remove some other stuffs (thumbnail etc) | 66 | // We need the list because we have to remove some other stuffs (thumbnail etc) |
67 | Video.listByUrlAndRemoteId(fromUrl, videoToRemoveData.remoteId, function (err, videosList) { | 67 | Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) { |
68 | if (err) { | 68 | if (err) { |
69 | logger.error('Cannot list videos from url and magnets.', { error: err }) | 69 | logger.error('Cannot list videos from host and magnets.', { error: err }) |
70 | return callback(err) | 70 | return callback(err) |
71 | } | 71 | } |
72 | 72 | ||
73 | if (videosList.length === 0) { | 73 | if (videosList.length === 0) { |
74 | logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podUrl: fromUrl }) | 74 | logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podHost: fromHost }) |
75 | } | 75 | } |
76 | 76 | ||
77 | each(videosList, function (video, callbackEach) { | 77 | each(videosList, function (video, callbackEach) { |
diff --git a/server/helpers/custom-validators/pods.js b/server/helpers/custom-validators/pods.js index 40f8b5d0b..0154a2424 100644 --- a/server/helpers/custom-validators/pods.js +++ b/server/helpers/custom-validators/pods.js | |||
@@ -5,14 +5,14 @@ const validator = require('express-validator').validator | |||
5 | const miscValidators = require('./misc') | 5 | const miscValidators = require('./misc') |
6 | 6 | ||
7 | const podsValidators = { | 7 | const podsValidators = { |
8 | isEachUniqueUrlValid | 8 | isEachUniqueHostValid |
9 | } | 9 | } |
10 | 10 | ||
11 | function isEachUniqueUrlValid (urls) { | 11 | function isEachUniqueHostValid (hosts) { |
12 | return miscValidators.isArray(urls) && | 12 | return miscValidators.isArray(hosts) && |
13 | urls.length !== 0 && | 13 | hosts.length !== 0 && |
14 | urls.every(function (url) { | 14 | hosts.every(function (host) { |
15 | return validator.isURL(url) && urls.indexOf(url) === urls.lastIndexOf(url) | 15 | return validator.isURL(host) && host.split('://').length === 1 && hosts.indexOf(host) === hosts.lastIndexOf(host) |
16 | }) | 16 | }) |
17 | } | 17 | } |
18 | 18 | ||
diff --git a/server/helpers/custom-validators/videos.js b/server/helpers/custom-validators/videos.js index 45acb7686..4a6a62326 100644 --- a/server/helpers/custom-validators/videos.js +++ b/server/helpers/custom-validators/videos.js | |||
@@ -15,7 +15,7 @@ const videosValidators = { | |||
15 | isVideoDurationValid, | 15 | isVideoDurationValid, |
16 | isVideoMagnetValid, | 16 | isVideoMagnetValid, |
17 | isVideoNameValid, | 17 | isVideoNameValid, |
18 | isVideoPodUrlValid, | 18 | isVideoPodHostValid, |
19 | isVideoTagsValid, | 19 | isVideoTagsValid, |
20 | isVideoThumbnailValid, | 20 | isVideoThumbnailValid, |
21 | isVideoThumbnail64Valid | 21 | isVideoThumbnail64Valid |
@@ -33,7 +33,7 @@ function isEachRemoteVideosValid (requests) { | |||
33 | isVideoDurationValid(video.duration) && | 33 | isVideoDurationValid(video.duration) && |
34 | isVideoMagnetValid(video.magnet) && | 34 | isVideoMagnetValid(video.magnet) && |
35 | isVideoNameValid(video.name) && | 35 | isVideoNameValid(video.name) && |
36 | isVideoPodUrlValid(video.podUrl) && | 36 | isVideoPodHostValid(video.podHost) && |
37 | isVideoTagsValid(video.tags) && | 37 | isVideoTagsValid(video.tags) && |
38 | isVideoThumbnail64Valid(video.thumbnailBase64) && | 38 | isVideoThumbnail64Valid(video.thumbnailBase64) && |
39 | isVideoRemoteIdValid(video.remoteId) | 39 | isVideoRemoteIdValid(video.remoteId) |
@@ -70,7 +70,7 @@ function isVideoNameValid (value) { | |||
70 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) | 70 | return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME) |
71 | } | 71 | } |
72 | 72 | ||
73 | function isVideoPodUrlValid (value) { | 73 | function isVideoPodHostValid (value) { |
74 | // TODO: set options (TLD...) | 74 | // TODO: set options (TLD...) |
75 | return validator.isURL(value) | 75 | return validator.isURL(value) |
76 | } | 76 | } |
diff --git a/server/helpers/requests.js b/server/helpers/requests.js index 95775c981..06109ce16 100644 --- a/server/helpers/requests.js +++ b/server/helpers/requests.js | |||
@@ -25,7 +25,7 @@ function makeRetryRequest (params, callback) { | |||
25 | 25 | ||
26 | function makeSecureRequest (params, callback) { | 26 | function makeSecureRequest (params, callback) { |
27 | const requestParams = { | 27 | const requestParams = { |
28 | url: params.toPod.url + params.path | 28 | url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path |
29 | } | 29 | } |
30 | 30 | ||
31 | // Add data with POST requst ? | 31 | // Add data with POST requst ? |
@@ -34,9 +34,11 @@ function makeSecureRequest (params, callback) { | |||
34 | 34 | ||
35 | // Add signature if it is specified in the params | 35 | // Add signature if it is specified in the params |
36 | if (params.sign === true) { | 36 | if (params.sign === true) { |
37 | const host = constants.CONFIG.WEBSERVER.HOST | ||
38 | |||
37 | requestParams.json.signature = { | 39 | requestParams.json.signature = { |
38 | url: constants.CONFIG.WEBSERVER.URL, | 40 | host, |
39 | signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL) | 41 | signature: peertubeCrypto.sign(host) |
40 | } | 42 | } |
41 | } | 43 | } |
42 | 44 | ||
diff --git a/server/initializers/constants.js b/server/initializers/constants.js index c808aff5f..d8a13d066 100644 --- a/server/initializers/constants.js +++ b/server/initializers/constants.js | |||
@@ -14,7 +14,7 @@ const PAGINATION_COUNT_DEFAULT = 15 | |||
14 | 14 | ||
15 | // Sortable columns per schema | 15 | // Sortable columns per schema |
16 | const SEARCHABLE_COLUMNS = { | 16 | const SEARCHABLE_COLUMNS = { |
17 | VIDEOS: [ 'name', 'magnetUri', 'podUrl', 'author', 'tags' ] | 17 | VIDEOS: [ 'name', 'magnetUri', 'podHost', 'author', 'tags' ] |
18 | } | 18 | } |
19 | 19 | ||
20 | // Sortable columns per schema | 20 | // Sortable columns per schema |
@@ -55,6 +55,7 @@ const CONFIG = { | |||
55 | } | 55 | } |
56 | } | 56 | } |
57 | CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | 57 | CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT |
58 | CONFIG.WEBSERVER.HOST = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT | ||
58 | 59 | ||
59 | // --------------------------------------------------------------------------- | 60 | // --------------------------------------------------------------------------- |
60 | 61 | ||
diff --git a/server/lib/friends.js b/server/lib/friends.js index eafffaab0..eaea040ca 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js | |||
@@ -6,7 +6,6 @@ const eachSeries = require('async/eachSeries') | |||
6 | const fs = require('fs') | 6 | const fs = require('fs') |
7 | const mongoose = require('mongoose') | 7 | const mongoose = require('mongoose') |
8 | const request = require('request') | 8 | const request = require('request') |
9 | const urlUtil = require('url') | ||
10 | const waterfall = require('async/waterfall') | 9 | const waterfall = require('async/waterfall') |
11 | 10 | ||
12 | const constants = require('../initializers/constants') | 11 | const constants = require('../initializers/constants') |
@@ -44,7 +43,7 @@ function getMyCertificate (callback) { | |||
44 | fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback) | 43 | fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback) |
45 | } | 44 | } |
46 | 45 | ||
47 | function makeFriends (urls, callback) { | 46 | function makeFriends (hosts, callback) { |
48 | const podsScore = {} | 47 | const podsScore = {} |
49 | 48 | ||
50 | logger.info('Make friends!') | 49 | logger.info('Make friends!') |
@@ -54,13 +53,13 @@ function makeFriends (urls, callback) { | |||
54 | return callback(err) | 53 | return callback(err) |
55 | } | 54 | } |
56 | 55 | ||
57 | eachSeries(urls, function (url, callbackEach) { | 56 | eachSeries(hosts, function (host, callbackEach) { |
58 | computeForeignPodsList(url, podsScore, callbackEach) | 57 | computeForeignPodsList(host, podsScore, callbackEach) |
59 | }, function (err) { | 58 | }, function (err) { |
60 | if (err) return callback(err) | 59 | if (err) return callback(err) |
61 | 60 | ||
62 | logger.debug('Pods scores computed.', { podsScore: podsScore }) | 61 | logger.debug('Pods scores computed.', { podsScore: podsScore }) |
63 | const podsList = computeWinningPods(urls, podsScore) | 62 | const podsList = computeWinningPods(hosts, podsScore) |
64 | logger.debug('Pods that we keep.', { podsToKeep: podsList }) | 63 | logger.debug('Pods that we keep.', { podsToKeep: podsList }) |
65 | 64 | ||
66 | makeRequestsToWinningPods(cert, podsList, callback) | 65 | makeRequestsToWinningPods(cert, podsList, callback) |
@@ -149,45 +148,45 @@ module.exports = friends | |||
149 | 148 | ||
150 | // --------------------------------------------------------------------------- | 149 | // --------------------------------------------------------------------------- |
151 | 150 | ||
152 | function computeForeignPodsList (url, podsScore, callback) { | 151 | function computeForeignPodsList (host, podsScore, callback) { |
153 | getForeignPodsList(url, function (err, foreignPodsList) { | 152 | getForeignPodsList(host, function (err, foreignPodsList) { |
154 | if (err) return callback(err) | 153 | if (err) return callback(err) |
155 | 154 | ||
156 | if (!foreignPodsList) foreignPodsList = [] | 155 | if (!foreignPodsList) foreignPodsList = [] |
157 | 156 | ||
158 | // Let's give 1 point to the pod we ask the friends list | 157 | // Let's give 1 point to the pod we ask the friends list |
159 | foreignPodsList.push({ url: url }) | 158 | foreignPodsList.push({ host }) |
160 | 159 | ||
161 | foreignPodsList.forEach(function (foreignPod) { | 160 | foreignPodsList.forEach(function (foreignPod) { |
162 | const foreignPodUrl = foreignPod.url | 161 | const foreignPodHost = foreignPod.host |
163 | 162 | ||
164 | if (podsScore[foreignPodUrl]) podsScore[foreignPodUrl]++ | 163 | if (podsScore[foreignPodHost]) podsScore[foreignPodHost]++ |
165 | else podsScore[foreignPodUrl] = 1 | 164 | else podsScore[foreignPodHost] = 1 |
166 | }) | 165 | }) |
167 | 166 | ||
168 | callback() | 167 | callback() |
169 | }) | 168 | }) |
170 | } | 169 | } |
171 | 170 | ||
172 | function computeWinningPods (urls, podsScore) { | 171 | function computeWinningPods (hosts, podsScore) { |
173 | // Build the list of pods to add | 172 | // Build the list of pods to add |
174 | // Only add a pod if it exists in more than a half base pods | 173 | // Only add a pod if it exists in more than a half base pods |
175 | const podsList = [] | 174 | const podsList = [] |
176 | const baseScore = urls.length / 2 | 175 | const baseScore = hosts.length / 2 |
177 | Object.keys(podsScore).forEach(function (podUrl) { | 176 | Object.keys(podsScore).forEach(function (podHost) { |
178 | // If the pod is not me and with a good score we add it | 177 | // If the pod is not me and with a good score we add it |
179 | if (isMe(podUrl) === false && podsScore[podUrl] > baseScore) { | 178 | if (isMe(podHost) === false && podsScore[podHost] > baseScore) { |
180 | podsList.push({ url: podUrl }) | 179 | podsList.push({ host: podHost }) |
181 | } | 180 | } |
182 | }) | 181 | }) |
183 | 182 | ||
184 | return podsList | 183 | return podsList |
185 | } | 184 | } |
186 | 185 | ||
187 | function getForeignPodsList (url, callback) { | 186 | function getForeignPodsList (host, callback) { |
188 | const path = '/api/' + constants.API_VERSION + '/pods' | 187 | const path = '/api/' + constants.API_VERSION + '/pods' |
189 | 188 | ||
190 | request.get(url + path, function (err, response, body) { | 189 | request.get(constants.REMOTE_SCHEME.HTTP + '://' + host + path, function (err, response, body) { |
191 | if (err) return callback(err) | 190 | if (err) return callback(err) |
192 | 191 | ||
193 | try { | 192 | try { |
@@ -207,26 +206,26 @@ function makeRequestsToWinningPods (cert, podsList, callback) { | |||
207 | 206 | ||
208 | eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) { | 207 | eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) { |
209 | const params = { | 208 | const params = { |
210 | url: pod.url + '/api/' + constants.API_VERSION + '/pods/', | 209 | url: constants.REMOTE_SCHEME.HTTP + '://' + pod.host + '/api/' + constants.API_VERSION + '/pods/', |
211 | method: 'POST', | 210 | method: 'POST', |
212 | json: { | 211 | json: { |
213 | url: constants.CONFIG.WEBSERVER.URL, | 212 | host: constants.CONFIG.WEBSERVER.HOST, |
214 | publicKey: cert | 213 | publicKey: cert |
215 | } | 214 | } |
216 | } | 215 | } |
217 | 216 | ||
218 | requests.makeRetryRequest(params, function (err, res, body) { | 217 | requests.makeRetryRequest(params, function (err, res, body) { |
219 | if (err) { | 218 | if (err) { |
220 | logger.error('Error with adding %s pod.', pod.url, { error: err }) | 219 | logger.error('Error with adding %s pod.', pod.host, { error: err }) |
221 | // Don't break the process | 220 | // Don't break the process |
222 | return callbackEach() | 221 | return callbackEach() |
223 | } | 222 | } |
224 | 223 | ||
225 | if (res.statusCode === 200) { | 224 | if (res.statusCode === 200) { |
226 | const podObj = new Pod({ url: pod.url, publicKey: body.cert }) | 225 | const podObj = new Pod({ host: pod.host, publicKey: body.cert }) |
227 | podObj.save(function (err, podCreated) { | 226 | podObj.save(function (err, podCreated) { |
228 | if (err) { | 227 | if (err) { |
229 | logger.error('Cannot add friend %s pod.', pod.url, { error: err }) | 228 | logger.error('Cannot add friend %s pod.', pod.host, { error: err }) |
230 | return callbackEach() | 229 | return callbackEach() |
231 | } | 230 | } |
232 | 231 | ||
@@ -236,7 +235,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) { | |||
236 | return callbackEach() | 235 | return callbackEach() |
237 | }) | 236 | }) |
238 | } else { | 237 | } else { |
239 | logger.error('Status not 200 for %s pod.', pod.url) | 238 | logger.error('Status not 200 for %s pod.', pod.host) |
240 | return callbackEach() | 239 | return callbackEach() |
241 | } | 240 | } |
242 | }) | 241 | }) |
@@ -268,14 +267,6 @@ function createRequest (type, endpoint, data, to) { | |||
268 | }) | 267 | }) |
269 | } | 268 | } |
270 | 269 | ||
271 | function isMe (url) { | 270 | function isMe (host) { |
272 | const parsedUrl = urlUtil.parse(url) | 271 | return host === constants.CONFIG.WEBSERVER.HOST |
273 | |||
274 | const hostname = parsedUrl.hostname | ||
275 | const port = parseInt(parsedUrl.port) | ||
276 | |||
277 | const myHostname = constants.CONFIG.WEBSERVER.HOSTNAME | ||
278 | const myPort = constants.CONFIG.WEBSERVER.PORT | ||
279 | |||
280 | return hostname === myHostname && port === myPort | ||
281 | } | 272 | } |
diff --git a/server/middlewares/pods.js b/server/middlewares/pods.js index 6e0874a76..487ea1259 100644 --- a/server/middlewares/pods.js +++ b/server/middlewares/pods.js | |||
@@ -1,38 +1,36 @@ | |||
1 | 'use strict' | 1 | 'use strict' |
2 | 2 | ||
3 | const urlModule = require('url') | 3 | const constants = require('../initializers/constants') |
4 | |||
5 | const logger = require('../helpers/logger') | ||
6 | 4 | ||
7 | const podsMiddleware = { | 5 | const podsMiddleware = { |
8 | setBodyUrlsPort, | 6 | setBodyHostsPort, |
9 | setBodyUrlPort | 7 | setBodyHostPort |
10 | } | 8 | } |
11 | 9 | ||
12 | function setBodyUrlsPort (req, res, next) { | 10 | function setBodyHostsPort (req, res, next) { |
13 | for (let i = 0; i < req.body.urls.length; i++) { | 11 | for (let i = 0; i < req.body.hosts.length; i++) { |
14 | const urlWithPort = getUrlWithPort(req.body.urls[i]) | 12 | const hostWithPort = getHostWithPort(req.body.hosts[i]) |
15 | 13 | ||
16 | // Problem with the url parsing? | 14 | // Problem with the url parsing? |
17 | if (urlWithPort === null) { | 15 | if (hostWithPort === null) { |
18 | return res.sendStatus(500) | 16 | return res.sendStatus(500) |
19 | } | 17 | } |
20 | 18 | ||
21 | req.body.urls[i] = urlWithPort | 19 | req.body.hosts[i] = hostWithPort |
22 | } | 20 | } |
23 | 21 | ||
24 | return next() | 22 | return next() |
25 | } | 23 | } |
26 | 24 | ||
27 | function setBodyUrlPort (req, res, next) { | 25 | function setBodyHostPort (req, res, next) { |
28 | const urlWithPort = getUrlWithPort(req.body.url) | 26 | const hostWithPort = getHostWithPort(req.body.host) |
29 | 27 | ||
30 | // Problem with the url parsing? | 28 | // Problem with the url parsing? |
31 | if (urlWithPort === null) { | 29 | if (hostWithPort === null) { |
32 | return res.sendStatus(500) | 30 | return res.sendStatus(500) |
33 | } | 31 | } |
34 | 32 | ||
35 | req.body.url = urlWithPort | 33 | req.body.host = hostWithPort |
36 | 34 | ||
37 | return next() | 35 | return next() |
38 | } | 36 | } |
@@ -43,20 +41,16 @@ module.exports = podsMiddleware | |||
43 | 41 | ||
44 | // --------------------------------------------------------------------------- | 42 | // --------------------------------------------------------------------------- |
45 | 43 | ||
46 | function getUrlWithPort (url) { | 44 | function getHostWithPort (host) { |
47 | const urlObj = urlModule.parse(url) | 45 | const splitted = host.split(':') |
48 | 46 | ||
49 | // Add the port if it is not specified | 47 | console.log(splitted) |
50 | if (urlObj.port === null) { | 48 | // The port was not specified |
51 | if (urlObj.protocol === 'http:') { | 49 | if (splitted.length === 1) { |
52 | return url + ':80' | 50 | if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443' |
53 | } else if (urlObj.protocol === 'https:') { | 51 | |
54 | return url + ':443' | 52 | return host + ':80' |
55 | } else { | ||
56 | logger.error('Unknown url protocol: ' + urlObj.protocol) | ||
57 | return null | ||
58 | } | ||
59 | } | 53 | } |
60 | 54 | ||
61 | return url | 55 | return host |
62 | } | 56 | } |
diff --git a/server/middlewares/secure.js b/server/middlewares/secure.js index 58f824d14..fd5bc51d6 100644 --- a/server/middlewares/secure.js +++ b/server/middlewares/secure.js | |||
@@ -12,27 +12,27 @@ const secureMiddleware = { | |||
12 | } | 12 | } |
13 | 13 | ||
14 | function checkSignature (req, res, next) { | 14 | function checkSignature (req, res, next) { |
15 | const url = req.body.signature.url | 15 | const host = req.body.signature.host |
16 | Pod.loadByUrl(url, function (err, pod) { | 16 | Pod.loadByHost(host, function (err, pod) { |
17 | if (err) { | 17 | if (err) { |
18 | logger.error('Cannot get signed url in decryptBody.', { error: err }) | 18 | logger.error('Cannot get signed host in decryptBody.', { error: err }) |
19 | return res.sendStatus(500) | 19 | return res.sendStatus(500) |
20 | } | 20 | } |
21 | 21 | ||
22 | if (pod === null) { | 22 | if (pod === null) { |
23 | logger.error('Unknown pod %s.', url) | 23 | logger.error('Unknown pod %s.', host) |
24 | return res.sendStatus(403) | 24 | return res.sendStatus(403) |
25 | } | 25 | } |
26 | 26 | ||
27 | logger.debug('Decrypting body from %s.', url) | 27 | logger.debug('Decrypting body from %s.', host) |
28 | 28 | ||
29 | const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature) | 29 | const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, host, req.body.signature.signature) |
30 | 30 | ||
31 | if (signatureOk === true) { | 31 | if (signatureOk === true) { |
32 | return next() | 32 | return next() |
33 | } | 33 | } |
34 | 34 | ||
35 | logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url) | 35 | logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.host) |
36 | return res.sendStatus(403) | 36 | return res.sendStatus(403) |
37 | }) | 37 | }) |
38 | } | 38 | } |
diff --git a/server/middlewares/validators/pods.js b/server/middlewares/validators/pods.js index fd3d1e2f2..4f8bad2f9 100644 --- a/server/middlewares/validators/pods.js +++ b/server/middlewares/validators/pods.js | |||
@@ -10,7 +10,7 @@ const validatorsPod = { | |||
10 | } | 10 | } |
11 | 11 | ||
12 | function makeFriends (req, res, next) { | 12 | function makeFriends (req, res, next) { |
13 | req.checkBody('urls', 'Should have an array of unique urls').isEachUniqueUrlValid() | 13 | req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid() |
14 | 14 | ||
15 | logger.debug('Checking makeFriends parameters', { parameters: req.body }) | 15 | logger.debug('Checking makeFriends parameters', { parameters: req.body }) |
16 | 16 | ||
@@ -32,7 +32,7 @@ function makeFriends (req, res, next) { | |||
32 | } | 32 | } |
33 | 33 | ||
34 | function podsAdd (req, res, next) { | 34 | function podsAdd (req, res, next) { |
35 | req.checkBody('url', 'Should have an url').notEmpty().isURL({ require_protocol: true }) | 35 | req.checkBody('host', 'Should have an host').notEmpty().isURL() |
36 | req.checkBody('publicKey', 'Should have a public key').notEmpty() | 36 | req.checkBody('publicKey', 'Should have a public key').notEmpty() |
37 | 37 | ||
38 | // TODO: check we don't have it already | 38 | // TODO: check we don't have it already |
diff --git a/server/middlewares/validators/remote.js b/server/middlewares/validators/remote.js index 8c29ef8ca..c6455e678 100644 --- a/server/middlewares/validators/remote.js +++ b/server/middlewares/validators/remote.js | |||
@@ -27,10 +27,10 @@ function remoteVideos (req, res, next) { | |||
27 | } | 27 | } |
28 | 28 | ||
29 | function signature (req, res, next) { | 29 | function signature (req, res, next) { |
30 | req.checkBody('signature.url', 'Should have a signature url').isURL() | 30 | req.checkBody('signature.host', 'Should have a signature host').isURL() |
31 | req.checkBody('signature.signature', 'Should have a signature').notEmpty() | 31 | req.checkBody('signature.signature', 'Should have a signature').notEmpty() |
32 | 32 | ||
33 | logger.debug('Checking signature parameters', { parameters: { signatureUrl: req.body.signature.url } }) | 33 | logger.debug('Checking signature parameters', { parameters: { signatureHost: req.body.signature.host } }) |
34 | 34 | ||
35 | checkErrors(req, res, next) | 35 | checkErrors(req, res, next) |
36 | } | 36 | } |
diff --git a/server/models/pods.js b/server/models/pods.js index 6ab018c1c..49c73472a 100644 --- a/server/models/pods.js +++ b/server/models/pods.js | |||
@@ -12,7 +12,7 @@ const Video = mongoose.model('Video') | |||
12 | // --------------------------------------------------------------------------- | 12 | // --------------------------------------------------------------------------- |
13 | 13 | ||
14 | const PodSchema = mongoose.Schema({ | 14 | const PodSchema = mongoose.Schema({ |
15 | url: String, | 15 | host: String, |
16 | publicKey: String, | 16 | publicKey: String, |
17 | score: { type: Number, max: constants.FRIEND_SCORE.MAX }, | 17 | score: { type: Number, max: constants.FRIEND_SCORE.MAX }, |
18 | createdDate: { | 18 | createdDate: { |
@@ -21,8 +21,7 @@ const PodSchema = mongoose.Schema({ | |||
21 | } | 21 | } |
22 | }) | 22 | }) |
23 | 23 | ||
24 | // TODO: set options (TLD...) | 24 | PodSchema.path('host').validate(validator.isURL) |
25 | PodSchema.path('url').validate(validator.isURL) | ||
26 | PodSchema.path('publicKey').required(true) | 25 | PodSchema.path('publicKey').required(true) |
27 | PodSchema.path('score').validate(function (value) { return !isNaN(value) }) | 26 | PodSchema.path('score').validate(function (value) { return !isNaN(value) }) |
28 | 27 | ||
@@ -37,14 +36,14 @@ PodSchema.statics = { | |||
37 | listAllIds, | 36 | listAllIds, |
38 | listBadPods, | 37 | listBadPods, |
39 | load, | 38 | load, |
40 | loadByUrl, | 39 | loadByHost, |
41 | removeAll | 40 | removeAll |
42 | } | 41 | } |
43 | 42 | ||
44 | PodSchema.pre('save', function (next) { | 43 | PodSchema.pre('save', function (next) { |
45 | const self = this | 44 | const self = this |
46 | 45 | ||
47 | Pod.loadByUrl(this.url, function (err, pod) { | 46 | Pod.loadByHost(this.host, function (err, pod) { |
48 | if (err) return next(err) | 47 | if (err) return next(err) |
49 | 48 | ||
50 | if (pod) return next(new Error('Pod already exists.')) | 49 | if (pod) return next(new Error('Pod already exists.')) |
@@ -56,7 +55,7 @@ PodSchema.pre('save', function (next) { | |||
56 | 55 | ||
57 | PodSchema.pre('remove', function (next) { | 56 | PodSchema.pre('remove', function (next) { |
58 | // Remove the videos owned by this pod too | 57 | // Remove the videos owned by this pod too |
59 | Video.listByUrl(this.url, function (err, videos) { | 58 | Video.listByHost(this.host, function (err, videos) { |
60 | if (err) return next(err) | 59 | if (err) return next(err) |
61 | 60 | ||
62 | each(videos, function (video, callbackEach) { | 61 | each(videos, function (video, callbackEach) { |
@@ -72,7 +71,7 @@ const Pod = mongoose.model('Pod', PodSchema) | |||
72 | function toFormatedJSON () { | 71 | function toFormatedJSON () { |
73 | const json = { | 72 | const json = { |
74 | id: this._id, | 73 | id: this._id, |
75 | url: this.url, | 74 | host: this.host, |
76 | score: this.score, | 75 | score: this.score, |
77 | createdDate: this.createdDate | 76 | createdDate: this.createdDate |
78 | } | 77 | } |
@@ -111,8 +110,8 @@ function load (id, callback) { | |||
111 | return this.findById(id, callback) | 110 | return this.findById(id, callback) |
112 | } | 111 | } |
113 | 112 | ||
114 | function loadByUrl (url, callback) { | 113 | function loadByHost (host, callback) { |
115 | return this.findOne({ url: url }, callback) | 114 | return this.findOne({ host }, callback) |
116 | } | 115 | } |
117 | 116 | ||
118 | function removeAll (callback) { | 117 | function removeAll (callback) { |
diff --git a/server/models/request.js b/server/models/request.js index f5eec2134..59bf440fe 100644 --- a/server/models/request.js +++ b/server/models/request.js | |||
@@ -121,7 +121,7 @@ function makeRequest (toPod, requestEndpoint, requestsToMake, callback) { | |||
121 | if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { | 121 | if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { |
122 | logger.error( | 122 | logger.error( |
123 | 'Error sending secure request to %s pod.', | 123 | 'Error sending secure request to %s pod.', |
124 | toPod.url, | 124 | toPod.host, |
125 | { | 125 | { |
126 | error: err || new Error('Status code not 20x : ' + res.statusCode) | 126 | error: err || new Error('Status code not 20x : ' + res.statusCode) |
127 | } | 127 | } |
diff --git a/server/models/video.js b/server/models/video.js index 0da2cb8ab..6d3fa3fb8 100644 --- a/server/models/video.js +++ b/server/models/video.js | |||
@@ -28,10 +28,9 @@ const VideoSchema = mongoose.Schema({ | |||
28 | magnet: { | 28 | magnet: { |
29 | infoHash: String | 29 | infoHash: String |
30 | }, | 30 | }, |
31 | podUrl: String, | 31 | podHost: String, |
32 | author: String, | 32 | author: String, |
33 | duration: Number, | 33 | duration: Number, |
34 | thumbnail: String, | ||
35 | tags: [ String ], | 34 | tags: [ String ], |
36 | createdDate: { | 35 | createdDate: { |
37 | type: Date, | 36 | type: Date, |
@@ -41,14 +40,9 @@ const VideoSchema = mongoose.Schema({ | |||
41 | 40 | ||
42 | VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid) | 41 | VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid) |
43 | VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid) | 42 | VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid) |
44 | VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid) | 43 | VideoSchema.path('podHost').validate(customVideosValidators.isVideoPodHostValid) |
45 | VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid) | 44 | VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid) |
46 | VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid) | 45 | VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid) |
47 | // The tumbnail can be the path or the data in base 64 | ||
48 | // The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename | ||
49 | VideoSchema.path('thumbnail').validate(function (value) { | ||
50 | return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value) | ||
51 | }) | ||
52 | VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid) | 46 | VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid) |
53 | 47 | ||
54 | VideoSchema.methods = { | 48 | VideoSchema.methods = { |
@@ -65,8 +59,8 @@ VideoSchema.methods = { | |||
65 | VideoSchema.statics = { | 59 | VideoSchema.statics = { |
66 | getDurationFromFile, | 60 | getDurationFromFile, |
67 | listForApi, | 61 | listForApi, |
68 | listByUrlAndRemoteId, | 62 | listByHostAndRemoteId, |
69 | listByUrl, | 63 | listByHost, |
70 | listOwned, | 64 | listOwned, |
71 | listOwnedByAuthor, | 65 | listOwnedByAuthor, |
72 | listRemotes, | 66 | listRemotes, |
@@ -107,7 +101,7 @@ VideoSchema.pre('save', function (next) { | |||
107 | 101 | ||
108 | if (video.isOwned()) { | 102 | if (video.isOwned()) { |
109 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) | 103 | const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename()) |
110 | this.podUrl = constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT | 104 | this.podHost = constants.CONFIG.WEBSERVER.HOST |
111 | 105 | ||
112 | tasks.push( | 106 | tasks.push( |
113 | // TODO: refractoring | 107 | // TODO: refractoring |
@@ -160,8 +154,8 @@ function generateMagnetUri () { | |||
160 | baseUrlHttp = constants.CONFIG.WEBSERVER.URL | 154 | baseUrlHttp = constants.CONFIG.WEBSERVER.URL |
161 | baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT | 155 | baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT |
162 | } else { | 156 | } else { |
163 | baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.podUrl | 157 | baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.podHost |
164 | baseUrlWs = constants.REMOTE_SCHEME.WS + this.podUrl | 158 | baseUrlWs = constants.REMOTE_SCHEME.WS + this.podHost |
165 | } | 159 | } |
166 | 160 | ||
167 | const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName() | 161 | const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName() |
@@ -215,7 +209,7 @@ function toFormatedJSON () { | |||
215 | id: this._id, | 209 | id: this._id, |
216 | name: this.name, | 210 | name: this.name, |
217 | description: this.description, | 211 | description: this.description, |
218 | podUrl: this.podUrl, | 212 | podHost: this.podHost, |
219 | isLocal: this.isOwned(), | 213 | isLocal: this.isOwned(), |
220 | magnetUri: this.generateMagnetUri(), | 214 | magnetUri: this.generateMagnetUri(), |
221 | author: this.author, | 215 | author: this.author, |
@@ -249,7 +243,7 @@ function toRemoteJSON (callback) { | |||
249 | thumbnailBase64: new Buffer(thumbnailData).toString('base64'), | 243 | thumbnailBase64: new Buffer(thumbnailData).toString('base64'), |
250 | tags: self.tags, | 244 | tags: self.tags, |
251 | createdDate: self.createdDate, | 245 | createdDate: self.createdDate, |
252 | podUrl: self.podUrl | 246 | podHost: self.podHost |
253 | } | 247 | } |
254 | 248 | ||
255 | return callback(null, remoteVideo) | 249 | return callback(null, remoteVideo) |
@@ -271,12 +265,12 @@ function listForApi (start, count, sort, callback) { | |||
271 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) | 265 | return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback) |
272 | } | 266 | } |
273 | 267 | ||
274 | function listByUrlAndRemoteId (fromUrl, remoteId, callback) { | 268 | function listByHostAndRemoteId (fromHost, remoteId, callback) { |
275 | this.find({ podUrl: fromUrl, remoteId: remoteId }, callback) | 269 | this.find({ podHost: fromHost, remoteId: remoteId }, callback) |
276 | } | 270 | } |
277 | 271 | ||
278 | function listByUrl (fromUrl, callback) { | 272 | function listByHost (fromHost, callback) { |
279 | this.find({ podUrl: fromUrl }, callback) | 273 | this.find({ podHost: fromHost }, callback) |
280 | } | 274 | } |
281 | 275 | ||
282 | function listOwned (callback) { | 276 | function listOwned (callback) { |