ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

checker-after-init.ts 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import { uniqify } from '@peertube/peertube-core-utils'
  2. import { getFFmpegVersion } from '@peertube/peertube-ffmpeg'
  3. import { VideoRedundancyConfigFilter } from '@peertube/peertube-models'
  4. import { isProdInstance } from '@peertube/peertube-node-utils'
  5. import config from 'config'
  6. import { readFileSync, writeFileSync } from 'fs'
  7. import { basename } from 'path'
  8. import { URL } from 'url'
  9. import { parseBytes, parseSemVersion } from '../helpers/core-utils.js'
  10. import { isArray } from '../helpers/custom-validators/misc.js'
  11. import { logger } from '../helpers/logger.js'
  12. import { ApplicationModel, getServerActor } from '../models/application/application.js'
  13. import { OAuthClientModel } from '../models/oauth/oauth-client.js'
  14. import { UserModel } from '../models/user/user.js'
  15. import { CONFIG, getLocalConfigFilePath, isEmailEnabled, reloadConfig } from './config.js'
  16. import { WEBSERVER } from './constants.js'
  17. async function checkActivityPubUrls () {
  18. const actor = await getServerActor()
  19. const parsed = new URL(actor.url)
  20. if (WEBSERVER.HOST !== parsed.host) {
  21. const NODE_ENV = config.util.getEnv('NODE_ENV')
  22. const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR')
  23. logger.warn(
  24. 'It seems PeerTube was started (and created some data) with another domain name. ' +
  25. 'This means you will not be able to federate! ' +
  26. 'Please use %s %s npm run update-host to fix this.',
  27. NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '',
  28. NODE_ENV ? `NODE_ENV=${NODE_ENV}` : ''
  29. )
  30. }
  31. }
  32. // Some checks on configuration files or throw if there is an error
  33. function checkConfig () {
  34. const configFiles = config.util.getConfigSources().map(s => s.name).join(' -> ')
  35. logger.info('Using following configuration file hierarchy: %s.', configFiles)
  36. checkRemovedConfigKeys()
  37. checkSecretsConfig()
  38. checkEmailConfig()
  39. checkNSFWPolicyConfig()
  40. checkLocalRedundancyConfig()
  41. checkRemoteRedundancyConfig()
  42. checkStorageConfig()
  43. checkTranscodingConfig()
  44. checkImportConfig()
  45. checkBroadcastMessageConfig()
  46. checkSearchConfig()
  47. checkLiveConfig()
  48. checkObjectStorageConfig()
  49. checkVideoStudioConfig()
  50. checkThumbnailsConfig()
  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 semvar = parseSemVersion(version)
  70. if (!semvar) {
  71. logger.warn('Your ffmpeg version (%s) does not use semvar. Unable to determine version compatibility.', version)
  72. return
  73. }
  74. const { major, minor, patch } = semvar
  75. if (major < 4 || (major === 4 && minor < 1)) {
  76. logger.warn('Your ffmpeg version (%s) is outdated. PeerTube supports ffmpeg >= 4.1. Please upgrade ffmpeg.', version)
  77. }
  78. if (major === 4 && minor === 4 && patch === 0) {
  79. logger.warn('There is a bug in ffmpeg 4.4.0 with HLS videos. Please upgrade ffmpeg.')
  80. }
  81. }
  82. // ---------------------------------------------------------------------------
  83. export {
  84. applicationExist,
  85. checkActivityPubUrls, checkConfig, checkFFmpegVersion, clientsExist, usersExist
  86. }
  87. // ---------------------------------------------------------------------------
  88. function checkRemovedConfigKeys () {
  89. // Moved configuration keys
  90. if (config.has('services.csp-logger')) {
  91. logger.warn('services.csp-logger configuration has been renamed to csp.report_uri. Please update your configuration file.')
  92. }
  93. if (config.has('transcoding.webtorrent.enabled')) {
  94. const localConfigPath = getLocalConfigFilePath()
  95. const content = readFileSync(localConfigPath, { encoding: 'utf-8' })
  96. if (!content.includes('"webtorrent"')) {
  97. throw new Error('Please rename transcoding.webtorrent.enabled key to transcoding.web_videos.enabled in your configuration file')
  98. }
  99. try {
  100. logger.info(
  101. 'Replacing "transcoding.webtorrent.enabled" key to "transcoding.web_videos.enabled" in your local configuration ' + localConfigPath
  102. )
  103. writeFileSync(localConfigPath, content.replace('"webtorrent"', '"web_videos"'), { encoding: 'utf-8' })
  104. reloadConfig()
  105. .catch(err => logger.error('Cannot reload configuration', { err }))
  106. } catch (err) {
  107. logger.error('Cannot write new configuration to file ' + localConfigPath, { err })
  108. }
  109. }
  110. }
  111. function checkSecretsConfig () {
  112. if (!CONFIG.SECRETS.PEERTUBE) {
  113. throw new Error('secrets.peertube is missing in config. Generate one using `openssl rand -hex 32`')
  114. }
  115. }
  116. function checkEmailConfig () {
  117. if (!isEmailEnabled()) {
  118. if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
  119. throw new Error('SMTP is not configured but you require signup email verification.')
  120. }
  121. if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_APPROVAL) {
  122. // eslint-disable-next-line max-len
  123. logger.warn('SMTP is not configured but signup approval is enabled: PeerTube will not be able to send an email to the user upon acceptance/rejection of the registration request')
  124. }
  125. if (CONFIG.CONTACT_FORM.ENABLED) {
  126. logger.warn('SMTP is not configured so the contact form will not work.')
  127. }
  128. }
  129. }
  130. function checkNSFWPolicyConfig () {
  131. const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY
  132. const available = [ 'do_not_list', 'blur', 'display' ]
  133. if (available.includes(defaultNSFWPolicy) === false) {
  134. throw new Error('NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy)
  135. }
  136. }
  137. function checkLocalRedundancyConfig () {
  138. const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES
  139. if (isArray(redundancyVideos)) {
  140. const available = [ 'most-views', 'trending', 'recently-added' ]
  141. for (const r of redundancyVideos) {
  142. if (available.includes(r.strategy) === false) {
  143. throw new Error('Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy)
  144. }
  145. // Lifetime should not be < 10 hours
  146. if (isProdInstance() && r.minLifetime < 1000 * 3600 * 10) {
  147. throw new Error('Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy)
  148. }
  149. }
  150. const filtered = uniqify(redundancyVideos.map(r => r.strategy))
  151. if (filtered.length !== redundancyVideos.length) {
  152. throw new Error('Redundancy video entries should have unique strategies')
  153. }
  154. const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added')
  155. if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) {
  156. throw new Error('Min views in recently added strategy is not a number')
  157. }
  158. } else {
  159. throw new Error('Videos redundancy should be an array (you must uncomment lines containing - too)')
  160. }
  161. }
  162. function checkRemoteRedundancyConfig () {
  163. const acceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM
  164. const acceptFromValues = new Set<VideoRedundancyConfigFilter>([ 'nobody', 'anybody', 'followings' ])
  165. if (acceptFromValues.has(acceptFrom) === false) {
  166. throw new Error('remote_redundancy.videos.accept_from has an incorrect value')
  167. }
  168. }
  169. function checkStorageConfig () {
  170. // Check storage directory locations
  171. if (isProdInstance()) {
  172. const configStorage = config.get<{ [ name: string ]: string }>('storage')
  173. for (const key of Object.keys(configStorage)) {
  174. if (configStorage[key].startsWith('storage/')) {
  175. logger.warn(
  176. 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.',
  177. key
  178. )
  179. }
  180. }
  181. const webVideosDirname = basename(CONFIG.STORAGE.WEB_VIDEOS_DIR)
  182. if (webVideosDirname !== 'web-videos') {
  183. logger.warn(`storage.web_videos configuration should have a "web-videos" directory name (current value: "${webVideosDirname}")`)
  184. }
  185. }
  186. if (CONFIG.STORAGE.WEB_VIDEOS_DIR === CONFIG.STORAGE.REDUNDANCY_DIR) {
  187. logger.warn('Redundancy directory should be different than the videos folder.')
  188. }
  189. }
  190. function checkTranscodingConfig () {
  191. if (CONFIG.TRANSCODING.ENABLED) {
  192. if (CONFIG.TRANSCODING.WEB_VIDEOS.ENABLED === false && CONFIG.TRANSCODING.HLS.ENABLED === false) {
  193. throw new Error('You need to enable at least Web Video transcoding or HLS transcoding.')
  194. }
  195. if (CONFIG.TRANSCODING.CONCURRENCY <= 0) {
  196. throw new Error('Transcoding concurrency should be > 0')
  197. }
  198. }
  199. if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED || CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED) {
  200. if (CONFIG.IMPORT.VIDEOS.CONCURRENCY <= 0) {
  201. throw new Error('Video import concurrency should be > 0')
  202. }
  203. }
  204. }
  205. function checkImportConfig () {
  206. if (CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED && !CONFIG.IMPORT.VIDEOS.HTTP) {
  207. throw new Error('You need to enable HTTP import to allow synchronization')
  208. }
  209. }
  210. function checkBroadcastMessageConfig () {
  211. if (CONFIG.BROADCAST_MESSAGE.ENABLED) {
  212. const currentLevel = CONFIG.BROADCAST_MESSAGE.LEVEL
  213. const available = [ 'info', 'warning', 'error' ]
  214. if (available.includes(currentLevel) === false) {
  215. throw new Error('Broadcast message level should be ' + available.join(' or ') + ' instead of ' + currentLevel)
  216. }
  217. }
  218. }
  219. function checkSearchConfig () {
  220. if (CONFIG.SEARCH.SEARCH_INDEX.ENABLED === true) {
  221. if (CONFIG.SEARCH.REMOTE_URI.USERS === false) {
  222. throw new Error('You cannot enable search index without enabling remote URI search for users.')
  223. }
  224. }
  225. }
  226. function checkLiveConfig () {
  227. if (CONFIG.LIVE.ENABLED === true) {
  228. if (CONFIG.LIVE.ALLOW_REPLAY === true && CONFIG.TRANSCODING.ENABLED === false) {
  229. throw new Error('Live allow replay cannot be enabled if transcoding is not enabled.')
  230. }
  231. if (CONFIG.LIVE.RTMP.ENABLED === false && CONFIG.LIVE.RTMPS.ENABLED === false) {
  232. throw new Error('You must enable at least RTMP or RTMPS')
  233. }
  234. if (CONFIG.LIVE.RTMPS.ENABLED) {
  235. if (!CONFIG.LIVE.RTMPS.KEY_FILE) {
  236. throw new Error('You must specify a key file to enable RTMPS')
  237. }
  238. if (!CONFIG.LIVE.RTMPS.CERT_FILE) {
  239. throw new Error('You must specify a cert file to enable RTMPS')
  240. }
  241. }
  242. }
  243. }
  244. function checkObjectStorageConfig () {
  245. if (CONFIG.OBJECT_STORAGE.ENABLED !== true) return
  246. if (!CONFIG.OBJECT_STORAGE.WEB_VIDEOS.BUCKET_NAME) {
  247. throw new Error('videos_bucket should be set when object storage support is enabled.')
  248. }
  249. if (!CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME) {
  250. throw new Error('streaming_playlists_bucket should be set when object storage support is enabled.')
  251. }
  252. // Check web videos and hls videos are not in the same bucket or directory
  253. if (
  254. CONFIG.OBJECT_STORAGE.WEB_VIDEOS.BUCKET_NAME === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME &&
  255. CONFIG.OBJECT_STORAGE.WEB_VIDEOS.PREFIX === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.PREFIX
  256. ) {
  257. if (CONFIG.OBJECT_STORAGE.WEB_VIDEOS.PREFIX === '') {
  258. throw new Error('Bucket prefixes should be set when the same bucket is used for both types of video.')
  259. }
  260. throw new Error(
  261. 'Bucket prefixes should be set to different values when the same bucket is used for both types of video.'
  262. )
  263. }
  264. if (CONFIG.TRANSCODING.ORIGINAL_FILE.KEEP) {
  265. if (!CONFIG.OBJECT_STORAGE.ORIGINAL_VIDEO_FILES.BUCKET_NAME) {
  266. throw new Error('original_video_files_bucket should be set when object storage support is enabled.')
  267. }
  268. // Check web videos/hls videos are not in the same bucket or directory as original video files
  269. if (
  270. CONFIG.OBJECT_STORAGE.WEB_VIDEOS.BUCKET_NAME === CONFIG.OBJECT_STORAGE.ORIGINAL_VIDEO_FILES.BUCKET_NAME &&
  271. CONFIG.OBJECT_STORAGE.WEB_VIDEOS.PREFIX === CONFIG.OBJECT_STORAGE.ORIGINAL_VIDEO_FILES.PREFIX
  272. ) {
  273. if (CONFIG.OBJECT_STORAGE.WEB_VIDEOS.PREFIX === '') {
  274. throw new Error('Bucket prefixes should be set when the same bucket is used for both original and web video files.')
  275. }
  276. throw new Error(
  277. 'Bucket prefixes should be set to different values when the same bucket is used for both original and web video files.'
  278. )
  279. }
  280. if (
  281. CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME === CONFIG.OBJECT_STORAGE.ORIGINAL_VIDEO_FILES.BUCKET_NAME &&
  282. CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.PREFIX === CONFIG.OBJECT_STORAGE.ORIGINAL_VIDEO_FILES.PREFIX
  283. ) {
  284. if (CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.PREFIX === '') {
  285. throw new Error('Bucket prefixes should be set when the same bucket is used for both original and hls files.')
  286. }
  287. throw new Error(
  288. 'Bucket prefixes should be set to different values when the same bucket is used for both original and hls files.'
  289. )
  290. }
  291. }
  292. if (CONFIG.OBJECT_STORAGE.MAX_UPLOAD_PART > parseBytes('250MB')) {
  293. // eslint-disable-next-line max-len
  294. 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).`)
  295. }
  296. }
  297. function checkVideoStudioConfig () {
  298. if (CONFIG.VIDEO_STUDIO.ENABLED === true && CONFIG.TRANSCODING.ENABLED === false) {
  299. throw new Error('Video studio cannot be enabled if transcoding is disabled')
  300. }
  301. }
  302. function checkThumbnailsConfig () {
  303. if (CONFIG.THUMBNAILS.GENERATION_FROM_VIDEO.FRAMES_TO_ANALYZE < 2) {
  304. throw new Error('thumbnails.generation_from_video.frames_to_analyze must be a number greater than 1')
  305. }
  306. if (!isArray(CONFIG.THUMBNAILS.SIZES) || CONFIG.THUMBNAILS.SIZES.length !== 2) {
  307. throw new Error('thumbnails.sizes must be an array of 2 sizes')
  308. }
  309. }