diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-11-14 20:03:04 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-11-16 20:29:26 +0100 |
commit | 49abbbbedca83b9031d3e2eb3ae9ad9b6a7d96ed (patch) | |
tree | 68c59d67637a297d513e07ea96ba236a7f0cd43b /server/lib/friends.js | |
parent | 41b5da1d8cb41f5c49f0e0a01a54106c9a5925dd (diff) | |
download | PeerTube-49abbbbedca83b9031d3e2eb3ae9ad9b6a7d96ed.tar.gz PeerTube-49abbbbedca83b9031d3e2eb3ae9ad9b6a7d96ed.tar.zst PeerTube-49abbbbedca83b9031d3e2eb3ae9ad9b6a7d96ed.zip |
Pod URL -> pod host. HTTPS is required to make friends.
Reason: in a network with mix http/https pods, https pods won't be able
to play videos from http pod (insecure requests).
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r-- | server/lib/friends.js | 59 |
1 files changed, 25 insertions, 34 deletions
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 | } |