checker-after-init.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import config from 'config'
  2. import { uniq } from 'lodash'
  3. import { URL } from 'url'
  4. import { getFFmpegVersion } from '@server/helpers/ffmpeg'
  5. import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type'
  6. import { RecentlyAddedStrategy } from '../../shared/models/redundancy'
  7. import { isProdInstance, isTestInstance, parseSemVersion } from '../helpers/core-utils'
  8. import { isArray } from '../helpers/custom-validators/misc'
  9. import { logger } from '../helpers/logger'
  10. import { ApplicationModel, getServerActor } from '../models/application/application'
  11. import { OAuthClientModel } from '../models/oauth/oauth-client'
  12. import { UserModel } from '../models/user/user'
  13. import { CONFIG, isEmailEnabled } from './config'
  14. import { WEBSERVER } from './constants'
  15. async function checkActivityPubUrls () {
  16. const actor = await getServerActor()
  17. const parsed = new URL(actor.url)
  18. if (WEBSERVER.HOST !== parsed.host) {
  19. const NODE_ENV = config.util.getEnv('NODE_ENV')
  20. const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
  21. logger.warn(
  22. 'It seems PeerTube was started (and created some data) with another domain name. ' +
  23. 'This means you will not be able to federate! ' +
  24. 'Please use %s %s npm run update-host to fix this.',
  25. NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
  26. NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
  27. )
  28. }
  29. }
  30. // Some checks on configuration files or throw if there is an error
  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. checkEmailConfig()
  37. checkNSFWPolicyConfig()
  38. checkLocalRedundancyConfig()
  39. checkRemoteRedundancyConfig()
  40. checkStorageConfig()
  41. checkTranscodingConfig()
  42. checkBroadcastMessageConfig()
  43. checkSearchConfig()
  44. checkLiveConfig()
  45. checkObjectStorageConfig()
  46. checkVideoStudioConfig()
  47. }
  48. // We get db by param to not import it in this file (import orders)
  49. async function clientsExist () {
  50. const totalClients = await OAuthClientModel.countTotal()
  51. return totalClients !== 0
  52. }
  53. // We get db by param to not import it in this file (import orders)
  54. async function usersExist () {
  55. const totalUsers = await UserModel.countTotal()
  56. return totalUsers !== 0
  57. }
  58. // We get db by param to not import it in this file (import orders)
  59. async function applicationExist () {
  60. const totalApplication = await ApplicationModel.countTotal()
  61. return totalApplication !== 0
  62. }
  63. async function checkFFmpegVersion () {
  64. const version = await getFFmpegVersion()
  65. const { major, minor } = parseSemVersion(version)
  66. if (major < 4 || (major === 4 && minor < 1)) {
  67. logger.warn('Your ffmpeg version (%s) is outdated. PeerTube supports ffmpeg >= 4.1. Please upgrade.', version)
  68. }
  69. }
  70. // ---------------------------------------------------------------------------
  71. export {
  72. checkConfig,
  73. clientsExist,
  74. checkFFmpegVersion,
  75. usersExist,
  76. applicationExist,
  77. checkActivityPubUrls
  78. }
  79. // ---------------------------------------------------------------------------
  80. function checkEmailConfig () {
  81. if (!isEmailEnabled()) {
  82. if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
  83. throw new Error('Emailer is disabled but you require signup email verification.')
  84. }
  85. if (CONFIG.CONTACT_FORM.ENABLED) {
  86. logger.warn('Emailer is disabled so the contact form will not work.')
  87. }
  88. }
  89. }
  90. function checkNSFWPolicyConfig () {
  91. const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
  92. const available = [ 'do_not_list', 'blur', 'display' ]
  93. if (available.includes(defaultNSFWPolicy) === false) {
  94. throw new Error('NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy)
  95. }
  96. }
  97. function checkLocalRedundancyConfig () {
  98. const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
  99. if (isArray(redundancyVideos)) {
  100. const available = [ 'most-views', 'trending', 'recently-added' ]
  101. for (const r of redundancyVideos) {
  102. if (available.includes(r.strategy) === false) {
  103. throw new Error('Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy)
  104. }
  105. // Lifetime should not be < 10 hours
  106. if (!isTestInstance() && r.minLifetime < 1000 * 3600 * 10) {
  107. throw new Error('Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy)
  108. }
  109. }
  110. const filtered = uniq(redundancyVideos.map(r => r.strategy))
  111. if (filtered.length !== redundancyVideos.length) {
  112. throw new Error('Redundancy video entries should have unique strategies')
  113. }
  114. const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
  115. if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
  116. throw new Error('Min views in recently added strategy is not a number')
  117. }
  118. } else {
  119. throw new Error('Videos redundancy should be an array (you must uncomment lines containing - too)')
  120. }
  121. }
  122. function checkRemoteRedundancyConfig () {
  123. const acceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM
  124. const acceptFromValues = new Set<VideoRedundancyConfigFilter>([ 'nobody', 'anybody', 'followings' ])
  125. if (acceptFromValues.has(acceptFrom) === false) {
  126. throw new Error('remote_redundancy.videos.accept_from has an incorrect value')
  127. }
  128. }
  129. function checkStorageConfig () {
  130. // Check storage directory locations
  131. if (isProdInstance()) {
  132. const configStorage = config.get('storage')
  133. for (const key of Object.keys(configStorage)) {
  134. if (configStorage[key].startsWith('storage/')) {
  135. logger.warn(
  136. 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
  137. key
  138. )
  139. }
  140. }
  141. }
  142. if (CONFIG.STORAGE.VIDEOS_DIR === CONFIG.STORAGE.REDUNDANCY_DIR) {
  143. logger.warn('Redundancy directory should be different than the videos folder.')
  144. }
  145. }
  146. function checkTranscodingConfig () {
  147. if (CONFIG.TRANSCODING.ENABLED) {
  148. if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false && CONFIG.TRANSCODING.HLS.ENABLED === false) {
  149. throw new Error('You need to enable at least WebTorrent transcoding or HLS transcoding.')
  150. }
  151. if (CONFIG.TRANSCODING.CONCURRENCY <= 0) {
  152. throw new Error('Transcoding concurrency should be > 0')
  153. }
  154. }
  155. if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED || CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED) {
  156. if (CONFIG.IMPORT.VIDEOS.CONCURRENCY <= 0) {
  157. throw new Error('Video import concurrency should be > 0')
  158. }
  159. }
  160. }
  161. function checkBroadcastMessageConfig () {
  162. if (CONFIG.BROADCAST_MESSAGE.ENABLED) {
  163. const currentLevel = CONFIG.BROADCAST_MESSAGE.LEVEL
  164. const available = [ 'info', 'warning', 'error' ]
  165. if (available.includes(currentLevel) === false) {
  166. throw new Error('Broadcast message level should be ' + available.join(' or ') + ' instead of ' + currentLevel)
  167. }
  168. }
  169. }
  170. function checkSearchConfig () {
  171. if (CONFIG.SEARCH.SEARCH_INDEX.ENABLED === true) {
  172. if (CONFIG.SEARCH.REMOTE_URI.USERS === false) {
  173. throw new Error('You cannot enable search index without enabling remote URI search for users.')
  174. }
  175. }
  176. }
  177. function checkLiveConfig () {
  178. if (CONFIG.LIVE.ENABLED === true) {
  179. if (CONFIG.LIVE.ALLOW_REPLAY === true && CONFIG.TRANSCODING.ENABLED === false) {
  180. throw new Error('Live allow replay cannot be enabled if transcoding is not enabled.')
  181. }
  182. if (CONFIG.LIVE.RTMP.ENABLED === false && CONFIG.LIVE.RTMPS.ENABLED === false) {
  183. throw new Error('You must enable at least RTMP or RTMPS')
  184. }
  185. if (CONFIG.LIVE.RTMPS.ENABLED) {
  186. if (!CONFIG.LIVE.RTMPS.KEY_FILE) {
  187. throw new Error('You must specify a key file to enabled RTMPS')
  188. }
  189. if (!CONFIG.LIVE.RTMPS.CERT_FILE) {
  190. throw new Error('You must specify a cert file to enable RTMPS')
  191. }
  192. }
  193. }
  194. }
  195. function checkObjectStorageConfig () {
  196. if (CONFIG.OBJECT_STORAGE.ENABLED === true) {
  197. if (!CONFIG.OBJECT_STORAGE.VIDEOS.BUCKET_NAME) {
  198. throw new Error('videos_bucket should be set when object storage support is enabled.')
  199. }
  200. if (!CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME) {
  201. throw new Error('streaming_playlists_bucket should be set when object storage support is enabled.')
  202. }
  203. if (
  204. CONFIG.OBJECT_STORAGE.VIDEOS.BUCKET_NAME === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME &&
  205. CONFIG.OBJECT_STORAGE.VIDEOS.PREFIX === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.PREFIX
  206. ) {
  207. if (CONFIG.OBJECT_STORAGE.VIDEOS.PREFIX === '') {
  208. throw new Error('Object storage bucket prefixes should be set when the same bucket is used for both types of video.')
  209. }
  210. throw new Error(
  211. 'Object storage bucket prefixes should be set to different values when the same bucket is used for both types of video.'
  212. )
  213. }
  214. }
  215. }
  216. function checkVideoStudioConfig () {
  217. if (CONFIG.VIDEO_STUDIO.ENABLED === true && CONFIG.TRANSCODING.ENABLED === false) {
  218. throw new Error('Video studio cannot be enabled if transcoding is disabled')
  219. }
  220. }