ActivityPub-federated video streaming platform using P2P directly in your web browser
Async signature and various fixes
Chocobozzz709756b8e183
diff --git a/server/controllers/api/remote/videos.ts b/server/controllers/api/remote/videos.tsindex ebe4eca365..eb033637e1 100644--- a/server/controllers/api/remote/videos.ts+++ b/server/controllers/api/remote/videos.ts@@ -64,8 +64,7 @@ function remoteVideos (req: express.Request, res: express.Response, next: expresconst fromPod = res.locals.secure.pod// We need to process in the same order to keep consistency- // TODO: optimization- Promise.mapSeries(requests, (request: any) => {+ Promise.each(requests, (request: any) => {const data = request.data// Get the function we need to call in order to process the request@@ -79,7 +78,7 @@ function remoteVideos (req: express.Request, res: express.Response, next: expres}).catch(err => logger.error('Error managing remote videos.', { error: err }))- // We don't need to keep the other pod waiting+ // Don't block the other podreturn res.type('json').status(204).end()}@@ -87,7 +86,7 @@ function remoteVideosQadu (req: express.Request, res: express.Response, next: exconst requests = req.body.dataconst fromPod = res.locals.secure.pod- Promise.mapSeries(requests, (request: any) => {+ Promise.each(requests, (request: any) => {const videoData = request.datareturn quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod)@@ -101,7 +100,7 @@ function remoteVideosEvents (req: express.Request, res: express.Response, next:const requests = req.body.dataconst fromPod = res.locals.secure.pod- Promise.mapSeries(requests, (request: any) => {+ Promise.each(requests, (request: any) => {const eventData = request.datareturn processVideosEventsRetryWrapper(eventData, fromPod)diff --git a/server/controllers/client.ts b/server/controllers/client.tsindex e4d69eae7d..d42e8396df 100644--- a/server/controllers/client.ts+++ b/server/controllers/client.ts@@ -8,15 +8,14 @@ import {CONFIG,REMOTE_SCHEME,STATIC_PATHS,- STATIC_MAX_AGE+ STATIC_MAX_AGE,+ OPENGRAPH_COMMENT} from '../initializers'import { root, readFileBufferPromise } from '../helpers'import { VideoInstance } from '../models'const clientsRouter = express.Router()-// TODO: move to constants-const opengraphComment = '<!-- opengraph tags -->'const distPath = join(root(), 'client', 'dist')const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')const indexPath = join(distPath, 'index.html')@@ -85,7 +84,7 @@ function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'})- return htmlStringPage.replace(opengraphComment, tagsString)+ return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString)}function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {diff --git a/server/helpers/peertube-crypto.ts b/server/helpers/peertube-crypto.tsindex 8e8001cd69..0c73e85390 100644--- a/server/helpers/peertube-crypto.ts+++ b/server/helpers/peertube-crypto.ts@@ -1,5 +1,5 @@import * as crypto from 'crypto'-import * as fs from 'fs'+import * as Promise from 'bluebird'import { join } from 'path'import {@@ -52,18 +52,15 @@ function sign (data: string|Object) {dataString = JSON.stringify(data)} catch (err) {logger.error('Cannot sign data.', { error: err })- return ''+ return Promise.resolve('')}}sign.update(dataString, 'utf8')- // TODO: make async- const certPath = join(CONFIG.STORAGE.CERT_DIR, PRIVATE_CERT_NAME)- const myKey = fs.readFileSync(certPath)- const signature = sign.sign(myKey.toString(), SIGNATURE_ENCODING)-- return signature+ return getMyPrivateCert().then(myKey => {+ return sign.sign(myKey, SIGNATURE_ENCODING)+ })}function comparePassword (plainPassword: string, hashPassword: string) {diff --git a/server/helpers/requests.ts b/server/helpers/requests.tsindex b31074373e..183f6df0da 100644--- a/server/helpers/requests.ts+++ b/server/helpers/requests.ts@@ -33,7 +33,6 @@ type MakeSecureRequestParams = {method: 'GET'|'POST'toPod: PodInstancepath: string- sign: booleandata?: Object}function makeSecureRequest (params: MakeSecureRequestParams) {@@ -47,31 +46,30 @@ function makeSecureRequest (params: MakeSecureRequestParams) {return rej(new Error('Cannot make a secure request with a non POST method.'))}- // Add signature if it is specified in the params- if (params.sign === true) {- const host = CONFIG.WEBSERVER.HOST+ const host = CONFIG.WEBSERVER.HOST- let dataToSign- if (params.data) {- dataToSign = params.data- } else {- // We do not have data to sign so we just take our host- // It is not ideal but the connection should be in HTTPS- dataToSign = host- }+ let dataToSign+ if (params.data) {+ dataToSign = params.data+ } else {+ // We do not have data to sign so we just take our host+ // It is not ideal but the connection should be in HTTPS+ dataToSign = host+ }+ sign(dataToSign).then(signature => {requestParams.json['signature'] = {host, // Which host we pretend to be- signature: sign(dataToSign)+ signature}- }- // If there are data informations- if (params.data) {- requestParams.json['data'] = params.data- }+ // If there are data informations+ if (params.data) {+ requestParams.json['data'] = params.data+ }- request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body }))+ request.post(requestParams, (err, response, body) => err ? rej(err) : res({ response, body }))+ })})}diff --git a/server/initializers/constants.ts b/server/initializers/constants.tsindex bf99f4df69..2792d32283 100644--- a/server/initializers/constants.ts+++ b/server/initializers/constants.ts@@ -287,6 +287,10 @@ const USER_ROLES: { [ id: string ]: UserRole } = {// ---------------------------------------------------------------------------+const OPENGRAPH_COMMENT = '<!-- opengraph tags -->'++// ---------------------------------------------------------------------------+// Special constants for a test instanceif (isTestInstance() === true) {CONSTRAINTS_FIELDS.VIDEOS.DURATION.max = 14@@ -306,12 +310,13 @@ export {CONFIG,CONSTRAINTS_FIELDS,FRIEND_SCORE,- JOBS_FETCHING_INTERVAL,JOB_STATES,JOBS_CONCURRENCY,JOBS_FETCH_LIMIT_PER_CYCLE,+ JOBS_FETCHING_INTERVAL,LAST_MIGRATION_VERSION,OAUTH_LIFETIME,+ OPENGRAPH_COMMENT,PAGINATION_COUNT_DEFAULT,PODS_SCORE,PREVIEWS_SIZE,diff --git a/server/initializers/migrator.ts b/server/initializers/migrator.tsindex d381551b58..3184ec9208 100644--- a/server/initializers/migrator.ts+++ b/server/initializers/migrator.ts@@ -35,9 +35,7 @@ function migrate () {return getMigrationScripts().then(migrationScripts => ({ actualVersion, migrationScripts }))}).then(({ actualVersion, migrationScripts }) => {- return Promise.mapSeries(migrationScripts, entity => {- return executeMigration(actualVersion, entity)- })+ return Promise.each(migrationScripts, entity => executeMigration(actualVersion, entity))}).then(() => {logger.info('Migrations finished. New migration version schema: %s', LAST_MIGRATION_VERSION)diff --git a/server/lib/friends.ts b/server/lib/friends.tsindex 498144318a..c24839cb6a 100644--- a/server/lib/friends.ts+++ b/server/lib/friends.ts@@ -141,9 +141,7 @@ function makeFriends (hosts: string[]) {logger.info('Make friends!')return getMyPublicCert().then(cert => {- return Promise.mapSeries(hosts, host => {- return computeForeignPodsList(host, podsScore)- }).then(() => cert)+ return Promise.each(hosts, host => computeForeignPodsList(host, podsScore)).then(() => cert)}).then(cert => {logger.debug('Pods scores computed.', { podsScore: podsScore })@@ -169,7 +167,6 @@ function quitFriends () {const requestParams = {method: 'POST' as 'POST',path: '/api/' + API_VERSION + '/remote/pods/remove',- sign: true,toPod: null}@@ -178,6 +175,7 @@ function quitFriends () {// The other pod will exclude us automatically after a whilereturn Promise.map(pods, pod => {requestParams.toPod = pod+return makeSecureRequest(requestParams)}, { concurrency: REQUESTS_IN_PARALLEL }).then(() => pods)diff --git a/server/lib/request/abstract-request-scheduler.ts b/server/lib/request/abstract-request-scheduler.tsindex dd77fddb71..128fc5b28e 100644--- a/server/lib/request/abstract-request-scheduler.ts+++ b/server/lib/request/abstract-request-scheduler.ts@@ -70,7 +70,6 @@ abstract class AbstractRequestScheduler <T> {protected makeRequest (toPod: PodInstance, requestEndpoint: string, requestsToMake: Object) {const params = {toPod: toPod,- sign: true, // Prove our identitymethod: 'POST' as 'POST',path: '/api/' + API_VERSION + '/remote/' + requestEndpoint,data: requestsToMake // Requests we need to makediff --git a/server/lib/request/request-scheduler.ts b/server/lib/request/request-scheduler.tsindex 0dd796fb07..8927d53bb8 100644--- a/server/lib/request/request-scheduler.ts+++ b/server/lib/request/request-scheduler.ts@@ -61,16 +61,9 @@ class RequestScheduler extends AbstractRequestScheduler<RequestsGrouped> {}createRequest ({ type, endpoint, data, toIds, transaction }: RequestSchedulerOptions) {- // TODO: check the setPods works- const podIds = []-// If there are no destination pods abortif (toIds.length === 0) return undefined- toIds.forEach(toPod => {- podIds.push(toPod)- })-const createQuery = {endpoint,request: {@@ -85,7 +78,7 @@ class RequestScheduler extends AbstractRequestScheduler<RequestsGrouped> {return db.Request.create(createQuery, dbRequestOptions).then(request => {- return request.setPods(podIds, dbRequestOptions)+ return request.setPods(toIds, dbRequestOptions)})}diff --git a/server/middlewares/secure.ts b/server/middlewares/secure.tsindex 0fa9ee9d28..f58bea734b 100644--- a/server/middlewares/secure.ts+++ b/server/middlewares/secure.ts@@ -41,7 +41,7 @@ function checkSignature (req: express.Request, res: express.Response, next: exprreturn res.sendStatus(403)}).catch(err => {- logger.error('Cannot get signed host in body.', { error: err })+ logger.error('Cannot get signed host in body.', { error: err.stack, signature: req.body.signature.signature })return res.sendStatus(500)})}