aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pods.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/pods.js')
-rw-r--r--src/pods.js154
1 files changed, 91 insertions, 63 deletions
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