checker-after-init.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import * as config from 'config'
  2. import { isProdInstance, isTestInstance } from '../helpers/core-utils'
  3. import { UserModel } from '../models/account/user'
  4. import { getServerActor, ApplicationModel } from '../models/application/application'
  5. import { OAuthClientModel } from '../models/oauth/oauth-client'
  6. import { URL } from 'url'
  7. import { CONFIG, isEmailEnabled } from './config'
  8. import { logger } from '../helpers/logger'
  9. import { RecentlyAddedStrategy } from '../../shared/models/redundancy'
  10. import { isArray } from '../helpers/custom-validators/misc'
  11. import { uniq } from 'lodash'
  12. import { WEBSERVER } from './constants'
  13. import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type'
  14. async function checkActivityPubUrls () {
  15. const actor = await getServerActor()
  16. const parsed = new URL(actor.url)
  17. if (WEBSERVER.HOST !== parsed.host) {
  18. const NODE_ENV = config.util.getEnv('NODE_ENV')
  19. const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
  20. logger.warn(
  21. 'It seems PeerTube was started (and created some data) with another domain name. ' +
  22. 'This means you will not be able to federate! ' +
  23. 'Please use %s %s npm run update-host to fix this.',
  24. NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
  25. NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
  26. )
  27. }
  28. }
  29. // Some checks on configuration files
  30. // Return an error message, or null if everything is okay
  31. function checkConfig () {
  32. // Moved configuration keys
  33. if (config.has('services.csp-logger')) {
  34. logger.warn('services.csp-logger configuration has been renamed to csp.report_uri. Please update your configuration file.')
  35. }
  36. // Email verification
  37. if (!isEmailEnabled()) {
  38. if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
  39. return 'Emailer is disabled but you require signup email verification.'
  40. }
  41. if (CONFIG.CONTACT_FORM.ENABLED) {
  42. logger.warn('Emailer is disabled so the contact form will not work.')
  43. }
  44. }
  45. // NSFW policy
  46. const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
  47. {
  48. const available = [ 'do_not_list', 'blur', 'display' ]
  49. if (available.includes(defaultNSFWPolicy) === false) {
  50. return 'NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy
  51. }
  52. }
  53. // Redundancies
  54. const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
  55. if (isArray(redundancyVideos)) {
  56. const available = [ 'most-views', 'trending', 'recently-added' ]
  57. for (const r of redundancyVideos) {
  58. if (available.includes(r.strategy) === false) {
  59. return 'Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy
  60. }
  61. // Lifetime should not be < 10 hours
  62. if (!isTestInstance() && r.minLifetime < 1000 * 3600 * 10) {
  63. return 'Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy
  64. }
  65. }
  66. const filtered = uniq(redundancyVideos.map(r => r.strategy))
  67. if (filtered.length !== redundancyVideos.length) {
  68. return 'Redundancy video entries should have unique strategies'
  69. }
  70. const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
  71. if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
  72. return 'Min views in recently added strategy is not a number'
  73. }
  74. } else {
  75. return 'Videos redundancy should be an array (you must uncomment lines containing - too)'
  76. }
  77. // Remote redundancies
  78. const acceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM
  79. const acceptFromValues = new Set<VideoRedundancyConfigFilter>([ 'nobody', 'anybody', 'followings' ])
  80. if (acceptFromValues.has(acceptFrom) === false) {
  81. return 'remote_redundancy.videos.accept_from has an incorrect value'
  82. }
  83. // Check storage directory locations
  84. if (isProdInstance()) {
  85. const configStorage = config.get('storage')
  86. for (const key of Object.keys(configStorage)) {
  87. if (configStorage[key].startsWith('storage/')) {
  88. logger.warn(
  89. 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
  90. key
  91. )
  92. }
  93. }
  94. }
  95. if (CONFIG.STORAGE.VIDEOS_DIR === CONFIG.STORAGE.REDUNDANCY_DIR) {
  96. logger.warn('Redundancy directory should be different than the videos folder.')
  97. }
  98. // Transcoding
  99. if (CONFIG.TRANSCODING.ENABLED) {
  100. if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false && CONFIG.TRANSCODING.HLS.ENABLED === false) {
  101. return 'You need to enable at least WebTorrent transcoding or HLS transcoding.'
  102. }
  103. }
  104. // Broadcast message
  105. if (CONFIG.BROADCAST_MESSAGE.ENABLED) {
  106. const currentLevel = CONFIG.BROADCAST_MESSAGE.LEVEL
  107. const available = [ 'info', 'warning', 'error' ]
  108. if (available.includes(currentLevel) === false) {
  109. return 'Broadcast message level should be ' + available.join(' or ') + ' instead of ' + currentLevel
  110. }
  111. }
  112. // Search index
  113. if (CONFIG.SEARCH.SEARCH_INDEX.ENABLED === true) {
  114. if (CONFIG.SEARCH.REMOTE_URI.USERS === false) {
  115. return 'You cannot enable search index without enabling remote URI search for users.'
  116. }
  117. }
  118. // Live
  119. if (CONFIG.LIVE.ENABLED === true) {
  120. if (CONFIG.LIVE.ALLOW_REPLAY === true && CONFIG.TRANSCODING.ENABLED === false) {
  121. return 'Live allow replay cannot be enabled if transcoding is not enabled.'
  122. }
  123. }
  124. return null
  125. }
  126. // We get db by param to not import it in this file (import orders)
  127. async function clientsExist () {
  128. const totalClients = await OAuthClientModel.countTotal()
  129. return totalClients !== 0
  130. }
  131. // We get db by param to not import it in this file (import orders)
  132. async function usersExist () {
  133. const totalUsers = await UserModel.countTotal()
  134. return totalUsers !== 0
  135. }
  136. // We get db by param to not import it in this file (import orders)
  137. async function applicationExist () {
  138. const totalApplication = await ApplicationModel.countTotal()
  139. return totalApplication !== 0
  140. }
  141. // ---------------------------------------------------------------------------
  142. export {
  143. checkConfig,
  144. clientsExist,
  145. usersExist,
  146. applicationExist,
  147. checkActivityPubUrls
  148. }