2016-02-07 05:23:23 -05:00
|
|
|
'use strict'
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const async = require('async')
|
|
|
|
const config = require('config')
|
|
|
|
const fs = require('fs')
|
|
|
|
const request = require('request')
|
|
|
|
|
|
|
|
const constants = require('../initializers/constants')
|
|
|
|
const logger = require('../helpers/logger')
|
|
|
|
const peertubeCrypto = require('../helpers/peertubeCrypto')
|
|
|
|
const Pods = require('../models/pods')
|
|
|
|
const poolRequests = require('../lib/poolRequests')
|
|
|
|
const requests = require('../helpers/requests')
|
|
|
|
const Videos = require('../models/videos')
|
|
|
|
|
|
|
|
const http = config.get('webserver.https') ? 'https' : 'http'
|
|
|
|
const host = config.get('webserver.host')
|
|
|
|
const port = config.get('webserver.port')
|
|
|
|
|
|
|
|
const pods = {
|
2016-02-07 05:23:23 -05:00
|
|
|
addVideoToFriends: addVideoToFriends,
|
|
|
|
hasFriends: hasFriends,
|
|
|
|
makeFriends: makeFriends,
|
|
|
|
quitFriends: quitFriends,
|
|
|
|
removeVideoToFriends: removeVideoToFriends
|
|
|
|
}
|
|
|
|
|
|
|
|
function addVideoToFriends (video) {
|
|
|
|
// To avoid duplicates
|
2016-03-16 17:29:27 -04:00
|
|
|
const id = video.name + video.magnetUri
|
2016-02-07 05:23:23 -05:00
|
|
|
// ensure namePath is null
|
|
|
|
video.namePath = null
|
|
|
|
poolRequests.addRequest(id, 'add', video)
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasFriends (callback) {
|
|
|
|
Pods.count(function (err, count) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const has_friends = (count !== 0)
|
2016-02-07 05:23:23 -05:00
|
|
|
callback(null, has_friends)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeFriends (callback) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const pods_score = {}
|
2016-02-07 05:23:23 -05:00
|
|
|
|
|
|
|
logger.info('Make friends!')
|
|
|
|
fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot read public cert.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const urls = config.get('network.friends')
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-29 12:52:12 -05:00
|
|
|
async.each(urls, function (url, callback) {
|
|
|
|
computeForeignPodsList(url, pods_score, callback)
|
|
|
|
}, function (err) {
|
2016-02-04 15:10:33 -05:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.debug('Pods scores computed.', { pods_score: pods_score })
|
2016-03-16 17:29:27 -04:00
|
|
|
const pods_list = computeWinningPods(urls, pods_score)
|
|
|
|
logger.debug('Pods that we keep.', { pods_to_keep: pods_list })
|
2016-02-07 05:23:23 -05:00
|
|
|
|
2016-02-29 12:52:12 -05:00
|
|
|
makeRequestsToWinningPods(cert, pods_list, callback)
|
2016-02-04 15:10:33 -05:00
|
|
|
})
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function quitFriends (callback) {
|
|
|
|
// Stop pool requests
|
|
|
|
poolRequests.deactivate()
|
|
|
|
// Flush pool requests
|
|
|
|
poolRequests.forceSend()
|
|
|
|
|
|
|
|
Pods.list(function (err, pods) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const request = {
|
2016-02-07 05:23:23 -05:00
|
|
|
method: 'POST',
|
|
|
|
path: '/api/' + constants.API_VERSION + '/pods/remove',
|
|
|
|
sign: true,
|
|
|
|
encrypt: true,
|
|
|
|
data: {
|
|
|
|
url: 'me' // Fake data
|
2016-02-04 15:10:33 -05:00
|
|
|
}
|
2016-02-07 05:23:23 -05:00
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// Announce we quit them
|
|
|
|
requests.makeMultipleRetryRequest(request, pods, function () {
|
|
|
|
Pods.removeAll(function (err) {
|
|
|
|
poolRequests.activate()
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
if (err) return callback(err)
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.info('Broke friends, so sad :(')
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
Videos.removeAllRemotes(function (err) {
|
|
|
|
if (err) return callback(err)
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
logger.info('Removed all remote videos.')
|
|
|
|
callback(null)
|
2016-02-04 15:10:33 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2016-02-07 05:23:23 -05:00
|
|
|
})
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function removeVideoToFriends (video) {
|
|
|
|
// To avoid duplicates
|
2016-03-16 17:29:27 -04:00
|
|
|
const id = video.name + video.magnetUri
|
2016-02-07 05:23:23 -05:00
|
|
|
poolRequests.addRequest(id, 'remove', video)
|
|
|
|
}
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
module.exports = pods
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-29 12:52:12 -05:00
|
|
|
function computeForeignPodsList (url, pods_score, callback) {
|
|
|
|
// Let's give 1 point to the pod we ask the friends list
|
|
|
|
pods_score[url] = 1
|
|
|
|
|
|
|
|
getForeignPodsList(url, function (err, foreign_pods_list) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
if (foreign_pods_list.length === 0) return callback()
|
|
|
|
|
|
|
|
async.each(foreign_pods_list, function (foreign_pod, callback_each) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const foreign_url = foreign_pod.url
|
2016-02-29 12:52:12 -05:00
|
|
|
|
|
|
|
if (pods_score[foreign_url]) pods_score[foreign_url]++
|
|
|
|
else pods_score[foreign_url] = 1
|
|
|
|
|
|
|
|
callback_each()
|
|
|
|
}, function () {
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function computeWinningPods (urls, pods_score) {
|
|
|
|
// Build the list of pods to add
|
|
|
|
// Only add a pod if it exists in more than a half base pods
|
2016-03-16 17:29:27 -04:00
|
|
|
const pods_list = []
|
|
|
|
const base_score = urls.length / 2
|
2016-02-29 12:52:12 -05:00
|
|
|
Object.keys(pods_score).forEach(function (pod) {
|
|
|
|
if (pods_score[pod] > base_score) pods_list.push({ url: pod })
|
|
|
|
})
|
|
|
|
|
|
|
|
return pods_list
|
|
|
|
}
|
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
function getForeignPodsList (url, callback) {
|
2016-03-16 17:29:27 -04:00
|
|
|
const path = '/api/' + constants.API_VERSION + '/pods'
|
2016-02-04 15:10:33 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
request.get(url + path, function (err, response, body) {
|
|
|
|
if (err) return callback(err)
|
2016-02-05 13:02:05 -05:00
|
|
|
|
2016-02-07 05:23:23 -05:00
|
|
|
callback(null, JSON.parse(body))
|
|
|
|
})
|
|
|
|
}
|
2016-02-29 12:52:12 -05:00
|
|
|
|
|
|
|
function makeRequestsToWinningPods (cert, pods_list, callback) {
|
|
|
|
// Stop pool requests
|
|
|
|
poolRequests.deactivate()
|
|
|
|
// Flush pool requests
|
|
|
|
poolRequests.forceSend()
|
|
|
|
|
|
|
|
// Get the list of our videos to send to our new friends
|
|
|
|
Videos.listOwned(function (err, videos_list) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get the list of videos we own.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2016-03-16 17:29:27 -04:00
|
|
|
const data = {
|
2016-02-29 12:52:12 -05:00
|
|
|
url: http + '://' + host + ':' + port,
|
|
|
|
publicKey: cert,
|
|
|
|
videos: videos_list
|
|
|
|
}
|
|
|
|
|
|
|
|
requests.makeMultipleRetryRequest(
|
|
|
|
{ method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
|
|
|
|
|
|
|
|
pods_list,
|
|
|
|
|
|
|
|
function eachRequest (err, response, body, url, pod, callback_each_request) {
|
|
|
|
// We add the pod if it responded correctly with its public certificate
|
|
|
|
if (!err && response.statusCode === 200) {
|
|
|
|
Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Error with adding %s pod.', pod.url, { error: err })
|
|
|
|
return callback_each_request()
|
|
|
|
}
|
|
|
|
|
|
|
|
Videos.addRemotes(body.videos, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Error with adding videos of pod.', pod.url, { error: err })
|
|
|
|
return callback_each_request()
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
|
|
|
|
return callback_each_request()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
|
|
|
|
return callback_each_request()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
function endRequests (err) {
|
|
|
|
// Now we made new friends, we can re activate the pool of requests
|
|
|
|
poolRequests.activate()
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
logger.error('There was some errors when we wanted to make friends.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('makeRequestsToWinningPods finished.')
|
|
|
|
return callback(null)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|