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