config.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import express from 'express'
  2. import { body } from 'express-validator'
  3. import { isIntOrNull } from '@server/helpers/custom-validators/misc'
  4. import { CONFIG, isEmailEnabled } from '@server/initializers/config'
  5. import { CustomConfig } from '../../../shared/models/server/custom-config.model'
  6. import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
  7. import { isUserNSFWPolicyValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid } from '../../helpers/custom-validators/users'
  8. import { logger } from '../../helpers/logger'
  9. import { isThemeRegistered } from '../../lib/plugins/theme-utils'
  10. import { areValidationErrors } from './shared'
  11. import { HttpStatusCode } from '@shared/models/http/http-error-codes'
  12. const customConfigUpdateValidator = [
  13. body('instance.name').exists().withMessage('Should have a valid instance name'),
  14. body('instance.shortDescription').exists().withMessage('Should have a valid instance short description'),
  15. body('instance.description').exists().withMessage('Should have a valid instance description'),
  16. body('instance.terms').exists().withMessage('Should have a valid instance terms'),
  17. body('instance.defaultNSFWPolicy').custom(isUserNSFWPolicyValid).withMessage('Should have a valid NSFW policy'),
  18. body('instance.defaultClientRoute').exists().withMessage('Should have a valid instance default client route'),
  19. body('instance.customizations.css').exists().withMessage('Should have a valid instance CSS customization'),
  20. body('instance.customizations.javascript').exists().withMessage('Should have a valid instance JavaScript customization'),
  21. body('services.twitter.username').exists().withMessage('Should have a valid twitter username'),
  22. body('services.twitter.whitelisted').isBoolean().withMessage('Should have a valid twitter whitelisted boolean'),
  23. body('cache.previews.size').isInt().withMessage('Should have a valid previews cache size'),
  24. body('cache.captions.size').isInt().withMessage('Should have a valid captions cache size'),
  25. body('cache.torrents.size').isInt().withMessage('Should have a valid torrents cache size'),
  26. body('signup.enabled').isBoolean().withMessage('Should have a valid signup enabled boolean'),
  27. body('signup.limit').isInt().withMessage('Should have a valid signup limit'),
  28. body('signup.requiresEmailVerification').isBoolean().withMessage('Should have a valid requiresEmailVerification boolean'),
  29. body('signup.minimumAge').isInt().withMessage("Should have a valid minimum age required"),
  30. body('admin.email').isEmail().withMessage('Should have a valid administrator email'),
  31. body('contactForm.enabled').isBoolean().withMessage('Should have a valid contact form enabled boolean'),
  32. body('user.videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid video quota'),
  33. body('user.videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily video quota'),
  34. body('videoChannels.maxPerUser').isInt().withMessage("Should have a valid maximum amount of video channels per user"),
  35. body('transcoding.enabled').isBoolean().withMessage('Should have a valid transcoding enabled boolean'),
  36. body('transcoding.allowAdditionalExtensions').isBoolean().withMessage('Should have a valid additional extensions boolean'),
  37. body('transcoding.threads').isInt().withMessage('Should have a valid transcoding threads number'),
  38. body('transcoding.concurrency').isInt({ min: 1 }).withMessage('Should have a valid transcoding concurrency number'),
  39. body('transcoding.resolutions.0p').isBoolean().withMessage('Should have a valid transcoding 0p resolution enabled boolean'),
  40. body('transcoding.resolutions.144p').isBoolean().withMessage('Should have a valid transcoding 144p resolution enabled boolean'),
  41. body('transcoding.resolutions.240p').isBoolean().withMessage('Should have a valid transcoding 240p resolution enabled boolean'),
  42. body('transcoding.resolutions.360p').isBoolean().withMessage('Should have a valid transcoding 360p resolution enabled boolean'),
  43. body('transcoding.resolutions.480p').isBoolean().withMessage('Should have a valid transcoding 480p resolution enabled boolean'),
  44. body('transcoding.resolutions.720p').isBoolean().withMessage('Should have a valid transcoding 720p resolution enabled boolean'),
  45. body('transcoding.resolutions.1080p').isBoolean().withMessage('Should have a valid transcoding 1080p resolution enabled boolean'),
  46. body('transcoding.resolutions.1440p').isBoolean().withMessage('Should have a valid transcoding 1440p resolution enabled boolean'),
  47. body('transcoding.resolutions.2160p').isBoolean().withMessage('Should have a valid transcoding 2160p resolution enabled boolean'),
  48. body('transcoding.webtorrent.enabled').isBoolean().withMessage('Should have a valid webtorrent transcoding enabled boolean'),
  49. body('transcoding.hls.enabled').isBoolean().withMessage('Should have a valid hls transcoding enabled boolean'),
  50. body('videoStudio.enabled').isBoolean().withMessage('Should have a valid video studio enabled boolean'),
  51. body('import.videos.concurrency').isInt({ min: 0 }).withMessage('Should have a valid import concurrency number'),
  52. body('import.videos.http.enabled').isBoolean().withMessage('Should have a valid import video http enabled boolean'),
  53. body('import.videos.torrent.enabled').isBoolean().withMessage('Should have a valid import video torrent enabled boolean'),
  54. body('trending.videos.algorithms.default').exists().withMessage('Should have a valid default trending algorithm'),
  55. body('trending.videos.algorithms.enabled').exists().withMessage('Should have a valid array of enabled trending algorithms'),
  56. body('followers.instance.enabled').isBoolean().withMessage('Should have a valid followers of instance boolean'),
  57. body('followers.instance.manualApproval').isBoolean().withMessage('Should have a valid manual approval boolean'),
  58. body('theme.default').custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
  59. body('broadcastMessage.enabled').isBoolean().withMessage('Should have a valid broadcast message enabled boolean'),
  60. body('broadcastMessage.message').exists().withMessage('Should have a valid broadcast message'),
  61. body('broadcastMessage.level').exists().withMessage('Should have a valid broadcast level'),
  62. body('broadcastMessage.dismissable').isBoolean().withMessage('Should have a valid broadcast dismissable boolean'),
  63. body('live.enabled').isBoolean().withMessage('Should have a valid live enabled boolean'),
  64. body('live.allowReplay').isBoolean().withMessage('Should have a valid live allow replay boolean'),
  65. body('live.maxDuration').isInt().withMessage('Should have a valid live max duration'),
  66. body('live.maxInstanceLives').custom(isIntOrNull).withMessage('Should have a valid max instance lives'),
  67. 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'),
  69. body('live.transcoding.threads').isInt().withMessage('Should have a valid live transcoding threads'),
  70. body('live.transcoding.resolutions.144p').isBoolean().withMessage('Should have a valid transcoding 144p resolution enabled boolean'),
  71. body('live.transcoding.resolutions.240p').isBoolean().withMessage('Should have a valid transcoding 240p resolution enabled boolean'),
  72. body('live.transcoding.resolutions.360p').isBoolean().withMessage('Should have a valid transcoding 360p resolution enabled boolean'),
  73. body('live.transcoding.resolutions.480p').isBoolean().withMessage('Should have a valid transcoding 480p resolution enabled boolean'),
  74. body('live.transcoding.resolutions.720p').isBoolean().withMessage('Should have a valid transcoding 720p resolution enabled boolean'),
  75. body('live.transcoding.resolutions.1080p').isBoolean().withMessage('Should have a valid transcoding 1080p resolution enabled boolean'),
  76. body('live.transcoding.resolutions.1440p').isBoolean().withMessage('Should have a valid transcoding 1440p resolution enabled boolean'),
  77. body('live.transcoding.resolutions.2160p').isBoolean().withMessage('Should have a valid transcoding 2160p resolution enabled boolean'),
  78. body('search.remoteUri.users').isBoolean().withMessage('Should have a remote URI search for users boolean'),
  79. body('search.remoteUri.anonymous').isBoolean().withMessage('Should have a valid remote URI search for anonymous boolean'),
  80. body('search.searchIndex.enabled').isBoolean().withMessage('Should have a valid search index enabled boolean'),
  81. body('search.searchIndex.url').exists().withMessage('Should have a valid search index URL'),
  82. body('search.searchIndex.disableLocalSearch').isBoolean().withMessage('Should have a valid search index disable local search boolean'),
  83. body('search.searchIndex.isDefaultSearch').isBoolean().withMessage('Should have a valid search index default enabled boolean'),
  84. (req: express.Request, res: express.Response, next: express.NextFunction) => {
  85. logger.debug('Checking customConfigUpdateValidator parameters', { parameters: req.body })
  86. if (areValidationErrors(req, res)) return
  87. if (!checkInvalidConfigIfEmailDisabled(req.body, res)) return
  88. if (!checkInvalidTranscodingConfig(req.body, res)) return
  89. if (!checkInvalidLiveConfig(req.body, res)) return
  90. if (!checkInvalidVideoStudioConfig(req.body, res)) return
  91. return next()
  92. }
  93. ]
  94. function ensureConfigIsEditable (req: express.Request, res: express.Response, next: express.NextFunction) {
  95. if (!CONFIG.WEBADMIN.CONFIGURATION.EDITION.ALLOWED) {
  96. return res.fail({
  97. status: HttpStatusCode.METHOD_NOT_ALLOWED_405,
  98. message: 'Server configuration is static and cannot be edited'
  99. })
  100. }
  101. return next()
  102. }
  103. // ---------------------------------------------------------------------------
  104. export {
  105. customConfigUpdateValidator,
  106. ensureConfigIsEditable
  107. }
  108. function checkInvalidConfigIfEmailDisabled (customConfig: CustomConfig, res: express.Response) {
  109. if (isEmailEnabled()) return true
  110. if (customConfig.signup.requiresEmailVerification === true) {
  111. res.fail({ message: 'Emailer is disabled but you require signup email verification.' })
  112. return false
  113. }
  114. return true
  115. }
  116. function checkInvalidTranscodingConfig (customConfig: CustomConfig, res: express.Response) {
  117. if (customConfig.transcoding.enabled === false) return true
  118. if (customConfig.transcoding.webtorrent.enabled === false && customConfig.transcoding.hls.enabled === false) {
  119. res.fail({ message: 'You need to enable at least webtorrent transcoding or hls transcoding' })
  120. return false
  121. }
  122. return true
  123. }
  124. function checkInvalidLiveConfig (customConfig: CustomConfig, res: express.Response) {
  125. if (customConfig.live.enabled === false) return true
  126. if (customConfig.live.allowReplay === true && customConfig.transcoding.enabled === false) {
  127. res.fail({ message: 'You cannot allow live replay if transcoding is not enabled' })
  128. return false
  129. }
  130. return true
  131. }
  132. function checkInvalidVideoStudioConfig (customConfig: CustomConfig, res: express.Response) {
  133. if (customConfig.videoStudio.enabled === false) return true
  134. if (customConfig.videoStudio.enabled === true && customConfig.transcoding.enabled === false) {
  135. res.fail({ message: 'You cannot enable video studio if transcoding is not enabled' })
  136. return false
  137. }
  138. return true
  139. }