aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/config.ts
blob: e20efe02c02e99f5bb3d293cf770f96b8e57a0b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import bytes from 'bytes'
import { IConfig } from 'config'
import decache from 'decache'
import { dirname, join } from 'path'
import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type'
import { BroadcastMessageLevel } from '@shared/models/server'
import { VideosRedundancyStrategy } from '../../shared/models'
import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type'
// Do not use barrels, remain constants as independent as possible
import { buildPath, parseBytes, parseDurationToMs, root } from '../helpers/core-utils'

// Use a variable to reload the configuration if we need
let config: IConfig = require('config')

const configChangedHandlers: Function[] = []

const CONFIG = {
  CUSTOM_FILE: getLocalConfigFilePath(),
  LISTEN: {
    PORT: config.get<number>('listen.port'),
    HOSTNAME: config.get<string>('listen.hostname')
  },
  DATABASE: {
    DBNAME: config.has('database.name') ? config.get<string>('database.name') : 'peertube' + config.get<string>('database.suffix'),
    HOSTNAME: config.get<string>('database.hostname'),
    PORT: config.get<number>('database.port'),
    SSL: config.get<boolean>('database.ssl'),
    USERNAME: config.get<string>('database.username'),
    PASSWORD: config.get<string>('database.password'),
    POOL: {
      MAX: config.get<number>('database.pool.max')
    }
  },
  REDIS: {
    HOSTNAME: config.has('redis.hostname') ? config.get<string>('redis.hostname') : null,
    PORT: config.has('redis.port') ? config.get<number>('redis.port') : null,
    SOCKET: config.has('redis.socket') ? config.get<string>('redis.socket') : null,
    AUTH: config.has('redis.auth') ? config.get<string>('redis.auth') : null,
    DB: config.has('redis.db') ? config.get<number>('redis.db') : null
  },
  SMTP: {
    TRANSPORT: config.has('smtp.transport') ? config.get<string>('smtp.transport') : 'smtp',
    SENDMAIL: config.has('smtp.sendmail') ? config.get<string>('smtp.sendmail') : null,
    HOSTNAME: config.get<string>('smtp.hostname'),
    PORT: config.get<number>('smtp.port'),
    USERNAME: config.get<string>('smtp.username'),
    PASSWORD: config.get<string>('smtp.password'),
    TLS: config.get<boolean>('smtp.tls'),
    DISABLE_STARTTLS: config.get<boolean>('smtp.disable_starttls'),
    CA_FILE: config.get<string>('smtp.ca_file'),
    FROM_ADDRESS: config.get<string>('smtp.from_address')
  },
  EMAIL: {
    BODY: {
      SIGNATURE: config.get<string>('email.body.signature')
    },
    SUBJECT: {
      PREFIX: config.get<string>('email.subject.prefix') + ' '
    }
  },

  CLIENT: {
    VIDEOS: {
      MINIATURE: {
        get PREFER_AUTHOR_DISPLAY_NAME () { return config.get<boolean>('client.videos.miniature.prefer_author_display_name') }
      }
    }
  },

  STORAGE: {
    TMP_DIR: buildPath(config.get<string>('storage.tmp')),
    BIN_DIR: buildPath(config.get<string>('storage.bin')),
    ACTOR_IMAGES: buildPath(config.get<string>('storage.avatars')),
    LOG_DIR: buildPath(config.get<string>('storage.logs')),
    VIDEOS_DIR: buildPath(config.get<string>('storage.videos')),
    STREAMING_PLAYLISTS_DIR: buildPath(config.get<string>('storage.streaming_playlists')),
    REDUNDANCY_DIR: buildPath(config.get<string>('storage.redundancy')),
    THUMBNAILS_DIR: buildPath(config.get<string>('storage.thumbnails')),
    PREVIEWS_DIR: buildPath(config.get<string>('storage.previews')),
    CAPTIONS_DIR: buildPath(config.get<string>('storage.captions')),
    TORRENTS_DIR: buildPath(config.get<string>('storage.torrents')),
    CACHE_DIR: buildPath(config.get<string>('storage.cache')),
    PLUGINS_DIR: buildPath(config.get<string>('storage.plugins')),
    CLIENT_OVERRIDES_DIR: buildPath(config.get<string>('storage.client_overrides'))
  },
  OBJECT_STORAGE: {
    ENABLED: config.get<boolean>('object_storage.enabled'),
    MAX_UPLOAD_PART: bytes.parse(config.get<string>('object_storage.max_upload_part')),
    ENDPOINT: config.get<string>('object_storage.endpoint'),
    REGION: config.get<string>('object_storage.region'),
    CREDENTIALS: {
      ACCESS_KEY_ID: config.get<string>('object_storage.credentials.access_key_id'),
      SECRET_ACCESS_KEY: config.get<string>('object_storage.credentials.secret_access_key')
    },
    VIDEOS: {
      BUCKET_NAME: config.get<string>('object_storage.videos.bucket_name'),
      PREFIX: config.get<string>('object_storage.videos.prefix'),
      BASE_URL: config.get<string>('object_storage.videos.base_url')
    },
    STREAMING_PLAYLISTS: {
      BUCKET_NAME: config.get<string>('object_storage.streaming_playlists.bucket_name'),
      PREFIX: config.get<string>('object_storage.streaming_playlists.prefix'),
      BASE_URL: config.get<string>('object_storage.streaming_playlists.base_url')
    }
  },
  WEBSERVER: {
    SCHEME: config.get<boolean>('webserver.https') === true ? 'https' : 'http',
    WS: config.get<boolean>('webserver.https') === true ? 'wss' : 'ws',
    HOSTNAME: config.get<string>('webserver.hostname'),
    PORT: config.get<number>('webserver.port')
  },
  RATES_LIMIT: {
    API: {
      WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.api.window')),
      MAX: config.get<number>('rates_limit.api.max')
    },
    SIGNUP: {
      WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.signup.window')),
      MAX: config.get<number>('rates_limit.signup.max')
    },
    LOGIN: {
      WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.login.window')),
      MAX: config.get<number>('rates_limit.login.max')
    },
    ASK_SEND_EMAIL: {
      WINDOW_MS: parseDurationToMs(config.get<string>('rates_limit.ask_send_email.window')),
      MAX: config.get<number>('rates_limit.ask_send_email.max')
    }
  },
  TRUST_PROXY: config.get<string[]>('trust_proxy'),
  LOG: {
    LEVEL: config.get<string>('log.level'),
    ROTATION: {
      ENABLED: config.get<boolean>('log.rotation.enabled'),
      MAX_FILE_SIZE: bytes.parse(config.get<string>('log.rotation.max_file_size')),
      MAX_FILES: config.get<number>('log.rotation.max_files')
    },
    ANONYMIZE_IP: config.get<boolean>('log.anonymize_ip'),
    LOG_PING_REQUESTS: config.get<boolean>('log.log_ping_requests'),
    PRETTIFY_SQL: config.get<boolean>('log.prettify_sql')
  },
  TRENDING: {
    VIDEOS: {
      INTERVAL_DAYS: config.get<number>('trending.videos.interval_days'),
      ALGORITHMS: {
        get ENABLED () { return config.get<string[]>('trending.videos.algorithms.enabled') },
        get DEFAULT () { return config.get<string>('trending.videos.algorithms.default') }
      }
    }
  },
  REDUNDANCY: {
    VIDEOS: {
      CHECK_INTERVAL: parseDurationToMs(config.get<string>('redundancy.videos.check_interval')),
      STRATEGIES: buildVideosRedundancy(config.get<any[]>('redundancy.videos.strategies'))
    }
  },
  REMOTE_REDUNDANCY: {
    VIDEOS: {
      ACCEPT_FROM: config.get<VideoRedundancyConfigFilter>('remote_redundancy.videos.accept_from')
    }
  },
  CSP: {
    ENABLED: config.get<boolean>('csp.enabled'),
    REPORT_ONLY: config.get<boolean>('csp.report_only'),
    REPORT_URI: config.get<string>('csp.report_uri')
  },
  SECURITY: {
    FRAMEGUARD: {
      ENABLED: config.get<boolean>('security.frameguard.enabled')
    }
  },
  TRACKER: {
    ENABLED: config.get<boolean>('tracker.enabled'),
    PRIVATE: config.get<boolean>('tracker.private'),
    REJECT_TOO_MANY_ANNOUNCES: config.get<boolean>('tracker.reject_too_many_announces')
  },
  HISTORY: {
    VIDEOS: {
      MAX_AGE: parseDurationToMs(config.get('history.videos.max_age'))
    }
  },
  VIEWS: {
    VIDEOS: {
      REMOTE: {
        MAX_AGE: parseDurationToMs(config.get('views.videos.remote.max_age'))
      }
    }
  },
  PLUGINS: {
    INDEX: {
      ENABLED: config.get<boolean>('plugins.index.enabled'),
      CHECK_LATEST_VERSIONS_INTERVAL: parseDurationToMs(config.get<string>('plugins.index.check_latest_versions_interval')),
      URL: config.get<string>('plugins.index.url')
    }
  },
  FEDERATION: {
    VIDEOS: {
      FEDERATE_UNLISTED: config.get<boolean>('federation.videos.federate_unlisted'),
      CLEANUP_REMOTE_INTERACTIONS: config.get<boolean>('federation.videos.cleanup_remote_interactions')
    }
  },
  PEERTUBE: {
    CHECK_LATEST_VERSION: {
      ENABLED: config.get<boolean>('peertube.check_latest_version.enabled'),
      URL: config.get<string>('peertube.check_latest_version.url')
    }
  },
  WEBADMIN: {
    CONFIGURATION: {
      EDITION: {
        ALLOWED: config.get<boolean>('webadmin.configuration.edition.allowed')
      }
    }
  },
  ADMIN: {
    get EMAIL () { return config.get<string>('admin.email') }
  },
  CONTACT_FORM: {
    get ENABLED () { return config.get<boolean>('contact_form.enabled') }
  },
  SIGNUP: {
    get ENABLED () { return config.get<boolean>('signup.enabled') },
    get LIMIT () { return config.get<number>('signup.limit') },
    get REQUIRES_EMAIL_VERIFICATION () { return config.get<boolean>('signup.requires_email_verification') },
    get MINIMUM_AGE () { return config.get<number>('signup.minimum_age') },
    FILTERS: {
      CIDR: {
        get WHITELIST () { return config.get<string[]>('signup.filters.cidr.whitelist') },
        get BLACKLIST () { return config.get<string[]>('signup.filters.cidr.blacklist') }
      }
    }
  },
  USER: {
    get VIDEO_QUOTA () { return parseBytes(config.get<number>('user.video_quota')) },
    get VIDEO_QUOTA_DAILY () { return parseBytes(config.get<number>('user.video_quota_daily')) }
  },
  TRANSCODING: {
    get ENABLED () { return config.get<boolean>('transcoding.enabled') },
    get ALLOW_ADDITIONAL_EXTENSIONS () { return config.get<boolean>('transcoding.allow_additional_extensions') },
    get ALLOW_AUDIO_FILES () { return config.get<boolean>('transcoding.allow_audio_files') },
    get THREADS () { return config.get<number>('transcoding.threads') },
    get CONCURRENCY () { return config.get<number>('transcoding.concurrency') },
    get PROFILE () { return config.get<string>('transcoding.profile') },
    RESOLUTIONS: {
      get '0p' () { return config.get<boolean>('transcoding.resolutions.0p') },
      get '240p' () { return config.get<boolean>('transcoding.resolutions.240p') },
      get '360p' () { return config.get<boolean>('transcoding.resolutions.360p') },
      get '480p' () { return config.get<boolean>('transcoding.resolutions.480p') },
      get '720p' () { return config.get<boolean>('transcoding.resolutions.720p') },
      get '1080p' () { return config.get<boolean>('transcoding.resolutions.1080p') },
      get '1440p' () { return config.get<boolean>('transcoding.resolutions.1440p') },
      get '2160p' () { return config.get<boolean>('transcoding.resolutions.2160p') }
    },
    HLS: {
      get ENABLED () { return config.get<boolean>('transcoding.hls.enabled') }
    },
    WEBTORRENT: {
      get ENABLED () { return config.get<boolean>('transcoding.webtorrent.enabled') }
    }
  },
  LIVE: {
    get ENABLED () { return config.get<boolean>('live.enabled') },

    get MAX_DURATION () { return parseDurationToMs(config.get<string>('live.max_duration')) },
    get MAX_INSTANCE_LIVES () { return config.get<number>('live.max_instance_lives') },
    get MAX_USER_LIVES () { return config.get<number>('live.max_user_lives') },

    get ALLOW_REPLAY () { return config.get<boolean>('live.allow_replay') },

    RTMP: {
      get PORT () { return config.get<number>('live.rtmp.port') }
    },

    TRANSCODING: {
      get ENABLED () { return config.get<boolean>('live.transcoding.enabled') },
      get THREADS () { return config.get<number>('live.transcoding.threads') },
      get PROFILE () { return config.get<string>('live.transcoding.profile') },

      RESOLUTIONS: {
        get '240p' () { return config.get<boolean>('live.transcoding.resolutions.240p') },
        get '360p' () { return config.get<boolean>('live.transcoding.resolutions.360p') },
        get '480p' () { return config.get<boolean>('live.transcoding.resolutions.480p') },
        get '720p' () { return config.get<boolean>('live.transcoding.resolutions.720p') },
        get '1080p' () { return config.get<boolean>('live.transcoding.resolutions.1080p') },
        get '1440p' () { return config.get<boolean>('live.transcoding.resolutions.1440p') },
        get '2160p' () { return config.get<boolean>('live.transcoding.resolutions.2160p') }
      }
    }
  },
  IMPORT: {
    VIDEOS: {
      get CONCURRENCY () { return config.get<number>('import.videos.concurrency') },

      HTTP: {
        get ENABLED () { return config.get<boolean>('import.videos.http.enabled') },

        YOUTUBE_DL_RELEASE: {
          get URL () { return config.get<string>('import.videos.http.youtube_dl_release.url') },
          get NAME () { return config.get<string>('import.videos.http.youtube_dl_release.name') }
        },

        get FORCE_IPV4 () { return config.get<boolean>('import.videos.http.force_ipv4') }
      },
      TORRENT: {
        get ENABLED () { return config.get<boolean>('import.videos.torrent.enabled') }
      }
    }
  },
  AUTO_BLACKLIST: {
    VIDEOS: {
      OF_USERS: {
        get ENABLED () { return config.get<boolean>('auto_blacklist.videos.of_users.enabled') }
      }
    }
  },
  CACHE: {
    PREVIEWS: {
      get SIZE () { return config.get<number>('cache.previews.size') }
    },
    VIDEO_CAPTIONS: {
      get SIZE () { return config.get<number>('cache.captions.size') }
    },
    TORRENTS: {
      get SIZE () { return config.get<number>('cache.torrents.size') }
    }
  },
  INSTANCE: {
    get NAME () { return config.get<string>('instance.name') },
    get SHORT_DESCRIPTION () { return config.get<string>('instance.short_description') },
    get DESCRIPTION () { return config.get<string>('instance.description') },
    get TERMS () { return config.get<string>('instance.terms') },
    get CODE_OF_CONDUCT () { return config.get<string>('instance.code_of_conduct') },

    get CREATION_REASON () { return config.get<string>('instance.creation_reason') },

    get MODERATION_INFORMATION () { return config.get<string>('instance.moderation_information') },
    get ADMINISTRATOR () { return config.get<string>('instance.administrator') },
    get MAINTENANCE_LIFETIME () { return config.get<string>('instance.maintenance_lifetime') },
    get BUSINESS_MODEL () { return config.get<string>('instance.business_model') },
    get HARDWARE_INFORMATION () { return config.get<string>('instance.hardware_information') },

    get LANGUAGES () { return config.get<string[]>('instance.languages') || [] },
    get CATEGORIES () { return config.get<number[]>('instance.categories') || [] },

    get IS_NSFW () { return config.get<boolean>('instance.is_nsfw') },
    get DEFAULT_NSFW_POLICY () { return config.get<NSFWPolicyType>('instance.default_nsfw_policy') },

    get DEFAULT_CLIENT_ROUTE () { return config.get<string>('instance.default_client_route') },

    CUSTOMIZATIONS: {
      get JAVASCRIPT () { return config.get<string>('instance.customizations.javascript') },
      get CSS () { return config.get<string>('instance.customizations.css') }
    },
    get ROBOTS () { return config.get<string>('instance.robots') },
    get SECURITYTXT () { return config.get<string>('instance.securitytxt') },
    get SECURITYTXT_CONTACT () { return config.get<string>('admin.email') }
  },
  SERVICES: {
    TWITTER: {
      get USERNAME () { return config.get<string>('services.twitter.username') },
      get WHITELISTED () { return config.get<boolean>('services.twitter.whitelisted') }
    }
  },
  FOLLOWERS: {
    INSTANCE: {
      get ENABLED () { return config.get<boolean>('followers.instance.enabled') },
      get MANUAL_APPROVAL () { return config.get<boolean>('followers.instance.manual_approval') }
    }
  },
  FOLLOWINGS: {
    INSTANCE: {
      AUTO_FOLLOW_BACK: {
        get ENABLED () {
          return config.get<boolean>('followings.instance.auto_follow_back.enabled')
        }
      },
      AUTO_FOLLOW_INDEX: {
        get ENABLED () {
          return config.get<boolean>('followings.instance.auto_follow_index.enabled')
        },
        get INDEX_URL () {
          return config.get<string>('followings.instance.auto_follow_index.index_url')
        }
      }
    }
  },
  THEME: {
    get DEFAULT () { return config.get<string>('theme.default') }
  },
  BROADCAST_MESSAGE: {
    get ENABLED () { return config.get<boolean>('broadcast_message.enabled') },
    get MESSAGE () { return config.get<string>('broadcast_message.message') },
    get LEVEL () { return config.get<BroadcastMessageLevel>('broadcast_message.level') },
    get DISMISSABLE () { return config.get<boolean>('broadcast_message.dismissable') }
  },
  SEARCH: {
    REMOTE_URI: {
      get USERS () { return config.get<boolean>('search.remote_uri.users') },
      get ANONYMOUS () { return config.get<boolean>('search.remote_uri.anonymous') }
    },
    SEARCH_INDEX: {
      get ENABLED () { return config.get<boolean>('search.search_index.enabled') },
      get URL () { return config.get<string>('search.search_index.url') },
      get DISABLE_LOCAL_SEARCH () { return config.get<boolean>('search.search_index.disable_local_search') },
      get IS_DEFAULT_SEARCH () { return config.get<boolean>('search.search_index.is_default_search') }
    }
  }
}

function registerConfigChangedHandler (fun: Function) {
  configChangedHandlers.push(fun)
}

function isEmailEnabled () {
  if (CONFIG.SMTP.TRANSPORT === 'sendmail' && CONFIG.SMTP.SENDMAIL) return true

  if (CONFIG.SMTP.TRANSPORT === 'smtp' && CONFIG.SMTP.HOSTNAME && CONFIG.SMTP.PORT) return true

  return false
}

// ---------------------------------------------------------------------------

export {
  CONFIG,
  registerConfigChangedHandler,
  isEmailEnabled
}

// ---------------------------------------------------------------------------

function getLocalConfigFilePath () {
  const localConfigDir = getLocalConfigDir()

  let filename = 'local'
  if (process.env.NODE_ENV) filename += `-${process.env.NODE_ENV}`
  if (process.env.NODE_APP_INSTANCE) filename += `-${process.env.NODE_APP_INSTANCE}`

  return join(localConfigDir, filename + '.json')
}

function getLocalConfigDir () {
  if (process.env.PEERTUBE_LOCAL_CONFIG) return process.env.PEERTUBE_LOCAL_CONFIG

  const configSources = config.util.getConfigSources()
  if (configSources.length === 0) throw new Error('Invalid config source.')

  return dirname(configSources[0].name)
}

function buildVideosRedundancy (objs: any[]): VideosRedundancyStrategy[] {
  if (!objs) return []

  if (!Array.isArray(objs)) return objs

  return objs.map(obj => {
    return Object.assign({}, obj, {
      minLifetime: parseDurationToMs(obj.min_lifetime),
      size: bytes.parse(obj.size),
      minViews: obj.min_views
    })
  })
}

export function reloadConfig () {

  function getConfigDirectories () {
    if (process.env.NODE_CONFIG_DIR) {
      return process.env.NODE_CONFIG_DIR.split(':')
    }

    return [ join(root(), 'config') ]
  }

  function purge () {
    const directories = getConfigDirectories()

    for (const fileName in require.cache) {
      if (directories.some((dir) => fileName.includes(dir)) === false) {
        continue
      }

      delete require.cache[fileName]
    }

    decache('config')
  }

  purge()

  config = require('config')

  for (const configChangedHandler of configChangedHandlers) {
    configChangedHandler()
  }
}