1 import bytes from 'bytes'
2 import { IConfig } from 'config'
3 import decache from 'decache'
4 import { dirname, join } from 'path'
5 import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type'
6 import { BroadcastMessageLevel } from '@shared/models/server'
7 import { VideosRedundancyStrategy } from '../../shared/models'
8 import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
9 // Do not use barrels, remain constants as independent as possible
10 import { buildPath, parseBytes, parseDurationToMs, root } from '../helpers/core-utils'
12 // Use a variable to reload the configuration if we need
13 let config: IConfig = require('config')
15 const configChangedHandlers: Function[] = []
18 CUSTOM_FILE: getLocalConfigFilePath(),
20 PORT: config.get<number>('listen.port'),
21 HOSTNAME: config.get<string>('listen.hostname')
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'),
31 MAX: config.get<number>('database.pool.max')
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
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')
55 SIGNATURE: config.get<string>('email.body.signature')
58 PREFIX: config.get<string>('email.subject.prefix') + ' '
62 TMP_DIR: buildPath(config.get<string>('storage.tmp')),
63 ACTOR_IMAGES: buildPath(config.get<string>('storage.avatars')),
64 LOG_DIR: buildPath(config.get<string>('storage.logs')),
65 VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
66 STREAMING_PLAYLISTS_DIR: buildPath(config.get<string>('storage.streaming_playlists')),
67 REDUNDANCY_DIR: buildPath(config.get<string>('storage.redundancy')),
68 THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
69 PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
70 CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
71 TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
72 CACHE_DIR: buildPath(config.get<string>('storage.cache')),
73 PLUGINS_DIR: buildPath(config.get<string>('storage.plugins')),
74 CLIENT_OVERRIDES_DIR: buildPath(config.get<string>('storage.client_overrides'))
77 ENABLED: config.get<boolean>('object_storage.enabled'),
78 MAX_UPLOAD_PART: bytes.parse(config.get<string>('object_storage.max_upload_part')),
79 ENDPOINT: config.get<string>('object_storage.endpoint'),
80 REGION: config.get<string>('object_storage.region'),
82 ACCESS_KEY_ID: config.get<string>('object_storage.credentials.access_key_id'),
83 SECRET_ACCESS_KEY: config.get<string>('object_storage.credentials.secret_access_key')
86 BUCKET_NAME: config.get<string>('object_storage.videos.bucket_name'),
87 PREFIX: config.get<string>('object_storage.videos.prefix'),
88 BASE_URL: config.get<string>('object_storage.videos.base_url')
90 STREAMING_PLAYLISTS: {
91 BUCKET_NAME: config.get<string>('object_storage.streaming_playlists.bucket_name'),
92 PREFIX: config.get<string>('object_storage.streaming_playlists.prefix'),
93 BASE_URL: config.get<string>('object_storage.streaming_playlists.base_url')
97 SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
98 WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
99 HOSTNAME: config.get<string>('webserver.hostname'),
100 PORT: config.get<number>('webserver.port')
104 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.api.window')),
105 MAX: config.get<number>('rates_limit.api.max')
108 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.signup.window')),
109 MAX: config.get<number>('rates_limit.signup.max')
112 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.login.window')),
113 MAX: config.get<number>('rates_limit.login.max')
116 WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.ask_send_email.window')),
117 MAX: config.get<number>('rates_limit.ask_send_email.max')
120 TRUST_PROXY: config.get<string[]>('trust_proxy'),
122 LEVEL: config.get<string>('log.level'),
124 ENABLED: config.get<boolean>('log.rotation.enabled'),
125 MAX_FILE_SIZE: bytes.parse(config.get<string>('log.rotation.maxFileSize')),
126 MAX_FILES: config.get<number>('log.rotation.maxFiles')
128 ANONYMIZE_IP: config.get<boolean>('log.anonymizeIP'),
129 LOG_PING_REQUESTS: config.get<boolean>('log.log_ping_requests'),
130 PRETTIFY_SQL: config.get<boolean>('log.prettify_sql')
134 INTERVAL_DAYS: config.get<number>('trending.videos.interval_days'),
136 get ENABLED () { return config.get<string[]>('trending.videos.algorithms.enabled') },
137 get DEFAULT () { return config.get<string>('trending.videos.algorithms.default') }
143 CHECK_INTERVAL: parseDurationToMs(config.get<string>('redundancy.videos.check_interval')),
144 STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies'))
149 ACCEPT_FROM: config.get<VideoRedundancyConfigFilter>('remote_redundancy.videos.accept_from')
153 ENABLED: config.get<boolean>('csp.enabled'),
154 REPORT_ONLY: config.get<boolean>('csp.report_only'),
155 REPORT_URI: config.get<string>('csp.report_uri')
159 ENABLED: config.get<boolean>('security.frameguard.enabled')
163 ENABLED: config.get<boolean>('tracker.enabled'),
164 PRIVATE: config.get<boolean>('tracker.private'),
165 REJECT_TOO_MANY_ANNOUNCES: config.get<boolean>('tracker.reject_too_many_announces')
169 MAX_AGE: parseDurationToMs(config.get('history.videos.max_age'))
175 MAX_AGE: parseDurationToMs(config.get('views.videos.remote.max_age'))
181 ENABLED: config.get<boolean>('plugins.index.enabled'),
182 CHECK_LATEST_VERSIONS_INTERVAL: parseDurationToMs(config.get<string>('plugins.index.check_latest_versions_interval')),
183 URL: config.get<string>('plugins.index.url')
188 FEDERATE_UNLISTED: config.get<boolean>('federation.videos.federate_unlisted'),
189 CLEANUP_REMOTE_INTERACTIONS: config.get<boolean>('federation.videos.cleanup_remote_interactions')
193 CHECK_LATEST_VERSION: {
194 ENABLED: config.get<boolean>('peertube.check_latest_version.enabled'),
195 URL: config.get<string>('peertube.check_latest_version.url')
199 get EMAIL () { return config.get<string>('admin.email') }
202 get ENABLED () { return config.get<boolean>('contact_form.enabled') }
205 get ENABLED () { return config.get<boolean>('signup.enabled') },
206 get LIMIT () { return config.get<number>('signup.limit') },
207 get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
208 get MINIMUM_AGE () { return config.get<number>('signup.minimum_age') },
211 get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
212 get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
217 get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
218 get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
221 get ENABLED () { return config.get<boolean>('transcoding.enabled') },
222 get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') },
223 get ALLOW_AUDIO_FILES () { return config.get<boolean>('transcoding.allow_audio_files') },
224 get THREADS () { return config.get<number>('transcoding.threads') },
225 get CONCURRENCY () { return config.get<number>('transcoding.concurrency') },
226 get PROFILE () { return config.get<string>('transcoding.profile') },
228 get '0p' () { return config.get<boolean>('transcoding.resolutions.0p') },
229 get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
230 get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
231 get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
232 get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
233 get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') },
234 get '1440p' () { return config.get<boolean>('transcoding.resolutions.1440p') },
235 get '2160p' () { return config.get<boolean>('transcoding.resolutions.2160p') }
238 get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') }
241 get ENABLED () { return config.get<boolean>('transcoding.webtorrent.enabled') }
245 get ENABLED () { return config.get<boolean>('live.enabled') },
247 get MAX_DURATION () { return parseDurationToMs(config.get<string>('live.max_duration')) },
248 get MAX_INSTANCE_LIVES () { return config.get<number>('live.max_instance_lives') },
249 get MAX_USER_LIVES () { return config.get<number>('live.max_user_lives') },
251 get ALLOW_REPLAY () { return config.get<boolean>('live.allow_replay') },
254 get PORT () { return config.get<number>('live.rtmp.port') }
258 get ENABLED () { return config.get<boolean>('live.transcoding.enabled') },
259 get THREADS () { return config.get<number>('live.transcoding.threads') },
260 get PROFILE () { return config.get<string>('live.transcoding.profile') },
263 get '240p' () { return config.get<boolean>('live.transcoding.resolutions.240p') },
264 get '360p' () { return config.get<boolean>('live.transcoding.resolutions.360p') },
265 get '480p' () { return config.get<boolean>('live.transcoding.resolutions.480p') },
266 get '720p' () { return config.get<boolean>('live.transcoding.resolutions.720p') },
267 get '1080p' () { return config.get<boolean>('live.transcoding.resolutions.1080p') },
268 get '1440p' () { return config.get<boolean>('live.transcoding.resolutions.1440p') },
269 get '2160p' () { return config.get<boolean>('live.transcoding.resolutions.2160p') }
275 get CONCURRENCY () { return config.get<number>('import.videos.concurrency') },
278 get ENABLED () { return config.get<boolean>('import.videos.http.enabled') },
279 get FORCE_IPV4 () { return config.get<boolean>('import.videos.http.force_ipv4') },
281 get ENABLED () { return config.get<boolean>('import.videos.http.proxy.enabled') },
282 get URL () { return config.get<string>('import.videos.http.proxy.url') }
286 get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
293 get ENABLED () { return config.get<boolean>('auto_blacklist.videos.of_users.enabled') }
299 get SIZE () { return config.get<number>('cache.previews.size') }
302 get SIZE () { return config.get<number>('cache.captions.size') }
305 get SIZE () { return config.get<number>('cache.torrents.size') }
309 get NAME () { return config.get<string>('instance.name') },
310 get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
311 get DESCRIPTION () { return config.get<string>('instance.description') },
312 get TERMS () { return config.get<string>('instance.terms') },
313 get CODE_OF_CONDUCT () { return config.get<string>('instance.code_of_conduct') },
315 get CREATION_REASON () { return config.get<string>('instance.creation_reason') },
317 get MODERATION_INFORMATION () { return config.get<string>('instance.moderation_information') },
318 get ADMINISTRATOR () { return config.get<string>('instance.administrator') },
319 get MAINTENANCE_LIFETIME () { return config.get<string>('instance.maintenance_lifetime') },
320 get BUSINESS_MODEL () { return config.get<string>('instance.business_model') },
321 get HARDWARE_INFORMATION () { return config.get<string>('instance.hardware_information') },
323 get LANGUAGES () { return config.get<string[]>('instance.languages') || [] },
324 get CATEGORIES () { return config.get<number[]>('instance.categories') || [] },
326 get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') },
327 get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },
329 get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },
332 get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
333 get CSS () { return config.get<string>('instance.customizations.css') }
335 get ROBOTS () { return config.get<string>('instance.robots') },
336 get SECURITYTXT () { return config.get<string>('instance.securitytxt') },
337 get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') }
341 get USERNAME () { return config.get<string>('services.twitter.username') },
342 get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
347 get ENABLED () { return config.get<boolean>('followers.instance.enabled') },
348 get MANUAL_APPROVAL () { return config.get<boolean>('followers.instance.manual_approval') }
355 return config.get<boolean>('followings.instance.auto_follow_back.enabled')
360 return config.get<boolean>('followings.instance.auto_follow_index.enabled')
363 return config.get<string>('followings.instance.auto_follow_index.index_url')
369 get DEFAULT () { return config.get<string>('theme.default') }
372 get ENABLED () { return config.get<boolean>('broadcast_message.enabled') },
373 get MESSAGE () { return config.get<string>('broadcast_message.message') },
374 get LEVEL () { return config.get<BroadcastMessageLevel>('broadcast_message.level') },
375 get DISMISSABLE () { return config.get<boolean>('broadcast_message.dismissable') }
379 get USERS () { return config.get<boolean>('search.remote_uri.users') },
380 get ANONYMOUS () { return config.get<boolean>('search.remote_uri.anonymous') }
383 get ENABLED () { return config.get<boolean>('search.search_index.enabled') },
384 get URL () { return config.get<string>('search.search_index.url') },
385 get DISABLE_LOCAL_SEARCH () { return config.get<boolean>('search.search_index.disable_local_search') },
386 get IS_DEFAULT_SEARCH () { return config.get<boolean>('search.search_index.is_default_search') }
391 function registerConfigChangedHandler (fun: Function) {
392 configChangedHandlers.push(fun)
395 function isEmailEnabled () {
396 if (CONFIG.SMTP.TRANSPORT === 'sendmail' && CONFIG.SMTP.SENDMAIL) return true
398 if (CONFIG.SMTP.TRANSPORT === 'smtp' && CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) return true
403 // ---------------------------------------------------------------------------
407 registerConfigChangedHandler,
411 // ---------------------------------------------------------------------------
413 function getLocalConfigFilePath () {
414 const configSources = config.util.getConfigSources()
415 if (configSources.length === 0) throw new Error('Invalid config source.')
417 let filename = 'local'
418 if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
419 if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`
421 return join(dirname(configSources[0].name), filename + '.json')
424 function buildVideosRedundancy (objs: any[]): VideosRedundancyStrategy[] {
427 if (!Array.isArray(objs)) return objs
429 return objs.map(obj => {
430 return Object.assign({}, obj, {
431 minLifetime: parseDurationToMs(obj.min_lifetime),
432 size: bytes.parse(obj.size),
433 minViews: obj.min_views
438 export function reloadConfig () {
440 function getConfigDirectory () {
441 if (process.env.NODE_CONFIG_DIR) {
442 return process.env.NODE_CONFIG_DIR
445 return join(root(), 'config')
449 const directory = getConfigDirectory()
451 for (const fileName in require.cache) {
452 if (fileName.includes(directory) === false) {
456 delete require.cache[fileName]
464 config = require('config')
466 for (const configChangedHandler of configChangedHandlers) {
467 configChangedHandler()