]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - src/pods.js
Add stylesheets vendor and global.css to ignore
[github/Chocobozzz/PeerTube.git] / src / pods.js
CommitLineData
8c308c2b
C
1;(function () {
2 'use strict'
3
8c308c2b 4 var async = require('async')
a1860380
C
5 var config = require('config')
6 var fs = require('fs')
8c308c2b
C
7 var request = require('request')
8
9 var logger = require('./logger')
8c308c2b 10 var PodsDB = require('./database').PodsDB
a1860380 11 var utils = require('./utils')
8c308c2b
C
12
13 var pods = {}
a1860380 14
8c308c2b
C
15 var http = config.get('webserver.https') ? 'https' : 'http'
16 var host = config.get('webserver.host')
17 var port = config.get('webserver.port')
18
19 // ----------- Private functions -----------
20
21 function getForeignPodsList (url, callback) {
f5a60a51 22 var path = '/api/' + global.API_VERSION + '/pods'
8c308c2b
C
23
24 request.get(url + path, function (err, response, body) {
25 if (err) throw err
26 callback(JSON.parse(body))
27 })
28 }
29
30 // ----------- Public functions -----------
a1860380 31
8c308c2b
C
32 pods.list = function (callback) {
33 PodsDB.find(function (err, pods_list) {
34 if (err) {
35 logger.error('Cannot get the list of the pods.', { error: err })
36 return callback(err)
37 }
38
39 return callback(null, pods_list)
40 })
41 }
42
43 // { url }
44 pods.add = function (data, callback) {
45 logger.info('Adding pod: %s', data.url)
46
47 var params = {
48 url: data.url,
49 publicKey: data.publicKey
50 }
51
52 PodsDB.create(params, function (err, pod) {
53 if (err) {
54 logger.error('Cannot insert the pod.', { error: err })
55 return callback(err)
56 }
57
d148f3b9 58 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
59 if (err) {
60 logger.error('Cannot read cert file.', { error: err })
61 return callback(err)
62 }
63
64 return callback(null, { cert: cert })
65 })
66 })
67 }
68
69 // { path, data }
70 pods.makeSecureRequest = function (data, callback) {
71 PodsDB.find({}, { url: 1, publicKey: 1 }).exec(function (err, urls) {
72 if (err) {
73 logger.error('Cannot get the list of the pods.', { error: err })
74 return callback(err)
75 }
76
77 logger.debug('Make multiple requests.')
a1860380
C
78
79 var params = {
80 encrypt: true,
81 sign: true,
82 method: data.method,
83 path: data.path,
84 data: data.data
85 }
86
8c308c2b 87 utils.makeMultipleRetryRequest(
a1860380 88 params,
8c308c2b
C
89
90 urls,
91
a1860380 92 function callbackEachPodFinished (err, response, body, url) {
8c308c2b
C
93 if (err || response.statusCode !== 200) {
94 logger.error('Error sending secure request to %s/%s pod.', url, data.path, { error: err })
95 }
96 },
97
a1860380 98 function callbackAllPodsFinished (err) {
8c308c2b
C
99 if (err) {
100 logger.error('There was some errors when sending the video meta data.', { error: err })
101 return callback(err)
102 }
103
104 logger.debug('Finished')
105 callback(null)
106 }
107 )
108 })
109 }
110
111 pods.makeFriends = function (callback) {
a1860380
C
112 var pods_score = {}
113
114 logger.info('Make friends!')
d148f3b9 115 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
116 if (err) {
117 logger.error('Cannot read public cert.', { error: err })
118 return callback(err)
119 }
120
121 var urls = config.get('network.friends')
8c308c2b 122
a1860380
C
123 async.each(urls, computeForeignPodsList, function () {
124 logger.debug('Pods scores computed.', { pods_score: pods_score })
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 // -----------------------------------------------------------------------
8c308c2b 134
a1860380
C
135 function computeForeignPodsList (url, callback) {
136 // Always add a trust pod
137 pods_score[url] = Infinity
8c308c2b 138
a1860380
C
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()
8c308c2b 151 })
a1860380
C
152 })
153 }
8c308c2b 154
a1860380
C
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 })
8c308c2b 163
a1860380
C
164 return pods_list
165 }
8c308c2b 166
a1860380
C
167 function makeRequestsToWinningPods (cert, pods_list) {
168 var data = {
169 url: http + '://' + host + ':' + port,
170 publicKey: cert
171 }
8c308c2b 172
a1860380
C
173 utils.makeMultipleRetryRequest(
174 { method: 'POST', path: '/api/' + global.API_VERSION + '/pods/', data: data },
175
176 pods_list,
177
178 function eachRequest (err, response, body, url) {
179 // We add the pod if it responded correctly with its public certificate
180 if (!err && response.statusCode === 200) {
181 pods.add({ url: url, publicKey: body.cert }, function (err) {
182 if (err) {
183 logger.error('Error with adding %s pod.', url, { error: err })
184 }
185 })
186 } else {
187 logger.error('Error with adding %s pod.', url, { error: err || new Error('Status not 200') })
8c308c2b 188 }
a1860380
C
189 },
190
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 }
8c308c2b
C
202 }
203
204 module.exports = pods
205})()