From cda021079ff455cc0fd0eb95a5395fa808ab63d1 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 30 Jan 2016 17:05:22 +0100 Subject: New directory organization --- src/checker.js | 45 -------- src/constants.js | 37 ------- src/customValidators.js | 29 ----- src/database.js | 61 ----------- src/logger.js | 40 ------- src/pods.js | 274 ------------------------------------------------ src/poolRequests.js | 206 ------------------------------------ src/utils.js | 202 ----------------------------------- src/videos.js | 272 ----------------------------------------------- src/webTorrentNode.js | 160 ---------------------------- src/webtorrent.js | 91 ---------------- 11 files changed, 1417 deletions(-) delete mode 100644 src/checker.js delete mode 100644 src/constants.js delete mode 100644 src/customValidators.js delete mode 100644 src/database.js delete mode 100644 src/logger.js delete mode 100644 src/pods.js delete mode 100644 src/poolRequests.js delete mode 100644 src/utils.js delete mode 100644 src/videos.js delete mode 100644 src/webTorrentNode.js delete mode 100644 src/webtorrent.js (limited to 'src') diff --git a/src/checker.js b/src/checker.js deleted file mode 100644 index 7a3a53616..000000000 --- a/src/checker.js +++ /dev/null @@ -1,45 +0,0 @@ -;(function () { - 'use strict' - - var config = require('config') - var mkdirp = require('mkdirp') - - var checker = {} - - // Check the config files - checker.checkConfig = function () { - var required = [ 'listen.port', - 'webserver.https', 'webserver.host', 'webserver.port', - 'database.host', 'database.port', 'database.suffix', - 'storage.certs', 'storage.uploads', 'storage.logs', - 'network.friends' ] - var miss = [] - - for (var key of required) { - if (!config.has(key)) { - miss.push(key) - } - } - - return miss - } - - // Create directories for the storage if it doesn't exist - checker.createDirectoriesIfNotExist = function () { - var storages = config.get('storage') - - for (var key of Object.keys(storages)) { - var path = storages[key] - try { - mkdirp.sync(__dirname + '/../' + path) - } catch (error) { - // Do not use logger - console.error('Cannot create ' + path + ':' + error) - process.exit(0) - } - } - } - - // ----------- Export ----------- - module.exports = checker -})() diff --git a/src/constants.js b/src/constants.js deleted file mode 100644 index 00b713961..000000000 --- a/src/constants.js +++ /dev/null @@ -1,37 +0,0 @@ -;(function () { - 'use strict' - - var constants = {} - - function isTestInstance () { - return (process.env.NODE_ENV === 'test') - } - - // API version of our pod - constants.API_VERSION = 'v1' - - // Score a pod has when we create it as a friend - constants.FRIEND_BASE_SCORE = 100 - - // Time to wait between requests to the friends - constants.INTERVAL = 60000 - - // Number of points we add/remove from a friend after a successful/bad request - constants.PODS_SCORE = { - MALUS: -10, - BONUS: 10 - } - - // Number of retries we make for the make retry requests (to friends...) - constants.REQUEST_RETRIES = 10 - - // Special constants for a test instance - if (isTestInstance() === true) { - constants.FRIEND_BASE_SCORE = 20 - constants.INTERVAL = 10000 - constants.REQUEST_RETRIES = 2 - } - - // ----------- Export ----------- - module.exports = constants -})() diff --git a/src/customValidators.js b/src/customValidators.js deleted file mode 100644 index 73c2f8461..000000000 --- a/src/customValidators.js +++ /dev/null @@ -1,29 +0,0 @@ -;(function () { - 'use strict' - - var validator = require('validator') - - var customValidators = {} - - customValidators.eachIsRemoteVideosAddValid = function (values) { - return values.every(function (val) { - return validator.isLength(val.name, 1, 50) && - validator.isLength(val.description, 1, 50) && - validator.isLength(val.magnetUri, 10) && - validator.isURL(val.podUrl) - }) - } - - customValidators.eachIsRemoteVideosRemoveValid = function (values) { - return values.every(function (val) { - return validator.isLength(val.magnetUri, 10) - }) - } - - customValidators.isArray = function (value) { - return Array.isArray(value) - } - - // ----------- Export ----------- - module.exports = customValidators -})() diff --git a/src/database.js b/src/database.js deleted file mode 100644 index e03176b31..000000000 --- a/src/database.js +++ /dev/null @@ -1,61 +0,0 @@ -;(function () { - 'use strict' - - var config = require('config') - var mongoose = require('mongoose') - - var constants = require('./constants') - var logger = require('./logger') - - var dbname = 'peertube' + config.get('database.suffix') - var host = config.get('database.host') - var port = config.get('database.port') - - // ----------- Videos ----------- - var videosSchema = mongoose.Schema({ - name: String, - namePath: String, - description: String, - magnetUri: String, - podUrl: String - }) - - var VideosDB = mongoose.model('videos', videosSchema) - - // ----------- Pods ----------- - var podsSchema = mongoose.Schema({ - url: String, - publicKey: String, - score: { type: Number, max: constants.FRIEND_BASE_SCORE } - }) - - var PodsDB = mongoose.model('pods', podsSchema) - - // ----------- PoolRequests ----------- - var poolRequestsSchema = mongoose.Schema({ - type: String, - id: String, // Special id to find duplicates (video created we want to remove...) - request: mongoose.Schema.Types.Mixed - }) - - var PoolRequestsDB = mongoose.model('poolRequests', poolRequestsSchema) - - // ----------- Connection ----------- - - mongoose.connect('mongodb://' + host + ':' + port + '/' + dbname) - mongoose.connection.on('error', function () { - logger.error('Mongodb connection error.') - process.exit(0) - }) - - mongoose.connection.on('open', function () { - logger.info('Connected to mongodb.') - }) - - // ----------- Export ----------- - module.exports = { - VideosDB: VideosDB, - PodsDB: PodsDB, - PoolRequestsDB: PoolRequestsDB - } -})() diff --git a/src/logger.js b/src/logger.js deleted file mode 100644 index 850af10cb..000000000 --- a/src/logger.js +++ /dev/null @@ -1,40 +0,0 @@ -;(function () { - // Thanks http://tostring.it/2014/06/23/advanced-logging-with-nodejs/ - 'use strict' - - var config = require('config') - var winston = require('winston') - - var logDir = __dirname + '/../' + config.get('storage.logs') - - winston.emitErrs = true - - var logger = new winston.Logger({ - transports: [ - new winston.transports.File({ - level: 'debug', - filename: logDir + '/all-logs.log', - handleExceptions: true, - json: true, - maxsize: 5242880, - maxFiles: 5, - colorize: false - }), - new winston.transports.Console({ - level: 'debug', - handleExceptions: true, - humanReadableUnhandledException: true, - json: false, - colorize: true - }) - ], - exitOnError: true - }) - - module.exports = logger - module.exports.stream = { - write: function (message, encoding) { - logger.info(message) - } - } -})() diff --git a/src/pods.js b/src/pods.js deleted file mode 100644 index defa9b1c1..000000000 --- a/src/pods.js +++ /dev/null @@ -1,274 +0,0 @@ -;(function () { - 'use strict' - - var async = require('async') - var config = require('config') - var fs = require('fs') - var request = require('request') - - var constants = require('./constants') - var logger = require('./logger') - var PodsDB = require('./database').PodsDB - var poolRequests = require('./poolRequests') - var utils = require('./utils') - - 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/' + constants.API_VERSION + '/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 } - // TODO: check if the pod is not already a friend - pods.add = function (data, callback) { - var videos = require('./videos') - logger.info('Adding pod: %s', data.url) - - var params = { - url: data.url, - publicKey: data.publicKey, - score: constants.FRIEND_BASE_SCORE - } - - PodsDB.create(params, function (err, pod) { - if (err) { - logger.error('Cannot insert the pod.', { error: err }) - return callback(err) - } - - videos.addRemotes(data.videos) - - fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) { - if (err) { - logger.error('Cannot read cert file.', { error: err }) - return callback(err) - } - - videos.listOwned(function (err, videos_list) { - if (err) { - logger.error('Cannot get the list of owned videos.', { error: err }) - return callback(err) - } - - return callback(null, { cert: cert, videos: videos_list }) - }) - }) - }) - } - - pods.remove = function (url, callback) { - var videos = require('./videos') - logger.info('Removing %s pod.', url) - - videos.removeAllRemotesOf(url, function (err) { - if (err) logger.error('Cannot remove all remote videos of %s.', url) - - PodsDB.remove({ url: url }, function (err) { - if (err) return callback(err) - - logger.info('%s pod removed.', url) - callback(null) - }) - }) - } - - pods.addVideoToFriends = function (video) { - // To avoid duplicates - var id = video.name + video.magnetUri - poolRequests.addToPoolRequests(id, 'add', video) - } - - pods.removeVideoToFriends = function (video) { - // To avoid duplicates - var id = video.name + video.magnetUri - poolRequests.addToPoolRequests(id, 'remove', video) - } - - pods.makeFriends = function (callback) { - var videos = require('./videos') - var pods_score = {} - - logger.info('Make friends!') - 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') - - async.each(urls, computeForeignPodsList, function () { - logger.debug('Pods scores computed.', { pods_score: pods_score }) - var pods_list = computeWinningPods(urls, pods_score) - logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) - - makeRequestsToWinningPods(cert, pods_list) - }) - }) - - // ----------------------------------------------------------------------- - - function computeForeignPodsList (url, callback) { - // Let's give 1 point to the pod we ask the friends list - pods_score[url] = 1 - - getForeignPodsList(url, function (foreign_pods_list) { - if (foreign_pods_list.length === 0) return callback() - - async.each(foreign_pods_list, function (foreign_pod, callback_each) { - var foreign_url = foreign_pod.url - - 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 - 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 }) - }) - - return pods_list - } - - function makeRequestsToWinningPods (cert, pods_list) { - // 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) throw err - - var data = { - url: http + '://' + host + ':' + port, - publicKey: cert, - videos: videos_list - } - - utils.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 }) - - videos.addRemotes(body.videos, function (err) { - if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err }) - - 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.', { error: err }) - return callback(err) - } - - logger.debug('makeRequestsToWinningPods finished.') - return callback(null) - } - ) - }) - } - } - - pods.quitFriends = function (callback) { - // Stop pool requests - poolRequests.deactivate() - // Flush pool requests - poolRequests.forceSend() - - PodsDB.find(function (err, pods) { - if (err) return callback(err) - - var request = { - method: 'POST', - path: '/api/' + constants.API_VERSION + '/pods/remove', - sign: true, - encrypt: true, - data: { - url: 'me' // Fake data - } - } - - // Announce we quit them - utils.makeMultipleRetryRequest(request, pods, function () { - PodsDB.remove(function (err) { - poolRequests.activate() - - if (err) return callback(err) - - logger.info('Broke friends, so sad :(') - - var videos = require('./videos') - videos.removeAllRemotes(function (err) { - if (err) return callback(err) - - logger.info('Removed all remote videos.') - callback(null) - }) - }) - }) - }) - } - - pods.hasFriends = function (callback) { - PodsDB.count(function (err, count) { - if (err) return callback(err) - - var has_friends = (count !== 0) - callback(null, has_friends) - }) - } - - module.exports = pods -})() diff --git a/src/poolRequests.js b/src/poolRequests.js deleted file mode 100644 index 7f422f372..000000000 --- a/src/poolRequests.js +++ /dev/null @@ -1,206 +0,0 @@ -;(function () { - 'use strict' - - var async = require('async') - - var constants = require('./constants') - var logger = require('./logger') - var database = require('./database') - var pluck = require('lodash-node/compat/collection/pluck') - var PoolRequestsDB = database.PoolRequestsDB - var PodsDB = database.PodsDB - var utils = require('./utils') - var VideosDB = database.VideosDB - - var poolRequests = {} - - // ----------- Private ----------- - var timer = null - - function removePoolRequestsFromDB (ids) { - PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { - if (err) { - logger.error('Cannot remove requests from the pool requests database.', { error: err }) - return - } - - logger.info('Pool requests flushed.') - }) - } - - function makePoolRequests () { - logger.info('Making pool requests to friends.') - - PoolRequestsDB.find({}, { _id: 1, type: 1, request: 1 }, function (err, pool_requests) { - if (err) throw err - - if (pool_requests.length === 0) return - - var requests = { - add: { - ids: [], - requests: [] - }, - remove: { - ids: [], - requests: [] - } - } - - async.each(pool_requests, function (pool_request, callback_each) { - if (pool_request.type === 'add') { - requests.add.requests.push(pool_request.request) - requests.add.ids.push(pool_request._id) - } else if (pool_request.type === 'remove') { - requests.remove.requests.push(pool_request.request) - requests.remove.ids.push(pool_request._id) - } else { - throw new Error('Unkown pool request type.') - } - - callback_each() - }, function () { - // Send the add requests - if (requests.add.requests.length !== 0) { - makePoolRequest('add', requests.add.requests, function (err) { - if (err) logger.error('Errors when sent add pool requests.', { error: err }) - - removePoolRequestsFromDB(requests.add.ids) - }) - } - - // Send the remove requests - if (requests.remove.requests.length !== 0) { - makePoolRequest('remove', requests.remove.requests, function (err) { - if (err) logger.error('Errors when sent remove pool requests.', { error: err }) - - removePoolRequestsFromDB(requests.remove.ids) - }) - } - }) - }) - } - - function updatePodsScore (good_pods, bad_pods) { - logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) - - PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec() - PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) { - if (err) throw err - removeBadPods() - }) - } - - function removeBadPods () { - PodsDB.find({ score: 0 }, { _id: 1, url: 1 }, function (err, pods) { - if (err) throw err - - if (pods.length === 0) return - - var urls = pluck(pods, 'url') - var ids = pluck(pods, '_id') - - VideosDB.remove({ podUrl: { $in: urls } }, function (err, r) { - if (err) logger.error('Cannot remove videos from a pod that we removing.', { error: err }) - var videos_removed = r.result.n - logger.info('Removed %d videos.', videos_removed) - - PodsDB.remove({ _id: { $in: ids } }, function (err, r) { - if (err) logger.error('Cannot remove bad pods.', { error: err }) - - var pods_removed = r.result.n - logger.info('Removed %d pods.', pods_removed) - }) - }) - }) - } - - function makePoolRequest (type, requests, callback) { - if (!callback) callback = function () {} - - PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) { - if (err) throw err - - var params = { - encrypt: true, - sign: true, - method: 'POST', - path: null, - data: requests - } - - if (type === 'add') { - params.path = '/api/' + constants.API_VERSION + '/remotevideos/add' - } else if (type === 'remove') { - params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' - } else { - throw new Error('Unkown pool request type.') - } - - var bad_pods = [] - var good_pods = [] - - utils.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished) - - function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) { - if (err || (response.statusCode !== 200 && response.statusCode !== 204)) { - bad_pods.push(pod._id) - logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') }) - } else { - good_pods.push(pod._id) - } - - return callback_each_pod_finished() - } - - function callbackAllPodsFinished (err) { - if (err) return callback(err) - - updatePodsScore(good_pods, bad_pods) - callback(null) - } - }) - } - - // ----------- Public ----------- - poolRequests.activate = function () { - logger.info('Pool requests activated.') - timer = setInterval(makePoolRequests, constants.INTERVAL) - } - - poolRequests.addToPoolRequests = function (id, type, request) { - logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) - - PoolRequestsDB.findOne({ id: id }, function (err, entity) { - if (err) logger.error(err) - - if (entity) { - if (entity.type === type) { - logger.error(new Error('Cannot insert two same requests.')) - return - } - - // Remove the request of the other type - PoolRequestsDB.remove({ id: id }, function (err) { - if (err) logger.error(err) - }) - } else { - PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) { - if (err) logger.error(err) - }) - } - }) - } - - poolRequests.deactivate = function () { - logger.info('Pool requests deactivated.') - clearInterval(timer) - } - - poolRequests.forceSend = function () { - logger.info('Force pool requests sending.') - makePoolRequests() - } - - module.exports = poolRequests -})() diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index 176648a31..000000000 --- a/src/utils.js +++ /dev/null @@ -1,202 +0,0 @@ -;(function () { - 'use strict' - - var async = require('async') - var config = require('config') - var crypto = require('crypto') - var fs = require('fs') - var openssl = require('openssl-wrapper') - var request = require('request') - var replay = require('request-replay') - var ursa = require('ursa') - - var constants = require('./constants') - var logger = require('./logger') - - var utils = {} - - var http = config.get('webserver.https') ? 'https' : 'http' - var host = config.get('webserver.host') - var port = config.get('webserver.port') - var algorithm = 'aes-256-ctr' - - // ----------- Private functions ---------- - - function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { - // Append the signature - if (signature) { - params.json.signature = { - url: from_url, - signature: signature - } - } - - logger.debug('Make retry requests to %s.', to_pod.url) - - replay( - request.post(params, function (err, response, body) { - callbackEach(err, response, body, params.url, to_pod) - }), - { - retries: constants.REQUEST_RETRIES, - factor: 3, - maxTimeout: Infinity, - errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] - } - ).on('replay', function (replay) { - logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.', - params.url, replay.error.code, replay.error.message, replay.number, replay.delay) - }) - } - - // ----------- Public attributes ---------- - utils.certDir = __dirname + '/../' + config.get('storage.certs') - - // { path, data } - utils.makeMultipleRetryRequest = function (all_data, pods, callbackEach, callback) { - if (!callback) { - callback = callbackEach - callbackEach = null - } - - var url = http + '://' + host + ':' + port - var signature - - // Add signature if it is specified in the params - if (all_data.method === 'POST' && all_data.data && all_data.sign === true) { - var myKey = ursa.createPrivateKey(fs.readFileSync(utils.certDir + 'peertube.key.pem')) - signature = myKey.hashAndSign('sha256', url, 'utf8', 'hex') - } - - // Make a request for each pod - async.each(pods, function (pod, callback_each_async) { - function callbackEachRetryRequest (err, response, body, url, pod) { - if (callbackEach !== null) { - callbackEach(err, response, body, url, pod, function () { - callback_each_async() - }) - } else { - callback_each_async() - } - } - - var params = { - url: pod.url + all_data.path, - method: all_data.method - } - - // Add data with POST requst ? - if (all_data.method === 'POST' && all_data.data) { - // Encrypt data ? - if (all_data.encrypt === true) { - var crt = ursa.createPublicKey(pod.publicKey) - - // TODO: ES6 with let - ;(function (crt_copy, copy_params, copy_url, copy_pod, copy_signature) { - utils.symetricEncrypt(JSON.stringify(all_data.data), function (err, dataEncrypted) { - if (err) throw err - - var passwordEncrypted = crt_copy.encrypt(dataEncrypted.password, 'utf8', 'hex') - copy_params.json = { - data: dataEncrypted.crypted, - key: passwordEncrypted - } - - makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest) - }) - })(crt, params, url, pod, signature) - } else { - params.json = { data: all_data.data } - makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) - } - } else { - makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) - } - }, callback) - } - - utils.certsExist = function (callback) { - fs.exists(utils.certDir + 'peertube.key.pem', function (exists) { - return callback(exists) - }) - } - - utils.createCerts = function (callback) { - utils.certsExist(function (exist) { - if (exist === true) { - var string = 'Certs already exist.' - logger.warning(string) - return callback(new Error(string)) - } - - logger.info('Generating a RSA key...') - openssl.exec('genrsa', { 'out': utils.certDir + 'peertube.key.pem', '2048': false }, function (err) { - if (err) { - logger.error('Cannot create private key on this pod.', { error: err }) - return callback(err) - } - logger.info('RSA key generated.') - - logger.info('Manage public key...') - openssl.exec('rsa', { 'in': utils.certDir + 'peertube.key.pem', 'pubout': true, 'out': utils.certDir + 'peertube.pub' }, function (err) { - if (err) { - logger.error('Cannot create public key on this pod .', { error: err }) - return callback(err) - } - - logger.info('Public key managed.') - return callback(null) - }) - }) - }) - } - - utils.createCertsIfNotExist = function (callback) { - utils.certsExist(function (exist) { - if (exist === true) { - return callback(null) - } - - utils.createCerts(function (err) { - return callback(err) - }) - }) - } - - utils.generatePassword = function (callback) { - crypto.randomBytes(32, function (err, buf) { - if (err) { - return callback(err) - } - - callback(null, buf.toString('utf8')) - }) - } - - utils.symetricEncrypt = function (text, callback) { - utils.generatePassword(function (err, password) { - if (err) { - return callback(err) - } - - var cipher = crypto.createCipher(algorithm, password) - var crypted = cipher.update(text, 'utf8', 'hex') - crypted += cipher.final('hex') - callback(null, { crypted: crypted, password: password }) - }) - } - - utils.symetricDecrypt = function (text, password) { - var decipher = crypto.createDecipher(algorithm, password) - var dec = decipher.update(text, 'hex', 'utf8') - dec += decipher.final('utf8') - return dec - } - - utils.cleanForExit = function (webtorrent_process) { - logger.info('Gracefully exiting') - process.kill(-webtorrent_process.pid) - } - - module.exports = utils -})() diff --git a/src/videos.js b/src/videos.js deleted file mode 100644 index 90821fdf6..000000000 --- a/src/videos.js +++ /dev/null @@ -1,272 +0,0 @@ -;(function () { - 'use strict' - - var async = require('async') - var config = require('config') - var dz = require('dezalgo') - var fs = require('fs') - var webtorrent = require('./webTorrentNode') - - var logger = require('./logger') - var pods = require('./pods') - var VideosDB = require('./database').VideosDB - - var videos = {} - - var http = config.get('webserver.https') === true ? 'https' : 'http' - var host = config.get('webserver.host') - var port = config.get('webserver.port') - - // ----------- Private functions ----------- - function seedVideo (path, callback) { - logger.info('Seeding %s...', path) - - webtorrent.seed(path, function (torrent) { - logger.info('%s seeded (%s).', path, torrent.magnetURI) - - return callback(null, torrent) - }) - } - - // ----------- Public attributes ---------- - videos.uploadDir = __dirname + '/../' + config.get('storage.uploads') - - // ----------- Public functions ----------- - videos.list = function (callback) { - VideosDB.find(function (err, videos_list) { - if (err) { - logger.error('Cannot get list of the videos.', { error: err }) - return callback(err) - } - - return callback(null, videos_list) - }) - } - - videos.listOwned = function (callback) { - // If namePath is not null this is *our* video - VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { - if (err) { - logger.error('Cannot get list of the videos.', { error: err }) - return callback(err) - } - - return callback(null, videos_list) - }) - } - - videos.add = function (data, callback) { - var video_file = data.video - var video_data = data.data - - logger.info('Adding %s video.', video_file.path) - seedVideo(video_file.path, function (err, torrent) { - if (err) { - logger.error('Cannot seed this video.', { error: err }) - return callback(err) - } - - var params = { - name: video_data.name, - namePath: video_file.filename, - description: video_data.description, - magnetUri: torrent.magnetURI, - podUrl: http + '://' + host + ':' + port - } - - VideosDB.create(params, function (err, video) { - if (err) { - logger.error('Cannot insert this video.', { error: err }) - return callback(err) - } - - // Now we'll add the video's meta data to our friends - params.namePath = null - - pods.addVideoToFriends(params) - callback(null) - }) - }) - } - - videos.remove = function (id, callback) { - // Maybe the torrent is not seeded, but we catch the error to don't stop the removing process - function removeTorrent (magnetUri, callback) { - try { - webtorrent.remove(magnetUri, callback) - } catch (err) { - logger.warn('Cannot remove the torrent from WebTorrent', { err: err }) - return callback(null) - } - } - - VideosDB.findById(id, function (err, video) { - if (err || !video) { - if (!err) err = new Error('Cannot find this video.') - logger.error('Cannot find this video.', { error: err }) - return callback(err) - } - - if (video.namePath === null) { - var error_string = 'Cannot remove the video of another pod.' - logger.error(error_string) - return callback(new Error(error_string)) - } - - logger.info('Removing %s video', video.name) - - removeTorrent(video.magnetUri, function () { - VideosDB.findByIdAndRemove(id, function (err) { - if (err) { - logger.error('Cannot remove the torrent.', { error: err }) - return callback(err) - } - - fs.unlink(videos.uploadDir + video.namePath, function (err) { - if (err) { - logger.error('Cannot remove this video file.', { error: err }) - return callback(err) - } - - var params = { - name: video.name, - magnetUri: video.magnetUri - } - - pods.removeVideoToFriends(params) - callback(null) - }) - }) - }) - }) - } - - // Use the magnet Uri because the _id field is not the same on different servers - videos.removeRemotes = function (fromUrl, magnetUris, callback) { - if (callback === undefined) callback = function () {} - - VideosDB.find({ magnetUri: { $in: magnetUris } }, function (err, videos) { - if (err || !videos) { - logger.error('Cannot find the torrent URI of these remote videos.') - return callback(err) - } - - var to_remove = [] - async.each(videos, function (video, callback_async) { - callback_async = dz(callback_async) - - if (video.podUrl !== fromUrl) { - logger.error('The pod %s has not the rights on the video of %s.', fromUrl, video.podUrl) - } else { - to_remove.push(video._id) - } - - callback_async() - }, function () { - VideosDB.remove({ _id: { $in: to_remove } }, function (err) { - if (err) { - logger.error('Cannot remove the remote videos.') - return callback(err) - } - - logger.info('Removed remote videos from %s.', fromUrl) - callback(null) - }) - }) - }) - } - - videos.removeAllRemotes = function (callback) { - VideosDB.remove({ namePath: null }, function (err) { - if (err) return callback(err) - - callback(null) - }) - } - - videos.removeAllRemotesOf = function (fromUrl, callback) { - VideosDB.remove({ podUrl: fromUrl }, function (err) { - if (err) return callback(err) - - callback(null) - }) - } - - // { name, magnetUri, podUrl } - // TODO: avoid doublons - videos.addRemotes = function (videos, callback) { - if (callback === undefined) callback = function () {} - - var to_add = [] - - async.each(videos, function (video, callback_each) { - callback_each = dz(callback_each) - logger.debug('Add remote video from pod: %s', video.podUrl) - - var params = { - name: video.name, - namePath: null, - description: video.description, - magnetUri: video.magnetUri, - podUrl: video.podUrl - } - - to_add.push(params) - - callback_each() - }, function () { - VideosDB.create(to_add, function (err, videos) { - if (err) { - logger.error('Cannot insert this remote video.', { error: err }) - return callback(err) - } - - return callback(null, videos) - }) - }) - } - - videos.get = function (id, callback) { - VideosDB.findById(id, function (err, video) { - if (err) { - logger.error('Cannot get this video.', { error: err }) - return callback(err) - } - - return callback(null, video) - }) - } - - videos.search = function (name, callback) { - VideosDB.find({ name: new RegExp(name) }, function (err, videos) { - if (err) { - logger.error('Cannot search the videos.', { error: err }) - return callback(err) - } - - return callback(null, videos) - }) - } - - videos.seedAll = function (callback) { - VideosDB.find({ namePath: { $ne: null } }, function (err, videos_list) { - if (err) { - logger.error('Cannot get list of the videos to seed.', { error: err }) - return callback(err) - } - - async.each(videos_list, function (video, each_callback) { - seedVideo(videos.uploadDir + video.namePath, function (err) { - if (err) { - logger.error('Cannot seed this video.', { error: err }) - return callback(err) - } - - each_callback(null) - }) - }, callback) - }) - } - - module.exports = videos -})() diff --git a/src/webTorrentNode.js b/src/webTorrentNode.js deleted file mode 100644 index d6801d0fb..000000000 --- a/src/webTorrentNode.js +++ /dev/null @@ -1,160 +0,0 @@ -;(function () { - 'use strict' - - var config = require('config') - var ipc = require('node-ipc') - var pathUtils = require('path') - var spawn = require('electron-spawn') - - var logger = require('./logger') - - var host = config.get('webserver.host') - var port = config.get('webserver.port') - - var nodeKey = 'webtorrentnode' + port - var processKey = 'webtorrent' + port - - ipc.config.silent = true - ipc.config.id = nodeKey - - var webtorrentnode = {} - - // Useful for beautiful tests - webtorrentnode.silent = false - - // Useful to kill it - webtorrentnode.app = null - - webtorrentnode.create = function (options, callback) { - if (typeof options === 'function') { - callback = options - options = {} - } - - // Override options - if (options.host) host = options.host - if (options.port) { - port = options.port - nodeKey = 'webtorrentnode' + port - processKey = 'webtorrent' + port - ipc.config.id = nodeKey - } - - ipc.serve(function () { - if (!webtorrentnode.silent) logger.info('IPC server ready.') - - // Run a timeout of 30s after which we exit the process - var timeout_webtorrent_process = setTimeout(function () { - logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') - process.exit() - }, 30000) - - ipc.server.on(processKey + '.ready', function () { - if (!webtorrentnode.silent) logger.info('Webtorrent process ready.') - clearTimeout(timeout_webtorrent_process) - callback() - }) - - ipc.server.on(processKey + '.exception', function (data) { - logger.error('Received exception error from webtorrent process.', { exception: data.exception }) - process.exit() - }) - - var webtorrent_process = spawn(__dirname + '/webtorrent.js', host, port, { detached: true }) - webtorrent_process.stderr.on('data', function (data) { - // logger.debug('Webtorrent process stderr: ', data.toString()) - }) - - webtorrent_process.stdout.on('data', function (data) { - // logger.debug('Webtorrent process:', data.toString()) - }) - - webtorrentnode.app = webtorrent_process - }) - - ipc.server.start() - } - - webtorrentnode.seed = function (path, callback) { - var extension = pathUtils.extname(path) - var basename = pathUtils.basename(path, extension) - var data = { - _id: basename, - args: { - path: path - } - } - - if (!webtorrentnode.silent) logger.debug('Node wants to seed %s.', data._id) - - // Finish signal - var event_key = nodeKey + '.seedDone.' + data._id - ipc.server.on(event_key, function listener (received) { - if (!webtorrentnode.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) - - // This is a fake object, we just use the magnetUri in this project - var torrent = { - magnetURI: received.magnetUri - } - - ipc.server.off(event_key) - callback(torrent) - }) - - ipc.server.broadcast(processKey + '.seed', data) - } - - webtorrentnode.add = function (magnetUri, callback) { - var data = { - _id: magnetUri, - args: { - magnetUri: magnetUri - } - } - - if (!webtorrentnode.silent) logger.debug('Node wants to add ' + data._id) - - // Finish signal - var event_key = nodeKey + '.addDone.' + data._id - ipc.server.on(event_key, function (received) { - if (!webtorrentnode.silent) logger.debug('Process added torrent.') - - // This is a fake object, we just use the magnetUri in this project - var torrent = { - files: received.files - } - - ipc.server.off(event_key) - callback(torrent) - }) - - ipc.server.broadcast(processKey + '.add', data) - } - - webtorrentnode.remove = function (magnetUri, callback) { - var data = { - _id: magnetUri, - args: { - magnetUri: magnetUri - } - } - - if (!webtorrentnode.silent) logger.debug('Node wants to stop seeding %s.', data._id) - - // Finish signal - var event_key = nodeKey + '.removeDone.' + data._id - ipc.server.on(event_key, function (received) { - if (!webtorrentnode.silent) logger.debug('Process removed torrent %s.', data._id) - - var err = null - if (received.err) err = received.err - - ipc.server.off(event_key) - callback(err) - }) - - ipc.server.broadcast(processKey + '.remove', data) - } - - module.exports = webtorrentnode -})() diff --git a/src/webtorrent.js b/src/webtorrent.js deleted file mode 100644 index b72bc500d..000000000 --- a/src/webtorrent.js +++ /dev/null @@ -1,91 +0,0 @@ -;(function () { - 'use strict' - - module.exports = function (args) { - var WebTorrent = require('webtorrent') - var ipc = require('node-ipc') - - if (args.length !== 3) { - console.log('Wrong arguments number: ' + args.length + '/3') - process.exit(-1) - } - - var host = args[1] - var port = args[2] - var nodeKey = 'webtorrentnode' + port - var processKey = 'webtorrent' + 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' - var wt = new WebTorrent({ dht: false }) - - function seed (data) { - var args = data.args - var path = args.path - var _id = data._id - - wt.seed(path, { announceList: '' }, function (torrent) { - var to_send = { - magnetUri: torrent.magnetURI - } - - ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send) - }) - } - - function add (data) { - var args = data.args - var magnetUri = args.magnetUri - var _id = data._id - - wt.add(magnetUri, function (torrent) { - var to_send = { - files: [] - } - - torrent.files.forEach(function (file) { - to_send.files.push({ path: file.path }) - }) - - ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send) - }) - } - - function remove (data) { - var args = data.args - var magnetUri = args.magnetUri - var _id = data._id - - try { - wt.remove(magnetUri, callback) - } catch (err) { - console.log('Cannot remove the torrent from WebTorrent', { err: err }) - return callback(null) - } - - function callback () { - var to_send = {} - ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send) - } - } - - 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 }) - }) - } -})() -- cgit v1.2.3