2016-02-07 04:23:23 -06:00
|
|
|
'use strict'
|
|
|
|
|
2016-03-16 16:29:27 -05: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')
|
2016-05-02 10:25:05 -05:00
|
|
|
const requestsScheduler = require('../lib/requestsScheduler')
|
2016-03-16 16:29:27 -05:00
|
|
|
const requests = require('../helpers/requests')
|
2016-05-10 14:19:24 -05:00
|
|
|
const videos = require('../lib/videos')
|
2016-03-16 16:29:27 -05:00
|
|
|
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 04:23:23 -06:00
|
|
|
addVideoToFriends: addVideoToFriends,
|
|
|
|
hasFriends: hasFriends,
|
2016-05-10 14:19:24 -05:00
|
|
|
getMyCertificate: getMyCertificate,
|
2016-02-07 04:23:23 -06:00
|
|
|
makeFriends: makeFriends,
|
|
|
|
quitFriends: quitFriends,
|
|
|
|
removeVideoToFriends: removeVideoToFriends
|
|
|
|
}
|
|
|
|
|
|
|
|
function addVideoToFriends (video) {
|
|
|
|
// To avoid duplicates
|
2016-03-16 16:29:27 -05:00
|
|
|
const id = video.name + video.magnetUri
|
2016-02-07 04:23:23 -06:00
|
|
|
// ensure namePath is null
|
|
|
|
video.namePath = null
|
2016-05-02 10:25:05 -05:00
|
|
|
requestsScheduler.addRequest(id, 'add', video)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function hasFriends (callback) {
|
|
|
|
Pods.count(function (err, count) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
const hasFriends = (count !== 0)
|
|
|
|
callback(null, hasFriends)
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-10 14:19:24 -05:00
|
|
|
function getMyCertificate (callback) {
|
|
|
|
fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', callback)
|
|
|
|
}
|
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function makeFriends (callback) {
|
2016-05-11 14:19:34 -05:00
|
|
|
const podsScore = {}
|
2016-02-07 04:23:23 -06:00
|
|
|
|
|
|
|
logger.info('Make friends!')
|
2016-05-10 14:19:24 -05:00
|
|
|
getMyCertificate(function (err, cert) {
|
2016-02-07 04:23:23 -06:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot read public cert.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const urls = config.get('network.friends')
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
async.each(urls, function (url, callbackEach) {
|
|
|
|
computeForeignPodsList(url, podsScore, callbackEach)
|
2016-02-29 11:52:12 -06:00
|
|
|
}, function (err) {
|
2016-02-04 14:10:33 -06:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
logger.debug('Pods scores computed.', { podsScore: podsScore })
|
|
|
|
const podsList = computeWinningPods(urls, podsScore)
|
|
|
|
logger.debug('Pods that we keep.', { podsToKeep: podsList })
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
makeRequestsToWinningPods(cert, podsList, callback)
|
2016-02-04 14:10:33 -06:00
|
|
|
})
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function quitFriends (callback) {
|
|
|
|
// Stop pool requests
|
2016-05-02 10:25:05 -05:00
|
|
|
requestsScheduler.deactivate()
|
2016-02-07 04:23:23 -06:00
|
|
|
// Flush pool requests
|
2016-05-02 10:25:05 -05:00
|
|
|
requestsScheduler.forceSend()
|
2016-02-07 04:23:23 -06:00
|
|
|
|
2016-05-15 03:42:17 -05:00
|
|
|
async.waterfall([
|
|
|
|
function getPodsList (callbackAsync) {
|
|
|
|
return Pods.list(callbackAsync)
|
|
|
|
},
|
|
|
|
|
|
|
|
function announceIQuitMyFriends (pods, callbackAsync) {
|
|
|
|
const request = {
|
|
|
|
method: 'POST',
|
|
|
|
path: '/api/' + constants.API_VERSION + '/pods/remove',
|
|
|
|
sign: true,
|
|
|
|
encrypt: true,
|
|
|
|
data: {
|
|
|
|
url: 'me' // Fake data
|
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
}
|
|
|
|
|
2016-05-15 03:42:17 -05:00
|
|
|
// Announce we quit them
|
|
|
|
requests.makeMultipleRetryRequest(request, pods, function (err) {
|
|
|
|
return callbackAsync(err)
|
|
|
|
})
|
|
|
|
},
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-05-15 03:42:17 -05:00
|
|
|
function removePodsFromDB (callbackAsync) {
|
|
|
|
Pods.removeAll(function (err) {
|
|
|
|
return callbackAsync(err)
|
|
|
|
})
|
|
|
|
},
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-05-15 03:42:17 -05:00
|
|
|
function listRemoteVideos (callbackAsync) {
|
|
|
|
logger.info('Broke friends, so sad :(')
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-05-15 03:42:17 -05:00
|
|
|
Videos.listFromRemotes(callbackAsync)
|
|
|
|
},
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-05-15 03:42:17 -05:00
|
|
|
function removeTheRemoteVideos (videosList, callbackAsync) {
|
|
|
|
videos.removeRemoteVideos(videosList, function (err) {
|
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot remove remote videos.', { error: err })
|
|
|
|
return callbackAsync(err)
|
|
|
|
}
|
2016-05-10 14:19:24 -05:00
|
|
|
|
2016-05-15 03:42:17 -05:00
|
|
|
return callbackAsync(null)
|
2016-02-04 14:10:33 -06:00
|
|
|
})
|
2016-05-15 03:42:17 -05:00
|
|
|
}
|
|
|
|
], function (err) {
|
|
|
|
// Don't forget to re activate the scheduler, even if there was an error
|
|
|
|
requestsScheduler.activate()
|
|
|
|
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
logger.info('Removed all remote videos.')
|
|
|
|
return callback(null)
|
2016-02-07 04:23:23 -06:00
|
|
|
})
|
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function removeVideoToFriends (video) {
|
|
|
|
// To avoid duplicates
|
2016-03-16 16:29:27 -05:00
|
|
|
const id = video.name + video.magnetUri
|
2016-05-02 10:25:05 -05:00
|
|
|
requestsScheduler.addRequest(id, 'remove', video)
|
2016-02-07 04:23:23 -06:00
|
|
|
}
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
module.exports = pods
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
function computeForeignPodsList (url, podsScore, callback) {
|
2016-02-29 11:52:12 -06:00
|
|
|
// Let's give 1 point to the pod we ask the friends list
|
2016-05-11 14:19:34 -05:00
|
|
|
podsScore[url] = 1
|
2016-02-29 11:52:12 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
getForeignPodsList(url, function (err, foreignPodsList) {
|
2016-02-29 11:52:12 -06:00
|
|
|
if (err) return callback(err)
|
2016-05-11 14:19:34 -05:00
|
|
|
if (foreignPodsList.length === 0) return callback()
|
2016-02-29 11:52:12 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
foreignPodsList.forEach(function (foreignPod) {
|
|
|
|
const foreignUrl = foreignPod.url
|
2016-02-29 11:52:12 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
if (podsScore[foreignUrl]) podsScore[foreignUrl]++
|
|
|
|
else podsScore[foreignUrl] = 1
|
2016-02-29 11:52:12 -06:00
|
|
|
})
|
2016-05-10 14:19:24 -05:00
|
|
|
|
|
|
|
callback()
|
2016-02-29 11:52:12 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
function computeWinningPods (urls, podsScore) {
|
2016-02-29 11:52:12 -06:00
|
|
|
// Build the list of pods to add
|
|
|
|
// Only add a pod if it exists in more than a half base pods
|
2016-05-11 14:19:34 -05:00
|
|
|
const podsList = []
|
|
|
|
const baseScore = urls.length / 2
|
2016-05-13 08:02:51 -05:00
|
|
|
Object.keys(podsScore).forEach(function (pod) {
|
2016-05-11 14:19:34 -05:00
|
|
|
if (podsScore[pod] > baseScore) podsList.push({ url: pod })
|
2016-02-29 11:52:12 -06:00
|
|
|
})
|
2016-05-15 03:42:17 -05:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
return podsList
|
2016-02-29 11:52:12 -06:00
|
|
|
}
|
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
function getForeignPodsList (url, callback) {
|
2016-03-16 16:29:27 -05:00
|
|
|
const path = '/api/' + constants.API_VERSION + '/pods'
|
2016-02-04 14:10:33 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
request.get(url + path, function (err, response, body) {
|
|
|
|
if (err) return callback(err)
|
2016-02-05 12:02:05 -06:00
|
|
|
|
2016-02-07 04:23:23 -06:00
|
|
|
callback(null, JSON.parse(body))
|
|
|
|
})
|
|
|
|
}
|
2016-02-29 11:52:12 -06:00
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
function makeRequestsToWinningPods (cert, podsList, callback) {
|
2016-02-29 11:52:12 -06:00
|
|
|
// Stop pool requests
|
2016-05-02 10:25:05 -05:00
|
|
|
requestsScheduler.deactivate()
|
2016-02-29 11:52:12 -06:00
|
|
|
// Flush pool requests
|
2016-05-02 10:25:05 -05:00
|
|
|
requestsScheduler.forceSend()
|
2016-02-29 11:52:12 -06:00
|
|
|
|
|
|
|
// Get the list of our videos to send to our new friends
|
2016-05-11 14:19:34 -05:00
|
|
|
Videos.listOwned(function (err, videosList) {
|
2016-02-29 11:52:12 -06:00
|
|
|
if (err) {
|
|
|
|
logger.error('Cannot get the list of videos we own.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
2016-03-16 16:29:27 -05:00
|
|
|
const data = {
|
2016-02-29 11:52:12 -06:00
|
|
|
url: http + '://' + host + ':' + port,
|
|
|
|
publicKey: cert,
|
2016-05-11 14:19:34 -05:00
|
|
|
videos: videosList
|
2016-02-29 11:52:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
requests.makeMultipleRetryRequest(
|
|
|
|
{ method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
|
|
|
|
|
2016-05-11 14:19:34 -05:00
|
|
|
podsList,
|
2016-02-29 11:52:12 -06:00
|
|
|
|
2016-05-15 03:42:17 -05:00
|
|
|
// Callback called after each request
|
2016-05-11 14:19:34 -05:00
|
|
|
function eachRequest (err, response, body, url, pod, callbackEachRequest) {
|
2016-02-29 11:52:12 -06:00
|
|
|
// 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 })
|
2016-05-11 14:19:34 -05:00
|
|
|
return callbackEachRequest()
|
2016-02-29 11:52:12 -06:00
|
|
|
}
|
2016-05-11 14:19:34 -05:00
|
|
|
|
2016-05-10 14:19:24 -05:00
|
|
|
videos.createRemoteVideos(body.videos, function (err) {
|
2016-02-29 11:52:12 -06:00
|
|
|
if (err) {
|
|
|
|
logger.error('Error with adding videos of pod.', pod.url, { error: err })
|
2016-05-11 14:19:34 -05:00
|
|
|
return callbackEachRequest()
|
2016-02-29 11:52:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
|
2016-05-11 14:19:34 -05:00
|
|
|
return callbackEachRequest()
|
2016-02-29 11:52:12 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
|
2016-05-11 14:19:34 -05:00
|
|
|
return callbackEachRequest()
|
2016-02-29 11:52:12 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-05-15 03:42:17 -05:00
|
|
|
// Final callback, we've ended all the requests
|
2016-02-29 11:52:12 -06:00
|
|
|
function endRequests (err) {
|
|
|
|
// Now we made new friends, we can re activate the pool of requests
|
2016-05-02 10:25:05 -05:00
|
|
|
requestsScheduler.activate()
|
2016-02-29 11:52:12 -06:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
logger.error('There was some errors when we wanted to make friends.')
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('makeRequestsToWinningPods finished.')
|
|
|
|
return callback(null)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|