aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pods.js
blob: 30d465ee5fbc8ae7451d14f113ddabf75c8cce21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
;(function () {
  'use strict'

  var fs = require('fs')
  var config = require('config')
  var async = require('async')
  var request = require('request')

  var logger = require('./logger')
  var utils = require('./utils')
  var PodsDB = require('./database').PodsDB

  var pods = {}
  var http = config.get('webserver.https') ? 'https' : 'http'
  var host = config.get('webserver.host')
  var port = config.get('webserver.port')

  // ----------- Private functions -----------

  function getForeignPodsList (url, callback) {
    var path = '/api/pods'

    request.get(url + path, function (err, response, body) {
      if (err) throw err
      callback(JSON.parse(body))
    })
  }

  // ----------- Public functions -----------
  pods.list = function (callback) {
    PodsDB.find(function (err, pods_list) {
      if (err) {
        logger.error('Cannot get the list of the pods.', { error: err })
        return callback(err)
      }

      return callback(null, pods_list)
    })
  }

  // { url }
  pods.add = function (data, callback) {
    logger.info('Adding pod: %s', data.url)

    var params = {
      url: data.url,
      publicKey: data.publicKey
    }

    PodsDB.create(params, function (err, pod) {
      if (err) {
        logger.error('Cannot insert the pod.', { error: err })
        return callback(err)
      }

      fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
        if (err) {
          logger.error('Cannot read cert file.', { error: err })
          return callback(err)
        }

        return callback(null, { cert: cert })
      })
    })
  }

  // { path, data }
  pods.makeSecureRequest = function (data, callback) {
    PodsDB.find({}, { url: 1, publicKey: 1 }).exec(function (err, urls) {
      if (err) {
        logger.error('Cannot get the list of the pods.', { error: err })
        return callback(err)
      }

      logger.debug('Make multiple requests.')
      utils.makeMultipleRetryRequest(
        { encrypt: true, sign: true, method: data.method, path: data.path, data: data.data },

        urls,

        function (err, response, body, url) {
          if (err || response.statusCode !== 200) {
            logger.error('Error sending secure request to %s/%s pod.', url, data.path, { error: err })
          }
        },

        function (err) {
          if (err) {
            logger.error('There was some errors when sending the video meta data.', { error: err })
            return callback(err)
          }

          logger.debug('Finished')
          callback(null)
        }
      )
    })
  }

  pods.makeFriends = function (callback) {
    logger.debug('Read public key...')
    fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
      if (err) {
        logger.error('Cannot read public cert.', { error: err })
        return callback(err)
      }

      var urls = config.get('network.friends')
      var pods_score = {}

      async.each(urls, function (url, callback) {
        // Always add a trust pod
        pods_score[url] = Infinity

        getForeignPodsList(url, function (foreign_pods_list) {
          if (foreign_pods_list.length === 0) return callback()

          async.each(foreign_pods_list, function (foreign_pod, callback) {
            var foreign_url = foreign_pod.url
            if (pods_score[foreign_url]) pods_score[foreign_url]++
            else pods_score[foreign_url] = 1
            callback()
          }, callback)
        })
      }, function () {
        logger.debug('Pods score', { pods_score: pods_score })

        // Build the list of pods to add
        // Only add a pod if it exists in more than a half base pods
        var pods_list = []
        var base_score = urls.length / 2
        Object.keys(pods_score).forEach(function (pod) {
          if (pods_score[pod] > base_score) pods_list.push({ url: pod })
        })

        logger.debug('Pods that we keep', { pods: pods_list })

        var data = {
          url: http + '://' + host + ':' + port,
          publicKey: cert
        }

        logger.debug('Make requests...')

        utils.makeMultipleRetryRequest(
          { method: 'POST', path: '/api/pods/', data: data },

          pods_list,

          function eachRequest (err, response, body, url) {
            if (!err && response.statusCode === 200) {
              pods.add({ url: url, publicKey: body.cert }, function (err) {
                if (err) {
                  logger.error('Error with adding %s pod.', url, { error: err })
                }
              })
            } else {
              logger.error('Error with adding %s pod.', url)
            }
          },

          function endRequests (err) {
            if (err) {
              logger.error('There was some errors when we wanted to make friends.', { error: err })
              return callback(err)
            }

            logger.debug('Finished')
            callback(null)
          }
        )
      })
    })
  }

  module.exports = pods
})()