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