aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-10-28 15:24:40 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-11-09 15:33:04 +0100
commita056ca4813c82f490dcd31ac97a64d6bf76d3dcc (patch)
tree11a0638cb92eee94f404e294f54632212836a4a6 /server
parentd846d99c6c81028bb7bd3cb20abd433cbf396a22 (diff)
downloadPeerTube-a056ca4813c82f490dcd31ac97a64d6bf76d3dcc.tar.gz
PeerTube-a056ca4813c82f490dcd31ac97a64d6bf76d3dcc.tar.zst
PeerTube-a056ca4813c82f490dcd31ac97a64d6bf76d3dcc.zip
Add max lives limit
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/config.ts4
-rw-r--r--server/initializers/checker-before-init.ts2
-rw-r--r--server/initializers/config.ts3
-rw-r--r--server/middlewares/validators/config.ts2
-rw-r--r--server/middlewares/validators/videos/video-live.ts35
-rw-r--r--server/models/video/video.ts31
-rw-r--r--server/tests/api/check-params/config.ts2
-rw-r--r--server/tests/api/server/config.ts6
8 files changed, 83 insertions, 2 deletions
diff --git a/server/controllers/api/config.ts b/server/controllers/api/config.ts
index 99aabba62..eb9f5f4b4 100644
--- a/server/controllers/api/config.ts
+++ b/server/controllers/api/config.ts
@@ -120,6 +120,8 @@ async function getConfig (req: express.Request, res: express.Response) {
120 120
121 allowReplay: CONFIG.LIVE.ALLOW_REPLAY, 121 allowReplay: CONFIG.LIVE.ALLOW_REPLAY,
122 maxDuration: CONFIG.LIVE.MAX_DURATION, 122 maxDuration: CONFIG.LIVE.MAX_DURATION,
123 maxInstanceLives: CONFIG.LIVE.MAX_INSTANCE_LIVES,
124 maxUserLives: CONFIG.LIVE.MAX_USER_LIVES,
123 125
124 transcoding: { 126 transcoding: {
125 enabled: CONFIG.LIVE.TRANSCODING.ENABLED, 127 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
@@ -430,6 +432,8 @@ function customConfig (): CustomConfig {
430 enabled: CONFIG.LIVE.ENABLED, 432 enabled: CONFIG.LIVE.ENABLED,
431 allowReplay: CONFIG.LIVE.ALLOW_REPLAY, 433 allowReplay: CONFIG.LIVE.ALLOW_REPLAY,
432 maxDuration: CONFIG.LIVE.MAX_DURATION, 434 maxDuration: CONFIG.LIVE.MAX_DURATION,
435 maxInstanceLives: CONFIG.LIVE.MAX_INSTANCE_LIVES,
436 maxUserLives: CONFIG.LIVE.MAX_USER_LIVES,
433 transcoding: { 437 transcoding: {
434 enabled: CONFIG.LIVE.TRANSCODING.ENABLED, 438 enabled: CONFIG.LIVE.TRANSCODING.ENABLED,
435 threads: CONFIG.LIVE.TRANSCODING.THREADS, 439 threads: CONFIG.LIVE.TRANSCODING.THREADS,
diff --git a/server/initializers/checker-before-init.ts b/server/initializers/checker-before-init.ts
index d4140e3fa..93b71a242 100644
--- a/server/initializers/checker-before-init.ts
+++ b/server/initializers/checker-before-init.ts
@@ -38,7 +38,7 @@ function checkMissedConfig () {
38 'federation.videos.federate_unlisted', 38 'federation.videos.federate_unlisted',
39 'search.remote_uri.users', 'search.remote_uri.anonymous', 'search.search_index.enabled', 'search.search_index.url', 39 'search.remote_uri.users', 'search.remote_uri.anonymous', 'search.search_index.enabled', 'search.search_index.url',
40 'search.search_index.disable_local_search', 'search.search_index.is_default_search', 40 'search.search_index.disable_local_search', 'search.search_index.is_default_search',
41 'live.enabled', 'live.allow_replay', 'live.max_duration', 41 'live.enabled', 'live.allow_replay', 'live.max_duration', 'live.max_user_lives', 'live.max_instance_lives',
42 'live.transcoding.enabled', 'live.transcoding.threads', 42 'live.transcoding.enabled', 'live.transcoding.threads',
43 'live.transcoding.resolutions.240p', 'live.transcoding.resolutions.360p', 'live.transcoding.resolutions.480p', 43 'live.transcoding.resolutions.240p', 'live.transcoding.resolutions.360p', 'live.transcoding.resolutions.480p',
44 'live.transcoding.resolutions.720p', 'live.transcoding.resolutions.1080p', 'live.transcoding.resolutions.2160p' 44 'live.transcoding.resolutions.720p', 'live.transcoding.resolutions.1080p', 'live.transcoding.resolutions.2160p'
diff --git a/server/initializers/config.ts b/server/initializers/config.ts
index 9e8927350..b70361aa9 100644
--- a/server/initializers/config.ts
+++ b/server/initializers/config.ts
@@ -202,6 +202,9 @@ const CONFIG = {
202 get ENABLED () { return config.get<boolean>('live.enabled') }, 202 get ENABLED () { return config.get<boolean>('live.enabled') },
203 203
204 get MAX_DURATION () { return parseDurationToMs(config.get<string>('live.max_duration')) }, 204 get MAX_DURATION () { return parseDurationToMs(config.get<string>('live.max_duration')) },
205 get MAX_INSTANCE_LIVES () { return config.get<number>('live.max_instance_lives') },
206 get MAX_USER_LIVES () { return config.get<number>('live.max_user_lives') },
207
205 get ALLOW_REPLAY () { return config.get<boolean>('live.allow_replay') }, 208 get ALLOW_REPLAY () { return config.get<boolean>('live.allow_replay') },
206 209
207 RTMP: { 210 RTMP: {
diff --git a/server/middlewares/validators/config.ts b/server/middlewares/validators/config.ts
index 41a6ae4f9..d0071ccc1 100644
--- a/server/middlewares/validators/config.ts
+++ b/server/middlewares/validators/config.ts
@@ -65,6 +65,8 @@ const customConfigUpdateValidator = [
65 body('live.enabled').isBoolean().withMessage('Should have a valid live enabled boolean'), 65 body('live.enabled').isBoolean().withMessage('Should have a valid live enabled boolean'),
66 body('live.allowReplay').isBoolean().withMessage('Should have a valid live allow replay boolean'), 66 body('live.allowReplay').isBoolean().withMessage('Should have a valid live allow replay boolean'),
67 body('live.maxDuration').custom(isIntOrNull).withMessage('Should have a valid live max duration'), 67 body('live.maxDuration').custom(isIntOrNull).withMessage('Should have a valid live max duration'),
68 body('live.maxInstanceLives').custom(isIntOrNull).withMessage('Should have a valid max instance lives'),
69 body('live.maxUserLives').custom(isIntOrNull).withMessage('Should have a valid max user lives'),
68 body('live.transcoding.enabled').isBoolean().withMessage('Should have a valid live transcoding enabled boolean'), 70 body('live.transcoding.enabled').isBoolean().withMessage('Should have a valid live transcoding enabled boolean'),
69 body('live.transcoding.threads').isInt().withMessage('Should have a valid live transcoding threads'), 71 body('live.transcoding.threads').isInt().withMessage('Should have a valid live transcoding threads'),
70 body('live.transcoding.resolutions.240p').isBoolean().withMessage('Should have a valid transcoding 240p resolution enabled boolean'), 72 body('live.transcoding.resolutions.240p').isBoolean().withMessage('Should have a valid transcoding 240p resolution enabled boolean'),
diff --git a/server/middlewares/validators/videos/video-live.ts b/server/middlewares/validators/videos/video-live.ts
index ab57e67bf..69200cb60 100644
--- a/server/middlewares/validators/videos/video-live.ts
+++ b/server/middlewares/validators/videos/video-live.ts
@@ -2,7 +2,7 @@ import * as express from 'express'
2import { body, param } from 'express-validator' 2import { body, param } from 'express-validator'
3import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos' 3import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos'
4import { VideoLiveModel } from '@server/models/video/video-live' 4import { VideoLiveModel } from '@server/models/video/video-live'
5import { UserRight, VideoState } from '@shared/models' 5import { ServerErrorCode, UserRight, VideoState } from '@shared/models'
6import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' 6import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
7import { isVideoNameValid } from '../../../helpers/custom-validators/videos' 7import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
8import { cleanUpReqFiles } from '../../../helpers/express-utils' 8import { cleanUpReqFiles } from '../../../helpers/express-utils'
@@ -10,6 +10,7 @@ import { logger } from '../../../helpers/logger'
10import { CONFIG } from '../../../initializers/config' 10import { CONFIG } from '../../../initializers/config'
11import { areValidationErrors } from '../utils' 11import { areValidationErrors } from '../utils'
12import { getCommonVideoEditAttributes } from './videos' 12import { getCommonVideoEditAttributes } from './videos'
13import { VideoModel } from '@server/models/video/video'
13 14
14const videoLiveGetValidator = [ 15const videoLiveGetValidator = [
15 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), 16 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
@@ -50,11 +51,15 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
50 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) 51 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
51 52
52 if (CONFIG.LIVE.ENABLED !== true) { 53 if (CONFIG.LIVE.ENABLED !== true) {
54 cleanUpReqFiles(req)
55
53 return res.status(403) 56 return res.status(403)
54 .json({ error: 'Live is not enabled on this instance' }) 57 .json({ error: 'Live is not enabled on this instance' })
55 } 58 }
56 59
57 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { 60 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
61 cleanUpReqFiles(req)
62
58 return res.status(403) 63 return res.status(403)
59 .json({ error: 'Saving live replay is not allowed instance' }) 64 .json({ error: 'Saving live replay is not allowed instance' })
60 } 65 }
@@ -64,6 +69,34 @@ const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
64 const user = res.locals.oauth.token.User 69 const user = res.locals.oauth.token.User
65 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) 70 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
66 71
72 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
73 const totalInstanceLives = await VideoModel.countLocalLives()
74
75 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
76 cleanUpReqFiles(req)
77
78 return res.status(403)
79 .json({
80 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
81 error: 'Cannot create this live because the max instance lives limit is reached.'
82 })
83 }
84 }
85
86 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
87 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
88
89 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
90 cleanUpReqFiles(req)
91
92 return res.status(403)
93 .json({
94 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
95 error: 'Cannot create this live because the max user lives limit is reached.'
96 })
97 }
98 }
99
67 return next() 100 return next()
68 } 101 }
69]) 102])
diff --git a/server/models/video/video.ts b/server/models/video/video.ts
index 78fec5585..d094f19b0 100644
--- a/server/models/video/video.ts
+++ b/server/models/video/video.ts
@@ -1142,6 +1142,37 @@ export class VideoModel extends Model<VideoModel> {
1142 return VideoModel.getAvailableForApi(queryOptions) 1142 return VideoModel.getAvailableForApi(queryOptions)
1143 } 1143 }
1144 1144
1145 static countLocalLives () {
1146 const options = {
1147 where: {
1148 remote: false,
1149 isLive: true
1150 }
1151 }
1152
1153 return VideoModel.count(options)
1154 }
1155
1156 static countLivesOfAccount (accountId: number) {
1157 const options = {
1158 where: {
1159 remote: false,
1160 isLive: true
1161 },
1162 include: [
1163 {
1164 required: true,
1165 model: VideoChannelModel.unscoped(),
1166 where: {
1167 accountId
1168 }
1169 }
1170 ]
1171 }
1172
1173 return VideoModel.count(options)
1174 }
1175
1145 static load (id: number | string, t?: Transaction): Bluebird<MVideoThumbnail> { 1176 static load (id: number | string, t?: Transaction): Bluebird<MVideoThumbnail> {
1146 const where = buildWhereIdOrUUID(id) 1177 const where = buildWhereIdOrUUID(id)
1147 const options = { 1178 const options = {
diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts
index 2882ceb7c..42ac5e1f9 100644
--- a/server/tests/api/check-params/config.ts
+++ b/server/tests/api/check-params/config.ts
@@ -105,6 +105,8 @@ describe('Test config API validators', function () {
105 105
106 allowReplay: false, 106 allowReplay: false,
107 maxDuration: null, 107 maxDuration: null,
108 maxInstanceLives: -1,
109 maxUserLives: 50,
108 110
109 transcoding: { 111 transcoding: {
110 enabled: true, 112 enabled: true,
diff --git a/server/tests/api/server/config.ts b/server/tests/api/server/config.ts
index a7f035362..6c37be113 100644
--- a/server/tests/api/server/config.ts
+++ b/server/tests/api/server/config.ts
@@ -81,6 +81,8 @@ function checkInitialConfig (server: ServerInfo, data: CustomConfig) {
81 expect(data.live.enabled).to.be.false 81 expect(data.live.enabled).to.be.false
82 expect(data.live.allowReplay).to.be.true 82 expect(data.live.allowReplay).to.be.true
83 expect(data.live.maxDuration).to.equal(1000 * 3600 * 5) 83 expect(data.live.maxDuration).to.equal(1000 * 3600 * 5)
84 expect(data.live.maxInstanceLives).to.equal(20)
85 expect(data.live.maxUserLives).to.equal(3)
84 expect(data.live.transcoding.enabled).to.be.false 86 expect(data.live.transcoding.enabled).to.be.false
85 expect(data.live.transcoding.threads).to.equal(2) 87 expect(data.live.transcoding.threads).to.equal(2)
86 expect(data.live.transcoding.resolutions['240p']).to.be.false 88 expect(data.live.transcoding.resolutions['240p']).to.be.false
@@ -166,6 +168,8 @@ function checkUpdatedConfig (data: CustomConfig) {
166 expect(data.live.enabled).to.be.true 168 expect(data.live.enabled).to.be.true
167 expect(data.live.allowReplay).to.be.false 169 expect(data.live.allowReplay).to.be.false
168 expect(data.live.maxDuration).to.equal(5000) 170 expect(data.live.maxDuration).to.equal(5000)
171 expect(data.live.maxInstanceLives).to.equal(-1)
172 expect(data.live.maxUserLives).to.equal(10)
169 expect(data.live.transcoding.enabled).to.be.true 173 expect(data.live.transcoding.enabled).to.be.true
170 expect(data.live.transcoding.threads).to.equal(4) 174 expect(data.live.transcoding.threads).to.equal(4)
171 expect(data.live.transcoding.resolutions['240p']).to.be.true 175 expect(data.live.transcoding.resolutions['240p']).to.be.true
@@ -330,6 +334,8 @@ describe('Test config', function () {
330 enabled: true, 334 enabled: true,
331 allowReplay: false, 335 allowReplay: false,
332 maxDuration: 5000, 336 maxDuration: 5000,
337 maxInstanceLives: -1,
338 maxUserLives: 10,
333 transcoding: { 339 transcoding: {
334 enabled: true, 340 enabled: true,
335 threads: 4, 341 threads: 4,