aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pods.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/pods.js')
-rw-r--r--src/pods.js177
1 files changed, 177 insertions, 0 deletions
diff --git a/src/pods.js b/src/pods.js
new file mode 100644
index 000000000..9d6539685
--- /dev/null
+++ b/src/pods.js
@@ -0,0 +1,177 @@
1;(function () {
2 'use strict'
3
4 var fs = require('fs')
5 var config = require('config')
6 var async = require('async')
7 var request = require('request')
8
9 var logger = require('./logger')
10 var utils = require('./utils')
11 var PodsDB = require('./database').PodsDB
12
13 var pods = {}
14 var http = config.get('webserver.https') ? 'https' : 'http'
15 var host = config.get('webserver.host')
16 var port = config.get('webserver.port')
17
18 // ----------- Private functions -----------
19
20 function getForeignPodsList (url, callback) {
21 var path = '/api/pods'
22
23 request.get(url + path, function (err, response, body) {
24 if (err) throw err
25 callback(JSON.parse(body))
26 })
27 }
28
29 // ----------- Public functions -----------
30 pods.list = function (callback) {
31 PodsDB.find(function (err, pods_list) {
32 if (err) {
33 logger.error('Cannot get the list of the pods.', { error: err })
34 return callback(err)
35 }
36
37 return callback(null, pods_list)
38 })
39 }
40
41 // { url }
42 pods.add = function (data, callback) {
43 logger.info('Adding pod: %s', data.url)
44
45 var params = {
46 url: data.url,
47 publicKey: data.publicKey
48 }
49
50 PodsDB.create(params, function (err, pod) {
51 if (err) {
52 logger.error('Cannot insert the pod.', { error: err })
53 return callback(err)
54 }
55
56 fs.readFile(utils.certDir + 'petube.pub', 'utf8', function (err, cert) {
57 if (err) {
58 logger.error('Cannot read cert file.', { error: err })
59 return callback(err)
60 }
61
62 return callback(null, { cert: cert })
63 })
64 })
65 }
66
67 // { path, data }
68 pods.makeSecureRequest = function (data, callback) {
69 PodsDB.find({}, { url: 1, publicKey: 1 }).exec(function (err, urls) {
70 if (err) {
71 logger.error('Cannot get the list of the pods.', { error: err })
72 return callback(err)
73 }
74
75 logger.debug('Make multiple requests.')
76 utils.makeMultipleRetryRequest(
77 { encrypt: true, sign: true, method: data.method, path: data.path, data: data.data },
78
79 urls,
80
81 function (err, response, body, url) {
82 if (err || response.statusCode !== 200) {
83 logger.error('Error sending secure request to %s/%s pod.', url, data.path, { error: err })
84 }
85 },
86
87 function (err) {
88 if (err) {
89 logger.error('There was some errors when sending the video meta data.', { error: err })
90 return callback(err)
91 }
92
93 logger.debug('Finished')
94 callback(null)
95 }
96 )
97 })
98 }
99
100 pods.makeFriends = function (callback) {
101 logger.debug('Read public key...')
102 fs.readFile(utils.certDir + 'petube.pub', 'utf8', function (err, cert) {
103 if (err) {
104 logger.error('Cannot read public cert.', { error: err })
105 return callback(err)
106 }
107
108 var urls = config.get('network.friends')
109 var pods_score = {}
110
111 async.each(urls, function (url, callback) {
112 // Always add a trust pod
113 pods_score[url] = Infinity
114
115 getForeignPodsList(url, function (foreign_pods_list) {
116 if (foreign_pods_list.length === 0) return callback()
117
118 async.each(foreign_pods_list, function (foreign_pod, callback) {
119 var foreign_url = foreign_pod.url
120 if (pods_score[foreign_url]) pods_score[foreign_url]++
121 else pods_score[foreign_url] = 1
122 callback()
123 }, callback)
124 })
125 }, function () {
126 logger.debug('Pods score', { pods_score: pods_score })
127
128 // Build the list of pods to add
129 // Only add a pod if it exists in more than a half base pods
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 })
135
136 logger.debug('Pods that we keep', { pods: pods_list })
137
138 var data = {
139 url: http + '://' + host + ':' + port,
140 publicKey: cert
141 }
142
143 logger.debug('Make requests...')
144
145 utils.makeMultipleRetryRequest(
146 { method: 'POST', path: '/api/pods/', data: data },
147
148 pods_list,
149
150 function eachRequest (err, response, body, url) {
151 if (!err && response.statusCode === 200) {
152 pods.add({ url: url, publicKey: body.cert }, function (err) {
153 if (err) {
154 logger.error('Error with adding %s pod.', url, { error: err })
155 }
156 })
157 } else {
158 logger.error('Error with adding %s pod.', url)
159 }
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 }
171 )
172 })
173 })
174 }
175
176 module.exports = pods
177})()