}
// ----------- PeerTube modules -----------
-const customValidators = require('./server/helpers/customValidators')
+const customValidators = require('./server/helpers/custom-validators')
const installer = require('./server/initializers/installer')
const mongoose = require('mongoose')
const routes = require('./server/controllers')
--- /dev/null
+'use strict'
+
+const validator = require('express-validator').validator
+
+const constants = require('../initializers/constants')
+const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS
+
+const customValidators = {
+ exists: exists,
+ isEachRemoteVideosValid: isEachRemoteVideosValid,
+ isArray: isArray,
+ isVideoAuthorValid: isVideoAuthorValid,
+ isVideoDateValid: isVideoDateValid,
+ isVideoDescriptionValid: isVideoDescriptionValid,
+ isVideoDurationValid: isVideoDurationValid,
+ isVideoMagnetUriValid: isVideoMagnetUriValid,
+ isVideoNameValid: isVideoNameValid,
+ isVideoPodUrlValid: isVideoPodUrlValid,
+ isVideoTagsValid: isVideoTagsValid,
+ isVideoThumbnailValid: isVideoThumbnailValid,
+ isVideoThumbnail64Valid: isVideoThumbnail64Valid
+}
+
+function exists (value) {
+ return value !== undefined && value !== null
+}
+
+function isEachRemoteVideosValid (requests) {
+ return requests.every(function (request) {
+ const video = request.data
+ return (
+ isRequestTypeAddValid(request.type) &&
+ isVideoAuthorValid(video.author) &&
+ isVideoDateValid(video.createdDate) &&
+ isVideoDescriptionValid(video.description) &&
+ isVideoDurationValid(video.duration) &&
+ isVideoMagnetUriValid(video.magnetUri) &&
+ isVideoNameValid(video.name) &&
+ isVideoPodUrlValid(video.podUrl) &&
+ isVideoTagsValid(video.tags) &&
+ isVideoThumbnail64Valid(video.thumbnailBase64)
+ ) ||
+ (
+ isRequestTypeRemoveValid(request.type) &&
+ isVideoNameValid(video.name) &&
+ isVideoMagnetUriValid(video.magnetUri)
+ )
+ })
+}
+
+function isArray (value) {
+ return Array.isArray(value)
+}
+
+function isRequestTypeAddValid (value) {
+ return value === 'add'
+}
+
+function isRequestTypeRemoveValid (value) {
+ return value === 'remove'
+}
+
+function isVideoAuthorValid (value) {
+ return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR)
+}
+
+function isVideoDateValid (value) {
+ return validator.isDate(value)
+}
+
+function isVideoDescriptionValid (value) {
+ return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
+}
+
+function isVideoDurationValid (value) {
+ return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
+}
+
+function isVideoMagnetUriValid (value) {
+ return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI)
+}
+
+function isVideoNameValid (value) {
+ return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
+}
+
+function isVideoPodUrlValid (value) {
+ // TODO: set options (TLD...)
+ return validator.isURL(value)
+}
+
+function isVideoTagsValid (tags) {
+ return isArray(tags) &&
+ validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
+ tags.every(function (tag) {
+ return validator.isAlphanumeric(tag) &&
+ validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
+ })
+}
+
+function isVideoThumbnailValid (value) {
+ return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
+}
+
+function isVideoThumbnail64Valid (value) {
+ return validator.isBase64(value) &&
+ validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64)
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = customValidators
+
+// ---------------------------------------------------------------------------
+++ /dev/null
-'use strict'
-
-const validator = require('express-validator').validator
-
-const constants = require('../initializers/constants')
-const VIDEOS_CONSTRAINTS_FIELDS = constants.VIDEOS_CONSTRAINTS_FIELDS
-
-const customValidators = {
- exists: exists,
- isEachRemoteVideosValid: isEachRemoteVideosValid,
- isArray: isArray,
- isVideoAuthorValid: isVideoAuthorValid,
- isVideoDateValid: isVideoDateValid,
- isVideoDescriptionValid: isVideoDescriptionValid,
- isVideoDurationValid: isVideoDurationValid,
- isVideoMagnetUriValid: isVideoMagnetUriValid,
- isVideoNameValid: isVideoNameValid,
- isVideoPodUrlValid: isVideoPodUrlValid,
- isVideoTagsValid: isVideoTagsValid,
- isVideoThumbnailValid: isVideoThumbnailValid,
- isVideoThumbnail64Valid: isVideoThumbnail64Valid
-}
-
-function exists (value) {
- return value !== undefined && value !== null
-}
-
-function isEachRemoteVideosValid (requests) {
- return requests.every(function (request) {
- const video = request.data
- return (
- isRequestTypeAddValid(request.type) &&
- isVideoAuthorValid(video.author) &&
- isVideoDateValid(video.createdDate) &&
- isVideoDescriptionValid(video.description) &&
- isVideoDurationValid(video.duration) &&
- isVideoMagnetUriValid(video.magnetUri) &&
- isVideoNameValid(video.name) &&
- isVideoPodUrlValid(video.podUrl) &&
- isVideoTagsValid(video.tags) &&
- isVideoThumbnail64Valid(video.thumbnailBase64)
- ) ||
- (
- isRequestTypeRemoveValid(request.type) &&
- isVideoNameValid(video.name) &&
- isVideoMagnetUriValid(video.magnetUri)
- )
- })
-}
-
-function isArray (value) {
- return Array.isArray(value)
-}
-
-function isRequestTypeAddValid (value) {
- return value === 'add'
-}
-
-function isRequestTypeRemoveValid (value) {
- return value === 'remove'
-}
-
-function isVideoAuthorValid (value) {
- return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.AUTHOR)
-}
-
-function isVideoDateValid (value) {
- return validator.isDate(value)
-}
-
-function isVideoDescriptionValid (value) {
- return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.DESCRIPTION)
-}
-
-function isVideoDurationValid (value) {
- return validator.isInt(value + '', VIDEOS_CONSTRAINTS_FIELDS.DURATION)
-}
-
-function isVideoMagnetUriValid (value) {
- return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.MAGNET_URI)
-}
-
-function isVideoNameValid (value) {
- return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.NAME)
-}
-
-function isVideoPodUrlValid (value) {
- // TODO: set options (TLD...)
- return validator.isURL(value)
-}
-
-function isVideoTagsValid (tags) {
- return isArray(tags) &&
- validator.isInt(tags.length, VIDEOS_CONSTRAINTS_FIELDS.TAGS) &&
- tags.every(function (tag) {
- return validator.isAlphanumeric(tag) &&
- validator.isLength(tag, VIDEOS_CONSTRAINTS_FIELDS.TAG)
- })
-}
-
-function isVideoThumbnailValid (value) {
- return validator.isLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL)
-}
-
-function isVideoThumbnail64Valid (value) {
- return validator.isBase64(value) &&
- validator.isByteLength(value, VIDEOS_CONSTRAINTS_FIELDS.THUMBNAIL64)
-}
-
-// ---------------------------------------------------------------------------
-
-module.exports = customValidators
-
-// ---------------------------------------------------------------------------
--- /dev/null
+'use strict'
+
+const config = require('config')
+const crypto = require('crypto')
+const fs = require('fs')
+const openssl = require('openssl-wrapper')
+const path = require('path')
+const ursa = require('ursa')
+
+const logger = require('./logger')
+
+const certDir = path.join(__dirname, '..', '..', config.get('storage.certs'))
+const algorithm = 'aes-256-ctr'
+
+const peertubeCrypto = {
+ checkSignature: checkSignature,
+ createCertsIfNotExist: createCertsIfNotExist,
+ decrypt: decrypt,
+ encrypt: encrypt,
+ getCertDir: getCertDir,
+ sign: sign
+}
+
+function checkSignature (publicKey, rawData, hexSignature) {
+ const crt = ursa.createPublicKey(publicKey)
+ const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex')
+ return isValid
+}
+
+function createCertsIfNotExist (callback) {
+ certsExist(function (exist) {
+ if (exist === true) {
+ return callback(null)
+ }
+
+ createCerts(function (err) {
+ return callback(err)
+ })
+ })
+}
+
+function decrypt (key, data, callback) {
+ fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
+ if (err) return callback(err)
+
+ const myPrivateKey = ursa.createPrivateKey(file)
+ const decryptedKey = myPrivateKey.decrypt(key, 'hex', 'utf8')
+ const decryptedData = symetricDecrypt(data, decryptedKey)
+
+ return callback(null, decryptedData)
+ })
+}
+
+function encrypt (publicKey, data, callback) {
+ const crt = ursa.createPublicKey(publicKey)
+
+ symetricEncrypt(data, function (err, dataEncrypted) {
+ if (err) return callback(err)
+
+ const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
+ const encrypted = {
+ data: dataEncrypted.crypted,
+ key: key
+ }
+
+ callback(null, encrypted)
+ })
+}
+
+function getCertDir () {
+ return certDir
+}
+
+function sign (data) {
+ const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
+ const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
+
+ return signature
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = peertubeCrypto
+
+// ---------------------------------------------------------------------------
+
+function certsExist (callback) {
+ fs.exists(certDir + 'peertube.key.pem', function (exists) {
+ return callback(exists)
+ })
+}
+
+function createCerts (callback) {
+ certsExist(function (exist) {
+ if (exist === true) {
+ const string = 'Certs already exist.'
+ logger.warning(string)
+ return callback(new Error(string))
+ }
+
+ logger.info('Generating a RSA key...')
+ openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
+ if (err) {
+ logger.error('Cannot create private key on this pod.')
+ return callback(err)
+ }
+ logger.info('RSA key generated.')
+
+ logger.info('Manage public key...')
+ openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
+ if (err) {
+ logger.error('Cannot create public key on this pod.')
+ return callback(err)
+ }
+
+ logger.info('Public key managed.')
+ return callback(null)
+ })
+ })
+ })
+}
+
+function generatePassword (callback) {
+ crypto.randomBytes(32, function (err, buf) {
+ if (err) return callback(err)
+
+ callback(null, buf.toString('utf8'))
+ })
+}
+
+function symetricDecrypt (text, password) {
+ const decipher = crypto.createDecipher(algorithm, password)
+ let dec = decipher.update(text, 'hex', 'utf8')
+ dec += decipher.final('utf8')
+ return dec
+}
+
+function symetricEncrypt (text, callback) {
+ generatePassword(function (err, password) {
+ if (err) return callback(err)
+
+ const cipher = crypto.createCipher(algorithm, password)
+ let crypted = cipher.update(text, 'utf8', 'hex')
+ crypted += cipher.final('hex')
+ callback(null, { crypted: crypted, password: password })
+ })
+}
+++ /dev/null
-'use strict'
-
-const config = require('config')
-const crypto = require('crypto')
-const fs = require('fs')
-const openssl = require('openssl-wrapper')
-const path = require('path')
-const ursa = require('ursa')
-
-const logger = require('./logger')
-
-const certDir = path.join(__dirname, '..', '..', config.get('storage.certs'))
-const algorithm = 'aes-256-ctr'
-
-const peertubeCrypto = {
- checkSignature: checkSignature,
- createCertsIfNotExist: createCertsIfNotExist,
- decrypt: decrypt,
- encrypt: encrypt,
- getCertDir: getCertDir,
- sign: sign
-}
-
-function checkSignature (publicKey, rawData, hexSignature) {
- const crt = ursa.createPublicKey(publicKey)
- const isValid = crt.hashAndVerify('sha256', new Buffer(rawData).toString('hex'), hexSignature, 'hex')
- return isValid
-}
-
-function createCertsIfNotExist (callback) {
- certsExist(function (exist) {
- if (exist === true) {
- return callback(null)
- }
-
- createCerts(function (err) {
- return callback(err)
- })
- })
-}
-
-function decrypt (key, data, callback) {
- fs.readFile(getCertDir() + 'peertube.key.pem', function (err, file) {
- if (err) return callback(err)
-
- const myPrivateKey = ursa.createPrivateKey(file)
- const decryptedKey = myPrivateKey.decrypt(key, 'hex', 'utf8')
- const decryptedData = symetricDecrypt(data, decryptedKey)
-
- return callback(null, decryptedData)
- })
-}
-
-function encrypt (publicKey, data, callback) {
- const crt = ursa.createPublicKey(publicKey)
-
- symetricEncrypt(data, function (err, dataEncrypted) {
- if (err) return callback(err)
-
- const key = crt.encrypt(dataEncrypted.password, 'utf8', 'hex')
- const encrypted = {
- data: dataEncrypted.crypted,
- key: key
- }
-
- callback(null, encrypted)
- })
-}
-
-function getCertDir () {
- return certDir
-}
-
-function sign (data) {
- const myKey = ursa.createPrivateKey(fs.readFileSync(certDir + 'peertube.key.pem'))
- const signature = myKey.hashAndSign('sha256', data, 'utf8', 'hex')
-
- return signature
-}
-
-// ---------------------------------------------------------------------------
-
-module.exports = peertubeCrypto
-
-// ---------------------------------------------------------------------------
-
-function certsExist (callback) {
- fs.exists(certDir + 'peertube.key.pem', function (exists) {
- return callback(exists)
- })
-}
-
-function createCerts (callback) {
- certsExist(function (exist) {
- if (exist === true) {
- const string = 'Certs already exist.'
- logger.warning(string)
- return callback(new Error(string))
- }
-
- logger.info('Generating a RSA key...')
- openssl.exec('genrsa', { 'out': certDir + 'peertube.key.pem', '2048': false }, function (err) {
- if (err) {
- logger.error('Cannot create private key on this pod.')
- return callback(err)
- }
- logger.info('RSA key generated.')
-
- logger.info('Manage public key...')
- openssl.exec('rsa', { 'in': certDir + 'peertube.key.pem', 'pubout': true, 'out': certDir + 'peertube.pub' }, function (err) {
- if (err) {
- logger.error('Cannot create public key on this pod.')
- return callback(err)
- }
-
- logger.info('Public key managed.')
- return callback(null)
- })
- })
- })
-}
-
-function generatePassword (callback) {
- crypto.randomBytes(32, function (err, buf) {
- if (err) return callback(err)
-
- callback(null, buf.toString('utf8'))
- })
-}
-
-function symetricDecrypt (text, password) {
- const decipher = crypto.createDecipher(algorithm, password)
- let dec = decipher.update(text, 'hex', 'utf8')
- dec += decipher.final('utf8')
- return dec
-}
-
-function symetricEncrypt (text, callback) {
- generatePassword(function (err, password) {
- if (err) return callback(err)
-
- const cipher = crypto.createCipher(algorithm, password)
- let crypted = cipher.update(text, 'utf8', 'hex')
- crypted += cipher.final('hex')
- callback(null, { crypted: crypted, password: password })
- })
-}
const request = require('request')
const constants = require('../initializers/constants')
-const peertubeCrypto = require('./peertubeCrypto')
+const peertubeCrypto = require('./peertube-crypto')
const http = config.get('webserver.https') ? 'https' : 'http'
const host = config.get('webserver.host')
const checker = require('./checker')
const logger = require('../helpers/logger')
-const peertubeCrypto = require('../helpers/peertubeCrypto')
+const peertubeCrypto = require('../helpers/peertube-crypto')
const Client = mongoose.model('OAuthClient')
const User = mongoose.model('User')
const constants = require('../initializers/constants')
const logger = require('../helpers/logger')
-const peertubeCrypto = require('../helpers/peertubeCrypto')
+const peertubeCrypto = require('../helpers/peertube-crypto')
const requests = require('../helpers/requests')
const http = config.get('webserver.https') ? 'https' : 'http'
--- /dev/null
+'use strict'
+
+const WebTorrent = require('webtorrent')
+const ipc = require('node-ipc')
+
+function webtorrent (args) {
+ if (args.length !== 3) {
+ throw new Error('Wrong arguments number: ' + args.length + '/3')
+ }
+
+ const host = args[1]
+ const port = args[2]
+ const nodeKey = 'webtorrentnode' + port
+ const processKey = 'webtorrentprocess' + port
+
+ ipc.config.silent = true
+ ipc.config.id = processKey
+
+ if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
+ else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
+ const wt = new WebTorrent({ dht: false })
+
+ function seed (data) {
+ const args = data.args
+ const path = args.path
+ const _id = data._id
+
+ wt.seed(path, { announceList: '' }, function (torrent) {
+ const toSend = {
+ magnetUri: torrent.magnetURI
+ }
+
+ ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, toSend)
+ })
+ }
+
+ function add (data) {
+ const args = data.args
+ const magnetUri = args.magnetUri
+ const _id = data._id
+
+ wt.add(magnetUri, function (torrent) {
+ const toSend = {
+ files: []
+ }
+
+ torrent.files.forEach(function (file) {
+ toSend.files.push({ path: file.path })
+ })
+
+ ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, toSend)
+ })
+ }
+
+ function remove (data) {
+ const args = data.args
+ const magnetUri = args.magnetUri
+ const _id = data._id
+
+ try {
+ wt.remove(magnetUri, callback)
+ } catch (err) {
+ console.log('Cannot remove the torrent from WebTorrent.')
+ return callback(null)
+ }
+
+ function callback () {
+ const toSend = {}
+ ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, toSend)
+ }
+ }
+
+ console.log('Configuration: ' + host + ':' + port)
+ console.log('Connecting to IPC...')
+
+ ipc.connectTo(nodeKey, function () {
+ ipc.of[nodeKey].on(processKey + '.seed', seed)
+ ipc.of[nodeKey].on(processKey + '.add', add)
+ ipc.of[nodeKey].on(processKey + '.remove', remove)
+
+ ipc.of[nodeKey].emit(processKey + '.ready')
+ console.log('Ready.')
+ })
+
+ process.on('uncaughtException', function (e) {
+ ipc.of[nodeKey].emit(processKey + '.exception', { exception: e.toString() })
+ })
+}
+
+// ---------------------------------------------------------------------------
+
+module.exports = webtorrent
throw new Error('Received exception error from webtorrent process : ' + data.exception)
})
- const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true })
+ const webtorrentProcess = spawn(pathUtils.join(__dirname, 'webtorrent-process.js'), host, port, { detached: true })
if (electronDebug === true) {
webtorrentProcess.stderr.on('data', function (data) {
+++ /dev/null
-'use strict'
-
-const WebTorrent = require('webtorrent')
-const ipc = require('node-ipc')
-
-function webtorrent (args) {
- if (args.length !== 3) {
- throw new Error('Wrong arguments number: ' + args.length + '/3')
- }
-
- const host = args[1]
- const port = args[2]
- const nodeKey = 'webtorrentnode' + port
- const processKey = 'webtorrentprocess' + port
-
- ipc.config.silent = true
- ipc.config.id = processKey
-
- if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = []
- else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket'
- const wt = new WebTorrent({ dht: false })
-
- function seed (data) {
- const args = data.args
- const path = args.path
- const _id = data._id
-
- wt.seed(path, { announceList: '' }, function (torrent) {
- const toSend = {
- magnetUri: torrent.magnetURI
- }
-
- ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, toSend)
- })
- }
-
- function add (data) {
- const args = data.args
- const magnetUri = args.magnetUri
- const _id = data._id
-
- wt.add(magnetUri, function (torrent) {
- const toSend = {
- files: []
- }
-
- torrent.files.forEach(function (file) {
- toSend.files.push({ path: file.path })
- })
-
- ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, toSend)
- })
- }
-
- function remove (data) {
- const args = data.args
- const magnetUri = args.magnetUri
- const _id = data._id
-
- try {
- wt.remove(magnetUri, callback)
- } catch (err) {
- console.log('Cannot remove the torrent from WebTorrent.')
- return callback(null)
- }
-
- function callback () {
- const toSend = {}
- ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, toSend)
- }
- }
-
- console.log('Configuration: ' + host + ':' + port)
- console.log('Connecting to IPC...')
-
- ipc.connectTo(nodeKey, function () {
- ipc.of[nodeKey].on(processKey + '.seed', seed)
- ipc.of[nodeKey].on(processKey + '.add', add)
- ipc.of[nodeKey].on(processKey + '.remove', remove)
-
- ipc.of[nodeKey].emit(processKey + '.ready')
- console.log('Ready.')
- })
-
- process.on('uncaughtException', function (e) {
- ipc.of[nodeKey].emit(processKey + '.exception', { exception: e.toString() })
- })
-}
-
-// ---------------------------------------------------------------------------
-
-module.exports = webtorrent
const logger = require('../helpers/logger')
const mongoose = require('mongoose')
-const peertubeCrypto = require('../helpers/peertubeCrypto')
+const peertubeCrypto = require('../helpers/peertube-crypto')
const Pod = mongoose.model('Pod')
const checkErrors = require('./utils').checkErrors
const constants = require('../../initializers/constants')
-const customValidators = require('../../helpers/customValidators')
+const customValidators = require('../../helpers/custom-validators')
const logger = require('../../helpers/logger')
const Video = mongoose.model('Video')
const mongoose = require('mongoose')
const constants = require('../initializers/constants')
-const customValidators = require('../helpers/customValidators')
+const customValidators = require('../helpers/custom-validators')
const logger = require('../helpers/logger')
const utils = require('../helpers/utils')
const webtorrent = require('../lib/webtorrent')