checker-after-init.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import config from 'config'
  2. import { URL } from 'url'
  3. import { getFFmpegVersion } from '@server/helpers/ffmpeg'
  4. import { uniqify } from '@shared/core-utils'
  5. import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type'
  6. import { RecentlyAddedStrategy } from '../../shared/models/redundancy'
  7. import { isProdInstance, parseBytes, 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. const configFiles = config.util.getConfigSources().map(s => s.name).join(' -> ')
  33. logger.info('Using following configuration file hierarchy: %s.', configFiles)
  34. // Moved configuration keys
  35. if (config.has('services.csp-logger')) {
  36. logger.warn('services.csp-logger configuration has been renamed to csp.report_uri. Please update your configuration file.')
  37. }
  38. checkSecretsConfig()
  39. checkEmailConfig()
  40. checkNSFWPolicyConfig()
  41. checkLocalRedundancyConfig()
  42. checkRemoteRedundancyConfig()
  43. checkStorageConfig()
  44. checkTranscodingConfig()
  45. checkImportConfig()
  46. checkBroadcastMessageConfig()
  47. checkSearchConfig()
  48. checkLiveConfig()
  49. checkObjectStorageConfig()
  50. checkVideoStudioConfig()
  51. }
  52. // We get db by param to not import it in this file (import orders)
  53. async function clientsExist () {
  54. const totalClients = await OAuthClientModel.countTotal()
  55. return totalClients !== 0
  56. }
  57. // We get db by param to not import it in this file (import orders)
  58. async function usersExist () {
  59. const totalUsers = await UserModel.countTotal()
  60. return totalUsers !== 0
  61. }
  62. // We get db by param to not import it in this file (import orders)
  63. async function applicationExist () {
  64. const totalApplication = await ApplicationModel.countTotal()
  65. return totalApplication !== 0
  66. }
  67. async function checkFFmpegVersion () {
  68. const version = await getFFmpegVersion()
  69. const { major, minor, patch } = parseSemVersion(version)
  70. if (major < 4 || (major === 4 && minor < 1)) {
  71. logger.warn('Your ffmpeg version (%s) is outdated. PeerTube supports ffmpeg >= 4.1. Please upgrade ffmpeg.', version)
  72. }
  73. if (major === 4 && minor === 4 && patch === 0) {
  74. logger.warn('There is a bug in ffmpeg 4.4.0 with HLS videos. Please upgrade ffmpeg.')
  75. }
  76. }
  77. // ---------------------------------------------------------------------------
  78. export {
  79. checkConfig,
  80. clientsExist,
  81. checkFFmpegVersion,
  82. usersExist,
  83. applicationExist,
  84. checkActivityPubUrls
  85. }
  86. // ---------------------------------------------------------------------------
  87. function checkSecretsConfig () {
  88. if (!CONFIG.SECRETS.PEERTUBE) {
  89. throw new Error('secrets.peertube is missing in config. Generate one using `openssl rand -hex 32`')
  90. }
  91. }
  92. function checkEmailConfig () {
  93. if (!isEmailEnabled()) {
  94. if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
  95. throw new Error('Emailer is disabled but you require signup email verification.')
  96. }
  97. if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_APPROVAL) {
  98. // eslint-disable-next-line max-len
  99. logger.warn('Emailer is disabled but signup approval is enabled: PeerTube will not be able to send an email to the user upon acceptance/rejection of the registration request')
  100. }
  101. if (CONFIG.CONTACT_FORM.ENABLED) {
  102. logger.warn('Emailer is disabled so the contact form will not work.')
  103. }
  104. }
  105. }
  106. function checkNSFWPolicyConfig () {
  107. const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
  108. const available = [ 'do_not_list', 'blur', 'display' ]
  109. if (available.includes(defaultNSFWPolicy) === false) {
  110. throw new Error('NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy)
  111. }
  112. }
  113. function checkLocalRedundancyConfig () {
  114. const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
  115. if (isArray(redundancyVideos)) {
  116. const available = [ 'most-views', 'trending', 'recently-added' ]
  117. for (const r of redundancyVideos) {
  118. if (available.includes(r.strategy) === false) {
  119. throw new Error('Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy)
  120. }
  121. // Lifetime should not be < 10 hours
  122. if (isProdInstance() && r.minLifetime < 1000 * 3600 * 10) {
  123. throw new Error('Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy)
  124. }
  125. }
  126. const filtered = uniqify(redundancyVideos.map(r => r.strategy))
  127. if (filtered.length !== redundancyVideos.length) {
  128. throw new Error('Redundancy video entries should have unique strategies')
  129. }
  130. const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy
  131. if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
  132. throw new Error('Min views in recently added strategy is not a number')
  133. }
  134. } else {
  135. throw new Error('Videos redundancy should be an array (you must uncomment lines containing - too)')
  136. }
  137. }
  138. function checkRemoteRedundancyConfig () {
  139. const acceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM
  140. const acceptFromValues = new Set<VideoRedundancyConfigFilter>([ 'nobody', 'anybody', 'followings' ])
  141. if (acceptFromValues.has(acceptFrom) === false) {
  142. throw new Error('remote_redundancy.videos.accept_from has an incorrect value')
  143. }
  144. }
  145. function checkStorageConfig () {
  146. // Check storage directory locations
  147. if (isProdInstance()) {
  148. const configStorage = config.get<{ [ name: string ]: string }>('storage')
  149. for (const key of Object.keys(configStorage)) {
  150. if (configStorage[key].startsWith('storage/')) {
  151. logger.warn(
  152. 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
  153. key
  154. )
  155. }
  156. }
  157. }
  158. if (CONFIG.STORAGE.VIDEOS_DIR === CONFIG.STORAGE.REDUNDANCY_DIR) {
  159. logger.warn('Redundancy directory should be different than the videos folder.')
  160. }
  161. }
  162. function checkTranscodingConfig () {
  163. if (CONFIG.TRANSCODING.ENABLED) {
  164. if (CONFIG.TRANSCODING.WEBTORRENT.ENABLED === false && CONFIG.TRANSCODING.HLS.ENABLED === false) {
  165. throw new Error('You need to enable at least WebTorrent transcoding or HLS transcoding.')
  166. }
  167. if (CONFIG.TRANSCODING.CONCURRENCY <= 0) {
  168. throw new Error('Transcoding concurrency should be > 0')
  169. }
  170. }
  171. if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED || CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED) {
  172. if (CONFIG.IMPORT.VIDEOS.CONCURRENCY <= 0) {
  173. throw new Error('Video import concurrency should be > 0')
  174. }
  175. }
  176. }
  177. function checkImportConfig () {
  178. if (CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED && !CONFIG.IMPORT.VIDEOS.HTTP) {
  179. throw new Error('You need to enable HTTP import to allow synchronization')
  180. }
  181. }
  182. function checkBroadcastMessageConfig () {
  183. if (CONFIG.BROADCAST_MESSAGE.ENABLED) {
  184. const currentLevel = CONFIG.BROADCAST_MESSAGE.LEVEL
  185. const available = [ 'info', 'warning', 'error' ]
  186. if (available.includes(currentLevel) === false) {
  187. throw new Error('Broadcast message level should be ' + available.join(' or ') + ' instead of ' + currentLevel)
  188. }
  189. }
  190. }
  191. function checkSearchConfig () {
  192. if (CONFIG.SEARCH.SEARCH_INDEX.ENABLED === true) {
  193. if (CONFIG.SEARCH.REMOTE_URI.USERS === false) {
  194. throw new Error('You cannot enable search index without enabling remote URI search for users.')
  195. }
  196. }
  197. }
  198. function checkLiveConfig () {
  199. if (CONFIG.LIVE.ENABLED === true) {
  200. if (CONFIG.LIVE.ALLOW_REPLAY === true && CONFIG.TRANSCODING.ENABLED === false) {
  201. throw new Error('Live allow replay cannot be enabled if transcoding is not enabled.')
  202. }
  203. if (CONFIG.LIVE.RTMP.ENABLED === false && CONFIG.LIVE.RTMPS.ENABLED === false) {
  204. throw new Error('You must enable at least RTMP or RTMPS')
  205. }
  206. if (CONFIG.LIVE.RTMPS.ENABLED) {
  207. if (!CONFIG.LIVE.RTMPS.KEY_FILE) {
  208. throw new Error('You must specify a key file to enabled RTMPS')
  209. }
  210. if (!CONFIG.LIVE.RTMPS.CERT_FILE) {
  211. throw new Error('You must specify a cert file to enable RTMPS')
  212. }
  213. }
  214. }
  215. }
  216. function checkObjectStorageConfig () {
  217. if (CONFIG.OBJECT_STORAGE.ENABLED === true) {
  218. if (!CONFIG.OBJECT_STORAGE.VIDEOS.BUCKET_NAME) {
  219. throw new Error('videos_bucket should be set when object storage support is enabled.')
  220. }
  221. if (!CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME) {
  222. throw new Error('streaming_playlists_bucket should be set when object storage support is enabled.')
  223. }
  224. if (
  225. CONFIG.OBJECT_STORAGE.VIDEOS.BUCKET_NAME === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME &&
  226. CONFIG.OBJECT_STORAGE.VIDEOS.PREFIX === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.PREFIX
  227. ) {
  228. if (CONFIG.OBJECT_STORAGE.VIDEOS.PREFIX === '') {
  229. throw new Error('Object storage bucket prefixes should be set when the same bucket is used for both types of video.')
  230. }
  231. throw new Error(
  232. 'Object storage bucket prefixes should be set to different values when the same bucket is used for both types of video.'
  233. )
  234. }
  235. if (CONFIG.OBJECT_STORAGE.MAX_UPLOAD_PART > parseBytes('250MB')) {
  236. // eslint-disable-next-line max-len
  237. logger.warn(`Object storage max upload part seems to have a big value (${CONFIG.OBJECT_STORAGE.MAX_UPLOAD_PART} bytes). Consider using a lower one (like 100MB).`)
  238. }
  239. }
  240. }
  241. function checkVideoStudioConfig () {
  242. if (CONFIG.VIDEO_STUDIO.ENABLED === true && CONFIG.TRANSCODING.ENABLED === false) {
  243. throw new Error('Video studio cannot be enabled if transcoding is disabled')
  244. }
  245. }