Pod URL -> pod host. HTTPS is required to make friends.

Reason: in a network with mix http/https pods, https pods won't be able
to play videos from http pod (insecure requests).
This commit is contained in:
Chocobozzz
2016-11-14 20:03:04 +01:00
parent 41b5da1d8c
commit 49abbbbedc
24 changed files with 167 additions and 195 deletions

View File

@@ -20,14 +20,14 @@ const Pod = mongoose.model('Pod')
router.get('/', listPods)
router.post('/',
validators.podsAdd,
podsMiddleware.setBodyUrlPort,
podsMiddleware.setBodyHostPort,
addPods
)
router.post('/makefriends',
oAuth.authenticate,
admin.ensureIsAdmin,
validators.makeFriends,
podsMiddleware.setBodyUrlsPort,
podsMiddleware.setBodyHostsPort,
makeFriends
)
router.get('/quitfriends',
@@ -84,17 +84,17 @@ function addPods (req, res, next) {
}
function listPods (req, res, next) {
Pod.list(function (err, podsUrlList) {
Pod.list(function (err, podsList) {
if (err) return next(err)
res.json(getFormatedPods(podsUrlList))
res.json(getFormatedPods(podsList))
})
}
function makeFriends (req, res, next) {
const urls = req.body.urls
const hosts = req.body.hosts
friends.makeFriends(urls, function (err) {
friends.makeFriends(hosts, function (err) {
if (err) {
logger.error('Could not make friends.', { error: err })
return
@@ -107,11 +107,11 @@ function makeFriends (req, res, next) {
}
function removePods (req, res, next) {
const url = req.body.signature.url
const host = req.body.signature.host
waterfall([
function loadPod (callback) {
Pod.loadByUrl(url, callback)
Pod.loadByHost(host, callback)
},
function removePod (pod, callback) {

View File

@@ -30,7 +30,7 @@ module.exports = router
function remoteVideos (req, res, next) {
const requests = req.body.data
const fromUrl = req.body.signature.url
const fromHost = req.body.signature.host
// We need to process in the same order to keep consistency
// TODO: optimization
@@ -40,7 +40,7 @@ function remoteVideos (req, res, next) {
if (request.type === 'add') {
addRemoteVideo(videoData, callbackEach)
} else if (request.type === 'remove') {
removeRemoteVideo(videoData, fromUrl, callbackEach)
removeRemoteVideo(videoData, fromHost, callbackEach)
} else {
logger.error('Unkown remote request type %s.', request.type)
}
@@ -62,16 +62,16 @@ function addRemoteVideo (videoToCreateData, callback) {
video.save(callback)
}
function removeRemoteVideo (videoToRemoveData, fromUrl, callback) {
function removeRemoteVideo (videoToRemoveData, fromHost, callback) {
// We need the list because we have to remove some other stuffs (thumbnail etc)
Video.listByUrlAndRemoteId(fromUrl, videoToRemoveData.remoteId, function (err, videosList) {
Video.listByHostAndRemoteId(fromHost, videoToRemoveData.remoteId, function (err, videosList) {
if (err) {
logger.error('Cannot list videos from url and magnets.', { error: err })
logger.error('Cannot list videos from host and magnets.', { error: err })
return callback(err)
}
if (videosList.length === 0) {
logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podUrl: fromUrl })
logger.error('No remote video was found for this pod.', { magnetUri: videoToRemoveData.magnetUri, podHost: fromHost })
}
each(videosList, function (video, callbackEach) {

View File

@@ -5,14 +5,14 @@ const validator = require('express-validator').validator
const miscValidators = require('./misc')
const podsValidators = {
isEachUniqueUrlValid
isEachUniqueHostValid
}
function isEachUniqueUrlValid (urls) {
return miscValidators.isArray(urls) &&
urls.length !== 0 &&
urls.every(function (url) {
return validator.isURL(url) && urls.indexOf(url) === urls.lastIndexOf(url)
function isEachUniqueHostValid (hosts) {
return miscValidators.isArray(hosts) &&
hosts.length !== 0 &&
hosts.every(function (host) {
return validator.isURL(host) && host.split('://').length === 1 && hosts.indexOf(host) === hosts.lastIndexOf(host)
})
}

View File

@@ -15,7 +15,7 @@ const videosValidators = {
isVideoDurationValid,
isVideoMagnetValid,
isVideoNameValid,
isVideoPodUrlValid,
isVideoPodHostValid,
isVideoTagsValid,
isVideoThumbnailValid,
isVideoThumbnail64Valid
@@ -33,7 +33,7 @@ function isEachRemoteVideosValid (requests) {
isVideoDurationValid(video.duration) &&
isVideoMagnetValid(video.magnet) &&
isVideoNameValid(video.name) &&
isVideoPodUrlValid(video.podUrl) &&
isVideoPodHostValid(video.podHost) &&
isVideoTagsValid(video.tags) &&
isVideoThumbnail64Valid(video.thumbnailBase64) &&
isVideoRemoteIdValid(video.remoteId)
@@ -70,7 +70,7 @@ function isVideoNameValid (value) {
return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
}
function isVideoPodUrlValid (value) {
function isVideoPodHostValid (value) {
// TODO: set options (TLD...)
return validator.isURL(value)
}

View File

@@ -25,7 +25,7 @@ function makeRetryRequest (params, callback) {
function makeSecureRequest (params, callback) {
const requestParams = {
url: params.toPod.url + params.path
url: constants.REMOTE_SCHEME.HTTP + '://' + params.toPod.host + params.path
}
// Add data with POST requst ?
@@ -34,9 +34,11 @@ function makeSecureRequest (params, callback) {
// Add signature if it is specified in the params
if (params.sign === true) {
const host = constants.CONFIG.WEBSERVER.HOST
requestParams.json.signature = {
url: constants.CONFIG.WEBSERVER.URL,
signature: peertubeCrypto.sign(constants.CONFIG.WEBSERVER.URL)
host,
signature: peertubeCrypto.sign(host)
}
}

View File

@@ -14,7 +14,7 @@ const PAGINATION_COUNT_DEFAULT = 15
// Sortable columns per schema
const SEARCHABLE_COLUMNS = {
VIDEOS: [ 'name', 'magnetUri', 'podUrl', 'author', 'tags' ]
VIDEOS: [ 'name', 'magnetUri', 'podHost', 'author', 'tags' ]
}
// Sortable columns per schema
@@ -55,6 +55,7 @@ const CONFIG = {
}
}
CONFIG.WEBSERVER.URL = CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
CONFIG.WEBSERVER.HOST = CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
// ---------------------------------------------------------------------------

View File

@@ -6,7 +6,6 @@ const eachSeries = require('async/eachSeries')
const fs = require('fs')
const mongoose = require('mongoose')
const request = require('request')
const urlUtil = require('url')
const waterfall = require('async/waterfall')
const constants = require('../initializers/constants')
@@ -44,7 +43,7 @@ function getMyCertificate (callback) {
fs.readFile(constants.CONFIG.STORAGE.CERT_DIR + 'peertube.pub', 'utf8', callback)
}
function makeFriends (urls, callback) {
function makeFriends (hosts, callback) {
const podsScore = {}
logger.info('Make friends!')
@@ -54,13 +53,13 @@ function makeFriends (urls, callback) {
return callback(err)
}
eachSeries(urls, function (url, callbackEach) {
computeForeignPodsList(url, podsScore, callbackEach)
eachSeries(hosts, function (host, callbackEach) {
computeForeignPodsList(host, podsScore, callbackEach)
}, function (err) {
if (err) return callback(err)
logger.debug('Pods scores computed.', { podsScore: podsScore })
const podsList = computeWinningPods(urls, podsScore)
const podsList = computeWinningPods(hosts, podsScore)
logger.debug('Pods that we keep.', { podsToKeep: podsList })
makeRequestsToWinningPods(cert, podsList, callback)
@@ -149,45 +148,45 @@ module.exports = friends
// ---------------------------------------------------------------------------
function computeForeignPodsList (url, podsScore, callback) {
getForeignPodsList(url, function (err, foreignPodsList) {
function computeForeignPodsList (host, podsScore, callback) {
getForeignPodsList(host, function (err, foreignPodsList) {
if (err) return callback(err)
if (!foreignPodsList) foreignPodsList = []
// Let's give 1 point to the pod we ask the friends list
foreignPodsList.push({ url: url })
foreignPodsList.push({ host })
foreignPodsList.forEach(function (foreignPod) {
const foreignPodUrl = foreignPod.url
const foreignPodHost = foreignPod.host
if (podsScore[foreignPodUrl]) podsScore[foreignPodUrl]++
else podsScore[foreignPodUrl] = 1
if (podsScore[foreignPodHost]) podsScore[foreignPodHost]++
else podsScore[foreignPodHost] = 1
})
callback()
})
}
function computeWinningPods (urls, podsScore) {
function computeWinningPods (hosts, podsScore) {
// Build the list of pods to add
// Only add a pod if it exists in more than a half base pods
const podsList = []
const baseScore = urls.length / 2
Object.keys(podsScore).forEach(function (podUrl) {
const baseScore = hosts.length / 2
Object.keys(podsScore).forEach(function (podHost) {
// If the pod is not me and with a good score we add it
if (isMe(podUrl) === false && podsScore[podUrl] > baseScore) {
podsList.push({ url: podUrl })
if (isMe(podHost) === false && podsScore[podHost] > baseScore) {
podsList.push({ host: podHost })
}
})
return podsList
}
function getForeignPodsList (url, callback) {
function getForeignPodsList (host, callback) {
const path = '/api/' + constants.API_VERSION + '/pods'
request.get(url + path, function (err, response, body) {
request.get(constants.REMOTE_SCHEME.HTTP + '://' + host + path, function (err, response, body) {
if (err) return callback(err)
try {
@@ -207,26 +206,26 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
const params = {
url: pod.url + '/api/' + constants.API_VERSION + '/pods/',
url: constants.REMOTE_SCHEME.HTTP + '://' + pod.host + '/api/' + constants.API_VERSION + '/pods/',
method: 'POST',
json: {
url: constants.CONFIG.WEBSERVER.URL,
host: constants.CONFIG.WEBSERVER.HOST,
publicKey: cert
}
}
requests.makeRetryRequest(params, function (err, res, body) {
if (err) {
logger.error('Error with adding %s pod.', pod.url, { error: err })
logger.error('Error with adding %s pod.', pod.host, { error: err })
// Don't break the process
return callbackEach()
}
if (res.statusCode === 200) {
const podObj = new Pod({ url: pod.url, publicKey: body.cert })
const podObj = new Pod({ host: pod.host, publicKey: body.cert })
podObj.save(function (err, podCreated) {
if (err) {
logger.error('Cannot add friend %s pod.', pod.url, { error: err })
logger.error('Cannot add friend %s pod.', pod.host, { error: err })
return callbackEach()
}
@@ -236,7 +235,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
return callbackEach()
})
} else {
logger.error('Status not 200 for %s pod.', pod.url)
logger.error('Status not 200 for %s pod.', pod.host)
return callbackEach()
}
})
@@ -268,14 +267,6 @@ function createRequest (type, endpoint, data, to) {
})
}
function isMe (url) {
const parsedUrl = urlUtil.parse(url)
const hostname = parsedUrl.hostname
const port = parseInt(parsedUrl.port)
const myHostname = constants.CONFIG.WEBSERVER.HOSTNAME
const myPort = constants.CONFIG.WEBSERVER.PORT
return hostname === myHostname && port === myPort
function isMe (host) {
return host === constants.CONFIG.WEBSERVER.HOST
}

View File

@@ -1,38 +1,36 @@
'use strict'
const urlModule = require('url')
const logger = require('../helpers/logger')
const constants = require('../initializers/constants')
const podsMiddleware = {
setBodyUrlsPort,
setBodyUrlPort
setBodyHostsPort,
setBodyHostPort
}
function setBodyUrlsPort (req, res, next) {
for (let i = 0; i < req.body.urls.length; i++) {
const urlWithPort = getUrlWithPort(req.body.urls[i])
function setBodyHostsPort (req, res, next) {
for (let i = 0; i < req.body.hosts.length; i++) {
const hostWithPort = getHostWithPort(req.body.hosts[i])
// Problem with the url parsing?
if (urlWithPort === null) {
if (hostWithPort === null) {
return res.sendStatus(500)
}
req.body.urls[i] = urlWithPort
req.body.hosts[i] = hostWithPort
}
return next()
}
function setBodyUrlPort (req, res, next) {
const urlWithPort = getUrlWithPort(req.body.url)
function setBodyHostPort (req, res, next) {
const hostWithPort = getHostWithPort(req.body.host)
// Problem with the url parsing?
if (urlWithPort === null) {
if (hostWithPort === null) {
return res.sendStatus(500)
}
req.body.url = urlWithPort
req.body.host = hostWithPort
return next()
}
@@ -43,20 +41,16 @@ module.exports = podsMiddleware
// ---------------------------------------------------------------------------
function getUrlWithPort (url) {
const urlObj = urlModule.parse(url)
function getHostWithPort (host) {
const splitted = host.split(':')
// Add the port if it is not specified
if (urlObj.port === null) {
if (urlObj.protocol === 'http:') {
return url + ':80'
} else if (urlObj.protocol === 'https:') {
return url + ':443'
} else {
logger.error('Unknown url protocol: ' + urlObj.protocol)
return null
}
console.log(splitted)
// The port was not specified
if (splitted.length === 1) {
if (constants.REMOTE_SCHEME.HTTP === 'https') return host + ':443'
return host + ':80'
}
return url
return host
}

View File

@@ -12,27 +12,27 @@ const secureMiddleware = {
}
function checkSignature (req, res, next) {
const url = req.body.signature.url
Pod.loadByUrl(url, function (err, pod) {
const host = req.body.signature.host
Pod.loadByHost(host, function (err, pod) {
if (err) {
logger.error('Cannot get signed url in decryptBody.', { error: err })
logger.error('Cannot get signed host in decryptBody.', { error: err })
return res.sendStatus(500)
}
if (pod === null) {
logger.error('Unknown pod %s.', url)
logger.error('Unknown pod %s.', host)
return res.sendStatus(403)
}
logger.debug('Decrypting body from %s.', url)
logger.debug('Decrypting body from %s.', host)
const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, url, req.body.signature.signature)
const signatureOk = peertubeCrypto.checkSignature(pod.publicKey, host, req.body.signature.signature)
if (signatureOk === true) {
return next()
}
logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.url)
logger.error('Signature is not okay in decryptBody for %s.', req.body.signature.host)
return res.sendStatus(403)
})
}

View File

@@ -10,7 +10,7 @@ const validatorsPod = {
}
function makeFriends (req, res, next) {
req.checkBody('urls', 'Should have an array of unique urls').isEachUniqueUrlValid()
req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
logger.debug('Checking makeFriends parameters', { parameters: req.body })
@@ -32,7 +32,7 @@ function makeFriends (req, res, next) {
}
function podsAdd (req, res, next) {
req.checkBody('url', 'Should have an url').notEmpty().isURL({ require_protocol: true })
req.checkBody('host', 'Should have an host').notEmpty().isURL()
req.checkBody('publicKey', 'Should have a public key').notEmpty()
// TODO: check we don't have it already

View File

@@ -27,10 +27,10 @@ function remoteVideos (req, res, next) {
}
function signature (req, res, next) {
req.checkBody('signature.url', 'Should have a signature url').isURL()
req.checkBody('signature.host', 'Should have a signature host').isURL()
req.checkBody('signature.signature', 'Should have a signature').notEmpty()
logger.debug('Checking signature parameters', { parameters: { signatureUrl: req.body.signature.url } })
logger.debug('Checking signature parameters', { parameters: { signatureHost: req.body.signature.host } })
checkErrors(req, res, next)
}

View File

@@ -12,7 +12,7 @@ const Video = mongoose.model('Video')
// ---------------------------------------------------------------------------
const PodSchema = mongoose.Schema({
url: String,
host: String,
publicKey: String,
score: { type: Number, max: constants.FRIEND_SCORE.MAX },
createdDate: {
@@ -21,8 +21,7 @@ const PodSchema = mongoose.Schema({
}
})
// TODO: set options (TLD...)
PodSchema.path('url').validate(validator.isURL)
PodSchema.path('host').validate(validator.isURL)
PodSchema.path('publicKey').required(true)
PodSchema.path('score').validate(function (value) { return !isNaN(value) })
@@ -37,14 +36,14 @@ PodSchema.statics = {
listAllIds,
listBadPods,
load,
loadByUrl,
loadByHost,
removeAll
}
PodSchema.pre('save', function (next) {
const self = this
Pod.loadByUrl(this.url, function (err, pod) {
Pod.loadByHost(this.host, function (err, pod) {
if (err) return next(err)
if (pod) return next(new Error('Pod already exists.'))
@@ -56,7 +55,7 @@ PodSchema.pre('save', function (next) {
PodSchema.pre('remove', function (next) {
// Remove the videos owned by this pod too
Video.listByUrl(this.url, function (err, videos) {
Video.listByHost(this.host, function (err, videos) {
if (err) return next(err)
each(videos, function (video, callbackEach) {
@@ -72,7 +71,7 @@ const Pod = mongoose.model('Pod', PodSchema)
function toFormatedJSON () {
const json = {
id: this._id,
url: this.url,
host: this.host,
score: this.score,
createdDate: this.createdDate
}
@@ -111,8 +110,8 @@ function load (id, callback) {
return this.findById(id, callback)
}
function loadByUrl (url, callback) {
return this.findOne({ url: url }, callback)
function loadByHost (host, callback) {
return this.findOne({ host }, callback)
}
function removeAll (callback) {

View File

@@ -121,7 +121,7 @@ function makeRequest (toPod, requestEndpoint, requestsToMake, callback) {
if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
logger.error(
'Error sending secure request to %s pod.',
toPod.url,
toPod.host,
{
error: err || new Error('Status code not 20x : ' + res.statusCode)
}

View File

@@ -28,10 +28,9 @@ const VideoSchema = mongoose.Schema({
magnet: {
infoHash: String
},
podUrl: String,
podHost: String,
author: String,
duration: Number,
thumbnail: String,
tags: [ String ],
createdDate: {
type: Date,
@@ -41,14 +40,9 @@ const VideoSchema = mongoose.Schema({
VideoSchema.path('name').validate(customVideosValidators.isVideoNameValid)
VideoSchema.path('description').validate(customVideosValidators.isVideoDescriptionValid)
VideoSchema.path('podUrl').validate(customVideosValidators.isVideoPodUrlValid)
VideoSchema.path('podHost').validate(customVideosValidators.isVideoPodHostValid)
VideoSchema.path('author').validate(customVideosValidators.isVideoAuthorValid)
VideoSchema.path('duration').validate(customVideosValidators.isVideoDurationValid)
// The tumbnail can be the path or the data in base 64
// The pre save hook will convert the base 64 data in a file on disk and replace the thumbnail key by the filename
VideoSchema.path('thumbnail').validate(function (value) {
return customVideosValidators.isVideoThumbnailValid(value) || customVideosValidators.isVideoThumbnail64Valid(value)
})
VideoSchema.path('tags').validate(customVideosValidators.isVideoTagsValid)
VideoSchema.methods = {
@@ -65,8 +59,8 @@ VideoSchema.methods = {
VideoSchema.statics = {
getDurationFromFile,
listForApi,
listByUrlAndRemoteId,
listByUrl,
listByHostAndRemoteId,
listByHost,
listOwned,
listOwnedByAuthor,
listRemotes,
@@ -107,7 +101,7 @@ VideoSchema.pre('save', function (next) {
if (video.isOwned()) {
const videoPath = pathUtils.join(constants.CONFIG.STORAGE.VIDEOS_DIR, video.getVideoFilename())
this.podUrl = constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
this.podHost = constants.CONFIG.WEBSERVER.HOST
tasks.push(
// TODO: refractoring
@@ -160,8 +154,8 @@ function generateMagnetUri () {
baseUrlHttp = constants.CONFIG.WEBSERVER.URL
baseUrlWs = constants.CONFIG.WEBSERVER.WS + '://' + constants.CONFIG.WEBSERVER.HOSTNAME + ':' + constants.CONFIG.WEBSERVER.PORT
} else {
baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.podUrl
baseUrlWs = constants.REMOTE_SCHEME.WS + this.podUrl
baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + this.podHost
baseUrlWs = constants.REMOTE_SCHEME.WS + this.podHost
}
const xs = baseUrlHttp + constants.STATIC_PATHS.TORRENTS + this.getTorrentName()
@@ -215,7 +209,7 @@ function toFormatedJSON () {
id: this._id,
name: this.name,
description: this.description,
podUrl: this.podUrl,
podHost: this.podHost,
isLocal: this.isOwned(),
magnetUri: this.generateMagnetUri(),
author: this.author,
@@ -249,7 +243,7 @@ function toRemoteJSON (callback) {
thumbnailBase64: new Buffer(thumbnailData).toString('base64'),
tags: self.tags,
createdDate: self.createdDate,
podUrl: self.podUrl
podHost: self.podHost
}
return callback(null, remoteVideo)
@@ -271,12 +265,12 @@ function listForApi (start, count, sort, callback) {
return modelUtils.listForApiWithCount.call(this, query, start, count, sort, callback)
}
function listByUrlAndRemoteId (fromUrl, remoteId, callback) {
this.find({ podUrl: fromUrl, remoteId: remoteId }, callback)
function listByHostAndRemoteId (fromHost, remoteId, callback) {
this.find({ podHost: fromHost, remoteId: remoteId }, callback)
}
function listByUrl (fromUrl, callback) {
this.find({ podUrl: fromUrl }, callback)
function listByHost (fromHost, callback) {
this.find({ podHost: fromHost }, callback)
}
function listOwned (callback) {