]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/initializers/config.ts
Use different p2p policy for embeds and webapp
[github/Chocobozzz/PeerTube.git] / server / initializers / config.ts
1 import bytes from 'bytes'
2 import { IConfig } from 'config'
3 import { dirname, join } from 'path'
4 import { decacheModule } from '@server/helpers/decache'
5 import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type'
6 import { BroadcastMessageLevel } from '@shared/models/server'
7 import { VideoPrivacy, VideosRedundancyStrategy } from '../../shared/models'
8 import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
9 import { buildPath, root } from '../../shared/core-utils'
10 import { parseBytes, parseDurationToMs } from '../helpers/core-utils'
11
12 // Use a variable to reload the configuration if we need
13 let config: IConfig = require('config')
14
15 const configChangedHandlers: Function[] = []
16
17 const CONFIG = {
18 CUSTOM_FILE: getLocalConfigFilePath(),
19 LISTEN: {
20 PORT: config.get<number>('listen.port'),
21 HOSTNAME: config.get<string>('listen.hostname')
22 },
23 DATABASE: {
24 DBNAME: config.has('database.name') ? config.get<string>('database.name') : 'peertube' + config.get<string>('database.suffix'),
25 HOSTNAME: config.get<string>('database.hostname'),
26 PORT: config.get<number>('database.port'),
27 SSL: config.get<boolean>('database.ssl'),
28 USERNAME: config.get<string>('database.username'),
29 PASSWORD: config.get<string>('database.password'),
30 POOL: {
31 MAX: config.get<number>('database.pool.max')
32 }
33 },
34 REDIS: {
35 HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
36 PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
37 SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
38 AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
39 DB: config.has('redis.db') ? config.get<number>('redis.db') : null
40 },
41 SMTP: {
42 TRANSPORT: config.has('smtp.transport') ? config.get<string>('smtp.transport') : 'smtp',
43 SENDMAIL: config.has('smtp.sendmail') ? config.get<string>('smtp.sendmail') : null,
44 HOSTNAME: config.get<string>('smtp.hostname'),
45 PORT: config.get<number>('smtp.port'),
46 USERNAME: config.get<string>('smtp.username'),
47 PASSWORD: config.get<string>('smtp.password'),
48 TLS: config.get<boolean>('smtp.tls'),
49 DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
50 CA_FILE: config.get<string>('smtp.ca_file'),
51 FROM_ADDRESS: config.get<string>('smtp.from_address')
52 },
53 EMAIL: {
54 BODY: {
55 SIGNATURE: config.get<string>('email.body.signature')
56 },
57 SUBJECT: {
58 PREFIX: config.get<string>('email.subject.prefix') + ' '
59 }
60 },
61
62 CLIENT: {
63 VIDEOS: {
64 MINIATURE: {
65 get PREFER_AUTHOR_DISPLAY_NAME () { return config.get<boolean>('client.videos.miniature.prefer_author_display_name') }
66 }
67 },
68 MENU: {
69 LOGIN: {
70 get REDIRECT_ON_SINGLE_EXTERNAL_AUTH () { return config.get<boolean>('client.menu.login.redirect_on_single_external_auth') }
71 }
72 }
73 },
74
75 DEFAULTS: {
76 PUBLISH: {
77 DOWNLOAD_ENABLED: config.get<boolean>('defaults.publish.download_enabled'),
78 COMMENTS_ENABLED: config.get<boolean>('defaults.publish.comments_enabled'),
79 PRIVACY: config.get<VideoPrivacy>('defaults.publish.privacy'),
80 LICENCE: config.get<number>('defaults.publish.licence')
81 },
82 P2P: {
83 WEBAPP: {
84 ENABLED: config.get<boolean>('defaults.p2p.webapp.enabled')
85 },
86 EMBED: {
87 ENABLED: config.get<boolean>('defaults.p2p.embed.enabled')
88 }
89 }
90 },
91
92 STORAGE: {
93 TMP_DIR: buildPath(config.get<string>('storage.tmp')),
94 BIN_DIR: buildPath(config.get<string>('storage.bin')),
95 ACTOR_IMAGES: buildPath(config.get<string>('storage.avatars')),
96 LOG_DIR: buildPath(config.get<string>('storage.logs')),
97 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
98 STREAMING_PLAYLISTS_DIR: buildPath(config.get<string>('storage.streaming_playlists')),
99 REDUNDANCY_DIR: buildPath(config.get<string>('storage.redundancy')),
100 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
101 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
102 CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
103 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
104 CACHE_DIR: buildPath(config.get<string>('storage.cache')),
105 PLUGINS_DIR: buildPath(config.get<string>('storage.plugins')),
106 CLIENT_OVERRIDES_DIR: buildPath(config.get<string>('storage.client_overrides'))
107 },
108 OBJECT_STORAGE: {
109 ENABLED: config.get<boolean>('object_storage.enabled'),
110 MAX_UPLOAD_PART: bytes.parse(config.get<string>('object_storage.max_upload_part')),
111 ENDPOINT: config.get<string>('object_storage.endpoint'),
112 REGION: config.get<string>('object_storage.region'),
113 CREDENTIALS: {
114 ACCESS_KEY_ID: config.get<string>('object_storage.credentials.access_key_id'),
115 SECRET_ACCESS_KEY: config.get<string>('object_storage.credentials.secret_access_key')
116 },
117 VIDEOS: {
118 BUCKET_NAME: config.get<string>('object_storage.videos.bucket_name'),
119 PREFIX: config.get<string>('object_storage.videos.prefix'),
120 BASE_URL: config.get<string>('object_storage.videos.base_url')
121 },
122 STREAMING_PLAYLISTS: {
123 BUCKET_NAME: config.get<string>('object_storage.streaming_playlists.bucket_name'),
124 PREFIX: config.get<string>('object_storage.streaming_playlists.prefix'),
125 BASE_URL: config.get<string>('object_storage.streaming_playlists.base_url')
126 }
127 },
128 WEBSERVER: {
129 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
130 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
131 HOSTNAME: config.get<string>('webserver.hostname'),
132 PORT: config.get<number>('webserver.port')
133 },
134 RATES_LIMIT: {
135 API: {
136 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.api.window')),
137 MAX: config.get<number>('rates_limit.api.max')
138 },
139 SIGNUP: {
140 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.signup.window')),
141 MAX: config.get<number>('rates_limit.signup.max')
142 },
143 LOGIN: {
144 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.login.window')),
145 MAX: config.get<number>('rates_limit.login.max')
146 },
147 ASK_SEND_EMAIL: {
148 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.ask_send_email.window')),
149 MAX: config.get<number>('rates_limit.ask_send_email.max')
150 }
151 },
152 TRUST_PROXY: config.get<string[]>('trust_proxy'),
153 LOG: {
154 LEVEL: config.get<string>('log.level'),
155 ROTATION: {
156 ENABLED: config.get<boolean>('log.rotation.enabled'),
157 MAX_FILE_SIZE: bytes.parse(config.get<string>('log.rotation.max_file_size')),
158 MAX_FILES: config.get<number>('log.rotation.max_files')
159 },
160 ANONYMIZE_IP: config.get<boolean>('log.anonymize_ip'),
161 LOG_PING_REQUESTS: config.get<boolean>('log.log_ping_requests'),
162 PRETTIFY_SQL: config.get<boolean>('log.prettify_sql')
163 },
164 TRENDING: {
165 VIDEOS: {
166 INTERVAL_DAYS: config.get<number>('trending.videos.interval_days'),
167 ALGORITHMS: {
168 get ENABLED () { return config.get<string[]>('trending.videos.algorithms.enabled') },
169 get DEFAULT () { return config.get<string>('trending.videos.algorithms.default') }
170 }
171 }
172 },
173 REDUNDANCY: {
174 VIDEOS: {
175 CHECK_INTERVAL: parseDurationToMs(config.get<string>('redundancy.videos.check_interval')),
176 STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies'))
177 }
178 },
179 REMOTE_REDUNDANCY: {
180 VIDEOS: {
181 ACCEPT_FROM: config.get<VideoRedundancyConfigFilter>('remote_redundancy.videos.accept_from')
182 }
183 },
184 CSP: {
185 ENABLED: config.get<boolean>('csp.enabled'),
186 REPORT_ONLY: config.get<boolean>('csp.report_only'),
187 REPORT_URI: config.get<string>('csp.report_uri')
188 },
189 SECURITY: {
190 FRAMEGUARD: {
191 ENABLED: config.get<boolean>('security.frameguard.enabled')
192 }
193 },
194 TRACKER: {
195 ENABLED: config.get<boolean>('tracker.enabled'),
196 PRIVATE: config.get<boolean>('tracker.private'),
197 REJECT_TOO_MANY_ANNOUNCES: config.get<boolean>('tracker.reject_too_many_announces')
198 },
199 HISTORY: {
200 VIDEOS: {
201 MAX_AGE: parseDurationToMs(config.get('history.videos.max_age'))
202 }
203 },
204 VIEWS: {
205 VIDEOS: {
206 REMOTE: {
207 MAX_AGE: parseDurationToMs(config.get('views.videos.remote.max_age'))
208 },
209 LOCAL_BUFFER_UPDATE_INTERVAL: parseDurationToMs(config.get('views.videos.local_buffer_update_interval')),
210 IP_VIEW_EXPIRATION: parseDurationToMs(config.get('views.videos.ip_view_expiration'))
211 }
212 },
213 PLUGINS: {
214 INDEX: {
215 ENABLED: config.get<boolean>('plugins.index.enabled'),
216 CHECK_LATEST_VERSIONS_INTERVAL: parseDurationToMs(config.get<string>('plugins.index.check_latest_versions_interval')),
217 URL: config.get<string>('plugins.index.url')
218 }
219 },
220 FEDERATION: {
221 VIDEOS: {
222 FEDERATE_UNLISTED: config.get<boolean>('federation.videos.federate_unlisted'),
223 CLEANUP_REMOTE_INTERACTIONS: config.get<boolean>('federation.videos.cleanup_remote_interactions')
224 }
225 },
226 PEERTUBE: {
227 CHECK_LATEST_VERSION: {
228 ENABLED: config.get<boolean>('peertube.check_latest_version.enabled'),
229 URL: config.get<string>('peertube.check_latest_version.url')
230 }
231 },
232 WEBADMIN: {
233 CONFIGURATION: {
234 EDITION: {
235 ALLOWED: config.get<boolean>('webadmin.configuration.edition.allowed')
236 }
237 }
238 },
239 ADMIN: {
240 get EMAIL () { return config.get<string>('admin.email') }
241 },
242 CONTACT_FORM: {
243 get ENABLED () { return config.get<boolean>('contact_form.enabled') }
244 },
245 SIGNUP: {
246 get ENABLED () { return config.get<boolean>('signup.enabled') },
247 get LIMIT () { return config.get<number>('signup.limit') },
248 get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
249 get MINIMUM_AGE () { return config.get<number>('signup.minimum_age') },
250 FILTERS: {
251 CIDR: {
252 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
253 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
254 }
255 }
256 },
257 USER: {
258 get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
259 get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
260 },
261 VIDEO_CHANNELS: {
262 get MAX_PER_USER () { return config.get<number>('video_channels.max_per_user') }
263 },
264 TRANSCODING: {
265 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
266 get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') },
267 get ALLOW_AUDIO_FILES () { return config.get<boolean>('transcoding.allow_audio_files') },
268 get THREADS () { return config.get<number>('transcoding.threads') },
269 get CONCURRENCY () { return config.get<number>('transcoding.concurrency') },
270 get PROFILE () { return config.get<string>('transcoding.profile') },
271 RESOLUTIONS: {
272 get '0p' () { return config.get<boolean>('transcoding.resolutions.0p') },
273 get '144p' () { return config.get<boolean>('transcoding.resolutions.144p') },
274 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
275 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
276 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
277 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
278 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') },
279 get '1440p' () { return config.get<boolean>('transcoding.resolutions.1440p') },
280 get '2160p' () { return config.get<boolean>('transcoding.resolutions.2160p') }
281 },
282 HLS: {
283 get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') }
284 },
285 WEBTORRENT: {
286 get ENABLED () { return config.get<boolean>('transcoding.webtorrent.enabled') }
287 }
288 },
289 LIVE: {
290 get ENABLED () { return config.get<boolean>('live.enabled') },
291
292 get MAX_DURATION () { return parseDurationToMs(config.get<string>('live.max_duration')) },
293 get MAX_INSTANCE_LIVES () { return config.get<number>('live.max_instance_lives') },
294 get MAX_USER_LIVES () { return config.get<number>('live.max_user_lives') },
295
296 get ALLOW_REPLAY () { return config.get<boolean>('live.allow_replay') },
297
298 RTMP: {
299 get ENABLED () { return config.get<boolean>('live.rtmp.enabled') },
300 get PORT () { return config.get<number>('live.rtmp.port') }
301 },
302
303 RTMPS: {
304 get ENABLED () { return config.get<boolean>('live.rtmps.enabled') },
305 get PORT () { return config.get<number>('live.rtmps.port') },
306 get KEY_FILE () { return config.get<string>('live.rtmps.key_file') },
307 get CERT_FILE () { return config.get<string>('live.rtmps.cert_file') }
308 },
309
310 TRANSCODING: {
311 get ENABLED () { return config.get<boolean>('live.transcoding.enabled') },
312 get THREADS () { return config.get<number>('live.transcoding.threads') },
313 get PROFILE () { return config.get<string>('live.transcoding.profile') },
314
315 RESOLUTIONS: {
316 get '144p' () { return config.get<boolean>('live.transcoding.resolutions.144p') },
317 get '240p' () { return config.get<boolean>('live.transcoding.resolutions.240p') },
318 get '360p' () { return config.get<boolean>('live.transcoding.resolutions.360p') },
319 get '480p' () { return config.get<boolean>('live.transcoding.resolutions.480p') },
320 get '720p' () { return config.get<boolean>('live.transcoding.resolutions.720p') },
321 get '1080p' () { return config.get<boolean>('live.transcoding.resolutions.1080p') },
322 get '1440p' () { return config.get<boolean>('live.transcoding.resolutions.1440p') },
323 get '2160p' () { return config.get<boolean>('live.transcoding.resolutions.2160p') }
324 }
325 }
326 },
327 IMPORT: {
328 VIDEOS: {
329 get CONCURRENCY () { return config.get<number>('import.videos.concurrency') },
330
331 HTTP: {
332 get ENABLED () { return config.get<boolean>('import.videos.http.enabled') },
333
334 YOUTUBE_DL_RELEASE: {
335 get URL () { return config.get<string>('import.videos.http.youtube_dl_release.url') },
336 get NAME () { return config.get<string>('import.videos.http.youtube_dl_release.name') }
337 },
338
339 get FORCE_IPV4 () { return config.get<boolean>('import.videos.http.force_ipv4') }
340 },
341 TORRENT: {
342 get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
343 }
344 }
345 },
346 AUTO_BLACKLIST: {
347 VIDEOS: {
348 OF_USERS: {
349 get ENABLED () { return config.get<boolean>('auto_blacklist.videos.of_users.enabled') }
350 }
351 }
352 },
353 CACHE: {
354 PREVIEWS: {
355 get SIZE () { return config.get<number>('cache.previews.size') }
356 },
357 VIDEO_CAPTIONS: {
358 get SIZE () { return config.get<number>('cache.captions.size') }
359 },
360 TORRENTS: {
361 get SIZE () { return config.get<number>('cache.torrents.size') }
362 }
363 },
364 INSTANCE: {
365 get NAME () { return config.get<string>('instance.name') },
366 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
367 get DESCRIPTION () { return config.get<string>('instance.description') },
368 get TERMS () { return config.get<string>('instance.terms') },
369 get CODE_OF_CONDUCT () { return config.get<string>('instance.code_of_conduct') },
370
371 get CREATION_REASON () { return config.get<string>('instance.creation_reason') },
372
373 get MODERATION_INFORMATION () { return config.get<string>('instance.moderation_information') },
374 get ADMINISTRATOR () { return config.get<string>('instance.administrator') },
375 get MAINTENANCE_LIFETIME () { return config.get<string>('instance.maintenance_lifetime') },
376 get BUSINESS_MODEL () { return config.get<string>('instance.business_model') },
377 get HARDWARE_INFORMATION () { return config.get<string>('instance.hardware_information') },
378
379 get LANGUAGES () { return config.get<string[]>('instance.languages') || [] },
380 get CATEGORIES () { return config.get<number[]>('instance.categories') || [] },
381
382 get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') },
383 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
384
385 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
386
387 CUSTOMIZATIONS: {
388 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
389 get CSS () { return config.get<string>('instance.customizations.css') }
390 },
391 get ROBOTS () { return config.get<string>('instance.robots') },
392 get SECURITYTXT () { return config.get<string>('instance.securitytxt') },
393 get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') }
394 },
395 SERVICES: {
396 TWITTER: {
397 get USERNAME () { return config.get<string>('services.twitter.username') },
398 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
399 }
400 },
401 FOLLOWERS: {
402 INSTANCE: {
403 get ENABLED () { return config.get<boolean>('followers.instance.enabled') },
404 get MANUAL_APPROVAL () { return config.get<boolean>('followers.instance.manual_approval') }
405 }
406 },
407 FOLLOWINGS: {
408 INSTANCE: {
409 AUTO_FOLLOW_BACK: {
410 get ENABLED () {
411 return config.get<boolean>('followings.instance.auto_follow_back.enabled')
412 }
413 },
414 AUTO_FOLLOW_INDEX: {
415 get ENABLED () {
416 return config.get<boolean>('followings.instance.auto_follow_index.enabled')
417 },
418 get INDEX_URL () {
419 return config.get<string>('followings.instance.auto_follow_index.index_url')
420 }
421 }
422 }
423 },
424 THEME: {
425 get DEFAULT () { return config.get<string>('theme.default') }
426 },
427 BROADCAST_MESSAGE: {
428 get ENABLED () { return config.get<boolean>('broadcast_message.enabled') },
429 get MESSAGE () { return config.get<string>('broadcast_message.message') },
430 get LEVEL () { return config.get<BroadcastMessageLevel>('broadcast_message.level') },
431 get DISMISSABLE () { return config.get<boolean>('broadcast_message.dismissable') }
432 },
433 SEARCH: {
434 REMOTE_URI: {
435 get USERS () { return config.get<boolean>('search.remote_uri.users') },
436 get ANONYMOUS () { return config.get<boolean>('search.remote_uri.anonymous') }
437 },
438 SEARCH_INDEX: {
439 get ENABLED () { return config.get<boolean>('search.search_index.enabled') },
440 get URL () { return config.get<string>('search.search_index.url') },
441 get DISABLE_LOCAL_SEARCH () { return config.get<boolean>('search.search_index.disable_local_search') },
442 get IS_DEFAULT_SEARCH () { return config.get<boolean>('search.search_index.is_default_search') }
443 }
444 }
445 }
446
447 function registerConfigChangedHandler (fun: Function) {
448 configChangedHandlers.push(fun)
449 }
450
451 function isEmailEnabled () {
452 if (CONFIG.SMTP.TRANSPORT === 'sendmail' && CONFIG.SMTP.SENDMAIL) return true
453
454 if (CONFIG.SMTP.TRANSPORT === 'smtp' && CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) return true
455
456 return false
457 }
458
459 // ---------------------------------------------------------------------------
460
461 export {
462 CONFIG,
463 registerConfigChangedHandler,
464 isEmailEnabled
465 }
466
467 // ---------------------------------------------------------------------------
468
469 function getLocalConfigFilePath () {
470 const localConfigDir = getLocalConfigDir()
471
472 let filename = 'local'
473 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
474 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
475
476 return join(localConfigDir, filename + '.json')
477 }
478
479 function getLocalConfigDir () {
480 if (process.env.PEERTUBE_LOCAL_CONFIG) return process.env.PEERTUBE_LOCAL_CONFIG
481
482 const configSources = config.util.getConfigSources()
483 if (configSources.length === 0) throw new Error('Invalid config source.')
484
485 return dirname(configSources[0].name)
486 }
487
488 function buildVideosRedundancy (objs: any[]): VideosRedundancyStrategy[] {
489 if (!objs) return []
490
491 if (!Array.isArray(objs)) return objs
492
493 return objs.map(obj => {
494 return Object.assign({}, obj, {
495 minLifetime: parseDurationToMs(obj.min_lifetime),
496 size: bytes.parse(obj.size),
497 minViews: obj.min_views
498 })
499 })
500 }
501
502 export function reloadConfig () {
503
504 function getConfigDirectories () {
505 if (process.env.NODE_CONFIG_DIR) {
506 return process.env.NODE_CONFIG_DIR.split(':')
507 }
508
509 return [ join(root(), 'config') ]
510 }
511
512 function purge () {
513 const directories = getConfigDirectories()
514
515 for (const fileName in require.cache) {
516 if (directories.some((dir) => fileName.includes(dir)) === false) {
517 continue
518 }
519
520 delete require.cache[fileName]
521 }
522
523 decacheModule('config')
524 }
525
526 purge()
527
528 config = require('config')
529
530 for (const configChangedHandler of configChangedHandlers) {
531 configChangedHandler()
532 }
533 }